blob: 3209d08dcca3f262d6dc8627a05e9c9c3f1152a7 [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 Moolenaarc9c967d2022-09-07 16:48:46 +0100256 // Shortcut to call a compiled function without 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 {
385 if (evalarg->eval_tofree != NULL)
386 {
387 if (eap != NULL)
388 {
389 // We may need to keep the original command line, e.g. for
390 // ":let" it has the variable names. But we may also need the
391 // new one, "nextcmd" points into it. Keep both.
392 vim_free(eap->cmdline_tofree);
393 eap->cmdline_tofree = *eap->cmdlinep;
394 *eap->cmdlinep = evalarg->eval_tofree;
395 }
396 else
397 vim_free(evalarg->eval_tofree);
398 evalarg->eval_tofree = NULL;
399 }
400
401 ga_clear_strings(&evalarg->eval_tofree_ga);
402 VIM_CLEAR(evalarg->eval_tofree_lambda);
403 }
404}
405
406/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000407 * Skip over an expression at "*pp".
408 * Return FAIL for an error, OK otherwise.
409 */
410 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200411skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000412{
Bram Moolenaar33570922005-01-25 22:26:29 +0000413 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000414
415 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200416 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000417}
418
419/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200420 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200421 * If in Vim9 script and line breaks are encountered, the lines are
422 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200423 * "arg" is advanced to just after the expression.
424 * "start" is set to the start of the expression, "end" to just after the end.
425 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200426 * Return FAIL for an error, OK otherwise.
427 */
428 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200429skip_expr_concatenate(
430 char_u **arg,
431 char_u **start,
432 char_u **end,
433 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200434{
435 typval_T rettv;
436 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200437 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200438 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200439 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200440 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200441 int evaluate = evalarg == NULL
442 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200443
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200444 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200445 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200446 {
447 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200448 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200449 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200450 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200451 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200452 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200453 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200454
455 // Don't evaluate the expression.
456 if (evalarg != NULL)
457 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200458 *arg = skipwhite(*arg);
459 res = eval1(arg, &rettv, evalarg);
460 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200461 if (evalarg != NULL)
462 evalarg->eval_flags = save_flags;
463
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200464 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200465 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200466 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200467 if (evalarg->eval_ga.ga_len == 1)
468 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200469 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200470 ga_clear(gap);
471 gap->ga_itemsize = 0;
472 }
473 else
474 {
475 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200476 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200477
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200478 // Line breaks encountered, concatenate all the lines.
479 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200480 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200481
482 // free the lines only when using getsourceline()
483 if (evalarg->eval_cookie != NULL)
484 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200485 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200486 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200487 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100488 // later. Also free "eval_tofree" later if needed.
489 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200490 evalarg->eval_tofree =
491 ((char_u **)gap->ga_data)[gap->ga_len - 1];
492 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200493 ga_clear_strings(gap);
494 }
495 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200496 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200497 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200498
499 // free lines that were explicitly marked for freeing
500 ga_clear_strings(freegap);
501 }
502
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200503 gap->ga_itemsize = 0;
504 if (p == NULL)
505 return FAIL;
506 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200507 vim_free(evalarg->eval_tofree_lambda);
508 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200509 // Compute "end" relative to the end.
510 *end = *start + STRLEN(*start) - endoff;
511 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200512 }
513
514 return res;
515}
516
517/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100518 * Convert "tv" to a string.
519 * When "convert" is TRUE convert a List into a sequence of lines and convert
520 * a Float to a String.
521 * Returns an allocated string (NULL when out of memory).
522 */
523 char_u *
524typval2string(typval_T *tv, int convert)
525{
526 garray_T ga;
527 char_u *retval;
528#ifdef FEAT_FLOAT
529 char_u numbuf[NUMBUFLEN];
530#endif
531
532 if (convert && tv->v_type == VAR_LIST)
533 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000534 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100535 if (tv->vval.v_list != NULL)
536 {
537 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
538 if (tv->vval.v_list->lv_len > 0)
539 ga_append(&ga, NL);
540 }
541 ga_append(&ga, NUL);
542 retval = (char_u *)ga.ga_data;
543 }
544#ifdef FEAT_FLOAT
545 else if (convert && tv->v_type == VAR_FLOAT)
546 {
547 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
548 retval = vim_strsave(numbuf);
549 }
550#endif
551 else
552 retval = vim_strsave(tv_get_string(tv));
553 return retval;
554}
555
556/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200557 * Top level evaluation function, returning a string. Does not handle line
558 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000559 * When "convert" is TRUE convert a List into a sequence of lines and convert
560 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 * Return pointer to allocated memory, or NULL for failure.
562 */
563 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100564eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100565 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100566 int convert,
567 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568{
Bram Moolenaar33570922005-01-25 22:26:29 +0000569 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100571 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100573 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
574 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 retval = NULL;
576 else
577 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100578 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000579 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100581 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582
583 return retval;
584}
585
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100586 char_u *
587eval_to_string(
588 char_u *arg,
589 int convert)
590{
591 return eval_to_string_eap(arg, convert, NULL);
592}
593
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000595 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100596 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200597 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 */
599 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100600eval_to_string_safe(
601 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000602 int use_sandbox,
603 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604{
605 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200606 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200607 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200608 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
Bram Moolenaar9530b582022-01-22 13:39:08 +0000610 if (!keep_script_version)
611 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200612 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000613 if (use_sandbox)
614 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100615 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200616 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200617 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000618 if (use_sandbox)
619 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100620 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200621 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200622 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200623 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 return retval;
625}
626
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627/*
628 * Top level evaluation function, returning a number.
629 * Evaluates "expr" silently.
630 * Returns -1 for an error.
631 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200632 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100633eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
Bram Moolenaar33570922005-01-25 22:26:29 +0000635 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200636 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000637 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639 ++emsg_off;
640
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200641 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 retval = -1;
643 else
644 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100645 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000646 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 }
648 --emsg_off;
649
650 return retval;
651}
652
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000653/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000654 * Top level evaluation function.
655 * Returns an allocated typval_T with the result.
656 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000657 */
658 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200659eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000660{
661 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200662 evalarg_T evalarg;
663
664 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000665
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200666 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200667 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100668 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000669
Bram Moolenaar37c83712020-06-30 21:18:36 +0200670 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000671 return tv;
672}
673
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000675 * "*arg" points to what can be a function name in the form of "import.Name" or
676 * "Funcref". Return the name of the function. Set "tofree" to something that
677 * was allocated.
678 * If "verbose" is FALSE no errors are given.
679 * Return NULL for any failure.
680 */
681 static char_u *
682deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000683 char_u **arg,
684 char_u **tofree,
685 evalarg_T *evalarg,
686 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000687{
688 typval_T ref;
689 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100690 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000691
692 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100693 if (evalarg != NULL)
694 {
695 // need to evaluate this to get an import, like in "a.Func"
696 save_flags = evalarg->eval_flags;
697 evalarg->eval_flags |= EVAL_EVALUATE;
698 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100699 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100700 {
701 dictitem_T *v;
702
703 // If <SID>VarName was used it would not be found, try another way.
704 v = find_var_also_in_script(name, NULL, FALSE);
705 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100706 {
707 name = NULL;
708 goto theend;
709 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100710 copy_tv(&v->di_tv, &ref);
711 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000712 if (*skipwhite(*arg) != NUL)
713 {
714 if (verbose)
715 semsg(_(e_trailing_characters_str), *arg);
716 name = NULL;
717 }
718 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
719 {
720 name = ref.vval.v_string;
721 ref.vval.v_string = NULL;
722 *tofree = name;
723 }
724 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
725 {
726 if (ref.vval.v_partial->pt_argc > 0
727 || ref.vval.v_partial->pt_dict != NULL)
728 {
729 if (verbose)
730 emsg(_(e_cannot_use_partial_here));
731 name = NULL;
732 }
733 else
734 {
735 name = vim_strsave(partial_name(ref.vval.v_partial));
736 *tofree = name;
737 }
738 }
739 else
740 {
741 if (verbose)
742 semsg(_(e_not_callable_type_str), name);
743 name = NULL;
744 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100745
746theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000747 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100748 if (evalarg != NULL)
749 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000750 return name;
751}
752
753/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100754 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200755 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
756 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000757 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200759 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100760call_vim_function(
761 char_u *func,
762 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200763 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200764 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000766 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200767 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000768 char_u *arg;
769 char_u *name;
770 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000771 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100773 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200774 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000775 funcexe.fe_firstline = curwin->w_cursor.lnum;
776 funcexe.fe_lastline = curwin->w_cursor.lnum;
777 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000778
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000779 // The name might be "import.Func" or "Funcref". We don't know, we need to
780 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000781 // autoload script has errors. Guess that when there is a dot in the name
782 // showing errors is the right choice.
783 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000784 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000785 if (ignore_errors)
786 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000787 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000788 if (ignore_errors)
789 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000790 if (name == NULL)
791 name = func;
792
793 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
794
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000795 if (ret == FAIL)
796 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000797 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000798
799 return ret;
800}
801
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100802/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100803 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000804 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
805 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000806 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000807 */
808 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100809call_func_retstr(
810 char_u *func,
811 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200812 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000813{
814 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000815 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000816
Bram Moolenaarded27a12018-08-01 19:06:03 +0200817 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000818 return NULL;
819
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100820 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000821 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 return retval;
823}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000824
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000825/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100826 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000827 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000828 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100829 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000830 */
831 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100832call_func_retlist(
833 char_u *func,
834 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200835 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000836{
837 typval_T rettv;
838
Bram Moolenaarded27a12018-08-01 19:06:03 +0200839 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000840 return NULL;
841
842 if (rettv.v_type != VAR_LIST)
843 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100844 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
845 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000846 clear_tv(&rettv);
847 return NULL;
848 }
849
850 return rettv.vval.v_list;
851}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
Bram Moolenaare70dd112022-01-21 16:31:11 +0000853#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100855 * Evaluate "arg", which is 'foldexpr'.
856 * Note: caller must set "curwin" to match "arg".
857 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
858 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 */
860 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000861eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000863 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000864 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200865 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000867 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000868 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
869 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870
Bram Moolenaare70dd112022-01-21 16:31:11 +0000871 arg = wp->w_p_fde;
872 current_sctx = wp->w_p_script_ctx[WV_FDE];
873
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000875 if (use_sandbox)
876 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100877 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200879 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 retval = 0;
881 else
882 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100883 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000884 if (tv.v_type == VAR_NUMBER)
885 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000886 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
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 string, check if there is a non-digit before
891 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000892 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (!VIM_ISDIGIT(*s) && *s != '-')
894 *cp = *s++;
895 retval = atol((char *)s);
896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000897 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 }
899 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000900 if (use_sandbox)
901 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100902 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200903 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000904 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200906 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907}
908#endif
909
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000911 * Get an lval: variable, Dict item or List item that can be assigned a value
912 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
913 * "name.key", "name.key[expr]" etc.
914 * Indexing only works if "name" is an existing List or Dictionary.
915 * "name" points to the start of the name.
916 * If "rettv" is not NULL it points to the value to be assigned.
917 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
918 * wrong; must end in space or cmd separator.
919 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100920 * flags:
921 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100922 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100923 * GLV_NO_AUTOLOAD: do not use script autoloading
924 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000925 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000926 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000927 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000928 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200929 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100930get_lval(
931 char_u *name,
932 typval_T *rettv,
933 lval_T *lp,
934 int unlet,
935 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100936 int flags, // GLV_ values
937 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000938{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000939 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 char_u *expr_start, *expr_end;
941 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000942 dictitem_T *v;
943 typval_T var1;
944 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000945 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000946 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000947 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100948 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100949 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100950 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000951 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000952
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100953 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200954 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000955
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100956 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000957 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100958 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000959 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200960 lp->ll_name_end = find_name_end(name, NULL, NULL,
961 FNE_INCL_BR | fne_flags);
962 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000963 }
964
Bram Moolenaara749a422022-02-12 19:52:25 +0000965 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000966 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000967 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
968 {
969 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
970 return NULL;
971 }
972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100973 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000974 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 if (expr_start != NULL)
977 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100978 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100979 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000980 && *p != '[' && *p != '.')
981 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000982 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000983 return NULL;
984 }
985
986 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
987 if (lp->ll_exp_name == NULL)
988 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100989 // Report an invalid expression in braces, unless the
990 // expression evaluation has been cancelled due to an
991 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 if (!aborting() && !quiet)
993 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000994 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000995 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 return NULL;
997 }
998 }
999 lp->ll_name = lp->ll_exp_name;
1000 }
1001 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001003 lp->ll_name = name;
1004
Bram Moolenaar4525a572022-02-13 11:57:33 +00001005 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001007 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001008 // However, "g:[key]" is indexing a dictionary.
1009 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001010 {
1011 --p;
1012 lp->ll_name_end = p;
1013 }
1014 if (*p == ':')
1015 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001016 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001017
Bram Moolenaar9510d222022-09-11 15:14:05 +01001018 if (is_scoped_variable(name))
1019 {
1020 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1021 return NULL;
1022 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001023 if (tp == p + 1 && !quiet)
1024 {
1025 semsg(_(e_white_space_required_after_str_str), ":", p);
1026 return NULL;
1027 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001028 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001029 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001030 semsg(_(e_using_type_not_in_script_context_str), p);
1031 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001032 }
1033
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001034 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001035 lp->ll_type = parse_type(&tp,
1036 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1037 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001038 if (lp->ll_type == NULL && !quiet)
1039 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001040 lp->ll_name_end = tp;
1041 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 }
1043 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001044 if (lp->ll_name == NULL)
1045 return p;
1046
Bram Moolenaar62aec932022-01-29 21:45:34 +00001047 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001048 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001049 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001050
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001051 if (import != NULL)
1052 {
1053 ufunc_T *ufunc;
1054 type_T *type;
1055
Bram Moolenaar753885b2022-08-24 16:30:36 +01001056 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001057 lp->ll_sid = import->imp_sid;
1058 lp->ll_name = skipwhite(p + 1);
1059 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1060 lp->ll_name_end = p;
1061
1062 // check the item is exported
1063 cc = *p;
1064 *p = NUL;
1065 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001066 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001067 {
1068 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001069 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001070 }
1071 *p = cc;
1072 }
1073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001075 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001076 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001077 return p;
1078
Bram Moolenaar4525a572022-02-13 11:57:33 +00001079 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001080 {
1081 // using local variable
1082 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001083 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001084 }
1085 else
1086 {
1087 cc = *p;
1088 *p = NUL;
1089 // When we would write to the variable pass &ht and prevent autoload.
1090 writing = !(flags & GLV_READ_ONLY);
1091 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001092 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001093 if (v == NULL && !quiet)
1094 semsg(_(e_undefined_variable_str), lp->ll_name);
1095 *p = cc;
1096 if (v == NULL)
1097 return NULL;
1098 lp->ll_tv = &v->di_tv;
1099 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001100
Bram Moolenaar4525a572022-02-13 11:57:33 +00001101 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001102 {
1103 if (!quiet)
1104 semsg(_(e_variable_already_declared), lp->ll_name);
1105 return NULL;
1106 }
1107
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001108 /*
1109 * Loop until no more [idx] or .key is following.
1110 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001111 var1.v_type = VAR_UNKNOWN;
1112 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001113 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001114 {
Bram Moolenaarf21d5462022-09-10 12:36:00 +01001115 int r = OK;
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001116
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001117 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1118 {
1119 if (!quiet)
1120 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1121 return NULL;
1122 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001123 if (lp->ll_tv->v_type != VAR_LIST
1124 && lp->ll_tv->v_type != VAR_DICT
1125 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001127 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001128 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001129 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001130 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001131
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001132 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaare65081d2021-06-27 15:04:05 +02001133 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001134 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaare65081d2021-06-27 15:04:05 +02001135 else if (lp->ll_tv->v_type == VAR_BLOB
1136 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001137 r = rettv_blob_alloc(lp->ll_tv);
1138 if (r == FAIL)
1139 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001140
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001143 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001144 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001146 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001147
Bram Moolenaar4525a572022-02-13 11:57:33 +00001148 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001149 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001150 && lp->ll_tv == &v->di_tv
1151 && ht != NULL && ht == get_script_local_ht())
1152 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001153 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001154
1155 // Vim9 script local variable: get the type
1156 if (sv != NULL)
1157 lp->ll_valtype = sv->sv_type;
1158 }
1159
Bram Moolenaar8c711452005-01-14 21:53:12 +00001160 len = -1;
1161 if (*p == '.')
1162 {
1163 key = p + 1;
1164 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1165 ;
1166 if (len == 0)
1167 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001168 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001169 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001170 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001171 }
1172 p = key + len;
1173 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001174 else
1175 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001176 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001177 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001178 if (*p == ':')
1179 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001180 else
1181 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001182 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001183 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001184 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001185 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001186 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001187 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001188 clear_tv(&var1);
1189 return NULL;
1190 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001191 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001192 }
1193
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001194 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001195 if (*p == ':')
1196 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001197 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001198 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001200 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001201 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001202 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001203 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001204 if (rettv != NULL
1205 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001206 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001207 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001208 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001209 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001211 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001212 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001213 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001214 }
1215 p = skipwhite(p + 1);
1216 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001217 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001218 else
1219 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001221 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001222 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001223 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001224 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001226 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001227 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001228 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001229 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001230 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001231 clear_tv(&var2);
1232 return NULL;
1233 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001235 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001236 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001237 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001239
Bram Moolenaar8c711452005-01-14 21:53:12 +00001240 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001241 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001243 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001244 clear_tv(&var1);
1245 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247 }
1248
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001249 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250 ++p;
1251 }
1252
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001253 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001254 {
1255 if (len == -1)
1256 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001257 // "[key]": get key from "var1"
1258 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001259 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001260 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001261 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001263 }
1264 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001265 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001266
1267 // a NULL dict is equivalent with an empty dict
1268 if (lp->ll_tv->vval.v_dict == NULL)
1269 {
1270 lp->ll_tv->vval.v_dict = dict_alloc();
1271 if (lp->ll_tv->vval.v_dict == NULL)
1272 {
1273 clear_tv(&var1);
1274 return NULL;
1275 }
1276 ++lp->ll_tv->vval.v_dict->dv_refcount;
1277 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001278 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001279
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001280 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001281
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001282 // When assigning to a scope dictionary check that a function and
1283 // variable name is valid (only variable name unless it is l: or
1284 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001285 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001286 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001287 int prevval;
1288 int wrong;
1289
1290 if (len != -1)
1291 {
1292 prevval = key[len];
1293 key[len] = NUL;
1294 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001295 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001296 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001297 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1298 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001299 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001300 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001301 if (len != -1)
1302 key[len] = prevval;
1303 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001304 {
1305 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001306 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001307 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001308 }
1309
Bram Moolenaar10c65862020-10-08 21:16:42 +02001310 if (lp->ll_valtype != NULL)
1311 // use the type of the member
1312 lp->ll_valtype = lp->ll_valtype->tt_member;
1313
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001314 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001315 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001316 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001317 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001318 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001319 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001320 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001321 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001322 return NULL;
1323 }
1324
Bram Moolenaar31b81602019-02-10 22:14:27 +01001325 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001326 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001327 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001328 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001329 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001330 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001331 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001332 }
1333 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001336 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001337 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001338 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001339 p = NULL;
1340 break;
1341 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001342 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001343 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001344 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1345 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001346 {
1347 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001348 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001349 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001350
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001351 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001352 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001353 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001354 else if (lp->ll_tv->v_type == VAR_BLOB)
1355 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001356 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1357
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001358 /*
1359 * Get the number and item for the only or first index of the List.
1360 */
1361 if (empty1)
1362 lp->ll_n1 = 0;
1363 else
1364 // is number or string
1365 lp->ll_n1 = (long)tv_get_number(&var1);
1366 clear_tv(&var1);
1367
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001368 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001369 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001370 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001371 return NULL;
1372 }
1373 if (lp->ll_range && !lp->ll_empty2)
1374 {
1375 lp->ll_n2 = (long)tv_get_number(&var2);
1376 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001377 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1378 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001379 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001380 }
1381 lp->ll_blob = lp->ll_tv->vval.v_blob;
1382 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001383 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001384 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001385 else
1386 {
1387 /*
1388 * Get the number and item for the only or first index of the List.
1389 */
1390 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001391 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001392 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001393 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001394 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001395 clear_tv(&var1);
1396
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001397 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001398 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001399 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1400 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001401 if (lp->ll_li == NULL)
1402 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001403 clear_tv(&var2);
1404 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001405 }
1406
Bram Moolenaar10c65862020-10-08 21:16:42 +02001407 if (lp->ll_valtype != NULL)
1408 // use the type of the member
1409 lp->ll_valtype = lp->ll_valtype->tt_member;
1410
Bram Moolenaar8c711452005-01-14 21:53:12 +00001411 /*
1412 * May need to find the item or absolute index for the second
1413 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001414 * When no index given: "lp->ll_empty2" is TRUE.
1415 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001416 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001418 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001419 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001420 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001421 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001422 if (check_range_index_two(lp->ll_list,
1423 &lp->ll_n1, lp->ll_li,
1424 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001425 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001426 }
1427
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001430 }
1431
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001432 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001434 return p;
1435}
1436
1437/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001438 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001439 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001440 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001441clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001442{
1443 vim_free(lp->ll_exp_name);
1444 vim_free(lp->ll_newkey);
1445}
1446
1447/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001448 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001449 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001450 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1451 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001452 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001453 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001454set_var_lval(
1455 lval_T *lp,
1456 char_u *endp,
1457 typval_T *rettv,
1458 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001459 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1460 char_u *op,
1461 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001462{
1463 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001464 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001465
1466 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001467 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001468 cc = *endp;
1469 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001470 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1471 return;
1472
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001473 if (lp->ll_blob != NULL)
1474 {
1475 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001476
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001477 if (op != NULL && *op != '=')
1478 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001479 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001480 return;
1481 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001482 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001483 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001484
1485 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1486 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001487 if (lp->ll_empty2)
1488 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1489
Bram Moolenaar68452172021-04-12 21:21:02 +02001490 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1491 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001492 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001493 }
1494 else
1495 {
1496 val = (int)tv_get_number_chk(rettv, &error);
1497 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001498 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001499 }
1500 }
1501 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001502 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001503 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001504
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001505 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1506 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001507 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001508 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001509 *endp = cc;
1510 return;
1511 }
1512
Bram Moolenaarff697e62019-02-12 22:28:33 +01001513 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001514 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001515 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001516 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001517 {
1518 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001519 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1520 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001521 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001522 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001523 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001524 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001526 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001527 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001528 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001529 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1530 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001531 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001532 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001533 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001534 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001535 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001536 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001537 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001538 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001539 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001540 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001541 else if (lp->ll_range)
1542 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001543 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1544 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001545 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001546 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001547 return;
1548 }
1549
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001550 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1551 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001552 }
1553 else
1554 {
1555 /*
1556 * Assign to a List or Dictionary item.
1557 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001558 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1559 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001560 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001561 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001562 return;
1563 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001564
1565 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001566 && check_typval_arg_type(lp->ll_valtype, rettv,
1567 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001568 return;
1569
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001570 if (lp->ll_newkey != NULL)
1571 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001572 if (op != NULL && *op != '=')
1573 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001574 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001575 return;
1576 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001577 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1578 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001579 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001580
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001581 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001582 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001583 if (di == NULL)
1584 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001585 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1586 {
1587 vim_free(di);
1588 return;
1589 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001590 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001591 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001592 else if (op != NULL && *op != '=')
1593 {
1594 tv_op(lp->ll_tv, rettv, op);
1595 return;
1596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001597 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001598 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001599
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001600 /*
1601 * Assign the value to the variable or list item.
1602 */
1603 if (copy)
1604 copy_tv(rettv, lp->ll_tv);
1605 else
1606 {
1607 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001608 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001609 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001610 }
1611 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001612}
1613
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001614/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001615 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1616 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001617 * Returns OK or FAIL.
1618 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001619 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001620tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001621{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001622 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001623 char_u numbuf[NUMBUFLEN];
1624 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001625 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001626
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001627 // Can't do anything with a Funcref or Dict on the right.
1628 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001629 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001630 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1631 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001632 {
1633 switch (tv1->v_type)
1634 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001635 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001636 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001638 case VAR_DICT:
1639 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001640 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001641 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001642 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001643 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001644 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001645 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001646 break;
1647
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001648 case VAR_BLOB:
1649 if (*op != '+' || tv2->v_type != VAR_BLOB)
1650 break;
1651 // BLOB += BLOB
1652 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1653 {
1654 blob_T *b1 = tv1->vval.v_blob;
1655 blob_T *b2 = tv2->vval.v_blob;
1656 int i, len = blob_len(b2);
1657 for (i = 0; i < len; i++)
1658 ga_append(&b1->bv_ga, blob_get(b2, i));
1659 }
1660 return OK;
1661
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001662 case VAR_LIST:
1663 if (*op != '+' || tv2->v_type != VAR_LIST)
1664 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001665 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001666 if (tv2->vval.v_list != NULL)
1667 {
1668 if (tv1->vval.v_list == NULL)
1669 {
1670 tv1->vval.v_list = tv2->vval.v_list;
1671 ++tv1->vval.v_list->lv_refcount;
1672 }
1673 else
1674 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1675 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001676 return OK;
1677
1678 case VAR_NUMBER:
1679 case VAR_STRING:
1680 if (tv2->v_type == VAR_LIST)
1681 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001682 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001683 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001684 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001685 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001686#ifdef FEAT_FLOAT
1687 if (tv2->v_type == VAR_FLOAT)
1688 {
1689 float_T f = n;
1690
Bram Moolenaarff697e62019-02-12 22:28:33 +01001691 if (*op == '%')
1692 break;
1693 switch (*op)
1694 {
1695 case '+': f += tv2->vval.v_float; break;
1696 case '-': f -= tv2->vval.v_float; break;
1697 case '*': f *= tv2->vval.v_float; break;
1698 case '/': f /= tv2->vval.v_float; break;
1699 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001700 clear_tv(tv1);
1701 tv1->v_type = VAR_FLOAT;
1702 tv1->vval.v_float = f;
1703 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001704 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001705#endif
1706 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001707 switch (*op)
1708 {
1709 case '+': n += tv_get_number(tv2); break;
1710 case '-': n -= tv_get_number(tv2); break;
1711 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001712 case '/': n = num_divide(n, tv_get_number(tv2),
1713 &failed); break;
1714 case '%': n = num_modulus(n, tv_get_number(tv2),
1715 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001716 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001717 clear_tv(tv1);
1718 tv1->v_type = VAR_NUMBER;
1719 tv1->vval.v_number = n;
1720 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001721 }
1722 else
1723 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001724 if (tv2->v_type == VAR_FLOAT)
1725 break;
1726
Bram Moolenaarff697e62019-02-12 22:28:33 +01001727 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001728 s = tv_get_string(tv1);
1729 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001730 clear_tv(tv1);
1731 tv1->v_type = VAR_STRING;
1732 tv1->vval.v_string = s;
1733 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001734 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001735
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001736 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001737#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001738 {
1739 float_T f;
1740
Bram Moolenaarff697e62019-02-12 22:28:33 +01001741 if (*op == '%' || *op == '.'
1742 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001743 && tv2->v_type != VAR_NUMBER
1744 && tv2->v_type != VAR_STRING))
1745 break;
1746 if (tv2->v_type == VAR_FLOAT)
1747 f = tv2->vval.v_float;
1748 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001749 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001750 switch (*op)
1751 {
1752 case '+': tv1->vval.v_float += f; break;
1753 case '-': tv1->vval.v_float -= f; break;
1754 case '*': tv1->vval.v_float *= f; break;
1755 case '/': tv1->vval.v_float /= f; break;
1756 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001757 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001758#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001759 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001760 }
1761 }
1762
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001763 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001764 return FAIL;
1765}
1766
1767/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001768 * Evaluate the expression used in a ":for var in expr" command.
1769 * "arg" points to "var".
1770 * Set "*errp" to TRUE for an error, FALSE otherwise;
1771 * Return a pointer that holds the info. Null when there is an error.
1772 */
1773 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001774eval_for_line(
1775 char_u *arg,
1776 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001777 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001778 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001779{
Bram Moolenaar33570922005-01-25 22:26:29 +00001780 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001781 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 typval_T tv;
1784 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001785 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001786
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001787 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001789 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001790 if (fi == NULL)
1791 return NULL;
1792
Bram Moolenaar404557e2021-07-05 21:41:48 +02001793 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1794 &fi->fi_semicolon, FALSE);
1795 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001796 return fi;
1797
Bram Moolenaar404557e2021-07-05 21:41:48 +02001798 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001799 if (expr[0] != 'i' || expr[1] != 'n'
1800 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001802 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1803 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1804 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001805 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001806 return fi;
1807 }
1808
1809 if (skip)
1810 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001811 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1812 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001813 {
1814 *errp = FALSE;
1815 if (!skip)
1816 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001817 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001818 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001819 l = tv.vval.v_list;
1820 if (l == NULL)
1821 {
1822 // a null list is like an empty list: do nothing
1823 clear_tv(&tv);
1824 }
1825 else
1826 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001828 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001830 // No need to increment the refcount, it's already set for
1831 // the list being used in "tv".
1832 fi->fi_list = l;
1833 list_add_watch(l, &fi->fi_lw);
1834 fi->fi_lw.lw_item = l->lv_first;
1835 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001836 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001837 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001838 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001839 fi->fi_bi = 0;
1840 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001841 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001842 typval_T btv;
1843
1844 // Make a copy, so that the iteration still works when the
1845 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001847 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001848 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001849 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001850 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001851 else if (tv.v_type == VAR_STRING)
1852 {
1853 fi->fi_byte_idx = 0;
1854 fi->fi_string = tv.vval.v_string;
1855 tv.vval.v_string = NULL;
1856 if (fi->fi_string == NULL)
1857 fi->fi_string = vim_strsave((char_u *)"");
1858 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 else
1860 {
Sean Dewar80d73952021-08-04 19:25:54 +02001861 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001862 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001863 }
1864 }
1865 }
1866 if (skip)
1867 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001868 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001869
1870 return fi;
1871}
1872
1873/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001874 * Used when looping over a :for line, skip the "in expr" part.
1875 */
1876 void
1877skip_for_lines(void *fi_void, evalarg_T *evalarg)
1878{
1879 forinfo_T *fi = (forinfo_T *)fi_void;
1880 int i;
1881
1882 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001883 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001884}
1885
1886/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 * Use the first item in a ":for" list. Advance to the next.
1888 * Assign the values to the variable (list). "arg" points to the first one.
1889 * Return TRUE when a valid item was found, FALSE when at end of list or
1890 * something wrong.
1891 */
1892 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001893next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001894{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001895 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001897 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001898 ? (ASSIGN_FINAL
1899 // first round: error if variable exists
1900 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01001901 | ASSIGN_NO_MEMBER_TYPE
1902 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001903 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001904 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001905 int skip_assign = in_vim9script() && arg[0] == '_'
1906 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001908 if (fi->fi_blob != NULL)
1909 {
1910 typval_T tv;
1911
1912 if (fi->fi_bi >= blob_len(fi->fi_blob))
1913 return FALSE;
1914 tv.v_type = VAR_NUMBER;
1915 tv.v_lock = VAR_FIXED;
1916 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1917 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001918 if (skip_assign)
1919 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001920 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001921 fi->fi_varcount, flag, NULL) == OK;
1922 }
1923
1924 if (fi->fi_string != NULL)
1925 {
1926 typval_T tv;
1927 int len;
1928
1929 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1930 if (len == 0)
1931 return FALSE;
1932 tv.v_type = VAR_STRING;
1933 tv.v_lock = VAR_FIXED;
1934 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1935 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001936 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001937 if (skip_assign)
1938 result = TRUE;
1939 else
1940 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001941 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001942 vim_free(tv.vval.v_string);
1943 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001944 }
1945
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 item = fi->fi_lw.lw_item;
1947 if (item == NULL)
1948 result = FALSE;
1949 else
1950 {
1951 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001952 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001953 if (skip_assign)
1954 result = TRUE;
1955 else
1956 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001957 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 }
1959 return result;
1960}
1961
1962/*
1963 * Free the structure used to store info used by ":for".
1964 */
1965 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001966free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967{
Bram Moolenaar33570922005-01-25 22:26:29 +00001968 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001969
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001970 if (fi == NULL)
1971 return;
1972 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001973 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001975 list_unref(fi->fi_list);
1976 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001977 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001978 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001979 else
1980 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 vim_free(fi);
1982}
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001985set_context_for_expression(
1986 expand_T *xp,
1987 char_u *arg,
1988 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001990 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001994 if (cmdidx == CMD_let || cmdidx == CMD_var
1995 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 {
1997 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001998 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002000 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002001 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 {
2003 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002004 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002005 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 break;
2007 }
2008 return;
2009 }
2010 }
2011 else
2012 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2013 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 while ((xp->xp_pattern = vim_strpbrk(arg,
2015 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2016 {
2017 c = *xp->xp_pattern;
2018 if (c == '&')
2019 {
2020 c = xp->xp_pattern[1];
2021 if (c == '&')
2022 {
2023 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002024 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
2026 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002029 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2030 xp->xp_pattern += 2;
2031
2032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 }
2034 else if (c == '$')
2035 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002036 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 xp->xp_context = EXPAND_ENV_VARS;
2038 }
2039 else if (c == '=')
2040 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002041 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 xp->xp_context = EXPAND_EXPRESSION;
2043 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002044 else if (c == '#'
2045 && xp->xp_context == EXPAND_EXPRESSION)
2046 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002047 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002048 break;
2049 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002050 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 && xp->xp_context == EXPAND_FUNCTIONS
2052 && vim_strchr(xp->xp_pattern, '(') == NULL)
2053 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002054 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 break;
2056 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002057 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002059 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
2061 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2062 if (c == '\\' && xp->xp_pattern[1] != NUL)
2063 ++xp->xp_pattern;
2064 xp->xp_context = EXPAND_NOTHING;
2065 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002066 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002068 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2070 /* skip */ ;
2071 xp->xp_context = EXPAND_NOTHING;
2072 }
2073 else if (c == '|')
2074 {
2075 if (xp->xp_pattern[1] == '|')
2076 {
2077 ++xp->xp_pattern;
2078 xp->xp_context = EXPAND_EXPRESSION;
2079 }
2080 else
2081 xp->xp_context = EXPAND_COMMANDS;
2082 }
2083 else
2084 xp->xp_context = EXPAND_EXPRESSION;
2085 }
2086 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002087 // Doesn't look like something valid, expand as an expression
2088 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002089 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 arg = xp->xp_pattern;
2091 if (*arg != NUL)
2092 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2093 /* skip */ ;
2094 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002095
2096 // ":exe one two" completes "two"
2097 if ((cmdidx == CMD_execute
2098 || cmdidx == CMD_echo
2099 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002100 || cmdidx == CMD_echomsg
2101 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002102 && xp->xp_context == EXPAND_EXPRESSION)
2103 {
2104 for (;;)
2105 {
2106 char_u *n = skiptowhite(arg);
2107
2108 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2109 break;
2110 arg = skipwhite(n);
2111 }
2112 }
2113
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 xp->xp_pattern = arg;
2115}
2116
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002118 * Return TRUE if "pat" matches "text".
2119 * Does not use 'cpo' and always uses 'magic'.
2120 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002121 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002122pattern_match(char_u *pat, char_u *text, int ic)
2123{
2124 int matches = FALSE;
2125 char_u *save_cpo;
2126 regmatch_T regmatch;
2127
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002128 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002129 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002130 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002131 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2132 if (regmatch.regprog != NULL)
2133 {
2134 regmatch.rm_ic = ic;
2135 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2136 vim_regfree(regmatch.regprog);
2137 }
2138 p_cpo = save_cpo;
2139 return matches;
2140}
2141
2142/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002143 * Handle a name followed by "(". Both for just "name(arg)" and for
2144 * "expr->name(arg)".
2145 * Returns OK or FAIL.
2146 */
2147 static int
2148eval_func(
2149 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002150 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002151 char_u *name,
2152 int name_len,
2153 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002154 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002155 typval_T *basetv) // "expr" for "expr->name(arg)"
2156{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002157 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002158 char_u *s = name;
2159 int len = name_len;
2160 partial_T *partial;
2161 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002162 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002163 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002164
2165 if (!evaluate)
2166 check_vars(s, len);
2167
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002168 // If "s" is the name of a variable of type VAR_FUNC
2169 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002170 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002171 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002172
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002173 // Need to make a copy, in case evaluating the arguments makes
2174 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002175 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002176 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002177 ret = FAIL;
2178 else
2179 {
2180 funcexe_T funcexe;
2181
2182 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002183 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002184 funcexe.fe_firstline = curwin->w_cursor.lnum;
2185 funcexe.fe_lastline = curwin->w_cursor.lnum;
2186 funcexe.fe_evaluate = evaluate;
2187 funcexe.fe_partial = partial;
2188 funcexe.fe_basetv = basetv;
2189 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002190 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002191 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002192 }
2193 vim_free(s);
2194
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002195 // If evaluate is FALSE rettv->v_type was not set in
2196 // get_func_tv, but it's needed in handle_subscript() to parse
2197 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002198 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2199 {
2200 rettv->vval.v_string = NULL;
2201 rettv->v_type = VAR_FUNC;
2202 }
2203
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002204 // Stop the expression evaluation when immediately
2205 // aborting on error, or when an interrupt occurred or
2206 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002207 if (evaluate && aborting())
2208 {
2209 if (ret == OK)
2210 clear_tv(rettv);
2211 ret = FAIL;
2212 }
2213 return ret;
2214}
2215
2216/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002217 * After a NL, skip over empty lines and comment-only lines.
2218 */
2219 static char_u *
2220newline_skip_comments(char_u *arg)
2221{
2222 char_u *p = arg + 1;
2223
2224 for (;;)
2225 {
2226 p = skipwhite(p);
2227
2228 if (*p == NUL)
2229 break;
2230 if (vim9_comment_start(p))
2231 {
2232 char_u *nl = vim_strchr(p, NL);
2233
2234 if (nl == NULL)
2235 break;
2236 p = nl;
2237 }
2238 if (*p != NL)
2239 break;
2240 ++p; // skip another NL
2241 }
2242 return p;
2243}
2244
2245/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002246 * Get the next line source line without advancing. But do skip over comment
2247 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002248 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002249 */
2250 static char_u *
2251getline_peek_skip_comments(evalarg_T *evalarg)
2252{
2253 for (;;)
2254 {
2255 char_u *next = getline_peek(evalarg->eval_getline,
2256 evalarg->eval_cookie);
2257 char_u *p;
2258
2259 if (next == NULL)
2260 break;
2261 p = skipwhite(next);
2262 if (*p != NUL && !vim9_comment_start(p))
2263 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002264 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002265 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002266 }
2267 return NULL;
2268}
2269
2270/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002271 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2272 * comment) and there is a next line, return the next line (skipping blanks)
2273 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002274 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2275 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002276 * "arg" must point somewhere inside a line, not at the start.
2277 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002278 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002279eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2280{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002281 char_u *p = skipwhite(arg);
2282
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002283 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002284 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002285 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002286 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2287 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002288 && (*p == NUL || *p == NL
2289 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002290 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002291 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002292
Bram Moolenaare442d592022-05-05 12:20:28 +01002293 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002294 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002295 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002296 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002297 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002298 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002299
Bram Moolenaar9d489562020-07-30 20:08:50 +02002300 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002301 {
2302 *getnext = TRUE;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002303 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002304 }
2305 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002306 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002307}
2308
2309/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002310 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002311 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002312 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002313 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002314eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002315{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002316 garray_T *gap = &evalarg->eval_ga;
2317 char_u *line;
2318
Bram Moolenaar39be4982022-05-06 21:51:50 +01002319 if (arg != NULL)
2320 {
2321 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002322 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002323 // Truncate before a trailing comment, so that concatenating the lines
2324 // won't turn the rest into a comment.
2325 if (*skipwhite(arg) == '#')
2326 *arg = NUL;
2327 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002328
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002329 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002330 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2331 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002332 else
2333 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002334 if (line == NULL)
2335 return NULL;
2336
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002337 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002338 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2339 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002340 char_u *p = skipwhite(line);
2341
2342 // Going to concatenate the lines after parsing. For an empty or
2343 // comment line use an empty string.
2344 if (*p == NUL || vim9_comment_start(p))
2345 {
2346 vim_free(line);
2347 line = vim_strsave((char_u *)"");
2348 }
2349
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002350 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2351 ++gap->ga_len;
2352 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002353 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002354 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002355 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002356 evalarg->eval_tofree = line;
2357 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002358
2359 // Advanced to the next line, "arg" no longer points into the previous
2360 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002361 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002362 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002363}
2364
Bram Moolenaare6b53242020-07-01 17:28:33 +02002365/*
2366 * Call eval_next_non_blank() and get the next line if needed.
2367 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002368 char_u *
2369skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2370{
2371 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002372 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002373
Bram Moolenaar962d7212020-07-04 14:15:00 +02002374 if (evalarg == NULL)
2375 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002376 eval_next_non_blank(p, evalarg, &getnext);
2377 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002378 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002379 return p;
2380}
2381
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2386 */
2387
2388/*
2389 * Handle zero level expression.
2390 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002391 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002392 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002393 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 * Return OK or FAIL.
2395 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002397eval0(
2398 char_u *arg,
2399 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002400 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002401 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002403 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2404}
2405
2406/*
2407 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2408 * expression and don't check what comes after the expression.
2409 */
2410 int
2411eval0_retarg(
2412 char_u *arg,
2413 typval_T *rettv,
2414 exarg_T *eap,
2415 evalarg_T *evalarg,
2416 char_u **retarg)
2417{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 int ret;
2419 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002420 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002421 int did_emsg_before = did_emsg;
2422 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002423 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002424 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002425 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
2427 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002428 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002429
Bram Moolenaar79481362022-06-27 20:15:10 +01002430 if (ret != FAIL)
2431 {
2432 expr_end = p;
2433 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002434
Bram Moolenaar79481362022-06-27 20:15:10 +01002435 // In Vim9 script a command block is not split at NL characters for
2436 // commands using an expression argument. Skip over a '#' comment to
2437 // check for a following NL. Require white space before the '#'.
2438 if (in_vim9script() && p > expr_end && retarg == NULL)
2439 while (*p == '#')
2440 {
2441 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002442
Bram Moolenaar79481362022-06-27 20:15:10 +01002443 if (nl == NULL)
2444 break;
2445 p = skipwhite(nl + 1);
2446 if (eap != NULL && *p != NUL)
2447 eap->nextcmd = p;
2448 check_for_end = FALSE;
2449 }
2450
2451 if (check_for_end)
2452 end_error = !ends_excmd2(arg, p);
2453 }
2454
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002455 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
2457 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002458 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 /*
2460 * Report the invalid expression unless the expression evaluation has
2461 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002462 * exception, or we already gave a more specific error.
2463 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002465 if (!aborting()
2466 && did_emsg == did_emsg_before
2467 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002468 && (flags & EVAL_CONSTANT) == 0
2469 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002470 {
2471 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002472 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002473 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002474 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002475 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002476
2477 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002478 // a next command to avoid more errors, unless "|" is following, which
2479 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002480 if (eap != NULL && p != NULL
2481 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002482 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002483 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002485
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002486 if (retarg != NULL)
2487 *retarg = p;
2488 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002489 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002490
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 return ret;
2492}
2493
2494/*
2495 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002496 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002497 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 *
2499 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002500 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002502 * Note: "rettv.v_lock" is not set.
2503 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 * Return OK or FAIL.
2505 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002506 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002507eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002509 char_u *p;
2510 int getnext;
2511
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002512 CLEAR_POINTER(rettv);
2513
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 /*
2515 * Get the first variable.
2516 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002517 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 return FAIL;
2519
Bram Moolenaar793648f2020-06-26 21:28:25 +02002520 p = eval_next_non_blank(*arg, evalarg, &getnext);
2521 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002523 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002524 int result;
2525 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002526 evalarg_T *evalarg_used = evalarg;
2527 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002529 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002530 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002531
2532 if (evalarg == NULL)
2533 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002534 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002535 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002536 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002537 orig_flags = evalarg_used->eval_flags;
2538 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002539
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002540 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002541 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002542 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002543 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002544 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002545 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002546 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002547 clear_tv(rettv);
2548 return FAIL;
2549 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002550 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002551 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002552
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002554 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002556 int error = FALSE;
2557
Bram Moolenaar13106602020-10-04 16:06:05 +02002558 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002559 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002560 else if (vim9script)
2561 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002562 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002564 if (error || !op_falsy || !result)
2565 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002566 if (error)
2567 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569
2570 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002571 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002573 if (op_falsy)
2574 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002575 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002576 {
mityu4ac198c2021-05-28 17:52:40 +02002577 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002578 clear_tv(rettv);
2579 return FAIL;
2580 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002581 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002582 evalarg_used->eval_flags = (op_falsy ? !result : result)
2583 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2584 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002585 {
2586 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002588 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002589 if (!op_falsy || !result)
2590 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002592 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002594 /*
2595 * Check for the ":".
2596 */
2597 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2598 if (*p != ':')
2599 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002600 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002601 if (evaluate && result)
2602 clear_tv(rettv);
2603 evalarg_used->eval_flags = orig_flags;
2604 return FAIL;
2605 }
2606 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002607 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002608 else
2609 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002610 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002611 {
2612 error_white_both(p, 1);
2613 clear_tv(rettv);
2614 evalarg_used->eval_flags = orig_flags;
2615 return FAIL;
2616 }
2617 *arg = p;
2618 }
2619
2620 /*
2621 * Get the third variable. Recursive!
2622 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002623 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002624 {
mityu4ac198c2021-05-28 17:52:40 +02002625 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002626 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002627 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002628 return FAIL;
2629 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002630 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2631 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002632 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002633 if (eval1(arg, &var2, evalarg_used) == FAIL)
2634 {
2635 if (evaluate && result)
2636 clear_tv(rettv);
2637 evalarg_used->eval_flags = orig_flags;
2638 return FAIL;
2639 }
2640 if (evaluate && !result)
2641 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002643
2644 if (evalarg == NULL)
2645 clear_evalarg(&local_evalarg, NULL);
2646 else
2647 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649
2650 return OK;
2651}
2652
2653/*
2654 * Handle first level expression:
2655 * expr2 || expr2 || expr2 logical OR
2656 *
2657 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002658 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 *
2660 * Return OK or FAIL.
2661 */
2662 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002663eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002665 char_u *p;
2666 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
2668 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002669 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002671 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 return FAIL;
2673
2674 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002675 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002677 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002678 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002680 evalarg_T *evalarg_used = evalarg;
2681 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002682 int evaluate;
2683 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002684 long result = FALSE;
2685 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002686 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002687 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002688
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002689 if (evalarg == NULL)
2690 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002691 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002692 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002693 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002694 orig_flags = evalarg_used->eval_flags;
2695 evaluate = orig_flags & EVAL_EVALUATE;
2696 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002697 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002698 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002699 result = tv_get_bool_chk(rettv, &error);
2700 else if (tv_get_number_chk(rettv, &error) != 0)
2701 result = TRUE;
2702 clear_tv(rettv);
2703 if (error)
2704 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 }
2706
2707 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002708 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002710 while (p[0] == '|' && p[1] == '|')
2711 {
2712 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002713 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002714 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002715 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002716 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002717 {
2718 error_white_both(p, 2);
2719 clear_tv(rettv);
2720 return FAIL;
2721 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002722 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002723 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002724
2725 /*
2726 * Get the second variable.
2727 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002728 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002729 {
mityu4ac198c2021-05-28 17:52:40 +02002730 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002731 clear_tv(rettv);
2732 return FAIL;
2733 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002734 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2735 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002736 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002737 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002738 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002739
2740 /*
2741 * Compute the result.
2742 */
2743 if (evaluate && !result)
2744 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002745 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002746 result = tv_get_bool_chk(&var2, &error);
2747 else if (tv_get_number_chk(&var2, &error) != 0)
2748 result = TRUE;
2749 clear_tv(&var2);
2750 if (error)
2751 return FAIL;
2752 }
2753 if (evaluate)
2754 {
2755 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002756 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002757 rettv->v_type = VAR_BOOL;
2758 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002759 }
2760 else
2761 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002762 rettv->v_type = VAR_NUMBER;
2763 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002764 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002765 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002766
2767 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002769
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002770 if (evalarg == NULL)
2771 clear_evalarg(&local_evalarg, NULL);
2772 else
2773 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775
2776 return OK;
2777}
2778
2779/*
2780 * Handle second level expression:
2781 * expr3 && expr3 && expr3 logical AND
2782 *
2783 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002784 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 *
2786 * Return OK or FAIL.
2787 */
2788 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002789eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002791 char_u *p;
2792 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
2794 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002795 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002797 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 return FAIL;
2799
2800 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002801 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002803 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002804 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002806 evalarg_T *evalarg_used = evalarg;
2807 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002808 int orig_flags;
2809 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002810 long result = TRUE;
2811 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002812 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002813 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002814
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002815 if (evalarg == NULL)
2816 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002817 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002818 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002819 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002820 orig_flags = evalarg_used->eval_flags;
2821 evaluate = orig_flags & EVAL_EVALUATE;
2822 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002823 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002824 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002825 result = tv_get_bool_chk(rettv, &error);
2826 else if (tv_get_number_chk(rettv, &error) == 0)
2827 result = FALSE;
2828 clear_tv(rettv);
2829 if (error)
2830 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 }
2832
2833 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002834 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002836 while (p[0] == '&' && p[1] == '&')
2837 {
2838 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002839 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002840 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002841 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002842 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002843 {
2844 error_white_both(p, 2);
2845 clear_tv(rettv);
2846 return FAIL;
2847 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002848 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002849 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002850
2851 /*
2852 * Get the second variable.
2853 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002854 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002855 {
mityu4ac198c2021-05-28 17:52:40 +02002856 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002857 clear_tv(rettv);
2858 return FAIL;
2859 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002860 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2861 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002862 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002863 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002864 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002865 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002866
2867 /*
2868 * Compute the result.
2869 */
2870 if (evaluate && result)
2871 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002872 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002873 result = tv_get_bool_chk(&var2, &error);
2874 else if (tv_get_number_chk(&var2, &error) == 0)
2875 result = FALSE;
2876 clear_tv(&var2);
2877 if (error)
2878 return FAIL;
2879 }
2880 if (evaluate)
2881 {
2882 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002883 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002884 rettv->v_type = VAR_BOOL;
2885 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002886 }
2887 else
2888 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002889 rettv->v_type = VAR_NUMBER;
2890 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002891 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002892 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002893
2894 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002896
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002897 if (evalarg == NULL)
2898 clear_evalarg(&local_evalarg, NULL);
2899 else
2900 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 }
2902
2903 return OK;
2904}
2905
2906/*
2907 * Handle third level expression:
2908 * var1 == var2
2909 * var1 =~ var2
2910 * var1 != var2
2911 * var1 !~ var2
2912 * var1 > var2
2913 * var1 >= var2
2914 * var1 < var2
2915 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002916 * var1 is var2
2917 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 *
2919 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002920 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 *
2922 * Return OK or FAIL.
2923 */
2924 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002925eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002928 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002929 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002931 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
2933 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002934 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002936 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 return FAIL;
2938
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002939 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002940
Bram Moolenaar696ba232020-07-29 21:20:41 +02002941 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942
2943 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002944 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002946 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002948 typval_T var2;
2949 int ic;
2950 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002951 int evaluate = evalarg == NULL
2952 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002953 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002954
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002955 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002956 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002957 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002958 p = *arg;
2959 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002960 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2961 {
mityu4ac198c2021-05-28 17:52:40 +02002962 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002963 clear_tv(rettv);
2964 return FAIL;
2965 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002966
Bram Moolenaar696ba232020-07-29 21:20:41 +02002967 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2968 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002969 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002970 clear_tv(rettv);
2971 return FAIL;
2972 }
2973
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002974 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 if (p[len] == '?')
2976 {
2977 ic = TRUE;
2978 ++len;
2979 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002980 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 else if (p[len] == '#')
2982 {
2983 ic = FALSE;
2984 ++len;
2985 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002986 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002988 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
2990 /*
2991 * Get the second variable.
2992 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002993 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2994 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002995 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002996 clear_tv(rettv);
2997 return FAIL;
2998 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002999 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003000 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003002 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 return FAIL;
3004 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003005 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003006 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003007 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003008
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003009 // use the line of the comparison for messages
3010 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003011 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003012 {
3013 ret = FAIL;
3014 clear_tv(rettv);
3015 }
3016 else
3017 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003018 clear_tv(&var2);
3019 return ret;
3020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 }
3022
3023 return OK;
3024}
3025
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003026/*
3027 * Make a copy of blob "tv1" and append blob "tv2".
3028 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 void
3030eval_addblob(typval_T *tv1, typval_T *tv2)
3031{
3032 blob_T *b1 = tv1->vval.v_blob;
3033 blob_T *b2 = tv2->vval.v_blob;
3034 blob_T *b = blob_alloc();
3035 int i;
3036
3037 if (b != NULL)
3038 {
3039 for (i = 0; i < blob_len(b1); i++)
3040 ga_append(&b->bv_ga, blob_get(b1, i));
3041 for (i = 0; i < blob_len(b2); i++)
3042 ga_append(&b->bv_ga, blob_get(b2, i));
3043
3044 clear_tv(tv1);
3045 rettv_blob_set(tv1, b);
3046 }
3047}
3048
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003049/*
3050 * Make a copy of list "tv1" and append list "tv2".
3051 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 int
3053eval_addlist(typval_T *tv1, typval_T *tv2)
3054{
3055 typval_T var3;
3056
3057 // concatenate Lists
3058 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3059 {
3060 clear_tv(tv1);
3061 clear_tv(tv2);
3062 return FAIL;
3063 }
3064 clear_tv(tv1);
3065 *tv1 = var3;
3066 return OK;
3067}
3068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003070 * Handle the bitwise left/right shift operator expression:
3071 * var1 << var2
3072 * var1 >> var2
3073 *
3074 * "arg" must point to the first non-white of the expression.
3075 * "arg" is advanced to just after the recognized expression.
3076 *
3077 * Return OK or FAIL.
3078 */
3079 static int
3080eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3081{
3082 /*
3083 * Get the first expression.
3084 */
3085 if (eval6(arg, rettv, evalarg) == FAIL)
3086 return FAIL;
3087
3088 /*
3089 * Repeat computing, until no '<<' or '>>' is following.
3090 */
3091 for (;;)
3092 {
3093 char_u *p;
3094 int getnext;
3095 exprtype_T type;
3096 int evaluate;
3097 typval_T var2;
3098 int vim9script;
3099
3100 p = eval_next_non_blank(*arg, evalarg, &getnext);
3101 if (p[0] == '<' && p[1] == '<')
3102 type = EXPR_LSHIFT;
3103 else if (p[0] == '>' && p[1] == '>')
3104 type = EXPR_RSHIFT;
3105 else
3106 return OK;
3107
3108 // Handle a bitwise left or right shift operator
3109 if (rettv->v_type != VAR_NUMBER)
3110 {
3111 // left operand should be a number
3112 emsg(_(e_bitshift_ops_must_be_number));
3113 clear_tv(rettv);
3114 return FAIL;
3115 }
3116
3117 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3118 vim9script = in_vim9script();
3119 if (getnext)
3120 {
3121 *arg = eval_next_line(*arg, evalarg);
3122 p = *arg;
3123 }
3124 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3125 {
3126 error_white_both(*arg, 2);
3127 clear_tv(rettv);
3128 return FAIL;
3129 }
3130
3131 /*
3132 * Get the second variable.
3133 */
3134 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3135 {
3136 error_white_both(p, 2);
3137 clear_tv(rettv);
3138 return FAIL;
3139 }
3140 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3141 if (eval6(arg, &var2, evalarg) == FAIL)
3142 {
3143 clear_tv(rettv);
3144 return FAIL;
3145 }
3146
3147 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3148 {
3149 // right operand should be a positive number
3150 if (var2.v_type != VAR_NUMBER)
3151 emsg(_(e_bitshift_ops_must_be_number));
3152 else
3153 emsg(_(e_bitshift_ops_must_be_postive));
3154 clear_tv(rettv);
3155 clear_tv(&var2);
3156 return FAIL;
3157 }
3158
3159 if (evaluate)
3160 {
3161 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3162 // shifting more bits than we have always results in zero
3163 rettv->vval.v_number = 0;
3164 else if (type == EXPR_LSHIFT)
3165 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003166 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003167 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003168 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003169 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003170 }
3171
3172 clear_tv(&var2);
3173 }
3174
3175 return OK;
3176}
3177
3178/*
3179 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003180 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003182 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003183 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 *
3185 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003186 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 *
3188 * Return OK or FAIL.
3189 */
3190 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003191eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003194 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003196 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 return FAIL;
3198
3199 /*
3200 * Repeat computing, until no '+', '-' or '.' is following.
3201 */
3202 for (;;)
3203 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003204 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003205 int getnext;
3206 char_u *p;
3207 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003208 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003209 int concat;
3210 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003211 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003212
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003213 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003214 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003215 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003216 p = eval_next_non_blank(*arg, evalarg, &getnext);
3217 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003218 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003219 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3220 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003222 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3223 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003224
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003225 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003226 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003227 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003228 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003229 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003230 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003231 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003232 {
mityu4ac198c2021-05-28 17:52:40 +02003233 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003234 clear_tv(rettv);
3235 return FAIL;
3236 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003237 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003238 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003239 if ((op != '+' || (rettv->v_type != VAR_LIST
3240 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003241#ifdef FEAT_FLOAT
3242 && (op == '.' || rettv->v_type != VAR_FLOAT)
3243#endif
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003244 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003245 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003246 int error = FALSE;
3247
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003248 // For "list + ...", an illegal use of the first operand as
3249 // a number cannot be determined before evaluating the 2nd
3250 // operand: if this is also a list, all is ok.
3251 // For "something . ...", "something - ..." or "non-list + ...",
3252 // we know that the first operand needs to be a string or number
3253 // without evaluating the 2nd operand. So check before to avoid
3254 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003255 if (op != '.')
3256 tv_get_number_chk(rettv, &error);
3257 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003258 {
3259 clear_tv(rettv);
3260 return FAIL;
3261 }
3262 }
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 /*
3265 * Get the second variable.
3266 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003267 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003268 {
mityu89dcb4d2021-05-28 13:50:17 +02003269 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003270 clear_tv(rettv);
3271 return FAIL;
3272 }
3273 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003274 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003276 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 return FAIL;
3278 }
3279
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003280 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 {
3282 /*
3283 * Compute the result.
3284 */
3285 if (op == '.')
3286 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003287 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3288 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003289 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003290
Bram Moolenaar2e086612020-08-25 22:37:48 +02003291 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003292 || var2.v_type == VAR_CHANNEL
3293 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003294 semsg(_(e_using_invalid_value_as_string_str),
3295 vartype_name(var2.v_type));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003296#ifdef FEAT_FLOAT
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 }
3303#endif
3304 else
3305 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003306 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003307 {
3308 clear_tv(rettv);
3309 clear_tv(&var2);
3310 return FAIL;
3311 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003312 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003313 clear_tv(rettv);
3314 rettv->v_type = VAR_STRING;
3315 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003317 else if (op == '+' && rettv->v_type == VAR_BLOB
3318 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003320 else if (op == '+' && rettv->v_type == VAR_LIST
3321 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003322 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003323 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003324 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 else
3327 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003328 int error = FALSE;
3329 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003330#ifdef FEAT_FLOAT
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003331 float_T f1 = 0, f2 = 0;
3332
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003333 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003334 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003335 f1 = rettv->vval.v_float;
3336 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003337 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003338 else
3339#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003340 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003341 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003342 if (error)
3343 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003344 // This can only happen for "list + non-list" or
3345 // "blob + non-blob". For "non-list + ..." or
3346 // "something - ...", we returned before evaluating the
3347 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003348 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003349 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003350 return FAIL;
3351 }
3352#ifdef FEAT_FLOAT
3353 if (var2.v_type == VAR_FLOAT)
3354 f1 = n1;
3355#endif
3356 }
3357#ifdef FEAT_FLOAT
3358 if (var2.v_type == VAR_FLOAT)
3359 {
3360 f2 = var2.vval.v_float;
3361 n2 = 0;
3362 }
3363 else
3364#endif
3365 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003366 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003367 if (error)
3368 {
3369 clear_tv(rettv);
3370 clear_tv(&var2);
3371 return FAIL;
3372 }
3373#ifdef FEAT_FLOAT
3374 if (rettv->v_type == VAR_FLOAT)
3375 f2 = n2;
3376#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003377 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003378 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003379
3380#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003381 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003382 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3383 {
3384 if (op == '+')
3385 f1 = f1 + f2;
3386 else
3387 f1 = f1 - f2;
3388 rettv->v_type = VAR_FLOAT;
3389 rettv->vval.v_float = f1;
3390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003392#endif
3393 {
3394 if (op == '+')
3395 n1 = n1 + n2;
3396 else
3397 n1 = n1 - n2;
3398 rettv->v_type = VAR_NUMBER;
3399 rettv->vval.v_number = n1;
3400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003402 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
3404 }
3405 return OK;
3406}
3407
3408/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003409 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 * * number multiplication
3411 * / number division
3412 * % number modulo
3413 *
3414 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003415 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 *
3417 * Return OK or FAIL.
3418 */
3419 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003420eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003421 char_u **arg,
3422 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003423 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003424 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003426#ifdef FEAT_FLOAT
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003427 int use_float = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003431 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003433 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 return FAIL;
3435
3436 /*
3437 * Repeat computing, until no '*', '/' or '%' is following.
3438 */
3439 for (;;)
3440 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003441 int evaluate;
3442 int getnext;
3443 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003444 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003445 int op;
3446 varnumber_T n1, n2;
3447#ifdef FEAT_FLOAT
3448 float_T f1, f2;
3449#endif
3450 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003451
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003452 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003453 p = eval_next_non_blank(*arg, evalarg, &getnext);
3454 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003455 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003457
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003458 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003459 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003460 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003461 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003462 {
3463 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3464 {
mityu4ac198c2021-05-28 17:52:40 +02003465 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003466 clear_tv(rettv);
3467 return FAIL;
3468 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003469 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003472#ifdef FEAT_FLOAT
3473 f1 = 0;
3474 f2 = 0;
3475#endif
3476 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003477 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003479#ifdef FEAT_FLOAT
3480 if (rettv->v_type == VAR_FLOAT)
3481 {
3482 f1 = rettv->vval.v_float;
3483 use_float = TRUE;
3484 n1 = 0;
3485 }
3486 else
3487#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003488 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003489 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003490 if (error)
3491 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493 else
3494 n1 = 0;
3495
3496 /*
3497 * Get the second variable.
3498 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003499 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3500 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003501 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003502 clear_tv(rettv);
3503 return FAIL;
3504 }
3505 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003506 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 return FAIL;
3508
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003509 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003511#ifdef FEAT_FLOAT
3512 if (var2.v_type == VAR_FLOAT)
3513 {
3514 if (!use_float)
3515 {
3516 f1 = n1;
3517 use_float = TRUE;
3518 }
3519 f2 = var2.vval.v_float;
3520 n2 = 0;
3521 }
3522 else
3523#endif
3524 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003525 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003526 clear_tv(&var2);
3527 if (error)
3528 return FAIL;
3529#ifdef FEAT_FLOAT
3530 if (use_float)
3531 f2 = n2;
3532#endif
3533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
3535 /*
3536 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003537 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003539#ifdef FEAT_FLOAT
3540 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003542 if (op == '*')
3543 f1 = f1 * f2;
3544 else if (op == '/')
3545 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003546# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003547 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003548 if (f2 == 0.0)
3549 {
3550 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003551 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003552 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003553 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003554 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003555 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003556 }
3557 else
3558 f1 = f1 / f2;
3559# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003560 // We rely on the floating point library to handle divide
3561 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003562 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003563# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003566 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003567 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003568 return FAIL;
3569 }
3570 rettv->v_type = VAR_FLOAT;
3571 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
3573 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003576 int failed = FALSE;
3577
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003578 if (op == '*')
3579 n1 = n1 * n2;
3580 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003581 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003583 n1 = num_modulus(n1, n2, &failed);
3584 if (failed)
3585 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003586
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003587 rettv->v_type = VAR_NUMBER;
3588 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
3591 }
3592
3593 return OK;
3594}
3595
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003596/*
3597 * Handle a type cast before a base level expression.
3598 * "arg" must point to the first non-white of the expression.
3599 * "arg" is advanced to just after the recognized expression.
3600 * Return OK or FAIL.
3601 */
3602 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003603eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003604 char_u **arg,
3605 typval_T *rettv,
3606 evalarg_T *evalarg,
3607 int want_string) // after "." operator
3608{
3609 type_T *want_type = NULL;
3610 garray_T type_list; // list of pointers to allocated types
3611 int res;
3612 int evaluate = evalarg == NULL ? 0
3613 : (evalarg->eval_flags & EVAL_EVALUATE);
3614
3615 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003616 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3617 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003618 {
3619 ++*arg;
3620 ga_init2(&type_list, sizeof(type_T *), 10);
3621 want_type = parse_type(arg, &type_list, TRUE);
3622 if (want_type == NULL && (evaluate || **arg != '>'))
3623 {
3624 clear_type_list(&type_list);
3625 return FAIL;
3626 }
3627
3628 if (**arg != '>')
3629 {
3630 if (*skipwhite(*arg) == '>')
3631 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3632 else
3633 emsg(_(e_missing_gt));
3634 clear_type_list(&type_list);
3635 return FAIL;
3636 }
3637 ++*arg;
3638 *arg = skipwhite_and_linebreak(*arg, evalarg);
3639 }
3640
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003641 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003642
3643 if (want_type != NULL && evaluate)
3644 {
3645 if (res == OK)
3646 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003647 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3648 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003649
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003650 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003651 {
3652 if (want_type == &t_bool && actual != &t_bool
3653 && (actual->tt_flags & TTFLAG_BOOL_OK))
3654 {
3655 int n = tv2bool(rettv);
3656
3657 // can use "0" and "1" for boolean in some places
3658 clear_tv(rettv);
3659 rettv->v_type = VAR_BOOL;
3660 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3661 }
3662 else
3663 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003664 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003665
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003666 where.wt_variable = TRUE;
3667 res = check_type(want_type, actual, TRUE, where);
3668 }
3669 }
3670 }
3671 clear_type_list(&type_list);
3672 }
3673
3674 return res;
3675}
3676
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003677 int
3678eval_leader(char_u **arg, int vim9)
3679{
3680 char_u *s = *arg;
3681 char_u *p = *arg;
3682
3683 while (*p == '!' || *p == '-' || *p == '+')
3684 {
3685 char_u *n = skipwhite(p + 1);
3686
3687 // ++, --, -+ and +- are not accepted in Vim9 script
3688 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3689 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003690 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003691 return FAIL;
3692 }
3693 p = n;
3694 }
3695 *arg = p;
3696 return OK;
3697}
3698
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003700 * Check for a predefined value "true", "false" and "null.*".
3701 * Return OK when recognized.
3702 */
3703 int
3704handle_predefined(char_u *s, int len, typval_T *rettv)
3705{
3706 switch (len)
3707 {
3708 case 4: if (STRNCMP(s, "true", 4) == 0)
3709 {
3710 rettv->v_type = VAR_BOOL;
3711 rettv->vval.v_number = VVAL_TRUE;
3712 return OK;
3713 }
3714 if (STRNCMP(s, "null", 4) == 0)
3715 {
3716 rettv->v_type = VAR_SPECIAL;
3717 rettv->vval.v_number = VVAL_NULL;
3718 return OK;
3719 }
3720 break;
3721 case 5: if (STRNCMP(s, "false", 5) == 0)
3722 {
3723 rettv->v_type = VAR_BOOL;
3724 rettv->vval.v_number = VVAL_FALSE;
3725 return OK;
3726 }
3727 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003728 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3729 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003730#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003731 rettv->v_type = VAR_JOB;
3732 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003733#else
3734 rettv->v_type = VAR_SPECIAL;
3735 rettv->vval.v_number = VVAL_NULL;
3736#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003737 return OK;
3738 }
3739 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003740 case 9:
3741 if (STRNCMP(s, "null_", 5) != 0)
3742 break;
3743 if (STRNCMP(s + 5, "list", 4) == 0)
3744 {
3745 rettv->v_type = VAR_LIST;
3746 rettv->vval.v_list = NULL;
3747 return OK;
3748 }
3749 if (STRNCMP(s + 5, "dict", 4) == 0)
3750 {
3751 rettv->v_type = VAR_DICT;
3752 rettv->vval.v_dict = NULL;
3753 return OK;
3754 }
3755 if (STRNCMP(s + 5, "blob", 4) == 0)
3756 {
3757 rettv->v_type = VAR_BLOB;
3758 rettv->vval.v_blob = NULL;
3759 return OK;
3760 }
3761 break;
3762 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3763 {
3764 rettv->v_type = VAR_STRING;
3765 rettv->vval.v_string = NULL;
3766 return OK;
3767 }
3768 break;
3769 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003770 if (STRNCMP(s, "null_channel", 12) == 0)
3771 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003772#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003773 rettv->v_type = VAR_CHANNEL;
3774 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003775#else
3776 rettv->v_type = VAR_SPECIAL;
3777 rettv->vval.v_number = VVAL_NULL;
3778#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003779 return OK;
3780 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003781 if (STRNCMP(s, "null_partial", 12) == 0)
3782 {
3783 rettv->v_type = VAR_PARTIAL;
3784 rettv->vval.v_partial = NULL;
3785 return OK;
3786 }
3787 break;
3788 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3789 {
3790 rettv->v_type = VAR_FUNC;
3791 rettv->vval.v_string = NULL;
3792 return OK;
3793 }
3794 break;
3795 }
3796 return FAIL;
3797}
3798
3799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 * Handle sixth level expression:
3801 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003802 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003803 * "string" string constant
3804 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 * &option-name option value
3806 * @r register contents
3807 * identifier variable value
3808 * function() function call
3809 * $VAR environment variable
3810 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003811 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003813 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003814 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 *
3816 * Also handle:
3817 * ! in front logical NOT
3818 * - in front unary minus
3819 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003820 * trailing [] subscript in String or List
3821 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003822 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 *
3824 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003825 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 *
3827 * Return OK or FAIL.
3828 */
3829 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003830eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003831 char_u **arg,
3832 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003833 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003836 int evaluate = evalarg != NULL
3837 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 int len;
3839 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003840 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 char_u *start_leader, *end_leader;
3842 int ret = OK;
3843 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003844 static int recurse = 0;
3845 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846
3847 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003848 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003849 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003851 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
3853 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003854 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 */
3856 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003857 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003858 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 end_leader = *arg;
3860
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003861 if (**arg == '.' && (!isdigit(*(*arg + 1))
3862#ifdef FEAT_FLOAT
Bram Moolenaardd9de502021-08-15 13:49:42 +02003863 || in_old_script(2)
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003864#endif
3865 ))
3866 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003867 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003868 ++*arg;
3869 return FAIL;
3870 }
3871
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003872 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003873 // and crash. With MSVC the stack is smaller.
3874 if (recurse ==
3875#ifdef _MSC_VER
3876 300
3877#else
3878 1000
3879#endif
3880 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003881 {
3882 semsg(_(e_expression_too_recursive_str), *arg);
3883 return FAIL;
3884 }
3885 ++recurse;
3886
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 switch (**arg)
3888 {
3889 /*
3890 * Number constant.
3891 */
3892 case '0':
3893 case '1':
3894 case '2':
3895 case '3':
3896 case '4':
3897 case '5':
3898 case '6':
3899 case '7':
3900 case '8':
3901 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003902 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003903
3904 // Apply prefixed "-" and "+" now. Matters especially when
3905 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003906 if (ret == OK && evaluate && end_leader > start_leader
3907 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003908 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 break;
3910
3911 /*
3912 * String constant: "string".
3913 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003914 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 break;
3916
3917 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003918 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003920 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003921 break;
3922
3923 /*
3924 * List: [expr, expr]
3925 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003926 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 break;
3928
3929 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003930 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003931 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003932 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003933 {
3934 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3935 }
3936 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003937 {
3938 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003939 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003940 }
3941 else
3942 ret = NOTDONE;
3943 break;
3944
3945 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003946 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003947 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003948 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003949 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003950 ret = NOTDONE;
3951 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003952 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003953 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003954 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003955 break;
3956
3957 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003958 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003960 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 break;
3962
3963 /*
3964 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003965 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 */
LemonBoy2eaef102022-05-06 13:14:50 +01003967 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3968 ret = eval_interp_string(arg, rettv, evaluate);
3969 else
3970 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 break;
3972
3973 /*
3974 * Register contents: @r.
3975 */
3976 case '@': ++*arg;
3977 if (evaluate)
3978 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003979 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003980 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003981 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003982 emsg_invreg(**arg);
3983 else
3984 {
3985 rettv->v_type = VAR_STRING;
3986 rettv->vval.v_string = get_reg_contents(**arg,
3987 GREG_EXPR_SRC);
3988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
3990 if (**arg != NUL)
3991 ++*arg;
3992 break;
3993
3994 /*
3995 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003996 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003998 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003999 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004000 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004001 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004002 if (ret == OK && evaluate)
4003 {
4004 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4005
Bram Moolenaara9931532021-06-12 15:58:16 +02004006 // Compile it here to get the return type. The return
4007 // type is optional, when it's missing use t_unknown.
4008 // This is recognized in compile_return().
4009 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4010 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004011 if (compile_def_function(ufunc, FALSE,
4012 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004013 {
4014 clear_tv(rettv);
4015 ret = FAIL;
4016 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004017 }
4018 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004019 if (ret == NOTDONE)
4020 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004021 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004022 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004023
Bram Moolenaar9215f012020-06-27 21:18:00 +02004024 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004025 if (**arg == ')')
4026 ++*arg;
4027 else if (ret == OK)
4028 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004029 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004030 clear_tv(rettv);
4031 ret = FAIL;
4032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
4034 break;
4035
Bram Moolenaar8c711452005-01-14 21:53:12 +00004036 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 break;
4038 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004039
4040 if (ret == NOTDONE)
4041 {
4042 /*
4043 * Must be a variable or function name.
4044 * Can also be a curly-braces kind of name: {expr}.
4045 */
4046 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004047 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004048 if (alias != NULL)
4049 s = alias;
4050
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004051 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004052 ret = FAIL;
4053 else
4054 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004055 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4056
Bram Moolenaar4525a572022-02-13 11:57:33 +00004057 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004058 {
4059 emsg(_(e_cannot_use_underscore_here));
4060 ret = FAIL;
4061 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004062 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004063 && s[0] == 's' && s[1] == ':')
4064 {
4065 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4066 ret = FAIL;
4067 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004068 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004069 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004070 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004071 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004072 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004073 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004074 else if (flags & EVAL_CONSTANT)
4075 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004076 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004077 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004078 // get the value of "true", "false", etc. or a variable
4079 ret = FAIL;
4080 if (vim9script)
4081 ret = handle_predefined(s, len, rettv);
4082 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004083 {
4084 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004085 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004086 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004087 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004088 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004089 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004090 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004091 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004092 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004093 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004094 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004095 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004096 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004097 }
4098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004099 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4100 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004101 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004102 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103
4104 /*
4105 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4106 */
4107 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004108 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004109
4110 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004111 return ret;
4112}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004113
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004114/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004115 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004116 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004117 * Adjusts "end_leaderp" until it is at "start_leader".
4118 */
4119 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004120eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004121 typval_T *rettv,
4122 int numeric_only,
4123 char_u *start_leader,
4124 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004125{
4126 char_u *end_leader = *end_leaderp;
4127 int ret = OK;
4128 int error = FALSE;
4129 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004130 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004131 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004132#ifdef FEAT_FLOAT
4133 float_T f = 0.0;
4134
4135 if (rettv->v_type == VAR_FLOAT)
4136 f = rettv->vval.v_float;
4137 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004138#endif
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004139 {
4140 while (VIM_ISWHITE(end_leader[-1]))
4141 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004142 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004143 val = tv2bool(rettv);
4144 else
4145 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004146 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004147 if (error)
4148 {
4149 clear_tv(rettv);
4150 ret = FAIL;
4151 }
4152 else
4153 {
4154 while (end_leader > start_leader)
4155 {
4156 --end_leader;
4157 if (*end_leader == '!')
4158 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004159 if (numeric_only)
4160 {
4161 ++end_leader;
4162 break;
4163 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004164#ifdef FEAT_FLOAT
4165 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004166 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004167 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004168 {
4169 rettv->v_type = VAR_BOOL;
4170 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4171 }
4172 else
4173 f = !f;
4174 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004175 else
4176#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004177 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004178 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004179 type = VAR_BOOL;
4180 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004181 }
4182 else if (*end_leader == '-')
4183 {
4184#ifdef FEAT_FLOAT
4185 if (rettv->v_type == VAR_FLOAT)
4186 f = -f;
4187 else
4188#endif
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004189 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004190 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004191 type = VAR_NUMBER;
4192 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004193 }
4194 }
4195#ifdef FEAT_FLOAT
4196 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004198 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004199 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004202#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004203 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004204 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004205 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004206 rettv->v_type = type;
4207 else
4208 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004209 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004212 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 return ret;
4214}
4215
4216/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004217 * Call the function referred to in "rettv".
4218 */
4219 static int
4220call_func_rettv(
4221 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004222 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004223 typval_T *rettv,
4224 int evaluate,
4225 dict_T *selfdict,
4226 typval_T *basetv)
4227{
4228 partial_T *pt = NULL;
4229 funcexe_T funcexe;
4230 typval_T functv;
4231 char_u *s;
4232 int ret;
4233
4234 // need to copy the funcref so that we can clear rettv
4235 if (evaluate)
4236 {
4237 functv = *rettv;
4238 rettv->v_type = VAR_UNKNOWN;
4239
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004240 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004241 if (functv.v_type == VAR_PARTIAL)
4242 {
4243 pt = functv.vval.v_partial;
4244 s = partial_name(pt);
4245 }
4246 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004247 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004248 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004249 if (s == NULL || *s == NUL)
4250 {
4251 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004252 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004253 goto theend;
4254 }
4255 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004256 }
4257 else
4258 s = (char_u *)"";
4259
Bram Moolenaara80faa82020-04-12 19:37:17 +02004260 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004261 funcexe.fe_firstline = curwin->w_cursor.lnum;
4262 funcexe.fe_lastline = curwin->w_cursor.lnum;
4263 funcexe.fe_evaluate = evaluate;
4264 funcexe.fe_partial = pt;
4265 funcexe.fe_selfdict = selfdict;
4266 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004267 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004268
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004269theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004270 // Clear the funcref afterwards, so that deleting it while
4271 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004272 if (evaluate)
4273 clear_tv(&functv);
4274
4275 return ret;
4276}
4277
4278/*
4279 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004280 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004281 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4282 */
4283 static int
4284eval_lambda(
4285 char_u **arg,
4286 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004287 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004288 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004289{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004290 int evaluate = evalarg != NULL
4291 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004292 typval_T base = *rettv;
4293 int ret;
4294
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004295 rettv->v_type = VAR_UNKNOWN;
4296
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004297 if (**arg == '{')
4298 {
4299 // ->{lambda}()
4300 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4301 }
4302 else
4303 {
4304 // ->(lambda)()
4305 ++*arg;
4306 ret = eval1(arg, rettv, evalarg);
4307 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004308 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004309 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004310 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004311 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004312 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004313 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4314 && rettv->v_type != VAR_PARTIAL)
4315 {
4316 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4317 return FAIL;
4318 }
4319 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004320 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004321 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004322 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004323
4324 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004325 {
4326 if (verbose)
4327 {
4328 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004329 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004330 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004331 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004332 }
4333 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004334 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004335 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004336 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004337 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004338
4339 // Clear the funcref afterwards, so that deleting it while
4340 // evaluating the arguments is possible (see test55).
4341 if (evaluate)
4342 clear_tv(&base);
4343
4344 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004345}
4346
4347/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004348 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004349 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004350 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4351 */
4352 static int
4353eval_method(
4354 char_u **arg,
4355 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004356 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004357 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004358{
4359 char_u *name;
4360 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004361 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004362 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004363 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004364 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004365 int evaluate = evalarg != NULL
4366 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004367
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004368 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004369
Bram Moolenaarac92e252019-08-03 21:58:38 +02004370 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004371 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004372 if (alias != NULL)
4373 name = alias;
4374
4375 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004376 {
4377 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004378 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004379 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004380 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004381 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004382 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004383 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004384
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004385 // If there is no "(" immediately following, but there is further on,
4386 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4387 // Does not handle anything where "(" is part of the expression.
4388 *arg = skipwhite(*arg);
4389
4390 if (**arg != '(' && alias == NULL
4391 && (paren = vim_strchr(*arg, '(')) != NULL)
4392 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004393 char_u *deref;
4394
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004395 *arg = name;
4396 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004397 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4398 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004399 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004400 *arg = name + len;
4401 ret = FAIL;
4402 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004403 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004404 {
4405 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004406 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004407 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004408 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004409 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004410
4411 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004412 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004413 *arg = skipwhite(*arg);
4414
4415 if (**arg != '(')
4416 {
4417 if (verbose)
4418 semsg(_(e_missing_parenthesis_str), name);
4419 ret = FAIL;
4420 }
4421 else if (VIM_ISWHITE((*arg)[-1]))
4422 {
4423 if (verbose)
4424 emsg(_(e_no_white_space_allowed_before_parenthesis));
4425 ret = FAIL;
4426 }
4427 else
4428 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004429 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004430 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004431 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004432
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004433 // Clear the funcref afterwards, so that deleting it while
4434 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004435 if (evaluate)
4436 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004437 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004438
Bram Moolenaarac92e252019-08-03 21:58:38 +02004439 return ret;
4440}
4441
4442/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004443 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4444 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004445 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4446 */
4447 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004448eval_index(
4449 char_u **arg,
4450 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004451 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004452 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004453{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004454 int evaluate = evalarg != NULL
4455 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004456 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004457 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004458 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004459 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004460 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004461 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004462
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004463 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4464 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004465
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004466 init_tv(&var1);
4467 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004468 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004469 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004470 /*
4471 * dict.name
4472 */
4473 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004474 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004475 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004476 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004477 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004478 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004479 }
4480 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004481 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004482 /*
4483 * something[idx]
4484 *
4485 * Get the (first) variable from inside the [].
4486 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004487 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004488 if (**arg == ':')
4489 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004490 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004491 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004492 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004493 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004494 semsg(_(e_white_space_required_before_and_after_str_at_str),
4495 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004496 clear_tv(&var1);
4497 return FAIL;
4498 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004499 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004500 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004501 int error = FALSE;
4502
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004503#ifdef FEAT_FLOAT
4504 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004505 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004506 && var1.v_type == VAR_FLOAT)
4507 {
4508 var1.vval.v_string = typval_tostring(&var1, TRUE);
4509 var1.v_type = VAR_STRING;
4510 }
4511#endif
Bram Moolenaar4525a572022-02-13 11:57:33 +00004512 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004513 tv_get_number_chk(&var1, &error);
4514 else
4515 error = tv_get_string_chk(&var1) == NULL;
4516 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004517 {
4518 // not a number or string
4519 clear_tv(&var1);
4520 return FAIL;
4521 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004522 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004523
4524 /*
4525 * Get the second variable from inside the [:].
4526 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004527 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004528 if (**arg == ':')
4529 {
4530 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004531 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004532 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004533 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004534 semsg(_(e_white_space_required_before_and_after_str_at_str),
4535 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004536 if (!empty1)
4537 clear_tv(&var1);
4538 return FAIL;
4539 }
4540 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004541 if (**arg == ']')
4542 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004543 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004544 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004545 if (!empty1)
4546 clear_tv(&var1);
4547 return FAIL;
4548 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004549 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004550 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004551 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004552 if (!empty1)
4553 clear_tv(&var1);
4554 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004555 return FAIL;
4556 }
4557 }
4558
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004559 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004560 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004561 if (**arg != ']')
4562 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004563 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004564 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004565 clear_tv(&var1);
4566 if (range)
4567 clear_tv(&var2);
4568 return FAIL;
4569 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004570 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004571 }
4572
4573 if (evaluate)
4574 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004575 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004576 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004577 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004578
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004579 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004580 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004581 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004582 clear_tv(&var2);
4583 return res;
4584 }
4585 return OK;
4586}
4587
4588/*
4589 * Check if "rettv" can have an [index] or [sli:ce]
4590 */
4591 int
4592check_can_index(typval_T *rettv, int evaluate, int verbose)
4593{
4594 switch (rettv->v_type)
4595 {
4596 case VAR_FUNC:
4597 case VAR_PARTIAL:
4598 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004599 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004600 return FAIL;
4601 case VAR_FLOAT:
4602#ifdef FEAT_FLOAT
4603 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004604 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004605 return FAIL;
4606#endif
4607 case VAR_BOOL:
4608 case VAR_SPECIAL:
4609 case VAR_JOB:
4610 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004611 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004612 if (verbose)
4613 emsg(_(e_cannot_index_special_variable));
4614 return FAIL;
4615 case VAR_UNKNOWN:
4616 case VAR_ANY:
4617 case VAR_VOID:
4618 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004620 emsg(_(e_cannot_index_special_variable));
4621 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004623 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004624
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004625 case VAR_STRING:
4626 case VAR_LIST:
4627 case VAR_DICT:
4628 case VAR_BLOB:
4629 break;
4630 case VAR_NUMBER:
4631 if (in_vim9script())
4632 emsg(_(e_cannot_index_number));
4633 break;
4634 }
4635 return OK;
4636}
4637
4638/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004639 * slice() function
4640 */
4641 void
4642f_slice(typval_T *argvars, typval_T *rettv)
4643{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004644 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004645 && ((argvars[0].v_type != VAR_STRING
4646 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004647 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004648 && check_for_list_arg(argvars, 0) == FAIL)
4649 || check_for_number_arg(argvars, 1) == FAIL
4650 || check_for_opt_number_arg(argvars, 2) == FAIL))
4651 return;
4652
Bram Moolenaar6601b622021-01-13 21:47:15 +01004653 if (check_can_index(argvars, TRUE, FALSE) == OK)
4654 {
4655 copy_tv(argvars, rettv);
4656 eval_index_inner(rettv, TRUE, argvars + 1,
4657 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4658 TRUE, NULL, 0, FALSE);
4659 }
4660}
4661
4662/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004663 * Apply index or range to "rettv".
4664 * "var1" is the first index, NULL for [:expr].
4665 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004666 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4667 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004668 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4669 */
4670 int
4671eval_index_inner(
4672 typval_T *rettv,
4673 int is_range,
4674 typval_T *var1,
4675 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004676 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004677 char_u *key,
4678 int keylen,
4679 int verbose)
4680{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004681 varnumber_T n1, n2 = 0;
4682 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004683
4684 n1 = 0;
4685 if (var1 != NULL && rettv->v_type != VAR_DICT)
4686 n1 = tv_get_number(var1);
4687
4688 if (is_range)
4689 {
4690 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004692 if (verbose)
4693 emsg(_(e_cannot_slice_dictionary));
4694 return FAIL;
4695 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004696 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004697 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004698 else
4699 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004700 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004701
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004702 switch (rettv->v_type)
4703 {
4704 case VAR_UNKNOWN:
4705 case VAR_ANY:
4706 case VAR_VOID:
4707 case VAR_FUNC:
4708 case VAR_PARTIAL:
4709 case VAR_FLOAT:
4710 case VAR_BOOL:
4711 case VAR_SPECIAL:
4712 case VAR_JOB:
4713 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004714 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004715 break; // not evaluating, skipping over subscript
4716
4717 case VAR_NUMBER:
4718 case VAR_STRING:
4719 {
4720 char_u *s = tv_get_string(rettv);
4721
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004722 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004723 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004724 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004725 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004726 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004727 else
4728 s = char_from_string(s, n1);
4729 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004730 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004731 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004732 // The resulting variable is a substring. If the indexes
4733 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004734 if (n1 < 0)
4735 {
4736 n1 = len + n1;
4737 if (n1 < 0)
4738 n1 = 0;
4739 }
4740 if (n2 < 0)
4741 n2 = len + n2;
4742 else if (n2 >= len)
4743 n2 = len;
4744 if (n1 >= len || n2 < 0 || n1 > n2)
4745 s = NULL;
4746 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004747 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748 }
4749 else
4750 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004751 // The resulting variable is a string of a single
4752 // character. If the index is too big or negative the
4753 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754 if (n1 >= len || n1 < 0)
4755 s = NULL;
4756 else
4757 s = vim_strnsave(s + n1, 1);
4758 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004759 clear_tv(rettv);
4760 rettv->v_type = VAR_STRING;
4761 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004762 }
4763 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004765 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004766 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4767 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004768 break;
4769
4770 case VAR_LIST:
4771 if (var1 == NULL)
4772 n1 = 0;
4773 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004774 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004775 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004776 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004777 return FAIL;
4778 break;
4779
4780 case VAR_DICT:
4781 {
4782 dictitem_T *item;
4783 typval_T tmp;
4784
4785 if (key == NULL)
4786 {
4787 key = tv_get_string_chk(var1);
4788 if (key == NULL)
4789 return FAIL;
4790 }
4791
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004792 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004793
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004794 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004795 {
4796 if (verbose)
4797 {
4798 if (keylen > 0)
4799 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004800 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004801 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004802 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004803 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004804
4805 copy_tv(&item->di_tv, &tmp);
4806 clear_tv(rettv);
4807 *rettv = tmp;
4808 }
4809 break;
4810 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004811 return OK;
4812}
4813
4814/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004815 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004816 */
4817 char_u *
4818partial_name(partial_T *pt)
4819{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004820 if (pt != NULL)
4821 {
4822 if (pt->pt_name != NULL)
4823 return pt->pt_name;
4824 if (pt->pt_func != NULL)
4825 return pt->pt_func->uf_name;
4826 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004827 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004828}
4829
Bram Moolenaarddecc252016-04-06 22:59:37 +02004830 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004831partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004832{
4833 int i;
4834
4835 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004836 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004837 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004838 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004839 if (pt->pt_name != NULL)
4840 {
4841 func_unref(pt->pt_name);
4842 vim_free(pt->pt_name);
4843 }
4844 else
4845 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004846
Bram Moolenaar54656012021-06-09 20:50:46 +02004847 // "out_up" is no longer used, decrement refcount on partial that owns it.
4848 partial_unref(pt->pt_outer.out_up_partial);
4849
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004850 // Using pt_outer from another partial.
4851 partial_unref(pt->pt_outer_partial);
4852
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004853 // Decrease the reference count for the context of a closure. If down
4854 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004855 if (pt->pt_funcstack != NULL)
4856 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004857 --pt->pt_funcstack->fs_refcount;
4858 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004859 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004860 // Similarly for loop variables.
4861 if (pt->pt_loopvars != NULL)
4862 {
4863 --pt->pt_loopvars->lvs_refcount;
4864 loopvars_check_refcount(pt->pt_loopvars);
4865 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004866
Bram Moolenaarddecc252016-04-06 22:59:37 +02004867 vim_free(pt);
4868}
4869
4870/*
4871 * Unreference a closure: decrement the reference count and free it when it
4872 * becomes zero.
4873 */
4874 void
4875partial_unref(partial_T *pt)
4876{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004877 if (pt != NULL)
4878 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004879 int done = FALSE;
4880
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004881 if (--pt->pt_refcount <= 0)
4882 partial_free(pt);
4883
4884 // If the reference count goes down to one, the funcstack may be the
4885 // only reference and can be freed if no other partials reference it.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004886 else if (pt->pt_refcount == 1)
4887 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004888 // careful: if the funcstack is freed it may contain this partial
4889 // and it gets freed as well
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004890 if (pt->pt_funcstack != NULL)
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004891 done = funcstack_check_refcount(pt->pt_funcstack);
4892
4893 if (!done && pt->pt_loopvars != NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004894 loopvars_check_refcount(pt->pt_loopvars);
4895 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004896 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004897}
4898
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004899/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004900 * Return the next (unique) copy ID.
4901 * Used for serializing nested structures.
4902 */
4903 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004904get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004905{
4906 current_copyID += COPYID_INC;
4907 return current_copyID;
4908}
4909
4910/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004911 * Garbage collection for lists and dictionaries.
4912 *
4913 * We use reference counts to be able to free most items right away when they
4914 * are no longer used. But for composite items it's possible that it becomes
4915 * unused while the reference count is > 0: When there is a recursive
4916 * reference. Example:
4917 * :let l = [1, 2, 3]
4918 * :let d = {9: l}
4919 * :let l[1] = d
4920 *
4921 * Since this is quite unusual we handle this with garbage collection: every
4922 * once in a while find out which lists and dicts are not referenced from any
4923 * variable.
4924 *
4925 * Here is a good reference text about garbage collection (refers to Python
4926 * but it applies to all reference-counting mechanisms):
4927 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004928 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004929
4930/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004931 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004932 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004933 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004934 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004935 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004936garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004937{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004938 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004939 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004940 buf_T *buf;
4941 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004942 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004943 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004944
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004945 if (!testing)
4946 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004947 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004948 want_garbage_collect = FALSE;
4949 may_garbage_collect = FALSE;
4950 garbage_collect_at_exit = FALSE;
4951 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004952
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004953 // The execution stack can grow big, limit the size.
4954 if (exestack.ga_maxlen - exestack.ga_len > 500)
4955 {
4956 size_t new_len;
4957 char_u *pp;
4958 int n;
4959
4960 // Keep 150% of the current size, with a minimum of the growth size.
4961 n = exestack.ga_len / 2;
4962 if (n < exestack.ga_growsize)
4963 n = exestack.ga_growsize;
4964
4965 // Don't make it bigger though.
4966 if (exestack.ga_len + n < exestack.ga_maxlen)
4967 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004968 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004969 pp = vim_realloc(exestack.ga_data, new_len);
4970 if (pp == NULL)
4971 return FAIL;
4972 exestack.ga_maxlen = exestack.ga_len + n;
4973 exestack.ga_data = pp;
4974 }
4975 }
4976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004977 // We advance by two because we add one for items referenced through
4978 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004979 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004980
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004981 /*
4982 * 1. Go through all accessible variables and mark all lists and dicts
4983 * with copyID.
4984 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 // Don't free variables in the previous_funccal list unless they are only
4987 // referenced through previous_funccal. This must be first, because if
4988 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004989 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004990
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004991 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004992 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004993
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004994 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004995 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004996 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4997 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004999 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005000 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005001 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5002 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005003 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005004 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5005 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005006#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005007 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005008 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5009 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005010 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005011 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005012 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5013 NULL, NULL);
5014#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005015
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005016 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005017 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005018 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5019 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005020 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005021 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005022
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005023 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005024 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005025
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005026 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005027 abort = abort || set_ref_in_functions(copyID);
5028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005029 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005030 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005031
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005032 // funcstacks keep variables for closures
5033 abort = abort || set_ref_in_funcstacks(copyID);
5034
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005035 // loopvars keep variables for loop blocks
5036 abort = abort || set_ref_in_loopvars(copyID);
5037
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005038 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005039 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005040
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005041 // callbacks in buffers
5042 abort = abort || set_ref_in_buffers(copyID);
5043
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005044 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5045 abort = abort || set_ref_in_insexpand_funcs(copyID);
5046
5047 // 'operatorfunc' callback
5048 abort = abort || set_ref_in_opfunc(copyID);
5049
5050 // 'tagfunc' callback
5051 abort = abort || set_ref_in_tagfunc(copyID);
5052
5053 // 'imactivatefunc' and 'imstatusfunc' callbacks
5054 abort = abort || set_ref_in_im_funcs(copyID);
5055
Bram Moolenaar1dced572012-04-05 16:54:08 +02005056#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005057 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005058#endif
5059
Bram Moolenaardb913952012-06-29 12:54:53 +02005060#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005061 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005062#endif
5063
5064#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005065 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005066#endif
5067
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005068#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005069 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005070 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005071#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005072#ifdef FEAT_NETBEANS_INTG
5073 abort = abort || set_ref_in_nb_channel(copyID);
5074#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005075
Bram Moolenaare3188e22016-05-31 21:13:04 +02005076#ifdef FEAT_TIMERS
5077 abort = abort || set_ref_in_timer(copyID);
5078#endif
5079
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005080#ifdef FEAT_QUICKFIX
5081 abort = abort || set_ref_in_quickfix(copyID);
5082#endif
5083
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005084#ifdef FEAT_TERMINAL
5085 abort = abort || set_ref_in_term(copyID);
5086#endif
5087
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005088#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005089 abort = abort || set_ref_in_popups(copyID);
5090#endif
5091
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005092 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005093 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005094 /*
5095 * 2. Free lists and dictionaries that are not referenced.
5096 */
5097 did_free = free_unref_items(copyID);
5098
5099 /*
5100 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005101 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005102 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005103 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005104 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005105 else if (p_verbose > 0)
5106 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005107 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005108 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005109
5110 return did_free;
5111}
5112
5113/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005114 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005115 */
5116 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005117free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005118{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005119 int did_free = FALSE;
5120
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005121 // Let all "free" functions know that we are here. This means no
5122 // dictionaries, lists, channels or jobs are to be freed, because we will
5123 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005124 in_free_unref_items = TRUE;
5125
5126 /*
5127 * PASS 1: free the contents of the items. We don't free the items
5128 * themselves yet, so that it is possible to decrement refcount counters
5129 */
5130
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005131 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005132 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005133
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005134 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005135 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005136
5137#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005138 // Go through the list of jobs and free items without the copyID. This
5139 // must happen before doing channels, because jobs refer to channels, but
5140 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005141 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5142
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005143 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005144 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5145#endif
5146
5147 /*
5148 * PASS 2: free the items themselves.
5149 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005150 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005151 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005152
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005153#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005154 // Go through the list of jobs and free items without the copyID. This
5155 // must happen before doing channels, because jobs refer to channels, but
5156 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005157 free_unused_jobs(copyID, COPYID_MASK);
5158
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005159 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005160 free_unused_channels(copyID, COPYID_MASK);
5161#endif
5162
5163 in_free_unref_items = FALSE;
5164
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005165 return did_free;
5166}
5167
5168/*
5169 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005170 * "list_stack" is used to add lists to be marked. Can be NULL.
5171 *
5172 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005173 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005174 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005175set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005176{
5177 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005178 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005179 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005180 hashtab_T *cur_ht;
5181 ht_stack_T *ht_stack = NULL;
5182 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005183
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005184 cur_ht = ht;
5185 for (;;)
5186 {
5187 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005188 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005189 // Mark each item in the hashtab. If the item contains a hashtab
5190 // it is added to ht_stack, if it contains a list it is added to
5191 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005192 todo = (int)cur_ht->ht_used;
5193 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5194 if (!HASHITEM_EMPTY(hi))
5195 {
5196 --todo;
5197 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5198 &ht_stack, list_stack);
5199 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005200 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005201
5202 if (ht_stack == NULL)
5203 break;
5204
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005205 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005206 cur_ht = ht_stack->ht;
5207 tempitem = ht_stack;
5208 ht_stack = ht_stack->prev;
5209 free(tempitem);
5210 }
5211
5212 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005213}
5214
Dominique Pelle748b3082022-01-08 12:41:16 +00005215#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5216 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005217/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005218 * Mark a dict and its items with "copyID".
5219 * Returns TRUE if setting references failed somehow.
5220 */
5221 int
5222set_ref_in_dict(dict_T *d, int copyID)
5223{
5224 if (d != NULL && d->dv_copyID != copyID)
5225 {
5226 d->dv_copyID = copyID;
5227 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5228 }
5229 return FALSE;
5230}
Dominique Pelle748b3082022-01-08 12:41:16 +00005231#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005232
5233/*
5234 * Mark a list and its items with "copyID".
5235 * Returns TRUE if setting references failed somehow.
5236 */
5237 int
5238set_ref_in_list(list_T *ll, int copyID)
5239{
5240 if (ll != NULL && ll->lv_copyID != copyID)
5241 {
5242 ll->lv_copyID = copyID;
5243 return set_ref_in_list_items(ll, copyID, NULL);
5244 }
5245 return FALSE;
5246}
5247
5248/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005249 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005250 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5251 *
5252 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005253 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005254 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005255set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005256{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005257 listitem_T *li;
5258 int abort = FALSE;
5259 list_T *cur_l;
5260 list_stack_T *list_stack = NULL;
5261 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005262
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005263 cur_l = l;
5264 for (;;)
5265 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005266 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005267 // Mark each item in the list. If the item contains a hashtab
5268 // it is added to ht_stack, if it contains a list it is added to
5269 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005270 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5271 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5272 ht_stack, &list_stack);
5273 if (list_stack == NULL)
5274 break;
5275
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005276 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005277 cur_l = list_stack->list;
5278 tempitem = list_stack;
5279 list_stack = list_stack->prev;
5280 free(tempitem);
5281 }
5282
5283 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005284}
5285
5286/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005287 * Mark the partial in callback 'cb' with "copyID".
5288 */
5289 int
5290set_ref_in_callback(callback_T *cb, int copyID)
5291{
5292 typval_T tv;
5293
5294 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5295 return FALSE;
5296
5297 tv.v_type = VAR_PARTIAL;
5298 tv.vval.v_partial = cb->cb_partial;
5299 return set_ref_in_item(&tv, copyID, NULL, NULL);
5300}
5301
5302/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005303 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005304 * "list_stack" is used to add lists to be marked. Can be NULL.
5305 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5306 *
5307 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005308 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005310set_ref_in_item(
5311 typval_T *tv,
5312 int copyID,
5313 ht_stack_T **ht_stack,
5314 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005315{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005316 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005317
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005318 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005319 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005320 dict_T *dd = tv->vval.v_dict;
5321
Bram Moolenaara03f2332016-02-06 18:09:59 +01005322 if (dd != NULL && dd->dv_copyID != copyID)
5323 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005324 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005325 dd->dv_copyID = copyID;
5326 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005327 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005328 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5329 }
5330 else
5331 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005332 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5333
Bram Moolenaara03f2332016-02-06 18:09:59 +01005334 if (newitem == NULL)
5335 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005336 else
5337 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005338 newitem->ht = &dd->dv_hashtab;
5339 newitem->prev = *ht_stack;
5340 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005341 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005342 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005343 }
5344 }
5345 else if (tv->v_type == VAR_LIST)
5346 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005347 list_T *ll = tv->vval.v_list;
5348
Bram Moolenaara03f2332016-02-06 18:09:59 +01005349 if (ll != NULL && ll->lv_copyID != copyID)
5350 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005351 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005352 ll->lv_copyID = copyID;
5353 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005354 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005355 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005356 }
5357 else
5358 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005359 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5360
Bram Moolenaara03f2332016-02-06 18:09:59 +01005361 if (newitem == NULL)
5362 abort = TRUE;
5363 else
5364 {
5365 newitem->list = ll;
5366 newitem->prev = *list_stack;
5367 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005368 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005369 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005370 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005371 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005372 else if (tv->v_type == VAR_FUNC)
5373 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005374 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005375 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005376 else if (tv->v_type == VAR_PARTIAL)
5377 {
5378 partial_T *pt = tv->vval.v_partial;
5379 int i;
5380
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005381 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005382 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005383 // Didn't see this partial yet.
5384 pt->pt_copyID = copyID;
5385
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005386 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005387
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005388 if (pt->pt_dict != NULL)
5389 {
5390 typval_T dtv;
5391
5392 dtv.v_type = VAR_DICT;
5393 dtv.vval.v_dict = pt->pt_dict;
5394 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5395 }
5396
5397 for (i = 0; i < pt->pt_argc; ++i)
5398 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5399 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005400 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005401 // pt_loopvars is handled in set_ref_in_loopvars()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005402 }
5403 }
5404#ifdef FEAT_JOB_CHANNEL
5405 else if (tv->v_type == VAR_JOB)
5406 {
5407 job_T *job = tv->vval.v_job;
5408 typval_T dtv;
5409
5410 if (job != NULL && job->jv_copyID != copyID)
5411 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005412 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005413 if (job->jv_channel != NULL)
5414 {
5415 dtv.v_type = VAR_CHANNEL;
5416 dtv.vval.v_channel = job->jv_channel;
5417 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5418 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005419 if (job->jv_exit_cb.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 = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005423 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5424 }
5425 }
5426 }
5427 else if (tv->v_type == VAR_CHANNEL)
5428 {
5429 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005430 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005431 typval_T dtv;
5432 jsonq_T *jq;
5433 cbq_T *cq;
5434
5435 if (ch != NULL && ch->ch_copyID != copyID)
5436 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005437 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005438 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005439 {
5440 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5441 jq = jq->jq_next)
5442 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5443 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5444 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005445 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005446 {
5447 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005448 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005449 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5450 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005451 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005452 {
5453 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005454 dtv.vval.v_partial =
5455 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005456 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5457 }
5458 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005459 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005460 {
5461 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005462 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005463 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5464 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005465 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005466 {
5467 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005468 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005469 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5470 }
5471 }
5472 }
5473#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005474 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005475}
5476
Bram Moolenaar8c711452005-01-14 21:53:12 +00005477/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 * Return a string with the string representation of a variable.
5479 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005480 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005481 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005482 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005483 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005484 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005485 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5486 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005487 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005489 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005490echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005491 typval_T *tv,
5492 char_u **tofree,
5493 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005494 int copyID,
5495 int echo_style,
5496 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005497 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005498{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005499 static int recurse = 0;
5500 char_u *r = NULL;
5501
Bram Moolenaar33570922005-01-25 22:26:29 +00005502 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005503 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005504 if (!did_echo_string_emsg)
5505 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005506 // Only give this message once for a recursive call to avoid
5507 // flooding the user with errors. And stop iterating over lists
5508 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005509 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005510 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005511 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005512 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005513 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005514 }
5515 ++recurse;
5516
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517 switch (tv->v_type)
5518 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005519 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005520 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005521 {
5522 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005523 r = tv->vval.v_string;
5524 if (r == NULL)
5525 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005526 }
5527 else
5528 {
5529 *tofree = string_quote(tv->vval.v_string, FALSE);
5530 r = *tofree;
5531 }
5532 break;
5533
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005535 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005536 char_u buf[MAX_FUNC_NAME_LEN];
5537
5538 if (echo_style)
5539 {
LemonBoya5d35902022-04-29 21:15:02 +01005540 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5541 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005542 buf, MAX_FUNC_NAME_LEN);
5543 if (r == buf)
5544 {
5545 r = vim_strsave(buf);
5546 *tofree = r;
5547 }
5548 else
5549 *tofree = NULL;
5550 }
5551 else
5552 {
5553 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5554 : make_ufunc_name_readable(
5555 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5556 TRUE);
5557 r = *tofree;
5558 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005559 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005560 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005561
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005562 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005563 {
5564 partial_T *pt = tv->vval.v_partial;
5565 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005566 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005567 garray_T ga;
5568 int i;
5569 char_u *tf;
5570
5571 ga_init2(&ga, 1, 100);
5572 ga_concat(&ga, (char_u *)"function(");
5573 if (fname != NULL)
5574 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005575 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005576 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005577 && vim_isupper(fname[1]))
5578 {
5579 ga_concat(&ga, (char_u *)"'g:");
5580 ga_concat(&ga, fname + 1);
5581 }
5582 else
5583 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005584 vim_free(fname);
5585 }
5586 if (pt != NULL && pt->pt_argc > 0)
5587 {
5588 ga_concat(&ga, (char_u *)", [");
5589 for (i = 0; i < pt->pt_argc; ++i)
5590 {
5591 if (i > 0)
5592 ga_concat(&ga, (char_u *)", ");
5593 ga_concat(&ga,
5594 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5595 vim_free(tf);
5596 }
5597 ga_concat(&ga, (char_u *)"]");
5598 }
5599 if (pt != NULL && pt->pt_dict != NULL)
5600 {
5601 typval_T dtv;
5602
5603 ga_concat(&ga, (char_u *)", ");
5604 dtv.v_type = VAR_DICT;
5605 dtv.vval.v_dict = pt->pt_dict;
5606 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5607 vim_free(tf);
5608 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005609 // terminate with ')' and a NUL
5610 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005611
5612 *tofree = ga.ga_data;
5613 r = *tofree;
5614 break;
5615 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005616
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005617 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005618 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005619 break;
5620
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005621 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005622 if (tv->vval.v_list == NULL)
5623 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005624 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005625 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005626 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005627 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005628 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5629 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005630 {
5631 *tofree = NULL;
5632 r = (char_u *)"[...]";
5633 }
5634 else
5635 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005636 int old_copyID = tv->vval.v_list->lv_copyID;
5637
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005638 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005639 *tofree = list2string(tv, copyID, restore_copyID);
5640 if (restore_copyID)
5641 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005642 r = *tofree;
5643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005644 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005645
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005647 if (tv->vval.v_dict == NULL)
5648 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005649 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005650 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005651 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005652 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005653 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5654 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005655 {
5656 *tofree = NULL;
5657 r = (char_u *)"{...}";
5658 }
5659 else
5660 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005661 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005662
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005663 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005664 *tofree = dict2string(tv, copyID, restore_copyID);
5665 if (restore_copyID)
5666 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005667 r = *tofree;
5668 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005669 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005670
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005672 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005673 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005675 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005676 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005677 break;
5678
Bram Moolenaar835dc632016-02-07 14:27:38 +01005679 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005680 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005681#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005682 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005683 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5684 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005685 if (composite_val)
5686 {
5687 *tofree = string_quote(r, FALSE);
5688 r = *tofree;
5689 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005690#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005691 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005692
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005693 case VAR_INSTR:
5694 *tofree = NULL;
5695 r = (char_u *)"instructions";
5696 break;
5697
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005698 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005699#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005700 *tofree = NULL;
5701 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5702 r = numbuf;
5703 break;
5704#endif
5705
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005706 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005707 case VAR_SPECIAL:
5708 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005709 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005710 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005711 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005712
Bram Moolenaar8502c702014-06-17 12:51:16 +02005713 if (--recurse == 0)
5714 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005715 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005716}
5717
5718/*
5719 * Return a string with the string representation of a variable.
5720 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5721 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005722 * Does not put quotes around strings, as ":echo" displays values.
5723 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5724 * May return NULL.
5725 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005726 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005727echo_string(
5728 typval_T *tv,
5729 char_u **tofree,
5730 char_u *numbuf,
5731 int copyID)
5732{
5733 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5734}
5735
5736/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005737 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5738 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005739 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005740 */
5741 int
5742buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5743{
5744 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005745 char_u *t;
5746 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005747
5748 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5749 return -1;
5750
5751 if (lnum > buf->b_ml.ml_line_count)
5752 lnum = buf->b_ml.ml_line_count;
5753
5754 str = ml_get_buf(buf, lnum, FALSE);
5755 if (str == NULL)
5756 return -1;
5757
5758 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005759 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005760
Bram Moolenaar91458462021-01-13 20:08:38 +01005761 // count the number of characters
5762 t = str;
5763 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5764 t += mb_ptr2len(t);
5765
5766 // In insert mode, when the cursor is at the end of a non-empty line,
5767 // byteidx points to the NUL character immediately past the end of the
5768 // string. In this case, add one to the character count.
5769 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5770 count++;
5771
5772 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005773}
5774
5775/*
5776 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005777 * byte index. Works only for loaded buffers. Returns -1 on failure.
5778 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005779 */
5780 int
5781buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5782{
5783 char_u *str;
5784 char_u *t;
5785
5786 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5787 return -1;
5788
5789 if (lnum > buf->b_ml.ml_line_count)
5790 lnum = buf->b_ml.ml_line_count;
5791
5792 str = ml_get_buf(buf, lnum, FALSE);
5793 if (str == NULL)
5794 return -1;
5795
5796 // Convert the character offset to a byte offset
5797 t = str;
5798 while (*t != NUL && --charidx > 0)
5799 t += mb_ptr2len(t);
5800
Bram Moolenaar91458462021-01-13 20:08:38 +01005801 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005802}
5803
5804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005806 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005808 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005809var2fpos(
5810 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005811 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005812 int *fnum, // set to fnum for '0, 'A, etc.
5813 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005815 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005817 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005819 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005820 if (varp->v_type == VAR_LIST)
5821 {
5822 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005823 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005824 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005825 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005826
5827 l = varp->vval.v_list;
5828 if (l == NULL)
5829 return NULL;
5830
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005831 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005832 pos.lnum = list_find_nr(l, 0L, &error);
5833 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005834 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005835 if (charcol)
5836 len = (long)mb_charlen(ml_get(pos.lnum));
5837 else
5838 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005839
Bram Moolenaarec65d772020-08-20 22:29:12 +02005840 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005841 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005842 li = list_find(l, 1L);
5843 if (li != NULL && li->li_tv.v_type == VAR_STRING
5844 && li->li_tv.vval.v_string != NULL
5845 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005846 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005847 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005848 }
5849 else
5850 {
5851 pos.col = list_find_nr(l, 1L, &error);
5852 if (error)
5853 return NULL;
5854 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005855
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005856 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005857 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005858 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005859 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005860
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005861 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005862 pos.coladd = list_find_nr(l, 2L, &error);
5863 if (error)
5864 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005865
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005866 return &pos;
5867 }
5868
Bram Moolenaarc5809432021-03-27 21:23:30 +01005869 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5870 return NULL;
5871
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005872 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005873 if (name == NULL)
5874 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005875
5876 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005877 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005878 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005879 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005880 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005881 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005882 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005883 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005884 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005885 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005886 pos = VIsual;
5887 else
5888 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005889 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005890 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005891 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005893 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005894 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5896 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005897 pos = *pp;
5898 }
5899 if (pos.lnum != 0)
5900 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005901 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005902 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5903 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005905
Bram Moolenaara5525202006-03-02 22:52:09 +00005906 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005907
Bram Moolenaar477933c2007-07-17 14:32:23 +00005908 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005909 {
5910 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005911 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005912 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005913 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005914 // In silent Ex mode topline is zero, but that's not a valid line
5915 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005916 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005917 return &pos;
5918 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005919 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005920 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005921 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005922 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005923 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005924 return &pos;
5925 }
5926 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005927 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005929 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 {
5931 pos.lnum = curbuf->b_ml.ml_line_count;
5932 pos.col = 0;
5933 }
5934 else
5935 {
5936 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005937 if (charcol)
5938 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5939 else
5940 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 }
5942 return &pos;
5943 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005944 if (in_vim9script())
5945 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 return NULL;
5947}
5948
5949/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005950 * Convert list in "arg" into a position and optional file number.
5951 * When "fnump" is NULL there is no file number, only 3 items.
5952 * Note that the column is passed on as-is, the caller may want to decrement
5953 * it to use 1 for the first column.
5954 * Return FAIL when conversion is not possible, doesn't check the position for
5955 * validity.
5956 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005957 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005958list2fpos(
5959 typval_T *arg,
5960 pos_T *posp,
5961 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005962 colnr_T *curswantp,
5963 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005964{
5965 list_T *l = arg->vval.v_list;
5966 long i = 0;
5967 long n;
5968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005969 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5970 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005971 if (arg->v_type != VAR_LIST
5972 || l == NULL
5973 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005974 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005975 return FAIL;
5976
5977 if (fnump != NULL)
5978 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005979 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005980 if (n < 0)
5981 return FAIL;
5982 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005983 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005984 *fnump = n;
5985 }
5986
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005987 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005988 if (n < 0)
5989 return FAIL;
5990 posp->lnum = n;
5991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005992 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005993 if (n < 0)
5994 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005995 // If character position is specified, then convert to byte position
5996 if (charcol)
5997 {
5998 buf_T *buf;
5999
6000 // Get the text for the specified line in a loaded buffer
6001 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6002 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6003 return FAIL;
6004
Bram Moolenaar91458462021-01-13 20:08:38 +01006005 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006006 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006007 posp->col = n;
6008
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006009 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006010 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006011 posp->coladd = 0;
6012 else
6013 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006014
Bram Moolenaar493c1782014-05-28 14:34:46 +02006015 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006016 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006017
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006018 return OK;
6019}
6020
6021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 * Get the length of an environment variable name.
6023 * Advance "arg" to the first character after the name.
6024 * Return 0 for error.
6025 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006026 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006027get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028{
6029 char_u *p;
6030 int len;
6031
6032 for (p = *arg; vim_isIDc(*p); ++p)
6033 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006034 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 return 0;
6036
6037 len = (int)(p - *arg);
6038 *arg = p;
6039 return len;
6040}
6041
6042/*
6043 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006044 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 * Return 0 if something is wrong.
6046 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006047 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006048get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049{
6050 char_u *p;
6051 int len;
6052
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006053 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006055 {
6056 if (*p == ':')
6057 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006058 // "s:" is start of "s:var", but "n:" is not and can be used in
6059 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006060 len = (int)(p - *arg);
6061 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6062 || len > 1)
6063 break;
6064 }
6065 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006066 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 return 0;
6068
6069 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006070 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071
6072 return len;
6073}
6074
6075/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006076 * Get the length of the name of a variable or function.
6077 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006079 * Return -1 if curly braces expansion failed.
6080 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 * If the name contains 'magic' {}'s, expand them and return the
6082 * expanded name in an allocated string via 'alias' - caller must free.
6083 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006084 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006085get_name_len(
6086 char_u **arg,
6087 char_u **alias,
6088 int evaluate,
6089 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090{
6091 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 char_u *p;
6093 char_u *expr_start;
6094 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006096 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097
6098 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6099 && (*arg)[2] == (int)KE_SNR)
6100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006101 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 *arg += 3;
6103 return get_id_len(arg) + 3;
6104 }
6105 len = eval_fname_script(*arg);
6106 if (len > 0)
6107 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006108 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 *arg += len;
6110 }
6111
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006113 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006115 p = find_name_end(*arg, &expr_start, &expr_end,
6116 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 if (expr_start != NULL)
6118 {
6119 char_u *temp_string;
6120
6121 if (!evaluate)
6122 {
6123 len += (int)(p - *arg);
6124 *arg = skipwhite(p);
6125 return len;
6126 }
6127
6128 /*
6129 * Include any <SID> etc in the expanded string:
6130 * Thus the -len here.
6131 */
6132 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6133 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006134 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 *alias = temp_string;
6136 *arg = skipwhite(p);
6137 return (int)STRLEN(temp_string);
6138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
6140 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006141 // Only give an error when there is something, otherwise it will be
6142 // reported at a higher level.
6143 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006144 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145
6146 return len;
6147}
6148
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006149/*
6150 * Find the end of a variable or function name, taking care of magic braces.
6151 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6152 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006153 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006154 * Return a pointer to just after the name. Equal to "arg" if there is no
6155 * valid name.
6156 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006157 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006158find_name_end(
6159 char_u *arg,
6160 char_u **expr_start,
6161 char_u **expr_end,
6162 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006164 int mb_nest = 0;
6165 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006167 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006168 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006170 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006172 *expr_start = NULL;
6173 *expr_end = NULL;
6174 }
6175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006176 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006177 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6178 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006179 return arg;
6180
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006181 for (p = arg; *p != NUL
6182 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006183 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006184 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006185 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006186 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006187 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006188 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006189 if (*p == '\'')
6190 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006191 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006192 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006193 ;
6194 if (*p == NUL)
6195 break;
6196 }
6197 else if (*p == '"')
6198 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006199 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006200 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006201 if (*p == '\\' && p[1] != NUL)
6202 ++p;
6203 if (*p == NUL)
6204 break;
6205 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006206 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6207 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006208 // "s:" is start of "s:var", but "n:" is not and can be used in
6209 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006210 len = (int)(p - arg);
6211 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006212 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006213 break;
6214 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006215
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006216 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006218 if (*p == '[')
6219 ++br_nest;
6220 else if (*p == ']')
6221 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006223
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006224 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006226 if (*p == '{')
6227 {
6228 mb_nest++;
6229 if (expr_start != NULL && *expr_start == NULL)
6230 *expr_start = p;
6231 }
6232 else if (*p == '}')
6233 {
6234 mb_nest--;
6235 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6236 *expr_end = p;
6237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 }
6240
6241 return p;
6242}
6243
6244/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006245 * Expands out the 'magic' {}'s in a variable/function name.
6246 * Note that this can call itself recursively, to deal with
6247 * constructs like foo{bar}{baz}{bam}
6248 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6249 * "in_start" ^
6250 * "expr_start" ^
6251 * "expr_end" ^
6252 * "in_end" ^
6253 *
6254 * Returns a new allocated string, which the caller must free.
6255 * Returns NULL for failure.
6256 */
6257 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006258make_expanded_name(
6259 char_u *in_start,
6260 char_u *expr_start,
6261 char_u *expr_end,
6262 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006263{
6264 char_u c1;
6265 char_u *retval = NULL;
6266 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006267
6268 if (expr_end == NULL || in_end == NULL)
6269 return NULL;
6270 *expr_start = NUL;
6271 *expr_end = NUL;
6272 c1 = *in_end;
6273 *in_end = NUL;
6274
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006275 temp_result = eval_to_string(expr_start + 1, FALSE);
6276 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006277 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006278 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6279 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006280 if (retval != NULL)
6281 {
6282 STRCPY(retval, in_start);
6283 STRCAT(retval, temp_result);
6284 STRCAT(retval, expr_end + 1);
6285 }
6286 }
6287 vim_free(temp_result);
6288
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006289 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006290 *expr_start = '{';
6291 *expr_end = '}';
6292
6293 if (retval != NULL)
6294 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006295 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006296 if (expr_start != NULL)
6297 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006298 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006299 temp_result = make_expanded_name(retval, expr_start,
6300 expr_end, temp_result);
6301 vim_free(retval);
6302 retval = temp_result;
6303 }
6304 }
6305
6306 return retval;
6307}
6308
6309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006311 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006313 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006314eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006316 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006317}
6318
6319/*
6320 * Return TRUE if character "c" can be used as the first character in a
6321 * variable or function name (excluding '{' and '}').
6322 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006323 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006324eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006325{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006326 return ASCII_ISALPHA(c) || c == '_';
6327}
6328
6329/*
6330 * Return TRUE if character "c" can be used as the first character of a
6331 * dictionary key.
6332 */
6333 int
6334eval_isdictc(int c)
6335{
6336 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337}
6338
6339/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006340 * Handle:
6341 * - expr[expr], expr[expr:expr] subscript
6342 * - ".name" lookup
6343 * - function call with Funcref variable: func(expr)
6344 * - method call: var->method()
6345 *
6346 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006347 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006348 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006349 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006350handle_subscript(
6351 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006352 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006353 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006354 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006355 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006356{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006357 int evaluate = evalarg != NULL
6358 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006359 int ret = OK;
6360 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006361 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006362 int getnext;
6363 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006364
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006365 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006366 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006367 // When at the end of the line and ".name" or "->{" or "->X" follows in
6368 // the next line then consume the line break.
6369 p = eval_next_non_blank(*arg, evalarg, &getnext);
6370 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006371 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006372 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6373 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6374 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006375 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006376 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006377 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006378 check_white = FALSE;
6379 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006380
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006381 if (rettv->v_type == VAR_ANY)
6382 {
6383 char_u *exp_name;
6384 int cc;
6385 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006386 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006387 type_T *type;
6388
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006389 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006390 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006391 if (**arg != '.')
6392 {
6393 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006394 semsg(_(e_expected_dot_after_name_str),
6395 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006396 ret = FAIL;
6397 break;
6398 }
6399 ++*arg;
6400 if (IS_WHITE_OR_NUL(**arg))
6401 {
6402 if (verbose)
6403 emsg(_(e_no_white_space_allowed_after_dot));
6404 ret = FAIL;
6405 break;
6406 }
6407
6408 // isolate the name
6409 exp_name = *arg;
6410 while (eval_isnamec(**arg))
6411 ++*arg;
6412 cc = **arg;
6413 **arg = NUL;
6414
6415 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006416 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006417 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006418
6419 if (idx < 0 && ufunc == NULL)
6420 {
6421 ret = FAIL;
6422 break;
6423 }
6424 if (idx >= 0)
6425 {
6426 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6427 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6428
6429 copy_tv(sv->sv_tv, rettv);
6430 }
6431 else
6432 {
6433 rettv->v_type = VAR_FUNC;
6434 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6435 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006436 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006437 }
6438
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006439 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6440 || rettv->v_type == VAR_PARTIAL))
6441 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006442 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006443 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6444 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006445
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006446 // Stop the expression evaluation when immediately aborting on
6447 // error, or when an interrupt occurred or an exception was thrown
6448 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006449 if (aborting())
6450 {
6451 if (ret == OK)
6452 clear_tv(rettv);
6453 ret = FAIL;
6454 }
6455 dict_unref(selfdict);
6456 selfdict = NULL;
6457 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006458 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006459 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006460 if (in_vim9script())
6461 *arg = skipwhite(p + 2);
6462 else
6463 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006464 if (ret == OK)
6465 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006466 if (VIM_ISWHITE(**arg))
6467 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006468 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006469 ret = FAIL;
6470 }
6471 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006472 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006473 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006474 else
6475 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006476 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006477 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006478 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006479 // "." is ".name" lookup when we found a dict or when evaluating and
6480 // scriptversion is at least 2, where string concatenation is "..".
6481 else if (**arg == '['
6482 || (**arg == '.' && (rettv->v_type == VAR_DICT
6483 || (!evaluate
6484 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006485 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006486 {
6487 dict_unref(selfdict);
6488 if (rettv->v_type == VAR_DICT)
6489 {
6490 selfdict = rettv->vval.v_dict;
6491 if (selfdict != NULL)
6492 ++selfdict->dv_refcount;
6493 }
6494 else
6495 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006496 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006497 {
6498 clear_tv(rettv);
6499 ret = FAIL;
6500 }
6501 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006502 else
6503 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006504 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006505
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006506 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6507 // Don't do this when "Func" is already a partial that was bound
6508 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006509 if (selfdict != NULL
6510 && (rettv->v_type == VAR_FUNC
6511 || (rettv->v_type == VAR_PARTIAL
6512 && (rettv->vval.v_partial->pt_auto
6513 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006514 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006515
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006516 dict_unref(selfdict);
6517 return ret;
6518}
6519
6520/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006521 * Make a copy of an item.
6522 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006523 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006524 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6525 * reference to an already copied list/dict can be used.
6526 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006527 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006528 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006529item_copy(
6530 typval_T *from,
6531 typval_T *to,
6532 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006533 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006534 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535{
6536 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006537 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006538
Bram Moolenaar33570922005-01-25 22:26:29 +00006539 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006540 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006541 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006542 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006543 }
6544 ++recurse;
6545
6546 switch (from->v_type)
6547 {
6548 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006549 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550 case VAR_STRING:
6551 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006552 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006553 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006554 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006555 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006556 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006557 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006558 copy_tv(from, to);
6559 break;
6560 case VAR_LIST:
6561 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006562 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006563 if (from->vval.v_list == NULL)
6564 to->vval.v_list = NULL;
6565 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6566 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006567 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006568 to->vval.v_list = from->vval.v_list->lv_copylist;
6569 ++to->vval.v_list->lv_refcount;
6570 }
6571 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006572 to->vval.v_list = list_copy(from->vval.v_list,
6573 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006574 if (to->vval.v_list == NULL)
6575 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006576 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006577 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006578 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006579 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006580 case VAR_DICT:
6581 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006582 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006583 if (from->vval.v_dict == NULL)
6584 to->vval.v_dict = NULL;
6585 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6586 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006587 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006588 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6589 ++to->vval.v_dict->dv_refcount;
6590 }
6591 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006592 to->vval.v_dict = dict_copy(from->vval.v_dict,
6593 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 if (to->vval.v_dict == NULL)
6595 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006596 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006597 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006598 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006599 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006600 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006602 }
6603 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006604 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006605}
6606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006607 void
6608echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6609{
6610 char_u *tofree;
6611 char_u numbuf[NUMBUFLEN];
6612 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6613
6614 if (*atstart)
6615 {
6616 *atstart = FALSE;
6617 // Call msg_start() after eval1(), evaluating the expression
6618 // may cause a message to appear.
6619 if (with_space)
6620 {
6621 // Mark the saved text as finishing the line, so that what
6622 // follows is displayed on a new line when scrolling back
6623 // at the more prompt.
6624 msg_sb_eol();
6625 msg_start();
6626 }
6627 }
6628 else if (with_space)
6629 msg_puts_attr(" ", echo_attr);
6630
6631 if (p != NULL)
6632 for ( ; *p != NUL && !got_int; ++p)
6633 {
6634 if (*p == '\n' || *p == '\r' || *p == TAB)
6635 {
6636 if (*p != TAB && *needclr)
6637 {
6638 // remove any text still there from the command
6639 msg_clr_eos();
6640 *needclr = FALSE;
6641 }
6642 msg_putchar_attr(*p, echo_attr);
6643 }
6644 else
6645 {
6646 if (has_mbyte)
6647 {
6648 int i = (*mb_ptr2len)(p);
6649
6650 (void)msg_outtrans_len_attr(p, i, echo_attr);
6651 p += i - 1;
6652 }
6653 else
6654 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6655 }
6656 }
6657 vim_free(tofree);
6658}
6659
Bram Moolenaare9a41262005-01-15 22:18:47 +00006660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 * ":echo expr1 ..." print each argument separated with a space, add a
6662 * newline at the end.
6663 * ":echon expr1 ..." print each argument plain.
6664 */
6665 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006666ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
6668 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006669 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006670 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 int needclr = TRUE;
6672 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006673 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006674 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006675 evalarg_T evalarg;
6676
Bram Moolenaare707c882020-07-01 13:07:37 +02006677 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 if (eap->skip)
6680 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006681 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006683 // If eval1() causes an error message the text from the command may
6684 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006685 need_clr_eos = needclr;
6686
Bram Moolenaar68db9962021-05-09 23:19:22 +02006687 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006688 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 {
6690 /*
6691 * Report the invalid expression unless the expression evaluation
6692 * has been cancelled due to an aborting error, an interrupt, or an
6693 * exception.
6694 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006695 if (!aborting() && did_emsg == did_emsg_before
6696 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006697 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006698 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 break;
6700 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006701 need_clr_eos = FALSE;
6702
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006704 {
6705 if (rettv.v_type == VAR_VOID)
6706 {
6707 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6708 break;
6709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006710 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006711 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006712
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006713 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 arg = skipwhite(arg);
6715 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006716 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006717 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
6719 if (eap->skip)
6720 --emsg_skip;
6721 else
6722 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006723 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 if (needclr)
6725 msg_clr_eos();
6726 if (eap->cmdidx == CMD_echo)
6727 msg_end();
6728 }
6729}
6730
6731/*
6732 * ":echohl {name}".
6733 */
6734 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006735ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006737 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738}
6739
6740/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006741 * Returns the :echo attribute
6742 */
6743 int
6744get_echo_attr(void)
6745{
6746 return echo_attr;
6747}
6748
6749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 * ":execute expr1 ..." execute the result of an expression.
6751 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006752 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006754 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 * Each gets spaces around each argument and a newline at the end for
6756 * echo commands
6757 */
6758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006759ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760{
6761 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006762 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 int ret = OK;
6764 char_u *p;
6765 garray_T ga;
6766 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006767 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768
6769 ga_init2(&ga, 1, 80);
6770
6771 if (eap->skip)
6772 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006773 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006775 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006776 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778
6779 if (!eap->skip)
6780 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006781 char_u buf[NUMBUFLEN];
6782
6783 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006784 {
6785 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6786 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006787 semsg(_(e_using_invalid_value_as_string_str),
6788 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006789 p = NULL;
6790 }
6791 else
6792 p = tv_get_string_buf(&rettv, buf);
6793 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006794 else
6795 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006796 if (p == NULL)
6797 {
6798 clear_tv(&rettv);
6799 ret = FAIL;
6800 break;
6801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 len = (int)STRLEN(p);
6803 if (ga_grow(&ga, len + 2) == FAIL)
6804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006805 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 ret = FAIL;
6807 break;
6808 }
6809 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 ga.ga_len += len;
6813 }
6814
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006815 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 arg = skipwhite(arg);
6817 }
6818
6819 if (ret != FAIL && ga.ga_data != NULL)
6820 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006821 // use the first line of continuation lines for messages
6822 SOURCING_LNUM = start_lnum;
6823
Bram Moolenaar37fef162022-08-29 18:16:32 +01006824 if (eap->cmdidx == CMD_echomsg
6825 || eap->cmdidx == CMD_echowindow
6826 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006827 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006828 // Mark the already saved text as finishing the line, so that what
6829 // follows is displayed on a new line when scrolling back at the
6830 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006831 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006832 }
6833
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006834 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006835 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006836 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006837 out_flush();
6838 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006839 else if (eap->cmdidx == CMD_echowindow)
6840 {
6841#ifdef HAS_MESSAGE_WINDOW
6842 start_echowindow();
6843#endif
6844 msg_attr(ga.ga_data, echo_attr);
6845#ifdef HAS_MESSAGE_WINDOW
6846 end_echowindow();
6847#endif
6848 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006849 else if (eap->cmdidx == CMD_echoconsole)
6850 {
6851 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6852 ui_write((char_u *)"\r\n", 2, TRUE);
6853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 else if (eap->cmdidx == CMD_echoerr)
6855 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006856 int save_did_emsg = did_emsg;
6857
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006858 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006859 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 if (!force_abort)
6861 did_emsg = save_did_emsg;
6862 }
6863 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006864 {
6865 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6866
6867 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6868 sticky_cmdmod_flags = cmdmod.cmod_flags
6869 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 do_cmdline((char_u *)ga.ga_data,
6871 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006872 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 }
6875
6876 ga_clear(&ga);
6877
6878 if (eap->skip)
6879 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006880 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881}
6882
6883/*
6884 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6885 * "arg" points to the "&" or '+' when called, to "option" when returning.
6886 * Returns NULL when no option name found. Otherwise pointer to the char
6887 * after the option name.
6888 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006889 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006890find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 char_u *p = *arg;
6893
6894 ++p;
6895 if (*p == 'g' && p[1] == ':')
6896 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006897 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 p += 2;
6899 }
6900 else if (*p == 'l' && p[1] == ':')
6901 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006902 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 p += 2;
6904 }
6905 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006906 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
6908 if (!ASCII_ISALPHA(*p))
6909 return NULL;
6910 *arg = p;
6911
6912 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006913 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 else
6915 while (ASCII_ISALPHA(*p))
6916 ++p;
6917 return p;
6918}
6919
6920/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006921 * Display script name where an item was last set.
6922 * Should only be invoked when 'verbose' is non-zero.
6923 */
6924 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006925last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006926{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006927 char_u *p;
6928
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006929 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006930 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006931 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006932 if (p != NULL)
6933 {
6934 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006935 msg_puts(_("\n\tLast set from "));
6936 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006937 if (script_ctx.sc_lnum > 0)
6938 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006939 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006940 msg_outnum((long)script_ctx.sc_lnum);
6941 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006942 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006943 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006944 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006945 }
6946}
6947
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006948#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
6950/*
6951 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006952 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 * "flags" can be "g" to do a global substitute.
6954 * Returns an allocated string, NULL for error.
6955 */
6956 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006957do_string_sub(
6958 char_u *str,
6959 char_u *pat,
6960 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006961 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006962 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963{
6964 int sublen;
6965 regmatch_T regmatch;
6966 int i;
6967 int do_all;
6968 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006969 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 garray_T ga;
6971 char_u *ret;
6972 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006973 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006975 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006977 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978
6979 ga_init2(&ga, 1, 200);
6980
6981 do_all = (flags[0] == 'g');
6982
6983 regmatch.rm_ic = p_ic;
6984 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6985 if (regmatch.regprog != NULL)
6986 {
6987 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006988 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006991 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006992 if (regmatch.startp[0] == regmatch.endp[0])
6993 {
6994 if (zero_width == regmatch.startp[0])
6995 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006996 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006997 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006998 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6999 (size_t)i);
7000 ga.ga_len += i;
7001 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007002 continue;
7003 }
7004 zero_width = regmatch.startp[0];
7005 }
7006
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 /*
7008 * Get some space for a temporary buffer to do the substitution
7009 * into. It will contain:
7010 * - The text up to where the match is.
7011 * - The substituted text.
7012 * - The text after the match.
7013 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007014 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01007015 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7017 {
7018 ga_clear(&ga);
7019 break;
7020 }
7021
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007022 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 i = (int)(regmatch.startp[0] - tail);
7024 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007025 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007026 (void)vim_regsub(&regmatch, sub, expr,
7027 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7028 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007030 tail = regmatch.endp[0];
7031 if (*tail == NUL)
7032 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 if (!do_all)
7034 break;
7035 }
7036
7037 if (ga.ga_data != NULL)
7038 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7039
Bram Moolenaar473de612013-06-08 18:19:48 +02007040 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 }
7042
7043 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7044 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007045 if (p_cpo == empty_option)
7046 p_cpo = save_cpo;
7047 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007048 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007049 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007050 // If it's still empty it was changed and restored, need to restore in
7051 // the complicated way.
7052 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007053 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007054 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056
7057 return ret;
7058}