blob: 77f0d7fb3cbd35eb65489cdbe3b9366fbe9e2a38 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020032static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
33static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
34static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
35static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010036static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020037static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010038static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
39static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
40static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010042static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020044
Bram Moolenaare21c1582019-03-02 11:57:09 +010045/*
46 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010047 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020049 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010050num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010051{
52 varnumber_T result;
53
Bram Moolenaar99880f92021-01-20 21:23:14 +010054 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010055 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010056 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010057 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010058 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010059 if (failed != NULL)
60 *failed = TRUE;
61 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010062 if (n1 == 0)
63 result = VARNUM_MIN; // similar to NaN
64 else if (n1 < 0)
65 result = -VARNUM_MAX;
66 else
67 result = VARNUM_MAX;
68 }
69 else
70 result = n1 / n2;
71
72 return result;
73}
74
75/*
76 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010077 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020079 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010080num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010081{
Bram Moolenaar99880f92021-01-20 21:23:14 +010082 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010084 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010085 if (failed != NULL)
86 *failed = TRUE;
87 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010088 return (n2 == 0) ? 0 : (n1 % n2);
89}
90
Bram Moolenaar33570922005-01-25 22:26:29 +000091/*
92 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000093 */
94 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010095eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +000096{
Bram Moolenaare5cdf152019-08-29 22:09:46 +020097 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +020098 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +000099}
100
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000101#if defined(EXITFREE) || defined(PROTO)
102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100103eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000104{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200105 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100106 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200107 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000108
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200109 // autoloaded script names
110 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000111
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200112 // unreferenced lists and dicts
113 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200114
115 // functions not garbage collected
116 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000117}
118#endif
119
Bram Moolenaare6b53242020-07-01 17:28:33 +0200120 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200121fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
122{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100123 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200124 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200125 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200126 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200127 evalarg->eval_cstack = eap->cstack;
Bram Moolenaara7583c42022-05-07 21:14:05 +0100128 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200129 {
130 evalarg->eval_getline = eap->getline;
131 evalarg->eval_cookie = eap->cookie;
132 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200133 }
134}
135
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136/*
137 * Top level evaluation function, returning a boolean.
138 * Sets "error" to TRUE if there was an error.
139 * Return TRUE or FALSE.
140 */
141 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100142eval_to_bool(
143 char_u *arg,
144 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200145 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100146 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147{
Bram Moolenaar33570922005-01-25 22:26:29 +0000148 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200149 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200150 evalarg_T evalarg;
151
Bram Moolenaar37c83712020-06-30 21:18:36 +0200152 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154 if (skip)
155 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200156 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 else
159 {
160 *error = FALSE;
161 if (!skip)
162 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200163 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200164 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200165 else
166 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000167 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 }
169 }
170 if (skip)
171 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200172 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200174 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175}
176
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100177/*
178 * Call eval1() and give an error message if not done at a lower level.
179 */
180 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200181eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100182{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100183 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100184 int ret;
185 int did_emsg_before = did_emsg;
186 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200187 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100188
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200189 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
190
191 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100192 if (ret == FAIL)
193 {
194 // Report the invalid expression unless the expression evaluation has
195 // been cancelled due to an aborting error, an interrupt, or an
196 // exception, or we already gave a more specific error.
197 // Also check called_emsg for when using assert_fails().
198 if (!aborting() && did_emsg == did_emsg_before
199 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200200 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100203 return ret;
204}
205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200207 * Return whether a typval is a valid expression to pass to eval_expr_typval()
208 * or eval_expr_to_bool(). An empty string returns FALSE;
209 */
210 int
211eval_expr_valid_arg(typval_T *tv)
212{
213 return tv->v_type != VAR_UNKNOWN
214 && (tv->v_type != VAR_STRING
215 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
216}
217
218/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 * Evaluate an expression, which can be a function, partial or string.
220 * Pass arguments "argv[argc]".
221 * Return the result in "rettv" and OK or FAIL.
222 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200223 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100224eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
225{
226 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100227 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200228 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100229
230 if (expr->v_type == VAR_FUNC)
231 {
232 s = expr->vval.v_string;
233 if (s == NULL || *s == NUL)
234 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200235 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000236 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200237 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100238 return FAIL;
239 }
240 else if (expr->v_type == VAR_PARTIAL)
241 {
242 partial_T *partial = expr->vval.v_partial;
243
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200244 if (partial == NULL)
245 return FAIL;
246
Bram Moolenaar822ba242020-05-24 23:00:18 +0200247 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200248 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 {
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100250 funccall_T *fc = create_funccal(partial->pt_func, rettv);
251 int r;
252
253 if (fc == NULL)
254 return FAIL;
255
Bram Moolenaar69082912022-09-22 21:35:19 +0100256 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100257 r = call_def_function(partial->pt_func, argc, argv,
258 DEF_USE_PT_ARGV, partial, fc, rettv);
259 remove_funccal();
260 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261 return FAIL;
262 }
263 else
264 {
265 s = partial_name(partial);
266 if (s == NULL || *s == NUL)
267 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200268 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000269 funcexe.fe_evaluate = TRUE;
270 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
272 return FAIL;
273 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100274 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200275 else if (expr->v_type == VAR_INSTR)
276 {
277 return exe_typval_instr(expr, rettv);
278 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100279 else
280 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000281 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100282 if (s == NULL)
283 return FAIL;
284 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200285 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100286 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200287 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100288 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200289 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200290 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100291 return FAIL;
292 }
293 }
294 return OK;
295}
296
297/*
298 * Like eval_to_bool() but using a typval_T instead of a string.
299 * Works for string, funcref and partial.
300 */
301 int
302eval_expr_to_bool(typval_T *expr, int *error)
303{
304 typval_T rettv;
305 int res;
306
307 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
308 {
309 *error = TRUE;
310 return FALSE;
311 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200312 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100313 clear_tv(&rettv);
314 return res;
315}
316
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317/*
318 * Top level evaluation function, returning a string. If "skip" is TRUE,
319 * only parsing to "nextcmd" is done, without reporting errors. Return
320 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
321 */
322 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100323eval_to_string_skip(
324 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200325 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100326 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327{
Bram Moolenaar33570922005-01-25 22:26:29 +0000328 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200330 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331
Bram Moolenaar37c83712020-06-30 21:18:36 +0200332 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 if (skip)
334 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200335 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 retval = NULL;
337 else
338 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100339 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000340 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 }
342 if (skip)
343 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200344 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
346 return retval;
347}
348
349/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100350 * Initialize "evalarg" for use.
351 */
352 void
353init_evalarg(evalarg_T *evalarg)
354{
355 CLEAR_POINTER(evalarg);
356 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
357}
358
359/*
360 * If "evalarg->eval_tofree" is not NULL free it later.
361 * Caller is expected to overwrite "evalarg->eval_tofree" next.
362 */
363 static void
364free_eval_tofree_later(evalarg_T *evalarg)
365{
366 if (evalarg->eval_tofree != NULL)
367 {
368 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
369 ((char_u **)evalarg->eval_tofree_ga.ga_data)
370 [evalarg->eval_tofree_ga.ga_len++]
371 = evalarg->eval_tofree;
372 else
373 vim_free(evalarg->eval_tofree);
374 }
375}
376
377/*
378 * After using "evalarg" filled from "eap": free the memory.
379 */
380 void
381clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
382{
383 if (evalarg != NULL)
384 {
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;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100528 char_u numbuf[NUMBUFLEN];
Bram Moolenaar883cf972021-01-15 18:04:43 +0100529
530 if (convert && tv->v_type == VAR_LIST)
531 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000532 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100533 if (tv->vval.v_list != NULL)
534 {
535 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
536 if (tv->vval.v_list->lv_len > 0)
537 ga_append(&ga, NL);
538 }
539 ga_append(&ga, NUL);
540 retval = (char_u *)ga.ga_data;
541 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100542 else if (convert && tv->v_type == VAR_FLOAT)
543 {
544 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
545 retval = vim_strsave(numbuf);
546 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100547 else
548 retval = vim_strsave(tv_get_string(tv));
549 return retval;
550}
551
552/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200553 * Top level evaluation function, returning a string. Does not handle line
554 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000555 * When "convert" is TRUE convert a List into a sequence of lines and convert
556 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 * Return pointer to allocated memory, or NULL for failure.
558 */
559 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100560eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100561 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100562 int convert,
563 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564{
Bram Moolenaar33570922005-01-25 22:26:29 +0000565 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100567 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100569 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
570 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 retval = NULL;
572 else
573 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100574 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000575 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100577 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578
579 return retval;
580}
581
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100582 char_u *
583eval_to_string(
584 char_u *arg,
585 int convert)
586{
587 return eval_to_string_eap(arg, convert, NULL);
588}
589
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000591 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100592 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200593 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 */
595 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100596eval_to_string_safe(
597 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000598 int use_sandbox,
599 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
601 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200602 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200603 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200604 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
Bram Moolenaar9530b582022-01-22 13:39:08 +0000606 if (!keep_script_version)
607 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200608 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000609 if (use_sandbox)
610 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100611 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200612 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200613 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000614 if (use_sandbox)
615 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100616 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200617 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200618 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200619 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 return retval;
621}
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623/*
624 * Top level evaluation function, returning a number.
625 * Evaluates "expr" silently.
626 * Returns -1 for an error.
627 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200628 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100629eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630{
Bram Moolenaar33570922005-01-25 22:26:29 +0000631 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200632 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000633 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634
635 ++emsg_off;
636
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200637 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 retval = -1;
639 else
640 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100641 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000642 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 }
644 --emsg_off;
645
646 return retval;
647}
648
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000649/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000650 * Top level evaluation function.
651 * Returns an allocated typval_T with the result.
652 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000653 */
654 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200655eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000656{
657 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200658 evalarg_T evalarg;
659
660 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000661
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200662 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200663 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100664 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000665
Bram Moolenaar37c83712020-06-30 21:18:36 +0200666 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000667 return tv;
668}
669
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000671 * "*arg" points to what can be a function name in the form of "import.Name" or
672 * "Funcref". Return the name of the function. Set "tofree" to something that
673 * was allocated.
674 * If "verbose" is FALSE no errors are given.
675 * Return NULL for any failure.
676 */
677 static char_u *
678deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000679 char_u **arg,
680 char_u **tofree,
681 evalarg_T *evalarg,
682 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000683{
684 typval_T ref;
685 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100686 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000687
688 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100689 if (evalarg != NULL)
690 {
691 // need to evaluate this to get an import, like in "a.Func"
692 save_flags = evalarg->eval_flags;
693 evalarg->eval_flags |= EVAL_EVALUATE;
694 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100695 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100696 {
697 dictitem_T *v;
698
699 // If <SID>VarName was used it would not be found, try another way.
700 v = find_var_also_in_script(name, NULL, FALSE);
701 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100702 {
703 name = NULL;
704 goto theend;
705 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100706 copy_tv(&v->di_tv, &ref);
707 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000708 if (*skipwhite(*arg) != NUL)
709 {
710 if (verbose)
711 semsg(_(e_trailing_characters_str), *arg);
712 name = NULL;
713 }
714 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
715 {
716 name = ref.vval.v_string;
717 ref.vval.v_string = NULL;
718 *tofree = name;
719 }
720 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
721 {
722 if (ref.vval.v_partial->pt_argc > 0
723 || ref.vval.v_partial->pt_dict != NULL)
724 {
725 if (verbose)
726 emsg(_(e_cannot_use_partial_here));
727 name = NULL;
728 }
729 else
730 {
731 name = vim_strsave(partial_name(ref.vval.v_partial));
732 *tofree = name;
733 }
734 }
735 else
736 {
737 if (verbose)
738 semsg(_(e_not_callable_type_str), name);
739 name = NULL;
740 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100741
742theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000743 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100744 if (evalarg != NULL)
745 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000746 return name;
747}
748
749/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100750 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200751 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
752 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000753 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200755 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100756call_vim_function(
757 char_u *func,
758 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200759 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200760 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000762 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200763 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000764 char_u *arg;
765 char_u *name;
766 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000767 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100769 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200770 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000771 funcexe.fe_firstline = curwin->w_cursor.lnum;
772 funcexe.fe_lastline = curwin->w_cursor.lnum;
773 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000774
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000775 // The name might be "import.Func" or "Funcref". We don't know, we need to
776 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000777 // autoload script has errors. Guess that when there is a dot in the name
778 // showing errors is the right choice.
779 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000780 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000781 if (ignore_errors)
782 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000783 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000784 if (ignore_errors)
785 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000786 if (name == NULL)
787 name = func;
788
789 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
790
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000791 if (ret == FAIL)
792 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000793 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000794
795 return ret;
796}
797
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100798/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100799 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000800 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
801 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000802 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000803 */
804 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100805call_func_retstr(
806 char_u *func,
807 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200808 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000809{
810 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000811 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000812
Bram Moolenaarded27a12018-08-01 19:06:03 +0200813 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000814 return NULL;
815
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100816 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000817 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 return retval;
819}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000820
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000821/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100822 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000823 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000824 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100825 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000826 */
827 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100828call_func_retlist(
829 char_u *func,
830 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200831 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832{
833 typval_T rettv;
834
Bram Moolenaarded27a12018-08-01 19:06:03 +0200835 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000836 return NULL;
837
838 if (rettv.v_type != VAR_LIST)
839 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100840 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
841 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000842 clear_tv(&rettv);
843 return NULL;
844 }
845
846 return rettv.vval.v_list;
847}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
Bram Moolenaare70dd112022-01-21 16:31:11 +0000849#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100851 * Evaluate "arg", which is 'foldexpr'.
852 * Note: caller must set "curwin" to match "arg".
853 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
854 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 */
856 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000857eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000859 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000860 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200861 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000863 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000864 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
865 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
Bram Moolenaare70dd112022-01-21 16:31:11 +0000867 arg = wp->w_p_fde;
868 current_sctx = wp->w_p_script_ctx[WV_FDE];
869
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000871 if (use_sandbox)
872 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100873 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200875 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 retval = 0;
877 else
878 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100879 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000880 if (tv.v_type == VAR_NUMBER)
881 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000882 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 retval = 0;
884 else
885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100886 // If the result is a string, check if there is a non-digit before
887 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000888 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (!VIM_ISDIGIT(*s) && *s != '-')
890 *cp = *s++;
891 retval = atol((char *)s);
892 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000893 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
895 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000896 if (use_sandbox)
897 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100898 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200899 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000900 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200902 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903}
904#endif
905
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000907 * Get an lval: variable, Dict item or List item that can be assigned a value
908 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
909 * "name.key", "name.key[expr]" etc.
910 * Indexing only works if "name" is an existing List or Dictionary.
911 * "name" points to the start of the name.
912 * If "rettv" is not NULL it points to the value to be assigned.
913 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
914 * wrong; must end in space or cmd separator.
915 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100916 * flags:
917 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100918 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100919 * GLV_NO_AUTOLOAD: do not use script autoloading
920 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000921 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000922 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000923 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000924 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200925 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100926get_lval(
927 char_u *name,
928 typval_T *rettv,
929 lval_T *lp,
930 int unlet,
931 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100932 int flags, // GLV_ values
933 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000934{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000935 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000936 char_u *expr_start, *expr_end;
937 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 dictitem_T *v;
939 typval_T var1;
940 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000941 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000942 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000943 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100944 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100945 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100946 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000947 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100949 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200950 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000951
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100952 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000953 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100954 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000955 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200956 lp->ll_name_end = find_name_end(name, NULL, NULL,
957 FNE_INCL_BR | fne_flags);
958 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000959 }
960
Bram Moolenaara749a422022-02-12 19:52:25 +0000961 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +0000962 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +0000963 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
964 {
965 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
966 return NULL;
967 }
968
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100969 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000970 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 if (expr_start != NULL)
973 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100974 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100975 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000976 && *p != '[' && *p != '.')
977 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000978 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 return NULL;
980 }
981
982 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
983 if (lp->ll_exp_name == NULL)
984 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100985 // Report an invalid expression in braces, unless the
986 // expression evaluation has been cancelled due to an
987 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000988 if (!aborting() && !quiet)
989 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000990 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000991 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 return NULL;
993 }
994 }
995 lp->ll_name = lp->ll_exp_name;
996 }
997 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000999 lp->ll_name = name;
1000
Bram Moolenaar4525a572022-02-13 11:57:33 +00001001 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001003 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001004 // However, "g:[key]" is indexing a dictionary.
1005 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001006 {
1007 --p;
1008 lp->ll_name_end = p;
1009 }
1010 if (*p == ':')
1011 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001012 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001013
Bram Moolenaar9510d222022-09-11 15:14:05 +01001014 if (is_scoped_variable(name))
1015 {
1016 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1017 return NULL;
1018 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001019 if (tp == p + 1 && !quiet)
1020 {
1021 semsg(_(e_white_space_required_after_str_str), ":", p);
1022 return NULL;
1023 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001024 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001025 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001026 semsg(_(e_using_type_not_in_script_context_str), p);
1027 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001028 }
1029
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001030 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001031 lp->ll_type = parse_type(&tp,
1032 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1033 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001034 if (lp->ll_type == NULL && !quiet)
1035 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001036 lp->ll_name_end = tp;
1037 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038 }
1039 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001040 if (lp->ll_name == NULL)
1041 return p;
1042
Bram Moolenaar62aec932022-01-29 21:45:34 +00001043 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001044 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001045 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001046
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001047 if (import != NULL)
1048 {
1049 ufunc_T *ufunc;
1050 type_T *type;
1051
Bram Moolenaar753885b2022-08-24 16:30:36 +01001052 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001053 lp->ll_sid = import->imp_sid;
1054 lp->ll_name = skipwhite(p + 1);
1055 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1056 lp->ll_name_end = p;
1057
1058 // check the item is exported
1059 cc = *p;
1060 *p = NUL;
1061 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001062 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001063 {
1064 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001065 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001066 }
1067 *p = cc;
1068 }
1069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001071 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001072 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 return p;
1074
Bram Moolenaar4525a572022-02-13 11:57:33 +00001075 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001076 {
1077 // using local variable
1078 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001079 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001080 }
1081 else
1082 {
1083 cc = *p;
1084 *p = NUL;
1085 // When we would write to the variable pass &ht and prevent autoload.
1086 writing = !(flags & GLV_READ_ONLY);
1087 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001088 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001089 if (v == NULL && !quiet)
1090 semsg(_(e_undefined_variable_str), lp->ll_name);
1091 *p = cc;
1092 if (v == NULL)
1093 return NULL;
1094 lp->ll_tv = &v->di_tv;
1095 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001096
Bram Moolenaar4525a572022-02-13 11:57:33 +00001097 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001098 {
1099 if (!quiet)
1100 semsg(_(e_variable_already_declared), lp->ll_name);
1101 return NULL;
1102 }
1103
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001104 /*
1105 * Loop until no more [idx] or .key is following.
1106 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001107 var1.v_type = VAR_UNKNOWN;
1108 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001109 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001110 {
Bram Moolenaarf21d5462022-09-10 12:36:00 +01001111 int r = OK;
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001112
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001113 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1114 {
1115 if (!quiet)
1116 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1117 return NULL;
1118 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001119 if (lp->ll_tv->v_type != VAR_LIST
1120 && lp->ll_tv->v_type != VAR_DICT
1121 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001122 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001123 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001124 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001125 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001127
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001128 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaare65081d2021-06-27 15:04:05 +02001129 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001130 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaare65081d2021-06-27 15:04:05 +02001131 else if (lp->ll_tv->v_type == VAR_BLOB
1132 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001133 r = rettv_blob_alloc(lp->ll_tv);
1134 if (r == FAIL)
1135 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001136
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001137 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001138 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001139 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001140 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001141 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001142 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001143
Bram Moolenaar4525a572022-02-13 11:57:33 +00001144 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001145 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001146 && lp->ll_tv == &v->di_tv
1147 && ht != NULL && ht == get_script_local_ht())
1148 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001149 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001150
1151 // Vim9 script local variable: get the type
1152 if (sv != NULL)
1153 lp->ll_valtype = sv->sv_type;
1154 }
1155
Bram Moolenaar8c711452005-01-14 21:53:12 +00001156 len = -1;
1157 if (*p == '.')
1158 {
1159 key = p + 1;
1160 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1161 ;
1162 if (len == 0)
1163 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001164 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001165 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001167 }
1168 p = key + len;
1169 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001170 else
1171 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001172 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001173 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 if (*p == ':')
1175 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001176 else
1177 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001178 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001179 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001180 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001181 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001182 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001183 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001184 clear_tv(&var1);
1185 return NULL;
1186 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001187 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001188 }
1189
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001190 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001191 if (*p == ':')
1192 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001193 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001194 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001196 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001197 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001198 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001199 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001200 if (rettv != NULL
1201 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001202 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001203 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001204 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001205 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001206 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001207 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001208 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001209 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001210 }
1211 p = skipwhite(p + 1);
1212 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001213 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001214 else
1215 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001216 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001217 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001218 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001219 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001220 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001222 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001223 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001224 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001225 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001226 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001227 clear_tv(&var2);
1228 return NULL;
1229 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001230 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001231 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001232 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001233 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001235
Bram Moolenaar8c711452005-01-14 21:53:12 +00001236 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001237 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001239 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001240 clear_tv(&var1);
1241 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001242 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001243 }
1244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001245 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 ++p;
1247 }
1248
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250 {
1251 if (len == -1)
1252 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001253 // "[key]": get key from "var1"
1254 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001255 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001256 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001257 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001258 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001259 }
1260 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001261 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001262
1263 // a NULL dict is equivalent with an empty dict
1264 if (lp->ll_tv->vval.v_dict == NULL)
1265 {
1266 lp->ll_tv->vval.v_dict = dict_alloc();
1267 if (lp->ll_tv->vval.v_dict == NULL)
1268 {
1269 clear_tv(&var1);
1270 return NULL;
1271 }
1272 ++lp->ll_tv->vval.v_dict->dv_refcount;
1273 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001274 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001275
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001276 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001277
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001278 // When assigning to a scope dictionary check that a function and
1279 // variable name is valid (only variable name unless it is l: or
1280 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001281 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001282 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001283 int prevval;
1284 int wrong;
1285
1286 if (len != -1)
1287 {
1288 prevval = key[len];
1289 key[len] = NUL;
1290 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001291 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001292 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001293 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1294 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001295 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001296 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001297 if (len != -1)
1298 key[len] = prevval;
1299 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001300 {
1301 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001302 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001303 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001304 }
1305
Bram Moolenaar10c65862020-10-08 21:16:42 +02001306 if (lp->ll_valtype != NULL)
1307 // use the type of the member
1308 lp->ll_valtype = lp->ll_valtype->tt_member;
1309
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001310 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001311 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001312 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001313 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001314 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001315 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001316 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001317 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001318 return NULL;
1319 }
1320
Bram Moolenaar31b81602019-02-10 22:14:27 +01001321 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001323 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001324 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001325 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001326 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001327 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001328 }
1329 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001330 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001331 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001333 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001334 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001335 p = NULL;
1336 break;
1337 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001338 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001339 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001340 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1341 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001342 {
1343 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001344 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001345 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001346
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001347 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001348 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001349 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001350 else if (lp->ll_tv->v_type == VAR_BLOB)
1351 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001352 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1353
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001354 /*
1355 * Get the number and item for the only or first index of the List.
1356 */
1357 if (empty1)
1358 lp->ll_n1 = 0;
1359 else
1360 // is number or string
1361 lp->ll_n1 = (long)tv_get_number(&var1);
1362 clear_tv(&var1);
1363
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001364 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001365 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001366 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001367 return NULL;
1368 }
1369 if (lp->ll_range && !lp->ll_empty2)
1370 {
1371 lp->ll_n2 = (long)tv_get_number(&var2);
1372 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001373 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1374 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001375 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001376 }
1377 lp->ll_blob = lp->ll_tv->vval.v_blob;
1378 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001379 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001380 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001381 else
1382 {
1383 /*
1384 * Get the number and item for the only or first index of the List.
1385 */
1386 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001387 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001388 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001389 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001390 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001391 clear_tv(&var1);
1392
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001393 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001394 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001395 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1396 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001397 if (lp->ll_li == NULL)
1398 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001399 clear_tv(&var2);
1400 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001401 }
1402
Bram Moolenaar10c65862020-10-08 21:16:42 +02001403 if (lp->ll_valtype != NULL)
1404 // use the type of the member
1405 lp->ll_valtype = lp->ll_valtype->tt_member;
1406
Bram Moolenaar8c711452005-01-14 21:53:12 +00001407 /*
1408 * May need to find the item or absolute index for the second
1409 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 * When no index given: "lp->ll_empty2" is TRUE.
1411 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001412 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001413 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001414 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001415 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001416 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001417 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001418 if (check_range_index_two(lp->ll_list,
1419 &lp->ll_n1, lp->ll_li,
1420 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001422 }
1423
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001425 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 }
1427
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001428 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001430 return p;
1431}
1432
1433/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001434 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001435 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001437clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001438{
1439 vim_free(lp->ll_exp_name);
1440 vim_free(lp->ll_newkey);
1441}
1442
1443/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001444 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001445 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001446 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1447 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001448 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001449 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001450set_var_lval(
1451 lval_T *lp,
1452 char_u *endp,
1453 typval_T *rettv,
1454 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001455 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1456 char_u *op,
1457 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001458{
1459 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001460 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001461
1462 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001463 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001464 cc = *endp;
1465 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001466 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1467 return;
1468
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001469 if (lp->ll_blob != NULL)
1470 {
1471 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001472
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001473 if (op != NULL && *op != '=')
1474 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001475 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001476 return;
1477 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001478 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001479 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001480
1481 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1482 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001483 if (lp->ll_empty2)
1484 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1485
Bram Moolenaar68452172021-04-12 21:21:02 +02001486 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1487 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001488 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001489 }
1490 else
1491 {
1492 val = (int)tv_get_number_chk(rettv, &error);
1493 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001494 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001495 }
1496 }
1497 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001498 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001499 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001500
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001501 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1502 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001503 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001504 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001505 *endp = cc;
1506 return;
1507 }
1508
Bram Moolenaarff697e62019-02-12 22:28:33 +01001509 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001510 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001511 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001512 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001513 {
1514 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001515 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1516 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001517 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001518 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001519 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001520 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001521 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001522 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001523 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001524 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001525 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1526 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001527 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001528 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001529 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001530 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001531 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001532 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001533 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001534 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001535 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001536 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001537 else if (lp->ll_range)
1538 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001539 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1540 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001541 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001542 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001543 return;
1544 }
1545
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001546 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1547 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001548 }
1549 else
1550 {
1551 /*
1552 * Assign to a List or Dictionary item.
1553 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001554 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1555 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001556 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001557 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001558 return;
1559 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001560
1561 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001562 && check_typval_arg_type(lp->ll_valtype, rettv,
1563 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001564 return;
1565
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001566 if (lp->ll_newkey != NULL)
1567 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001568 if (op != NULL && *op != '=')
1569 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001570 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001571 return;
1572 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001573 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1574 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001575 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001577 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001578 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001579 if (di == NULL)
1580 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001581 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1582 {
1583 vim_free(di);
1584 return;
1585 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001586 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001587 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001588 else if (op != NULL && *op != '=')
1589 {
1590 tv_op(lp->ll_tv, rettv, op);
1591 return;
1592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001593 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001594 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001596 /*
1597 * Assign the value to the variable or list item.
1598 */
1599 if (copy)
1600 copy_tv(rettv, lp->ll_tv);
1601 else
1602 {
1603 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001604 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001605 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001606 }
1607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001608}
1609
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001610/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001611 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1612 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001613 * Returns OK or FAIL.
1614 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001616tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001617{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001618 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001619 char_u numbuf[NUMBUFLEN];
1620 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001621 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001622
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001623 // Can't do anything with a Funcref or Dict on the right.
1624 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001625 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001626 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1627 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628 {
1629 switch (tv1->v_type)
1630 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001631 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001632 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001634 case VAR_DICT:
1635 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001636 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001637 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001638 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001639 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001640 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001641 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001642 break;
1643
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001644 case VAR_BLOB:
1645 if (*op != '+' || tv2->v_type != VAR_BLOB)
1646 break;
1647 // BLOB += BLOB
1648 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1649 {
1650 blob_T *b1 = tv1->vval.v_blob;
1651 blob_T *b2 = tv2->vval.v_blob;
1652 int i, len = blob_len(b2);
1653 for (i = 0; i < len; i++)
1654 ga_append(&b1->bv_ga, blob_get(b2, i));
1655 }
1656 return OK;
1657
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658 case VAR_LIST:
1659 if (*op != '+' || tv2->v_type != VAR_LIST)
1660 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001661 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001662 if (tv2->vval.v_list != NULL)
1663 {
1664 if (tv1->vval.v_list == NULL)
1665 {
1666 tv1->vval.v_list = tv2->vval.v_list;
1667 ++tv1->vval.v_list->lv_refcount;
1668 }
1669 else
1670 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1671 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001672 return OK;
1673
1674 case VAR_NUMBER:
1675 case VAR_STRING:
1676 if (tv2->v_type == VAR_LIST)
1677 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001678 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001679 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001680 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001681 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001682 if (tv2->v_type == VAR_FLOAT)
1683 {
1684 float_T f = n;
1685
Bram Moolenaarff697e62019-02-12 22:28:33 +01001686 if (*op == '%')
1687 break;
1688 switch (*op)
1689 {
1690 case '+': f += tv2->vval.v_float; break;
1691 case '-': f -= tv2->vval.v_float; break;
1692 case '*': f *= tv2->vval.v_float; break;
1693 case '/': f /= tv2->vval.v_float; break;
1694 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001695 clear_tv(tv1);
1696 tv1->v_type = VAR_FLOAT;
1697 tv1->vval.v_float = f;
1698 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001699 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001700 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001701 switch (*op)
1702 {
1703 case '+': n += tv_get_number(tv2); break;
1704 case '-': n -= tv_get_number(tv2); break;
1705 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001706 case '/': n = num_divide(n, tv_get_number(tv2),
1707 &failed); break;
1708 case '%': n = num_modulus(n, tv_get_number(tv2),
1709 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001710 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001711 clear_tv(tv1);
1712 tv1->v_type = VAR_NUMBER;
1713 tv1->vval.v_number = n;
1714 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001715 }
1716 else
1717 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001718 if (tv2->v_type == VAR_FLOAT)
1719 break;
1720
Bram Moolenaarff697e62019-02-12 22:28:33 +01001721 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001722 s = tv_get_string(tv1);
1723 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001724 clear_tv(tv1);
1725 tv1->v_type = VAR_STRING;
1726 tv1->vval.v_string = s;
1727 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001728 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001729
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001730 case VAR_FLOAT:
1731 {
1732 float_T f;
1733
Bram Moolenaarff697e62019-02-12 22:28:33 +01001734 if (*op == '%' || *op == '.'
1735 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001736 && tv2->v_type != VAR_NUMBER
1737 && tv2->v_type != VAR_STRING))
1738 break;
1739 if (tv2->v_type == VAR_FLOAT)
1740 f = tv2->vval.v_float;
1741 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001742 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001743 switch (*op)
1744 {
1745 case '+': tv1->vval.v_float += f; break;
1746 case '-': tv1->vval.v_float -= f; break;
1747 case '*': tv1->vval.v_float *= f; break;
1748 case '/': tv1->vval.v_float /= f; break;
1749 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001750 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001751 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001752 }
1753 }
1754
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001755 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001756 return FAIL;
1757}
1758
1759/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001760 * Evaluate the expression used in a ":for var in expr" command.
1761 * "arg" points to "var".
1762 * Set "*errp" to TRUE for an error, FALSE otherwise;
1763 * Return a pointer that holds the info. Null when there is an error.
1764 */
1765 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001766eval_for_line(
1767 char_u *arg,
1768 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001769 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001770 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001771{
Bram Moolenaar33570922005-01-25 22:26:29 +00001772 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001773 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001774 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001775 typval_T tv;
1776 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001777 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001779 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001781 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001782 if (fi == NULL)
1783 return NULL;
1784
Bram Moolenaar404557e2021-07-05 21:41:48 +02001785 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1786 &fi->fi_semicolon, FALSE);
1787 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 return fi;
1789
Bram Moolenaar404557e2021-07-05 21:41:48 +02001790 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001791 if (expr[0] != 'i' || expr[1] != 'n'
1792 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001793 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001794 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1795 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1796 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001797 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001798 return fi;
1799 }
1800
1801 if (skip)
1802 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001803 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1804 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001805 {
1806 *errp = FALSE;
1807 if (!skip)
1808 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001809 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001810 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001811 l = tv.vval.v_list;
1812 if (l == NULL)
1813 {
1814 // a null list is like an empty list: do nothing
1815 clear_tv(&tv);
1816 }
1817 else
1818 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001820 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001822 // No need to increment the refcount, it's already set for
1823 // the list being used in "tv".
1824 fi->fi_list = l;
1825 list_add_watch(l, &fi->fi_lw);
1826 fi->fi_lw.lw_item = l->lv_first;
1827 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001828 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001829 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001830 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001831 fi->fi_bi = 0;
1832 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001833 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001834 typval_T btv;
1835
1836 // Make a copy, so that the iteration still works when the
1837 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001839 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001840 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001841 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001842 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001843 else if (tv.v_type == VAR_STRING)
1844 {
1845 fi->fi_byte_idx = 0;
1846 fi->fi_string = tv.vval.v_string;
1847 tv.vval.v_string = NULL;
1848 if (fi->fi_string == NULL)
1849 fi->fi_string = vim_strsave((char_u *)"");
1850 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 else
1852 {
Sean Dewar80d73952021-08-04 19:25:54 +02001853 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001854 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 }
1856 }
1857 }
1858 if (skip)
1859 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001860 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861
1862 return fi;
1863}
1864
1865/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001866 * Used when looping over a :for line, skip the "in expr" part.
1867 */
1868 void
1869skip_for_lines(void *fi_void, evalarg_T *evalarg)
1870{
1871 forinfo_T *fi = (forinfo_T *)fi_void;
1872 int i;
1873
1874 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001875 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001876}
1877
1878/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 * Use the first item in a ":for" list. Advance to the next.
1880 * Assign the values to the variable (list). "arg" points to the first one.
1881 * Return TRUE when a valid item was found, FALSE when at end of list or
1882 * something wrong.
1883 */
1884 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001885next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001887 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001888 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001889 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001890 ? (ASSIGN_FINAL
1891 // first round: error if variable exists
1892 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01001893 | ASSIGN_NO_MEMBER_TYPE
1894 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001895 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001896 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001897 int skip_assign = in_vim9script() && arg[0] == '_'
1898 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001900 if (fi->fi_blob != NULL)
1901 {
1902 typval_T tv;
1903
1904 if (fi->fi_bi >= blob_len(fi->fi_blob))
1905 return FALSE;
1906 tv.v_type = VAR_NUMBER;
1907 tv.v_lock = VAR_FIXED;
1908 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1909 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001910 if (skip_assign)
1911 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001912 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001913 fi->fi_varcount, flag, NULL) == OK;
1914 }
1915
1916 if (fi->fi_string != NULL)
1917 {
1918 typval_T tv;
1919 int len;
1920
1921 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1922 if (len == 0)
1923 return FALSE;
1924 tv.v_type = VAR_STRING;
1925 tv.v_lock = VAR_FIXED;
1926 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1927 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001928 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001929 if (skip_assign)
1930 result = TRUE;
1931 else
1932 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001933 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001934 vim_free(tv.vval.v_string);
1935 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001936 }
1937
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 item = fi->fi_lw.lw_item;
1939 if (item == NULL)
1940 result = FALSE;
1941 else
1942 {
1943 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001944 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001945 if (skip_assign)
1946 result = TRUE;
1947 else
1948 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001949 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 }
1951 return result;
1952}
1953
1954/*
1955 * Free the structure used to store info used by ":for".
1956 */
1957 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001958free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959{
Bram Moolenaar33570922005-01-25 22:26:29 +00001960 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001962 if (fi == NULL)
1963 return;
1964 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001965 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001967 list_unref(fi->fi_list);
1968 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001969 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001970 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001971 else
1972 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 vim_free(fi);
1974}
1975
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001977set_context_for_expression(
1978 expand_T *xp,
1979 char_u *arg,
1980 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001982 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001986 if (cmdidx == CMD_let || cmdidx == CMD_var
1987 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 {
1989 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001990 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001992 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001993 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 {
1995 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001996 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001997 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998 break;
1999 }
2000 return;
2001 }
2002 }
2003 else
2004 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2005 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 while ((xp->xp_pattern = vim_strpbrk(arg,
2007 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2008 {
2009 c = *xp->xp_pattern;
2010 if (c == '&')
2011 {
2012 c = xp->xp_pattern[1];
2013 if (c == '&')
2014 {
2015 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002016 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002021 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2022 xp->xp_pattern += 2;
2023
2024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
2026 else if (c == '$')
2027 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002028 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 xp->xp_context = EXPAND_ENV_VARS;
2030 }
2031 else if (c == '=')
2032 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002033 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 xp->xp_context = EXPAND_EXPRESSION;
2035 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002036 else if (c == '#'
2037 && xp->xp_context == EXPAND_EXPRESSION)
2038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002039 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002040 break;
2041 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002042 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 && xp->xp_context == EXPAND_FUNCTIONS
2044 && vim_strchr(xp->xp_pattern, '(') == NULL)
2045 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002046 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 break;
2048 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002049 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002051 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
2053 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2054 if (c == '\\' && xp->xp_pattern[1] != NUL)
2055 ++xp->xp_pattern;
2056 xp->xp_context = EXPAND_NOTHING;
2057 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002058 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002060 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2062 /* skip */ ;
2063 xp->xp_context = EXPAND_NOTHING;
2064 }
2065 else if (c == '|')
2066 {
2067 if (xp->xp_pattern[1] == '|')
2068 {
2069 ++xp->xp_pattern;
2070 xp->xp_context = EXPAND_EXPRESSION;
2071 }
2072 else
2073 xp->xp_context = EXPAND_COMMANDS;
2074 }
2075 else
2076 xp->xp_context = EXPAND_EXPRESSION;
2077 }
2078 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002079 // Doesn't look like something valid, expand as an expression
2080 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 arg = xp->xp_pattern;
2083 if (*arg != NUL)
2084 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2085 /* skip */ ;
2086 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002087
2088 // ":exe one two" completes "two"
2089 if ((cmdidx == CMD_execute
2090 || cmdidx == CMD_echo
2091 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002092 || cmdidx == CMD_echomsg
2093 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002094 && xp->xp_context == EXPAND_EXPRESSION)
2095 {
2096 for (;;)
2097 {
2098 char_u *n = skiptowhite(arg);
2099
2100 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2101 break;
2102 arg = skipwhite(n);
2103 }
2104 }
2105
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 xp->xp_pattern = arg;
2107}
2108
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002110 * Return TRUE if "pat" matches "text".
2111 * Does not use 'cpo' and always uses 'magic'.
2112 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002113 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002114pattern_match(char_u *pat, char_u *text, int ic)
2115{
2116 int matches = FALSE;
2117 char_u *save_cpo;
2118 regmatch_T regmatch;
2119
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002120 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002121 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002122 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002123 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2124 if (regmatch.regprog != NULL)
2125 {
2126 regmatch.rm_ic = ic;
2127 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2128 vim_regfree(regmatch.regprog);
2129 }
2130 p_cpo = save_cpo;
2131 return matches;
2132}
2133
2134/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002135 * Handle a name followed by "(". Both for just "name(arg)" and for
2136 * "expr->name(arg)".
2137 * Returns OK or FAIL.
2138 */
2139 static int
2140eval_func(
2141 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002142 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002143 char_u *name,
2144 int name_len,
2145 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002146 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002147 typval_T *basetv) // "expr" for "expr->name(arg)"
2148{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002149 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002150 char_u *s = name;
2151 int len = name_len;
2152 partial_T *partial;
2153 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002154 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002155 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002156
2157 if (!evaluate)
2158 check_vars(s, len);
2159
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002160 // If "s" is the name of a variable of type VAR_FUNC
2161 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002162 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002163 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002165 // Need to make a copy, in case evaluating the arguments makes
2166 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002167 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002168 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002169 ret = FAIL;
2170 else
2171 {
2172 funcexe_T funcexe;
2173
2174 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002175 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002176 funcexe.fe_firstline = curwin->w_cursor.lnum;
2177 funcexe.fe_lastline = curwin->w_cursor.lnum;
2178 funcexe.fe_evaluate = evaluate;
2179 funcexe.fe_partial = partial;
2180 funcexe.fe_basetv = basetv;
2181 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002182 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002183 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002184 }
2185 vim_free(s);
2186
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002187 // If evaluate is FALSE rettv->v_type was not set in
2188 // get_func_tv, but it's needed in handle_subscript() to parse
2189 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002190 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2191 {
2192 rettv->vval.v_string = NULL;
2193 rettv->v_type = VAR_FUNC;
2194 }
2195
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002196 // Stop the expression evaluation when immediately
2197 // aborting on error, or when an interrupt occurred or
2198 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002199 if (evaluate && aborting())
2200 {
2201 if (ret == OK)
2202 clear_tv(rettv);
2203 ret = FAIL;
2204 }
2205 return ret;
2206}
2207
2208/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002209 * After a NL, skip over empty lines and comment-only lines.
2210 */
2211 static char_u *
2212newline_skip_comments(char_u *arg)
2213{
2214 char_u *p = arg + 1;
2215
2216 for (;;)
2217 {
2218 p = skipwhite(p);
2219
2220 if (*p == NUL)
2221 break;
2222 if (vim9_comment_start(p))
2223 {
2224 char_u *nl = vim_strchr(p, NL);
2225
2226 if (nl == NULL)
2227 break;
2228 p = nl;
2229 }
2230 if (*p != NL)
2231 break;
2232 ++p; // skip another NL
2233 }
2234 return p;
2235}
2236
2237/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002238 * Get the next line source line without advancing. But do skip over comment
2239 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002240 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002241 */
2242 static char_u *
2243getline_peek_skip_comments(evalarg_T *evalarg)
2244{
2245 for (;;)
2246 {
2247 char_u *next = getline_peek(evalarg->eval_getline,
2248 evalarg->eval_cookie);
2249 char_u *p;
2250
2251 if (next == NULL)
2252 break;
2253 p = skipwhite(next);
2254 if (*p != NUL && !vim9_comment_start(p))
2255 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002256 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002257 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002258 }
2259 return NULL;
2260}
2261
2262/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002263 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2264 * comment) and there is a next line, return the next line (skipping blanks)
2265 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002266 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2267 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002268 * "arg" must point somewhere inside a line, not at the start.
2269 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002270 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002271eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2272{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002273 char_u *p = skipwhite(arg);
2274
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002275 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002276 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002277 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002278 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2279 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002280 && (*p == NUL || *p == NL
2281 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002282 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002283 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002284
Bram Moolenaare442d592022-05-05 12:20:28 +01002285 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002286 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002287 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002288 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002289 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002290 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002291
Bram Moolenaar9d489562020-07-30 20:08:50 +02002292 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002293 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002294 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002295 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002296 }
2297 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002298 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002299}
2300
2301/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002302 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002303 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002304 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002305 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002306eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002307{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002308 garray_T *gap = &evalarg->eval_ga;
2309 char_u *line;
2310
Bram Moolenaar39be4982022-05-06 21:51:50 +01002311 if (arg != NULL)
2312 {
2313 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002314 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002315 // Truncate before a trailing comment, so that concatenating the lines
2316 // won't turn the rest into a comment.
2317 if (*skipwhite(arg) == '#')
2318 *arg = NUL;
2319 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002320
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002321 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002322 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2323 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002324 else
2325 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002326 if (line == NULL)
2327 return NULL;
2328
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002329 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002330 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2331 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002332 char_u *p = skipwhite(line);
2333
2334 // Going to concatenate the lines after parsing. For an empty or
2335 // comment line use an empty string.
2336 if (*p == NUL || vim9_comment_start(p))
2337 {
2338 vim_free(line);
2339 line = vim_strsave((char_u *)"");
2340 }
2341
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002342 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2343 ++gap->ga_len;
2344 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002345 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002346 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002347 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002348 evalarg->eval_tofree = line;
2349 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002350
2351 // Advanced to the next line, "arg" no longer points into the previous
2352 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002353 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002354 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002355}
2356
Bram Moolenaare6b53242020-07-01 17:28:33 +02002357/*
2358 * Call eval_next_non_blank() and get the next line if needed.
2359 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002360 char_u *
2361skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2362{
2363 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002364 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002365
Bram Moolenaar962d7212020-07-04 14:15:00 +02002366 if (evalarg == NULL)
2367 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002368 eval_next_non_blank(p, evalarg, &getnext);
2369 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002370 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002371 return p;
2372}
2373
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2378 */
2379
2380/*
2381 * Handle zero level expression.
2382 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002384 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002385 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 * Return OK or FAIL.
2387 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002389eval0(
2390 char_u *arg,
2391 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002392 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002393 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002395 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2396}
2397
2398/*
2399 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2400 * expression and don't check what comes after the expression.
2401 */
2402 int
2403eval0_retarg(
2404 char_u *arg,
2405 typval_T *rettv,
2406 exarg_T *eap,
2407 evalarg_T *evalarg,
2408 char_u **retarg)
2409{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 int ret;
2411 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002412 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002413 int did_emsg_before = did_emsg;
2414 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002415 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002416 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002417 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
2419 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002420 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002421
Bram Moolenaar79481362022-06-27 20:15:10 +01002422 if (ret != FAIL)
2423 {
2424 expr_end = p;
2425 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002426
Bram Moolenaar79481362022-06-27 20:15:10 +01002427 // In Vim9 script a command block is not split at NL characters for
2428 // commands using an expression argument. Skip over a '#' comment to
2429 // check for a following NL. Require white space before the '#'.
2430 if (in_vim9script() && p > expr_end && retarg == NULL)
2431 while (*p == '#')
2432 {
2433 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002434
Bram Moolenaar79481362022-06-27 20:15:10 +01002435 if (nl == NULL)
2436 break;
2437 p = skipwhite(nl + 1);
2438 if (eap != NULL && *p != NUL)
2439 eap->nextcmd = p;
2440 check_for_end = FALSE;
2441 }
2442
2443 if (check_for_end)
2444 end_error = !ends_excmd2(arg, p);
2445 }
2446
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002447 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
2449 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 /*
2452 * Report the invalid expression unless the expression evaluation has
2453 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002454 * exception, or we already gave a more specific error.
2455 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002457 if (!aborting()
2458 && did_emsg == did_emsg_before
2459 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002460 && (flags & EVAL_CONSTANT) == 0
2461 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002462 {
2463 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002464 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002465 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002466 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002467 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002468
2469 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002470 // a next command to avoid more errors, unless "|" is following, which
2471 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002472 if (eap != NULL && p != NULL
2473 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002474 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002475 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002477
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002478 if (retarg != NULL)
2479 *retarg = p;
2480 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002481 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 return ret;
2484}
2485
2486/*
2487 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002488 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002489 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 *
2491 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002492 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002494 * Note: "rettv.v_lock" is not set.
2495 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 * Return OK or FAIL.
2497 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002498 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002499eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002501 char_u *p;
2502 int getnext;
2503
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002504 CLEAR_POINTER(rettv);
2505
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 /*
2507 * Get the first variable.
2508 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002509 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 return FAIL;
2511
Bram Moolenaar793648f2020-06-26 21:28:25 +02002512 p = eval_next_non_blank(*arg, evalarg, &getnext);
2513 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002515 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002516 int result;
2517 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002518 evalarg_T *evalarg_used = evalarg;
2519 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002520 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002521 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002522 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002523
2524 if (evalarg == NULL)
2525 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002526 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002527 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002528 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002529 orig_flags = evalarg_used->eval_flags;
2530 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002531
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002532 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002533 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002534 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002535 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002536 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002537 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002538 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002539 clear_tv(rettv);
2540 return FAIL;
2541 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002542 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002543 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002544
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002546 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002548 int error = FALSE;
2549
Bram Moolenaar13106602020-10-04 16:06:05 +02002550 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002551 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002552 else if (vim9script)
2553 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002554 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002556 if (error || !op_falsy || !result)
2557 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002558 if (error)
2559 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
2561
2562 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002563 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002565 if (op_falsy)
2566 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002567 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002568 {
mityu4ac198c2021-05-28 17:52:40 +02002569 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002570 clear_tv(rettv);
2571 return FAIL;
2572 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002573 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002574 evalarg_used->eval_flags = (op_falsy ? !result : result)
2575 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2576 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002577 {
2578 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002580 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002581 if (!op_falsy || !result)
2582 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002584 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002586 /*
2587 * Check for the ":".
2588 */
2589 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2590 if (*p != ':')
2591 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002592 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002593 if (evaluate && result)
2594 clear_tv(rettv);
2595 evalarg_used->eval_flags = orig_flags;
2596 return FAIL;
2597 }
2598 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002599 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002600 else
2601 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002602 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002603 {
2604 error_white_both(p, 1);
2605 clear_tv(rettv);
2606 evalarg_used->eval_flags = orig_flags;
2607 return FAIL;
2608 }
2609 *arg = p;
2610 }
2611
2612 /*
2613 * Get the third variable. Recursive!
2614 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002615 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002616 {
mityu4ac198c2021-05-28 17:52:40 +02002617 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002618 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002619 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002620 return FAIL;
2621 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002622 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2623 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002624 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002625 if (eval1(arg, &var2, evalarg_used) == FAIL)
2626 {
2627 if (evaluate && result)
2628 clear_tv(rettv);
2629 evalarg_used->eval_flags = orig_flags;
2630 return FAIL;
2631 }
2632 if (evaluate && !result)
2633 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002635
2636 if (evalarg == NULL)
2637 clear_evalarg(&local_evalarg, NULL);
2638 else
2639 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 }
2641
2642 return OK;
2643}
2644
2645/*
2646 * Handle first level expression:
2647 * expr2 || expr2 || expr2 logical OR
2648 *
2649 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002650 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 *
2652 * Return OK or FAIL.
2653 */
2654 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002655eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002657 char_u *p;
2658 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
2660 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002661 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002663 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 return FAIL;
2665
2666 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002667 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002669 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002670 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002672 evalarg_T *evalarg_used = evalarg;
2673 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002674 int evaluate;
2675 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002676 long result = FALSE;
2677 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002678 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002679 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002680
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002681 if (evalarg == NULL)
2682 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002683 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002684 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002685 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002686 orig_flags = evalarg_used->eval_flags;
2687 evaluate = orig_flags & EVAL_EVALUATE;
2688 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002689 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002690 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002691 result = tv_get_bool_chk(rettv, &error);
2692 else if (tv_get_number_chk(rettv, &error) != 0)
2693 result = TRUE;
2694 clear_tv(rettv);
2695 if (error)
2696 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 }
2698
2699 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002700 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002702 while (p[0] == '|' && p[1] == '|')
2703 {
2704 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002705 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002706 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002707 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002708 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002709 {
2710 error_white_both(p, 2);
2711 clear_tv(rettv);
2712 return FAIL;
2713 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002714 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002715 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002716
2717 /*
2718 * Get the second variable.
2719 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002720 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002721 {
mityu4ac198c2021-05-28 17:52:40 +02002722 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002723 clear_tv(rettv);
2724 return FAIL;
2725 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002726 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2727 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002728 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002729 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002730 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002731
2732 /*
2733 * Compute the result.
2734 */
2735 if (evaluate && !result)
2736 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002737 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002738 result = tv_get_bool_chk(&var2, &error);
2739 else if (tv_get_number_chk(&var2, &error) != 0)
2740 result = TRUE;
2741 clear_tv(&var2);
2742 if (error)
2743 return FAIL;
2744 }
2745 if (evaluate)
2746 {
2747 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002748 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002749 rettv->v_type = VAR_BOOL;
2750 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002751 }
2752 else
2753 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002754 rettv->v_type = VAR_NUMBER;
2755 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002756 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002757 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002758
2759 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002761
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002762 if (evalarg == NULL)
2763 clear_evalarg(&local_evalarg, NULL);
2764 else
2765 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767
2768 return OK;
2769}
2770
2771/*
2772 * Handle second level expression:
2773 * expr3 && expr3 && expr3 logical AND
2774 *
2775 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002776 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 *
2778 * Return OK or FAIL.
2779 */
2780 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002781eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002783 char_u *p;
2784 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
2786 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002787 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002789 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 return FAIL;
2791
2792 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002793 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002795 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002796 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002798 evalarg_T *evalarg_used = evalarg;
2799 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002800 int orig_flags;
2801 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002802 long result = TRUE;
2803 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002804 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002805 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002806
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002807 if (evalarg == NULL)
2808 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002809 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002810 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002811 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002812 orig_flags = evalarg_used->eval_flags;
2813 evaluate = orig_flags & EVAL_EVALUATE;
2814 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002815 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002816 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002817 result = tv_get_bool_chk(rettv, &error);
2818 else if (tv_get_number_chk(rettv, &error) == 0)
2819 result = FALSE;
2820 clear_tv(rettv);
2821 if (error)
2822 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 }
2824
2825 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002826 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002828 while (p[0] == '&' && p[1] == '&')
2829 {
2830 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002831 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002832 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002833 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002834 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002835 {
2836 error_white_both(p, 2);
2837 clear_tv(rettv);
2838 return FAIL;
2839 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002840 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002841 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002842
2843 /*
2844 * Get the second variable.
2845 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002846 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002847 {
mityu4ac198c2021-05-28 17:52:40 +02002848 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002849 clear_tv(rettv);
2850 return FAIL;
2851 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002852 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2853 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002854 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002855 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002856 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002857 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002858
2859 /*
2860 * Compute the result.
2861 */
2862 if (evaluate && result)
2863 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002864 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002865 result = tv_get_bool_chk(&var2, &error);
2866 else if (tv_get_number_chk(&var2, &error) == 0)
2867 result = FALSE;
2868 clear_tv(&var2);
2869 if (error)
2870 return FAIL;
2871 }
2872 if (evaluate)
2873 {
2874 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002875 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002876 rettv->v_type = VAR_BOOL;
2877 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002878 }
2879 else
2880 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002881 rettv->v_type = VAR_NUMBER;
2882 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002883 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002884 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002885
2886 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002888
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002889 if (evalarg == NULL)
2890 clear_evalarg(&local_evalarg, NULL);
2891 else
2892 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
2894
2895 return OK;
2896}
2897
2898/*
2899 * Handle third level expression:
2900 * var1 == var2
2901 * var1 =~ var2
2902 * var1 != var2
2903 * var1 !~ var2
2904 * var1 > var2
2905 * var1 >= var2
2906 * var1 < var2
2907 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002908 * var1 is var2
2909 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 *
2911 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002912 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 *
2914 * Return OK or FAIL.
2915 */
2916 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002917eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002920 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002921 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002923 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
2925 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002926 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002928 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 return FAIL;
2930
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002931 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002932
Bram Moolenaar696ba232020-07-29 21:20:41 +02002933 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
2935 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002936 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002938 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002940 typval_T var2;
2941 int ic;
2942 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002943 int evaluate = evalarg == NULL
2944 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002945 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002946
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002947 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002948 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002949 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002950 p = *arg;
2951 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002952 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2953 {
mityu4ac198c2021-05-28 17:52:40 +02002954 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002955 clear_tv(rettv);
2956 return FAIL;
2957 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002958
Bram Moolenaar696ba232020-07-29 21:20:41 +02002959 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
2960 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02002961 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02002962 clear_tv(rettv);
2963 return FAIL;
2964 }
2965
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002966 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 if (p[len] == '?')
2968 {
2969 ic = TRUE;
2970 ++len;
2971 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002972 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 else if (p[len] == '#')
2974 {
2975 ic = FALSE;
2976 ++len;
2977 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002978 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002980 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981
2982 /*
2983 * Get the second variable.
2984 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002985 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
2986 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002987 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002988 clear_tv(rettv);
2989 return FAIL;
2990 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02002991 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002992 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 return FAIL;
2996 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002997 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01002998 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02002999 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003000
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003001 // use the line of the comparison for messages
3002 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003003 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003004 {
3005 ret = FAIL;
3006 clear_tv(rettv);
3007 }
3008 else
3009 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003010 clear_tv(&var2);
3011 return ret;
3012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
3014
3015 return OK;
3016}
3017
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003018/*
3019 * Make a copy of blob "tv1" and append blob "tv2".
3020 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 void
3022eval_addblob(typval_T *tv1, typval_T *tv2)
3023{
3024 blob_T *b1 = tv1->vval.v_blob;
3025 blob_T *b2 = tv2->vval.v_blob;
3026 blob_T *b = blob_alloc();
3027 int i;
3028
3029 if (b != NULL)
3030 {
3031 for (i = 0; i < blob_len(b1); i++)
3032 ga_append(&b->bv_ga, blob_get(b1, i));
3033 for (i = 0; i < blob_len(b2); i++)
3034 ga_append(&b->bv_ga, blob_get(b2, i));
3035
3036 clear_tv(tv1);
3037 rettv_blob_set(tv1, b);
3038 }
3039}
3040
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003041/*
3042 * Make a copy of list "tv1" and append list "tv2".
3043 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 int
3045eval_addlist(typval_T *tv1, typval_T *tv2)
3046{
3047 typval_T var3;
3048
3049 // concatenate Lists
3050 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3051 {
3052 clear_tv(tv1);
3053 clear_tv(tv2);
3054 return FAIL;
3055 }
3056 clear_tv(tv1);
3057 *tv1 = var3;
3058 return OK;
3059}
3060
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003062 * Handle the bitwise left/right shift operator expression:
3063 * var1 << var2
3064 * var1 >> var2
3065 *
3066 * "arg" must point to the first non-white of the expression.
3067 * "arg" is advanced to just after the recognized expression.
3068 *
3069 * Return OK or FAIL.
3070 */
3071 static int
3072eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3073{
3074 /*
3075 * Get the first expression.
3076 */
3077 if (eval6(arg, rettv, evalarg) == FAIL)
3078 return FAIL;
3079
3080 /*
3081 * Repeat computing, until no '<<' or '>>' is following.
3082 */
3083 for (;;)
3084 {
3085 char_u *p;
3086 int getnext;
3087 exprtype_T type;
3088 int evaluate;
3089 typval_T var2;
3090 int vim9script;
3091
3092 p = eval_next_non_blank(*arg, evalarg, &getnext);
3093 if (p[0] == '<' && p[1] == '<')
3094 type = EXPR_LSHIFT;
3095 else if (p[0] == '>' && p[1] == '>')
3096 type = EXPR_RSHIFT;
3097 else
3098 return OK;
3099
3100 // Handle a bitwise left or right shift operator
3101 if (rettv->v_type != VAR_NUMBER)
3102 {
3103 // left operand should be a number
3104 emsg(_(e_bitshift_ops_must_be_number));
3105 clear_tv(rettv);
3106 return FAIL;
3107 }
3108
3109 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3110 vim9script = in_vim9script();
3111 if (getnext)
3112 {
3113 *arg = eval_next_line(*arg, evalarg);
3114 p = *arg;
3115 }
3116 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3117 {
3118 error_white_both(*arg, 2);
3119 clear_tv(rettv);
3120 return FAIL;
3121 }
3122
3123 /*
3124 * Get the second variable.
3125 */
3126 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3127 {
3128 error_white_both(p, 2);
3129 clear_tv(rettv);
3130 return FAIL;
3131 }
3132 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3133 if (eval6(arg, &var2, evalarg) == FAIL)
3134 {
3135 clear_tv(rettv);
3136 return FAIL;
3137 }
3138
3139 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3140 {
3141 // right operand should be a positive number
3142 if (var2.v_type != VAR_NUMBER)
3143 emsg(_(e_bitshift_ops_must_be_number));
3144 else
3145 emsg(_(e_bitshift_ops_must_be_postive));
3146 clear_tv(rettv);
3147 clear_tv(&var2);
3148 return FAIL;
3149 }
3150
3151 if (evaluate)
3152 {
3153 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3154 // shifting more bits than we have always results in zero
3155 rettv->vval.v_number = 0;
3156 else if (type == EXPR_LSHIFT)
3157 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003158 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003159 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003160 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003161 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003162 }
3163
3164 clear_tv(&var2);
3165 }
3166
3167 return OK;
3168}
3169
3170/*
3171 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003172 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003174 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003175 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 *
3177 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003178 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 *
3180 * Return OK or FAIL.
3181 */
3182 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003183eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003186 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003188 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 return FAIL;
3190
3191 /*
3192 * Repeat computing, until no '+', '-' or '.' is following.
3193 */
3194 for (;;)
3195 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003196 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003197 int getnext;
3198 char_u *p;
3199 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003200 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003201 int concat;
3202 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003203 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003204
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003205 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003206 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003207 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003208 p = eval_next_non_blank(*arg, evalarg, &getnext);
3209 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003210 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003211 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3212 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003214 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3215 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003216
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003217 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003218 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003219 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003220 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003221 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003222 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003223 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003224 {
mityu4ac198c2021-05-28 17:52:40 +02003225 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003226 clear_tv(rettv);
3227 return FAIL;
3228 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003229 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003230 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003231 if ((op != '+' || (rettv->v_type != VAR_LIST
3232 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003233 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003234 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003235 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003236 int error = FALSE;
3237
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003238 // For "list + ...", an illegal use of the first operand as
3239 // a number cannot be determined before evaluating the 2nd
3240 // operand: if this is also a list, all is ok.
3241 // For "something . ...", "something - ..." or "non-list + ...",
3242 // we know that the first operand needs to be a string or number
3243 // without evaluating the 2nd operand. So check before to avoid
3244 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003245 if (op != '.')
3246 tv_get_number_chk(rettv, &error);
3247 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003248 {
3249 clear_tv(rettv);
3250 return FAIL;
3251 }
3252 }
3253
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 /*
3255 * Get the second variable.
3256 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003257 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003258 {
mityu89dcb4d2021-05-28 13:50:17 +02003259 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003260 clear_tv(rettv);
3261 return FAIL;
3262 }
3263 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003264 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003266 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 return FAIL;
3268 }
3269
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003270 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
3272 /*
3273 * Compute the result.
3274 */
3275 if (op == '.')
3276 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003277 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3278 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003279 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003280
Bram Moolenaar2e086612020-08-25 22:37:48 +02003281 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003282 || var2.v_type == VAR_CHANNEL
3283 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003284 semsg(_(e_using_invalid_value_as_string_str),
3285 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003286 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003287 {
3288 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3289 var2.vval.v_float);
3290 s2 = buf2;
3291 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003292 else
3293 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003294 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003295 {
3296 clear_tv(rettv);
3297 clear_tv(&var2);
3298 return FAIL;
3299 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003300 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003301 clear_tv(rettv);
3302 rettv->v_type = VAR_STRING;
3303 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003305 else if (op == '+' && rettv->v_type == VAR_BLOB
3306 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003308 else if (op == '+' && rettv->v_type == VAR_LIST
3309 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003310 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003312 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 else
3315 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003316 int error = FALSE;
3317 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003318 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003319
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003320 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003321 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003322 f1 = rettv->vval.v_float;
3323 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003324 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003325 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003326 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003327 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003328 if (error)
3329 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003330 // This can only happen for "list + non-list" or
3331 // "blob + non-blob". For "non-list + ..." or
3332 // "something - ...", we returned before evaluating the
3333 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003334 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003335 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003336 return FAIL;
3337 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003338 if (var2.v_type == VAR_FLOAT)
3339 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003340 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003341 if (var2.v_type == VAR_FLOAT)
3342 {
3343 f2 = var2.vval.v_float;
3344 n2 = 0;
3345 }
3346 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003347 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003348 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003349 if (error)
3350 {
3351 clear_tv(rettv);
3352 clear_tv(&var2);
3353 return FAIL;
3354 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003355 if (rettv->v_type == VAR_FLOAT)
3356 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003358 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003359
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003360 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003361 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3362 {
3363 if (op == '+')
3364 f1 = f1 + f2;
3365 else
3366 f1 = f1 - f2;
3367 rettv->v_type = VAR_FLOAT;
3368 rettv->vval.v_float = f1;
3369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003371 {
3372 if (op == '+')
3373 n1 = n1 + n2;
3374 else
3375 n1 = n1 - n2;
3376 rettv->v_type = VAR_NUMBER;
3377 rettv->vval.v_number = n1;
3378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003380 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
3382 }
3383 return OK;
3384}
3385
3386/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003387 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 * * number multiplication
3389 * / number division
3390 * % number modulo
3391 *
3392 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003393 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 *
3395 * Return OK or FAIL.
3396 */
3397 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003398eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003399 char_u **arg,
3400 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003401 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003402 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003404 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
3406 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003407 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003409 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 return FAIL;
3411
3412 /*
3413 * Repeat computing, until no '*', '/' or '%' is following.
3414 */
3415 for (;;)
3416 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003417 int evaluate;
3418 int getnext;
3419 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003420 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003421 int op;
3422 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003423 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003424 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003425
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003426 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003427 p = eval_next_non_blank(*arg, evalarg, &getnext);
3428 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003429 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003431
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003432 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003433 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003434 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003435 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003436 {
3437 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3438 {
mityu4ac198c2021-05-28 17:52:40 +02003439 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003440 clear_tv(rettv);
3441 return FAIL;
3442 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003443 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003446 f1 = 0;
3447 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003448 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003449 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003451 if (rettv->v_type == VAR_FLOAT)
3452 {
3453 f1 = rettv->vval.v_float;
3454 use_float = TRUE;
3455 n1 = 0;
3456 }
3457 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003458 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003459 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003460 if (error)
3461 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 }
3463 else
3464 n1 = 0;
3465
3466 /*
3467 * Get the second variable.
3468 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003469 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3470 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003471 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003472 clear_tv(rettv);
3473 return FAIL;
3474 }
3475 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003476 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 return FAIL;
3478
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003479 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003481 if (var2.v_type == VAR_FLOAT)
3482 {
3483 if (!use_float)
3484 {
3485 f1 = n1;
3486 use_float = TRUE;
3487 }
3488 f2 = var2.vval.v_float;
3489 n2 = 0;
3490 }
3491 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003492 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003493 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003494 clear_tv(&var2);
3495 if (error)
3496 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003497 if (use_float)
3498 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
3501 /*
3502 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003503 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003505 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003507 if (op == '*')
3508 f1 = f1 * f2;
3509 else if (op == '/')
3510 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003511#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003512 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003513 if (f2 == 0.0)
3514 {
3515 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003516 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003517 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003518 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003519 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003520 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003521 }
3522 else
3523 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003524#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003525 // We rely on the floating point library to handle divide
3526 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003527 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003528#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003531 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003532 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003533 return FAIL;
3534 }
3535 rettv->v_type = VAR_FLOAT;
3536 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538 else
3539 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003540 int failed = FALSE;
3541
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003542 if (op == '*')
3543 n1 = n1 * n2;
3544 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003545 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003547 n1 = num_modulus(n1, n2, &failed);
3548 if (failed)
3549 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003550
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003551 rettv->v_type = VAR_NUMBER;
3552 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555 }
3556
3557 return OK;
3558}
3559
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003560/*
3561 * Handle a type cast before a base level expression.
3562 * "arg" must point to the first non-white of the expression.
3563 * "arg" is advanced to just after the recognized expression.
3564 * Return OK or FAIL.
3565 */
3566 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003567eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003568 char_u **arg,
3569 typval_T *rettv,
3570 evalarg_T *evalarg,
3571 int want_string) // after "." operator
3572{
3573 type_T *want_type = NULL;
3574 garray_T type_list; // list of pointers to allocated types
3575 int res;
3576 int evaluate = evalarg == NULL ? 0
3577 : (evalarg->eval_flags & EVAL_EVALUATE);
3578
3579 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003580 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3581 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003582 {
3583 ++*arg;
3584 ga_init2(&type_list, sizeof(type_T *), 10);
3585 want_type = parse_type(arg, &type_list, TRUE);
3586 if (want_type == NULL && (evaluate || **arg != '>'))
3587 {
3588 clear_type_list(&type_list);
3589 return FAIL;
3590 }
3591
3592 if (**arg != '>')
3593 {
3594 if (*skipwhite(*arg) == '>')
3595 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3596 else
3597 emsg(_(e_missing_gt));
3598 clear_type_list(&type_list);
3599 return FAIL;
3600 }
3601 ++*arg;
3602 *arg = skipwhite_and_linebreak(*arg, evalarg);
3603 }
3604
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003605 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003606
3607 if (want_type != NULL && evaluate)
3608 {
3609 if (res == OK)
3610 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003611 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3612 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003613
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003614 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003615 {
3616 if (want_type == &t_bool && actual != &t_bool
3617 && (actual->tt_flags & TTFLAG_BOOL_OK))
3618 {
3619 int n = tv2bool(rettv);
3620
3621 // can use "0" and "1" for boolean in some places
3622 clear_tv(rettv);
3623 rettv->v_type = VAR_BOOL;
3624 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3625 }
3626 else
3627 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003628 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003629
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003630 where.wt_variable = TRUE;
3631 res = check_type(want_type, actual, TRUE, where);
3632 }
3633 }
3634 }
3635 clear_type_list(&type_list);
3636 }
3637
3638 return res;
3639}
3640
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003641 int
3642eval_leader(char_u **arg, int vim9)
3643{
3644 char_u *s = *arg;
3645 char_u *p = *arg;
3646
3647 while (*p == '!' || *p == '-' || *p == '+')
3648 {
3649 char_u *n = skipwhite(p + 1);
3650
3651 // ++, --, -+ and +- are not accepted in Vim9 script
3652 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3653 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003654 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003655 return FAIL;
3656 }
3657 p = n;
3658 }
3659 *arg = p;
3660 return OK;
3661}
3662
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003664 * Check for a predefined value "true", "false" and "null.*".
3665 * Return OK when recognized.
3666 */
3667 int
3668handle_predefined(char_u *s, int len, typval_T *rettv)
3669{
3670 switch (len)
3671 {
3672 case 4: if (STRNCMP(s, "true", 4) == 0)
3673 {
3674 rettv->v_type = VAR_BOOL;
3675 rettv->vval.v_number = VVAL_TRUE;
3676 return OK;
3677 }
3678 if (STRNCMP(s, "null", 4) == 0)
3679 {
3680 rettv->v_type = VAR_SPECIAL;
3681 rettv->vval.v_number = VVAL_NULL;
3682 return OK;
3683 }
3684 break;
3685 case 5: if (STRNCMP(s, "false", 5) == 0)
3686 {
3687 rettv->v_type = VAR_BOOL;
3688 rettv->vval.v_number = VVAL_FALSE;
3689 return OK;
3690 }
3691 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003692 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3693 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003694#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003695 rettv->v_type = VAR_JOB;
3696 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003697#else
3698 rettv->v_type = VAR_SPECIAL;
3699 rettv->vval.v_number = VVAL_NULL;
3700#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003701 return OK;
3702 }
3703 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003704 case 9:
3705 if (STRNCMP(s, "null_", 5) != 0)
3706 break;
3707 if (STRNCMP(s + 5, "list", 4) == 0)
3708 {
3709 rettv->v_type = VAR_LIST;
3710 rettv->vval.v_list = NULL;
3711 return OK;
3712 }
3713 if (STRNCMP(s + 5, "dict", 4) == 0)
3714 {
3715 rettv->v_type = VAR_DICT;
3716 rettv->vval.v_dict = NULL;
3717 return OK;
3718 }
3719 if (STRNCMP(s + 5, "blob", 4) == 0)
3720 {
3721 rettv->v_type = VAR_BLOB;
3722 rettv->vval.v_blob = NULL;
3723 return OK;
3724 }
3725 break;
3726 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3727 {
3728 rettv->v_type = VAR_STRING;
3729 rettv->vval.v_string = NULL;
3730 return OK;
3731 }
3732 break;
3733 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003734 if (STRNCMP(s, "null_channel", 12) == 0)
3735 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003736#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003737 rettv->v_type = VAR_CHANNEL;
3738 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003739#else
3740 rettv->v_type = VAR_SPECIAL;
3741 rettv->vval.v_number = VVAL_NULL;
3742#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003743 return OK;
3744 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003745 if (STRNCMP(s, "null_partial", 12) == 0)
3746 {
3747 rettv->v_type = VAR_PARTIAL;
3748 rettv->vval.v_partial = NULL;
3749 return OK;
3750 }
3751 break;
3752 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3753 {
3754 rettv->v_type = VAR_FUNC;
3755 rettv->vval.v_string = NULL;
3756 return OK;
3757 }
3758 break;
3759 }
3760 return FAIL;
3761}
3762
3763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 * Handle sixth level expression:
3765 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003766 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003767 * "string" string constant
3768 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 * &option-name option value
3770 * @r register contents
3771 * identifier variable value
3772 * function() function call
3773 * $VAR environment variable
3774 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003777 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003778 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 *
3780 * Also handle:
3781 * ! in front logical NOT
3782 * - in front unary minus
3783 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003784 * trailing [] subscript in String or List
3785 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003786 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 *
3788 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003789 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 *
3791 * Return OK or FAIL.
3792 */
3793 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003794eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003795 char_u **arg,
3796 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003797 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003800 int evaluate = evalarg != NULL
3801 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 int len;
3803 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003804 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 char_u *start_leader, *end_leader;
3806 int ret = OK;
3807 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003808 static int recurse = 0;
3809 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
3811 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003812 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003813 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003815 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816
3817 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003818 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 */
3820 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003821 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003822 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 end_leader = *arg;
3824
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003825 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003826 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003827 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003828 ++*arg;
3829 return FAIL;
3830 }
3831
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003832 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003833 // and crash. With MSVC the stack is smaller.
3834 if (recurse ==
3835#ifdef _MSC_VER
3836 300
3837#else
3838 1000
3839#endif
3840 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003841 {
3842 semsg(_(e_expression_too_recursive_str), *arg);
3843 return FAIL;
3844 }
3845 ++recurse;
3846
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 switch (**arg)
3848 {
3849 /*
3850 * Number constant.
3851 */
3852 case '0':
3853 case '1':
3854 case '2':
3855 case '3':
3856 case '4':
3857 case '5':
3858 case '6':
3859 case '7':
3860 case '8':
3861 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003862 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003863
3864 // Apply prefixed "-" and "+" now. Matters especially when
3865 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003866 if (ret == OK && evaluate && end_leader > start_leader
3867 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003868 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 break;
3870
3871 /*
3872 * String constant: "string".
3873 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003874 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 break;
3876
3877 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003878 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003880 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003881 break;
3882
3883 /*
3884 * List: [expr, expr]
3885 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003886 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 break;
3888
3889 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003890 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003891 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003892 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003893 {
3894 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3895 }
3896 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003897 {
3898 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003899 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003900 }
3901 else
3902 ret = NOTDONE;
3903 break;
3904
3905 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003906 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003907 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003908 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003909 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003910 ret = NOTDONE;
3911 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003912 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003913 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003914 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003915 break;
3916
3917 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003918 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003920 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 break;
3922
3923 /*
3924 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003925 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 */
LemonBoy2eaef102022-05-06 13:14:50 +01003927 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3928 ret = eval_interp_string(arg, rettv, evaluate);
3929 else
3930 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 break;
3932
3933 /*
3934 * Register contents: @r.
3935 */
3936 case '@': ++*arg;
3937 if (evaluate)
3938 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003939 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003940 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003941 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003942 emsg_invreg(**arg);
3943 else
3944 {
3945 rettv->v_type = VAR_STRING;
3946 rettv->vval.v_string = get_reg_contents(**arg,
3947 GREG_EXPR_SRC);
3948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 if (**arg != NUL)
3951 ++*arg;
3952 break;
3953
3954 /*
3955 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003956 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003958 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003959 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01003960 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003961 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01003962 if (ret == OK && evaluate)
3963 {
3964 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
3965
Bram Moolenaara9931532021-06-12 15:58:16 +02003966 // Compile it here to get the return type. The return
3967 // type is optional, when it's missing use t_unknown.
3968 // This is recognized in compile_return().
3969 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
3970 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00003971 if (compile_def_function(ufunc, FALSE,
3972 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01003973 {
3974 clear_tv(rettv);
3975 ret = FAIL;
3976 }
Bram Moolenaar06409502021-02-17 17:00:27 +01003977 }
3978 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003979 if (ret == NOTDONE)
3980 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02003981 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003982 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02003983
Bram Moolenaar9215f012020-06-27 21:18:00 +02003984 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003985 if (**arg == ')')
3986 ++*arg;
3987 else if (ret == OK)
3988 {
Bram Moolenaare1242042021-12-16 20:56:57 +00003989 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003990 clear_tv(rettv);
3991 ret = FAIL;
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 break;
3995
Bram Moolenaar8c711452005-01-14 21:53:12 +00003996 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 break;
3998 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003999
4000 if (ret == NOTDONE)
4001 {
4002 /*
4003 * Must be a variable or function name.
4004 * Can also be a curly-braces kind of name: {expr}.
4005 */
4006 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004007 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004008 if (alias != NULL)
4009 s = alias;
4010
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004011 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004012 ret = FAIL;
4013 else
4014 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004015 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4016
Bram Moolenaar4525a572022-02-13 11:57:33 +00004017 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004018 {
4019 emsg(_(e_cannot_use_underscore_here));
4020 ret = FAIL;
4021 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004022 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004023 && s[0] == 's' && s[1] == ':')
4024 {
4025 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4026 ret = FAIL;
4027 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004028 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004029 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004030 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004031 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004032 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004033 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004034 else if (flags & EVAL_CONSTANT)
4035 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004036 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004037 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004038 // get the value of "true", "false", etc. or a variable
4039 ret = FAIL;
4040 if (vim9script)
4041 ret = handle_predefined(s, len, rettv);
4042 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004043 {
4044 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004045 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004046 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004047 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004048 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004049 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004050 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004051 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004052 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004053 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004054 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004055 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004056 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004057 }
4058
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004059 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4060 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004061 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004062 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064 /*
4065 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4066 */
4067 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004068 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004069
4070 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004071 return ret;
4072}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004073
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004074/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004075 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004076 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004077 * Adjusts "end_leaderp" until it is at "start_leader".
4078 */
4079 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004080eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004081 typval_T *rettv,
4082 int numeric_only,
4083 char_u *start_leader,
4084 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004085{
4086 char_u *end_leader = *end_leaderp;
4087 int ret = OK;
4088 int error = FALSE;
4089 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004090 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004091 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004092 float_T f = 0.0;
4093
4094 if (rettv->v_type == VAR_FLOAT)
4095 f = rettv->vval.v_float;
4096 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004097 {
4098 while (VIM_ISWHITE(end_leader[-1]))
4099 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004100 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004101 val = tv2bool(rettv);
4102 else
4103 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004104 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004105 if (error)
4106 {
4107 clear_tv(rettv);
4108 ret = FAIL;
4109 }
4110 else
4111 {
4112 while (end_leader > start_leader)
4113 {
4114 --end_leader;
4115 if (*end_leader == '!')
4116 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004117 if (numeric_only)
4118 {
4119 ++end_leader;
4120 break;
4121 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004122 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004123 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004124 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004125 {
4126 rettv->v_type = VAR_BOOL;
4127 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4128 }
4129 else
4130 f = !f;
4131 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004132 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004133 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004134 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004135 type = VAR_BOOL;
4136 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004137 }
4138 else if (*end_leader == '-')
4139 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004140 if (rettv->v_type == VAR_FLOAT)
4141 f = -f;
4142 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004143 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004144 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004145 type = VAR_NUMBER;
4146 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004147 }
4148 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004149 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004151 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004152 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004154 else
4155 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004156 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004157 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004158 rettv->v_type = type;
4159 else
4160 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004161 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004164 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 return ret;
4166}
4167
4168/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004169 * Call the function referred to in "rettv".
4170 */
4171 static int
4172call_func_rettv(
4173 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004174 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004175 typval_T *rettv,
4176 int evaluate,
4177 dict_T *selfdict,
4178 typval_T *basetv)
4179{
4180 partial_T *pt = NULL;
4181 funcexe_T funcexe;
4182 typval_T functv;
4183 char_u *s;
4184 int ret;
4185
4186 // need to copy the funcref so that we can clear rettv
4187 if (evaluate)
4188 {
4189 functv = *rettv;
4190 rettv->v_type = VAR_UNKNOWN;
4191
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004192 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004193 if (functv.v_type == VAR_PARTIAL)
4194 {
4195 pt = functv.vval.v_partial;
4196 s = partial_name(pt);
4197 }
4198 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004199 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004200 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004201 if (s == NULL || *s == NUL)
4202 {
4203 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004204 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004205 goto theend;
4206 }
4207 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004208 }
4209 else
4210 s = (char_u *)"";
4211
Bram Moolenaara80faa82020-04-12 19:37:17 +02004212 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004213 funcexe.fe_firstline = curwin->w_cursor.lnum;
4214 funcexe.fe_lastline = curwin->w_cursor.lnum;
4215 funcexe.fe_evaluate = evaluate;
4216 funcexe.fe_partial = pt;
4217 funcexe.fe_selfdict = selfdict;
4218 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004219 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004220
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004221theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004222 // Clear the funcref afterwards, so that deleting it while
4223 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004224 if (evaluate)
4225 clear_tv(&functv);
4226
4227 return ret;
4228}
4229
4230/*
4231 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004232 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004233 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4234 */
4235 static int
4236eval_lambda(
4237 char_u **arg,
4238 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004239 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004240 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004241{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004242 int evaluate = evalarg != NULL
4243 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004244 typval_T base = *rettv;
4245 int ret;
4246
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004247 rettv->v_type = VAR_UNKNOWN;
4248
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004249 if (**arg == '{')
4250 {
4251 // ->{lambda}()
4252 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4253 }
4254 else
4255 {
4256 // ->(lambda)()
4257 ++*arg;
4258 ret = eval1(arg, rettv, evalarg);
4259 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004260 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004261 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004262 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004263 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004264 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004265 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4266 && rettv->v_type != VAR_PARTIAL)
4267 {
4268 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4269 return FAIL;
4270 }
4271 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004272 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004273 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004274 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004275
4276 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004277 {
4278 if (verbose)
4279 {
4280 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004281 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004282 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004283 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004284 }
4285 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004286 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004287 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004288 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004289 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004290
4291 // Clear the funcref afterwards, so that deleting it while
4292 // evaluating the arguments is possible (see test55).
4293 if (evaluate)
4294 clear_tv(&base);
4295
4296 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004297}
4298
4299/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004300 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004301 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004302 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4303 */
4304 static int
4305eval_method(
4306 char_u **arg,
4307 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004308 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004309 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004310{
4311 char_u *name;
4312 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004313 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004314 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004315 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004316 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004317 int evaluate = evalarg != NULL
4318 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004319
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004320 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004321
Bram Moolenaarac92e252019-08-03 21:58:38 +02004322 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004323 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004324 if (alias != NULL)
4325 name = alias;
4326
4327 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004328 {
4329 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004330 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004331 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004332 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004333 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004334 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004335 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004336
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004337 // If there is no "(" immediately following, but there is further on,
4338 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4339 // Does not handle anything where "(" is part of the expression.
4340 *arg = skipwhite(*arg);
4341
4342 if (**arg != '(' && alias == NULL
4343 && (paren = vim_strchr(*arg, '(')) != NULL)
4344 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004345 char_u *deref;
4346
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004347 *arg = name;
4348 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004349 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4350 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004351 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004352 *arg = name + len;
4353 ret = FAIL;
4354 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004355 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004356 {
4357 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004358 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004359 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004360 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004361 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004362
4363 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004364 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004365 *arg = skipwhite(*arg);
4366
4367 if (**arg != '(')
4368 {
4369 if (verbose)
4370 semsg(_(e_missing_parenthesis_str), name);
4371 ret = FAIL;
4372 }
4373 else if (VIM_ISWHITE((*arg)[-1]))
4374 {
4375 if (verbose)
4376 emsg(_(e_no_white_space_allowed_before_parenthesis));
4377 ret = FAIL;
4378 }
4379 else
4380 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004381 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004382 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004383 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004384
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004385 // Clear the funcref afterwards, so that deleting it while
4386 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004387 if (evaluate)
4388 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004389 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004390
Bram Moolenaarac92e252019-08-03 21:58:38 +02004391 return ret;
4392}
4393
4394/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004395 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4396 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004397 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4398 */
4399 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004400eval_index(
4401 char_u **arg,
4402 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004403 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004404 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004405{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004406 int evaluate = evalarg != NULL
4407 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004408 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004409 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004410 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004411 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004412 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004413 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004414
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004415 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4416 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004417
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004418 init_tv(&var1);
4419 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004420 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004421 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004422 /*
4423 * dict.name
4424 */
4425 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004426 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004427 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004428 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004429 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004430 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004431 }
4432 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004433 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004434 /*
4435 * something[idx]
4436 *
4437 * Get the (first) variable from inside the [].
4438 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004439 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004440 if (**arg == ':')
4441 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004442 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004443 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004444 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004445 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004446 semsg(_(e_white_space_required_before_and_after_str_at_str),
4447 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004448 clear_tv(&var1);
4449 return FAIL;
4450 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004451 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004452 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004453 int error = FALSE;
4454
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004455 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004456 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004457 && var1.v_type == VAR_FLOAT)
4458 {
4459 var1.vval.v_string = typval_tostring(&var1, TRUE);
4460 var1.v_type = VAR_STRING;
4461 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004462
Bram Moolenaar4525a572022-02-13 11:57:33 +00004463 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004464 tv_get_number_chk(&var1, &error);
4465 else
4466 error = tv_get_string_chk(&var1) == NULL;
4467 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004468 {
4469 // not a number or string
4470 clear_tv(&var1);
4471 return FAIL;
4472 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004473 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004474
4475 /*
4476 * Get the second variable from inside the [:].
4477 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004478 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004479 if (**arg == ':')
4480 {
4481 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004482 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004483 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004484 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004485 semsg(_(e_white_space_required_before_and_after_str_at_str),
4486 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004487 if (!empty1)
4488 clear_tv(&var1);
4489 return FAIL;
4490 }
4491 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004492 if (**arg == ']')
4493 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004494 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004495 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004496 if (!empty1)
4497 clear_tv(&var1);
4498 return FAIL;
4499 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004500 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004501 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004502 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004503 if (!empty1)
4504 clear_tv(&var1);
4505 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004506 return FAIL;
4507 }
4508 }
4509
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004510 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004511 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004512 if (**arg != ']')
4513 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004514 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004515 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004516 clear_tv(&var1);
4517 if (range)
4518 clear_tv(&var2);
4519 return FAIL;
4520 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004521 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004522 }
4523
4524 if (evaluate)
4525 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004526 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004527 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004528 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004529
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004530 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004531 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004532 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004533 clear_tv(&var2);
4534 return res;
4535 }
4536 return OK;
4537}
4538
4539/*
4540 * Check if "rettv" can have an [index] or [sli:ce]
4541 */
4542 int
4543check_can_index(typval_T *rettv, int evaluate, int verbose)
4544{
4545 switch (rettv->v_type)
4546 {
4547 case VAR_FUNC:
4548 case VAR_PARTIAL:
4549 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004550 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004551 return FAIL;
4552 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004553 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004554 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004555 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004556 case VAR_BOOL:
4557 case VAR_SPECIAL:
4558 case VAR_JOB:
4559 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004560 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004561 if (verbose)
4562 emsg(_(e_cannot_index_special_variable));
4563 return FAIL;
4564 case VAR_UNKNOWN:
4565 case VAR_ANY:
4566 case VAR_VOID:
4567 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004568 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004569 emsg(_(e_cannot_index_special_variable));
4570 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004571 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004572 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004573
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004574 case VAR_STRING:
4575 case VAR_LIST:
4576 case VAR_DICT:
4577 case VAR_BLOB:
4578 break;
4579 case VAR_NUMBER:
4580 if (in_vim9script())
4581 emsg(_(e_cannot_index_number));
4582 break;
4583 }
4584 return OK;
4585}
4586
4587/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004588 * slice() function
4589 */
4590 void
4591f_slice(typval_T *argvars, typval_T *rettv)
4592{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004593 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004594 && ((argvars[0].v_type != VAR_STRING
4595 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004596 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004597 && check_for_list_arg(argvars, 0) == FAIL)
4598 || check_for_number_arg(argvars, 1) == FAIL
4599 || check_for_opt_number_arg(argvars, 2) == FAIL))
4600 return;
4601
Bram Moolenaar6601b622021-01-13 21:47:15 +01004602 if (check_can_index(argvars, TRUE, FALSE) == OK)
4603 {
4604 copy_tv(argvars, rettv);
4605 eval_index_inner(rettv, TRUE, argvars + 1,
4606 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4607 TRUE, NULL, 0, FALSE);
4608 }
4609}
4610
4611/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004612 * Apply index or range to "rettv".
4613 * "var1" is the first index, NULL for [:expr].
4614 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004615 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4616 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004617 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4618 */
4619 int
4620eval_index_inner(
4621 typval_T *rettv,
4622 int is_range,
4623 typval_T *var1,
4624 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004625 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004626 char_u *key,
4627 int keylen,
4628 int verbose)
4629{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004630 varnumber_T n1, n2 = 0;
4631 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004632
4633 n1 = 0;
4634 if (var1 != NULL && rettv->v_type != VAR_DICT)
4635 n1 = tv_get_number(var1);
4636
4637 if (is_range)
4638 {
4639 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004640 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004641 if (verbose)
4642 emsg(_(e_cannot_slice_dictionary));
4643 return FAIL;
4644 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004645 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004646 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004647 else
4648 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004649 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004650
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004651 switch (rettv->v_type)
4652 {
4653 case VAR_UNKNOWN:
4654 case VAR_ANY:
4655 case VAR_VOID:
4656 case VAR_FUNC:
4657 case VAR_PARTIAL:
4658 case VAR_FLOAT:
4659 case VAR_BOOL:
4660 case VAR_SPECIAL:
4661 case VAR_JOB:
4662 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004663 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004664 break; // not evaluating, skipping over subscript
4665
4666 case VAR_NUMBER:
4667 case VAR_STRING:
4668 {
4669 char_u *s = tv_get_string(rettv);
4670
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004671 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004672 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004673 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004674 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004675 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004676 else
4677 s = char_from_string(s, n1);
4678 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004679 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004680 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004681 // The resulting variable is a substring. If the indexes
4682 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683 if (n1 < 0)
4684 {
4685 n1 = len + n1;
4686 if (n1 < 0)
4687 n1 = 0;
4688 }
4689 if (n2 < 0)
4690 n2 = len + n2;
4691 else if (n2 >= len)
4692 n2 = len;
4693 if (n1 >= len || n2 < 0 || n1 > n2)
4694 s = NULL;
4695 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004696 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004697 }
4698 else
4699 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004700 // The resulting variable is a string of a single
4701 // character. If the index is too big or negative the
4702 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004703 if (n1 >= len || n1 < 0)
4704 s = NULL;
4705 else
4706 s = vim_strnsave(s + n1, 1);
4707 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004708 clear_tv(rettv);
4709 rettv->v_type = VAR_STRING;
4710 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004711 }
4712 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004713
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004714 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004715 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4716 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004717 break;
4718
4719 case VAR_LIST:
4720 if (var1 == NULL)
4721 n1 = 0;
4722 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004723 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004724 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004725 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004726 return FAIL;
4727 break;
4728
4729 case VAR_DICT:
4730 {
4731 dictitem_T *item;
4732 typval_T tmp;
4733
4734 if (key == NULL)
4735 {
4736 key = tv_get_string_chk(var1);
4737 if (key == NULL)
4738 return FAIL;
4739 }
4740
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004741 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004742
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004743 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004744 {
4745 if (verbose)
4746 {
4747 if (keylen > 0)
4748 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004749 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004750 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004751 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004752 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004753
4754 copy_tv(&item->di_tv, &tmp);
4755 clear_tv(rettv);
4756 *rettv = tmp;
4757 }
4758 break;
4759 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004760 return OK;
4761}
4762
4763/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004764 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004765 */
4766 char_u *
4767partial_name(partial_T *pt)
4768{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004769 if (pt != NULL)
4770 {
4771 if (pt->pt_name != NULL)
4772 return pt->pt_name;
4773 if (pt->pt_func != NULL)
4774 return pt->pt_func->uf_name;
4775 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004776 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004777}
4778
Bram Moolenaarddecc252016-04-06 22:59:37 +02004779 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004780partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004781{
4782 int i;
4783
4784 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004785 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004786 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004787 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004788 if (pt->pt_name != NULL)
4789 {
4790 func_unref(pt->pt_name);
4791 vim_free(pt->pt_name);
4792 }
4793 else
4794 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004795
Bram Moolenaar54656012021-06-09 20:50:46 +02004796 // "out_up" is no longer used, decrement refcount on partial that owns it.
4797 partial_unref(pt->pt_outer.out_up_partial);
4798
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004799 // Using pt_outer from another partial.
4800 partial_unref(pt->pt_outer_partial);
4801
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004802 // Decrease the reference count for the context of a closure. If down
4803 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004804 if (pt->pt_funcstack != NULL)
4805 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004806 --pt->pt_funcstack->fs_refcount;
4807 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004808 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004809 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004810 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
4811 if (pt->pt_loopvars[i] != NULL)
4812 {
4813 --pt->pt_loopvars[i]->lvs_refcount;
4814 loopvars_check_refcount(pt->pt_loopvars[i]);
4815 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004816
Bram Moolenaarddecc252016-04-06 22:59:37 +02004817 vim_free(pt);
4818}
4819
4820/*
4821 * Unreference a closure: decrement the reference count and free it when it
4822 * becomes zero.
4823 */
4824 void
4825partial_unref(partial_T *pt)
4826{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004827 if (pt != NULL)
4828 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004829 int done = FALSE;
4830
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004831 if (--pt->pt_refcount <= 0)
4832 partial_free(pt);
4833
4834 // If the reference count goes down to one, the funcstack may be the
4835 // only reference and can be freed if no other partials reference it.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004836 else if (pt->pt_refcount == 1)
4837 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004838 // careful: if the funcstack is freed it may contain this partial
4839 // and it gets freed as well
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004840 if (pt->pt_funcstack != NULL)
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004841 done = funcstack_check_refcount(pt->pt_funcstack);
4842
Bram Moolenaarcc341812022-09-19 15:54:34 +01004843 if (!done)
4844 {
4845 int depth;
4846
4847 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
4848 if (pt->pt_loopvars[depth] != NULL
4849 && loopvars_check_refcount(pt->pt_loopvars[depth]))
4850 break;
4851 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004852 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004853 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004854}
4855
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004856/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004857 * Return the next (unique) copy ID.
4858 * Used for serializing nested structures.
4859 */
4860 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004861get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004862{
4863 current_copyID += COPYID_INC;
4864 return current_copyID;
4865}
4866
4867/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004868 * Garbage collection for lists and dictionaries.
4869 *
4870 * We use reference counts to be able to free most items right away when they
4871 * are no longer used. But for composite items it's possible that it becomes
4872 * unused while the reference count is > 0: When there is a recursive
4873 * reference. Example:
4874 * :let l = [1, 2, 3]
4875 * :let d = {9: l}
4876 * :let l[1] = d
4877 *
4878 * Since this is quite unusual we handle this with garbage collection: every
4879 * once in a while find out which lists and dicts are not referenced from any
4880 * variable.
4881 *
4882 * Here is a good reference text about garbage collection (refers to Python
4883 * but it applies to all reference-counting mechanisms):
4884 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004885 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004886
4887/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004888 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004889 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004890 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004891 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004892 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004893garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004894{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004895 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004896 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004897 buf_T *buf;
4898 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004899 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004900 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004901
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004902 if (!testing)
4903 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004904 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004905 want_garbage_collect = FALSE;
4906 may_garbage_collect = FALSE;
4907 garbage_collect_at_exit = FALSE;
4908 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004909
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004910 // The execution stack can grow big, limit the size.
4911 if (exestack.ga_maxlen - exestack.ga_len > 500)
4912 {
4913 size_t new_len;
4914 char_u *pp;
4915 int n;
4916
4917 // Keep 150% of the current size, with a minimum of the growth size.
4918 n = exestack.ga_len / 2;
4919 if (n < exestack.ga_growsize)
4920 n = exestack.ga_growsize;
4921
4922 // Don't make it bigger though.
4923 if (exestack.ga_len + n < exestack.ga_maxlen)
4924 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004925 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004926 pp = vim_realloc(exestack.ga_data, new_len);
4927 if (pp == NULL)
4928 return FAIL;
4929 exestack.ga_maxlen = exestack.ga_len + n;
4930 exestack.ga_data = pp;
4931 }
4932 }
4933
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004934 // We advance by two because we add one for items referenced through
4935 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004936 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004937
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004938 /*
4939 * 1. Go through all accessible variables and mark all lists and dicts
4940 * with copyID.
4941 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004942
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004943 // Don't free variables in the previous_funccal list unless they are only
4944 // referenced through previous_funccal. This must be first, because if
4945 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004946 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004947
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004948 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004949 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004951 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004952 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004953 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4954 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004955
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004956 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004957 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004958 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4959 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004960 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004961 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4962 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004963#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004964 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004965 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4966 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004967 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02004968 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004969 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4970 NULL, NULL);
4971#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004973 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004974 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004975 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4976 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004977 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004978 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004979
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004980 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004981 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004983 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004984 abort = abort || set_ref_in_functions(copyID);
4985
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004986 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004987 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004988
Bram Moolenaar7509ad82021-12-14 18:14:37 +00004989 // funcstacks keep variables for closures
4990 abort = abort || set_ref_in_funcstacks(copyID);
4991
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004992 // loopvars keep variables for loop blocks
4993 abort = abort || set_ref_in_loopvars(copyID);
4994
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004995 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004996 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004997
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004998 // callbacks in buffers
4999 abort = abort || set_ref_in_buffers(copyID);
5000
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005001 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5002 abort = abort || set_ref_in_insexpand_funcs(copyID);
5003
5004 // 'operatorfunc' callback
5005 abort = abort || set_ref_in_opfunc(copyID);
5006
5007 // 'tagfunc' callback
5008 abort = abort || set_ref_in_tagfunc(copyID);
5009
5010 // 'imactivatefunc' and 'imstatusfunc' callbacks
5011 abort = abort || set_ref_in_im_funcs(copyID);
5012
Bram Moolenaar1dced572012-04-05 16:54:08 +02005013#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005014 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005015#endif
5016
Bram Moolenaardb913952012-06-29 12:54:53 +02005017#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005018 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005019#endif
5020
5021#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005022 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005023#endif
5024
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005025#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005026 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005027 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005028#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005029#ifdef FEAT_NETBEANS_INTG
5030 abort = abort || set_ref_in_nb_channel(copyID);
5031#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005032
Bram Moolenaare3188e22016-05-31 21:13:04 +02005033#ifdef FEAT_TIMERS
5034 abort = abort || set_ref_in_timer(copyID);
5035#endif
5036
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005037#ifdef FEAT_QUICKFIX
5038 abort = abort || set_ref_in_quickfix(copyID);
5039#endif
5040
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005041#ifdef FEAT_TERMINAL
5042 abort = abort || set_ref_in_term(copyID);
5043#endif
5044
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005045#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005046 abort = abort || set_ref_in_popups(copyID);
5047#endif
5048
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005049 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005050 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005051 /*
5052 * 2. Free lists and dictionaries that are not referenced.
5053 */
5054 did_free = free_unref_items(copyID);
5055
5056 /*
5057 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005058 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005059 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005060 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005061 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005062 else if (p_verbose > 0)
5063 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005064 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005065 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005066
5067 return did_free;
5068}
5069
5070/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005071 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005072 */
5073 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005074free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005075{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005076 int did_free = FALSE;
5077
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005078 // Let all "free" functions know that we are here. This means no
5079 // dictionaries, lists, channels or jobs are to be freed, because we will
5080 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005081 in_free_unref_items = TRUE;
5082
5083 /*
5084 * PASS 1: free the contents of the items. We don't free the items
5085 * themselves yet, so that it is possible to decrement refcount counters
5086 */
5087
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005088 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005089 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005090
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005091 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005092 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005093
5094#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005095 // Go through the list of jobs and free items without the copyID. This
5096 // must happen before doing channels, because jobs refer to channels, but
5097 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005098 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5099
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005100 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005101 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5102#endif
5103
5104 /*
5105 * PASS 2: free the items themselves.
5106 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005107 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005108 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005109
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005110#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005111 // Go through the list of jobs and free items without the copyID. This
5112 // must happen before doing channels, because jobs refer to channels, but
5113 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005114 free_unused_jobs(copyID, COPYID_MASK);
5115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005117 free_unused_channels(copyID, COPYID_MASK);
5118#endif
5119
5120 in_free_unref_items = FALSE;
5121
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005122 return did_free;
5123}
5124
5125/*
5126 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005127 * "list_stack" is used to add lists to be marked. Can be NULL.
5128 *
5129 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005130 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005131 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005132set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005133{
5134 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005135 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005136 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005137 hashtab_T *cur_ht;
5138 ht_stack_T *ht_stack = NULL;
5139 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005140
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005141 cur_ht = ht;
5142 for (;;)
5143 {
5144 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005145 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005146 // Mark each item in the hashtab. If the item contains a hashtab
5147 // it is added to ht_stack, if it contains a list it is added to
5148 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005149 todo = (int)cur_ht->ht_used;
5150 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5151 if (!HASHITEM_EMPTY(hi))
5152 {
5153 --todo;
5154 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5155 &ht_stack, list_stack);
5156 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005157 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005158
5159 if (ht_stack == NULL)
5160 break;
5161
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005162 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005163 cur_ht = ht_stack->ht;
5164 tempitem = ht_stack;
5165 ht_stack = ht_stack->prev;
5166 free(tempitem);
5167 }
5168
5169 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005170}
5171
Dominique Pelle748b3082022-01-08 12:41:16 +00005172#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5173 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005174/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005175 * Mark a dict and its items with "copyID".
5176 * Returns TRUE if setting references failed somehow.
5177 */
5178 int
5179set_ref_in_dict(dict_T *d, int copyID)
5180{
5181 if (d != NULL && d->dv_copyID != copyID)
5182 {
5183 d->dv_copyID = copyID;
5184 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5185 }
5186 return FALSE;
5187}
Dominique Pelle748b3082022-01-08 12:41:16 +00005188#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005189
5190/*
5191 * Mark a list and its items with "copyID".
5192 * Returns TRUE if setting references failed somehow.
5193 */
5194 int
5195set_ref_in_list(list_T *ll, int copyID)
5196{
5197 if (ll != NULL && ll->lv_copyID != copyID)
5198 {
5199 ll->lv_copyID = copyID;
5200 return set_ref_in_list_items(ll, copyID, NULL);
5201 }
5202 return FALSE;
5203}
5204
5205/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005206 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005207 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5208 *
5209 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005210 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005211 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005212set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005213{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005214 listitem_T *li;
5215 int abort = FALSE;
5216 list_T *cur_l;
5217 list_stack_T *list_stack = NULL;
5218 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005219
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005220 cur_l = l;
5221 for (;;)
5222 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005223 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005224 // Mark each item in the list. If the item contains a hashtab
5225 // it is added to ht_stack, if it contains a list it is added to
5226 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005227 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5228 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5229 ht_stack, &list_stack);
5230 if (list_stack == NULL)
5231 break;
5232
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005233 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005234 cur_l = list_stack->list;
5235 tempitem = list_stack;
5236 list_stack = list_stack->prev;
5237 free(tempitem);
5238 }
5239
5240 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005241}
5242
5243/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005244 * Mark the partial in callback 'cb' with "copyID".
5245 */
5246 int
5247set_ref_in_callback(callback_T *cb, int copyID)
5248{
5249 typval_T tv;
5250
5251 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5252 return FALSE;
5253
5254 tv.v_type = VAR_PARTIAL;
5255 tv.vval.v_partial = cb->cb_partial;
5256 return set_ref_in_item(&tv, copyID, NULL, NULL);
5257}
5258
5259/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005260 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005261 * "list_stack" is used to add lists to be marked. Can be NULL.
5262 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5263 *
5264 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005265 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005267set_ref_in_item(
5268 typval_T *tv,
5269 int copyID,
5270 ht_stack_T **ht_stack,
5271 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005272{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005273 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005274
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005275 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005276 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005277 dict_T *dd = tv->vval.v_dict;
5278
Bram Moolenaara03f2332016-02-06 18:09:59 +01005279 if (dd != NULL && dd->dv_copyID != copyID)
5280 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005281 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005282 dd->dv_copyID = copyID;
5283 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005284 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005285 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5286 }
5287 else
5288 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005289 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5290
Bram Moolenaara03f2332016-02-06 18:09:59 +01005291 if (newitem == NULL)
5292 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005293 else
5294 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005295 newitem->ht = &dd->dv_hashtab;
5296 newitem->prev = *ht_stack;
5297 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005298 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005299 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005300 }
5301 }
5302 else if (tv->v_type == VAR_LIST)
5303 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005304 list_T *ll = tv->vval.v_list;
5305
Bram Moolenaara03f2332016-02-06 18:09:59 +01005306 if (ll != NULL && ll->lv_copyID != copyID)
5307 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005308 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005309 ll->lv_copyID = copyID;
5310 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005311 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005312 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005313 }
5314 else
5315 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005316 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5317
Bram Moolenaara03f2332016-02-06 18:09:59 +01005318 if (newitem == NULL)
5319 abort = TRUE;
5320 else
5321 {
5322 newitem->list = ll;
5323 newitem->prev = *list_stack;
5324 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005325 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005326 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005327 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005328 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005329 else if (tv->v_type == VAR_FUNC)
5330 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005331 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005332 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005333 else if (tv->v_type == VAR_PARTIAL)
5334 {
5335 partial_T *pt = tv->vval.v_partial;
5336 int i;
5337
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005338 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005339 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005340 // Didn't see this partial yet.
5341 pt->pt_copyID = copyID;
5342
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005343 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005344
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005345 if (pt->pt_dict != NULL)
5346 {
5347 typval_T dtv;
5348
5349 dtv.v_type = VAR_DICT;
5350 dtv.vval.v_dict = pt->pt_dict;
5351 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5352 }
5353
5354 for (i = 0; i < pt->pt_argc; ++i)
5355 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5356 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005357 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005358 // pt_loopvars is handled in set_ref_in_loopvars()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005359 }
5360 }
5361#ifdef FEAT_JOB_CHANNEL
5362 else if (tv->v_type == VAR_JOB)
5363 {
5364 job_T *job = tv->vval.v_job;
5365 typval_T dtv;
5366
5367 if (job != NULL && job->jv_copyID != copyID)
5368 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005369 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005370 if (job->jv_channel != NULL)
5371 {
5372 dtv.v_type = VAR_CHANNEL;
5373 dtv.vval.v_channel = job->jv_channel;
5374 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5375 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005376 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005377 {
5378 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005379 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005380 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5381 }
5382 }
5383 }
5384 else if (tv->v_type == VAR_CHANNEL)
5385 {
5386 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005387 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005388 typval_T dtv;
5389 jsonq_T *jq;
5390 cbq_T *cq;
5391
5392 if (ch != NULL && ch->ch_copyID != copyID)
5393 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005394 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005395 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005396 {
5397 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5398 jq = jq->jq_next)
5399 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5400 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5401 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005402 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005403 {
5404 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005405 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005406 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5407 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005408 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005409 {
5410 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005411 dtv.vval.v_partial =
5412 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005413 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5414 }
5415 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005416 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005417 {
5418 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005419 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005420 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5421 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005422 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005423 {
5424 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005425 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005426 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5427 }
5428 }
5429 }
5430#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005431 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005432}
5433
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 * Return a string with the string representation of a variable.
5436 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005437 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005438 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005439 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005440 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005441 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005442 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5443 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005444 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005446 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005447echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005448 typval_T *tv,
5449 char_u **tofree,
5450 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005451 int copyID,
5452 int echo_style,
5453 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005454 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005456 static int recurse = 0;
5457 char_u *r = NULL;
5458
Bram Moolenaar33570922005-01-25 22:26:29 +00005459 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005460 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005461 if (!did_echo_string_emsg)
5462 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005463 // Only give this message once for a recursive call to avoid
5464 // flooding the user with errors. And stop iterating over lists
5465 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005466 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005467 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005468 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005469 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005470 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005471 }
5472 ++recurse;
5473
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 switch (tv->v_type)
5475 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005476 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005477 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005478 {
5479 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005480 r = tv->vval.v_string;
5481 if (r == NULL)
5482 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005483 }
5484 else
5485 {
5486 *tofree = string_quote(tv->vval.v_string, FALSE);
5487 r = *tofree;
5488 }
5489 break;
5490
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005492 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005493 char_u buf[MAX_FUNC_NAME_LEN];
5494
5495 if (echo_style)
5496 {
LemonBoya5d35902022-04-29 21:15:02 +01005497 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5498 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005499 buf, MAX_FUNC_NAME_LEN);
5500 if (r == buf)
5501 {
5502 r = vim_strsave(buf);
5503 *tofree = r;
5504 }
5505 else
5506 *tofree = NULL;
5507 }
5508 else
5509 {
5510 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5511 : make_ufunc_name_readable(
5512 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5513 TRUE);
5514 r = *tofree;
5515 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005516 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005517 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005518
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005519 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005520 {
5521 partial_T *pt = tv->vval.v_partial;
5522 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005523 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005524 garray_T ga;
5525 int i;
5526 char_u *tf;
5527
5528 ga_init2(&ga, 1, 100);
5529 ga_concat(&ga, (char_u *)"function(");
5530 if (fname != NULL)
5531 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005532 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005533 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005534 && vim_isupper(fname[1]))
5535 {
5536 ga_concat(&ga, (char_u *)"'g:");
5537 ga_concat(&ga, fname + 1);
5538 }
5539 else
5540 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005541 vim_free(fname);
5542 }
5543 if (pt != NULL && pt->pt_argc > 0)
5544 {
5545 ga_concat(&ga, (char_u *)", [");
5546 for (i = 0; i < pt->pt_argc; ++i)
5547 {
5548 if (i > 0)
5549 ga_concat(&ga, (char_u *)", ");
5550 ga_concat(&ga,
5551 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5552 vim_free(tf);
5553 }
5554 ga_concat(&ga, (char_u *)"]");
5555 }
5556 if (pt != NULL && pt->pt_dict != NULL)
5557 {
5558 typval_T dtv;
5559
5560 ga_concat(&ga, (char_u *)", ");
5561 dtv.v_type = VAR_DICT;
5562 dtv.vval.v_dict = pt->pt_dict;
5563 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5564 vim_free(tf);
5565 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005566 // terminate with ')' and a NUL
5567 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005568
5569 *tofree = ga.ga_data;
5570 r = *tofree;
5571 break;
5572 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005573
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005574 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005575 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005576 break;
5577
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005579 if (tv->vval.v_list == NULL)
5580 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005581 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005582 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005583 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005584 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005585 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5586 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005587 {
5588 *tofree = NULL;
5589 r = (char_u *)"[...]";
5590 }
5591 else
5592 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005593 int old_copyID = tv->vval.v_list->lv_copyID;
5594
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005595 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005596 *tofree = list2string(tv, copyID, restore_copyID);
5597 if (restore_copyID)
5598 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005599 r = *tofree;
5600 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005601 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005602
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005604 if (tv->vval.v_dict == NULL)
5605 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005606 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005607 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005608 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005609 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005610 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5611 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005612 {
5613 *tofree = NULL;
5614 r = (char_u *)"{...}";
5615 }
5616 else
5617 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005618 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005619
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005620 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005621 *tofree = dict2string(tv, copyID, restore_copyID);
5622 if (restore_copyID)
5623 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005624 r = *tofree;
5625 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005626 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005627
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005628 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005629 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005630 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005631 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005632 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005633 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005634 break;
5635
Bram Moolenaar835dc632016-02-07 14:27:38 +01005636 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005637 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005638#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005639 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005640 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5641 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005642 if (composite_val)
5643 {
5644 *tofree = string_quote(r, FALSE);
5645 r = *tofree;
5646 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005647#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005649
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005650 case VAR_INSTR:
5651 *tofree = NULL;
5652 r = (char_u *)"instructions";
5653 break;
5654
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005655 case VAR_FLOAT:
5656 *tofree = NULL;
5657 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5658 r = numbuf;
5659 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005660
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005661 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005662 case VAR_SPECIAL:
5663 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005664 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005665 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005667
Bram Moolenaar8502c702014-06-17 12:51:16 +02005668 if (--recurse == 0)
5669 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005670 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005671}
5672
5673/*
5674 * Return a string with the string representation of a variable.
5675 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5676 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005677 * Does not put quotes around strings, as ":echo" displays values.
5678 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5679 * May return NULL.
5680 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005681 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005682echo_string(
5683 typval_T *tv,
5684 char_u **tofree,
5685 char_u *numbuf,
5686 int copyID)
5687{
5688 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5689}
5690
5691/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005692 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5693 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005694 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005695 */
5696 int
5697buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5698{
5699 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005700 char_u *t;
5701 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005702
5703 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5704 return -1;
5705
5706 if (lnum > buf->b_ml.ml_line_count)
5707 lnum = buf->b_ml.ml_line_count;
5708
5709 str = ml_get_buf(buf, lnum, FALSE);
5710 if (str == NULL)
5711 return -1;
5712
5713 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005714 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005715
Bram Moolenaar91458462021-01-13 20:08:38 +01005716 // count the number of characters
5717 t = str;
5718 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5719 t += mb_ptr2len(t);
5720
5721 // In insert mode, when the cursor is at the end of a non-empty line,
5722 // byteidx points to the NUL character immediately past the end of the
5723 // string. In this case, add one to the character count.
5724 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5725 count++;
5726
5727 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005728}
5729
5730/*
5731 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005732 * byte index. Works only for loaded buffers. Returns -1 on failure.
5733 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005734 */
5735 int
5736buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5737{
5738 char_u *str;
5739 char_u *t;
5740
5741 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5742 return -1;
5743
5744 if (lnum > buf->b_ml.ml_line_count)
5745 lnum = buf->b_ml.ml_line_count;
5746
5747 str = ml_get_buf(buf, lnum, FALSE);
5748 if (str == NULL)
5749 return -1;
5750
5751 // Convert the character offset to a byte offset
5752 t = str;
5753 while (*t != NUL && --charidx > 0)
5754 t += mb_ptr2len(t);
5755
Bram Moolenaar91458462021-01-13 20:08:38 +01005756 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005757}
5758
5759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005761 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005763 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005764var2fpos(
5765 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005766 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005767 int *fnum, // set to fnum for '0, 'A, etc.
5768 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005770 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005772 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005774 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005775 if (varp->v_type == VAR_LIST)
5776 {
5777 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005778 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005779 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005780 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005781
5782 l = varp->vval.v_list;
5783 if (l == NULL)
5784 return NULL;
5785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005786 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005787 pos.lnum = list_find_nr(l, 0L, &error);
5788 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005789 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005790 if (charcol)
5791 len = (long)mb_charlen(ml_get(pos.lnum));
5792 else
5793 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005794
Bram Moolenaarec65d772020-08-20 22:29:12 +02005795 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005796 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005797 li = list_find(l, 1L);
5798 if (li != NULL && li->li_tv.v_type == VAR_STRING
5799 && li->li_tv.vval.v_string != NULL
5800 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005801 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005802 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005803 }
5804 else
5805 {
5806 pos.col = list_find_nr(l, 1L, &error);
5807 if (error)
5808 return NULL;
5809 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005810
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005811 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005812 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005813 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005814 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005816 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005817 pos.coladd = list_find_nr(l, 2L, &error);
5818 if (error)
5819 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005820
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005821 return &pos;
5822 }
5823
Bram Moolenaarc5809432021-03-27 21:23:30 +01005824 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5825 return NULL;
5826
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005827 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005828 if (name == NULL)
5829 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005830
5831 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005832 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005833 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005834 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005835 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005836 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005837 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005838 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005839 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005840 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005841 pos = VIsual;
5842 else
5843 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005844 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005845 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005846 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005848 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005849 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5851 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005852 pos = *pp;
5853 }
5854 if (pos.lnum != 0)
5855 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005856 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005857 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5858 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005860
Bram Moolenaara5525202006-03-02 22:52:09 +00005861 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005862
Bram Moolenaar477933c2007-07-17 14:32:23 +00005863 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005864 {
5865 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005866 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005867 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005868 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005869 // In silent Ex mode topline is zero, but that's not a valid line
5870 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005871 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005872 return &pos;
5873 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005874 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005875 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005876 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005877 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005878 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005879 return &pos;
5880 }
5881 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005882 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005884 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {
5886 pos.lnum = curbuf->b_ml.ml_line_count;
5887 pos.col = 0;
5888 }
5889 else
5890 {
5891 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005892 if (charcol)
5893 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5894 else
5895 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 }
5897 return &pos;
5898 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005899 if (in_vim9script())
5900 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 return NULL;
5902}
5903
5904/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005905 * Convert list in "arg" into a position and optional file number.
5906 * When "fnump" is NULL there is no file number, only 3 items.
5907 * Note that the column is passed on as-is, the caller may want to decrement
5908 * it to use 1 for the first column.
5909 * Return FAIL when conversion is not possible, doesn't check the position for
5910 * validity.
5911 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005912 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005913list2fpos(
5914 typval_T *arg,
5915 pos_T *posp,
5916 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005917 colnr_T *curswantp,
5918 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005919{
5920 list_T *l = arg->vval.v_list;
5921 long i = 0;
5922 long n;
5923
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005924 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5925 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005926 if (arg->v_type != VAR_LIST
5927 || l == NULL
5928 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005929 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005930 return FAIL;
5931
5932 if (fnump != NULL)
5933 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005934 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005935 if (n < 0)
5936 return FAIL;
5937 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005938 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005939 *fnump = n;
5940 }
5941
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005942 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005943 if (n < 0)
5944 return FAIL;
5945 posp->lnum = n;
5946
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005947 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005948 if (n < 0)
5949 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005950 // If character position is specified, then convert to byte position
5951 if (charcol)
5952 {
5953 buf_T *buf;
5954
5955 // Get the text for the specified line in a loaded buffer
5956 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5957 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5958 return FAIL;
5959
Bram Moolenaar91458462021-01-13 20:08:38 +01005960 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005961 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005962 posp->col = n;
5963
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005964 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005965 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005966 posp->coladd = 0;
5967 else
5968 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005969
Bram Moolenaar493c1782014-05-28 14:34:46 +02005970 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005971 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02005972
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005973 return OK;
5974}
5975
5976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 * Get the length of an environment variable name.
5978 * Advance "arg" to the first character after the name.
5979 * Return 0 for error.
5980 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005982get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983{
5984 char_u *p;
5985 int len;
5986
5987 for (p = *arg; vim_isIDc(*p); ++p)
5988 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005989 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 return 0;
5991
5992 len = (int)(p - *arg);
5993 *arg = p;
5994 return len;
5995}
5996
5997/*
5998 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02005999 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 * Return 0 if something is wrong.
6001 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006002 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006003get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004{
6005 char_u *p;
6006 int len;
6007
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006008 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006010 {
6011 if (*p == ':')
6012 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006013 // "s:" is start of "s:var", but "n:" is not and can be used in
6014 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006015 len = (int)(p - *arg);
6016 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6017 || len > 1)
6018 break;
6019 }
6020 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006021 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 return 0;
6023
6024 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006025 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026
6027 return len;
6028}
6029
6030/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006031 * Get the length of the name of a variable or function.
6032 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006034 * Return -1 if curly braces expansion failed.
6035 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 * If the name contains 'magic' {}'s, expand them and return the
6037 * expanded name in an allocated string via 'alias' - caller must free.
6038 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006039 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006040get_name_len(
6041 char_u **arg,
6042 char_u **alias,
6043 int evaluate,
6044 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
6046 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 char_u *p;
6048 char_u *expr_start;
6049 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006051 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052
6053 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6054 && (*arg)[2] == (int)KE_SNR)
6055 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006056 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 *arg += 3;
6058 return get_id_len(arg) + 3;
6059 }
6060 len = eval_fname_script(*arg);
6061 if (len > 0)
6062 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006063 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 *arg += len;
6065 }
6066
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006068 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006070 p = find_name_end(*arg, &expr_start, &expr_end,
6071 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 if (expr_start != NULL)
6073 {
6074 char_u *temp_string;
6075
6076 if (!evaluate)
6077 {
6078 len += (int)(p - *arg);
6079 *arg = skipwhite(p);
6080 return len;
6081 }
6082
6083 /*
6084 * Include any <SID> etc in the expanded string:
6085 * Thus the -len here.
6086 */
6087 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6088 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006089 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 *alias = temp_string;
6091 *arg = skipwhite(p);
6092 return (int)STRLEN(temp_string);
6093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094
6095 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006096 // Only give an error when there is something, otherwise it will be
6097 // reported at a higher level.
6098 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006099 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100
6101 return len;
6102}
6103
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006104/*
6105 * Find the end of a variable or function name, taking care of magic braces.
6106 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6107 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006108 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006109 * Return a pointer to just after the name. Equal to "arg" if there is no
6110 * valid name.
6111 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006112 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006113find_name_end(
6114 char_u *arg,
6115 char_u **expr_start,
6116 char_u **expr_end,
6117 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006119 int mb_nest = 0;
6120 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006122 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006123 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006125 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006127 *expr_start = NULL;
6128 *expr_end = NULL;
6129 }
6130
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006131 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006132 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6133 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006134 return arg;
6135
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006136 for (p = arg; *p != NUL
6137 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006138 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006139 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006140 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006141 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006142 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006143 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006144 if (*p == '\'')
6145 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006146 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006147 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006148 ;
6149 if (*p == NUL)
6150 break;
6151 }
6152 else if (*p == '"')
6153 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006154 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006155 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006156 if (*p == '\\' && p[1] != NUL)
6157 ++p;
6158 if (*p == NUL)
6159 break;
6160 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006161 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6162 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006163 // "s:" is start of "s:var", but "n:" is not and can be used in
6164 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006165 len = (int)(p - arg);
6166 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006167 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006168 break;
6169 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006170
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006171 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006173 if (*p == '[')
6174 ++br_nest;
6175 else if (*p == ']')
6176 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006178
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006179 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006181 if (*p == '{')
6182 {
6183 mb_nest++;
6184 if (expr_start != NULL && *expr_start == NULL)
6185 *expr_start = p;
6186 }
6187 else if (*p == '}')
6188 {
6189 mb_nest--;
6190 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6191 *expr_end = p;
6192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 }
6195
6196 return p;
6197}
6198
6199/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006200 * Expands out the 'magic' {}'s in a variable/function name.
6201 * Note that this can call itself recursively, to deal with
6202 * constructs like foo{bar}{baz}{bam}
6203 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6204 * "in_start" ^
6205 * "expr_start" ^
6206 * "expr_end" ^
6207 * "in_end" ^
6208 *
6209 * Returns a new allocated string, which the caller must free.
6210 * Returns NULL for failure.
6211 */
6212 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006213make_expanded_name(
6214 char_u *in_start,
6215 char_u *expr_start,
6216 char_u *expr_end,
6217 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006218{
6219 char_u c1;
6220 char_u *retval = NULL;
6221 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006222
6223 if (expr_end == NULL || in_end == NULL)
6224 return NULL;
6225 *expr_start = NUL;
6226 *expr_end = NUL;
6227 c1 = *in_end;
6228 *in_end = NUL;
6229
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006230 temp_result = eval_to_string(expr_start + 1, FALSE);
6231 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006232 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006233 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6234 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006235 if (retval != NULL)
6236 {
6237 STRCPY(retval, in_start);
6238 STRCAT(retval, temp_result);
6239 STRCAT(retval, expr_end + 1);
6240 }
6241 }
6242 vim_free(temp_result);
6243
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006244 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006245 *expr_start = '{';
6246 *expr_end = '}';
6247
6248 if (retval != NULL)
6249 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006250 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006251 if (expr_start != NULL)
6252 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006253 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006254 temp_result = make_expanded_name(retval, expr_start,
6255 expr_end, temp_result);
6256 vim_free(retval);
6257 retval = temp_result;
6258 }
6259 }
6260
6261 return retval;
6262}
6263
6264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006266 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006268 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006269eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006271 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006272}
6273
6274/*
6275 * Return TRUE if character "c" can be used as the first character in a
6276 * variable or function name (excluding '{' and '}').
6277 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006278 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006279eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006280{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006281 return ASCII_ISALPHA(c) || c == '_';
6282}
6283
6284/*
6285 * Return TRUE if character "c" can be used as the first character of a
6286 * dictionary key.
6287 */
6288 int
6289eval_isdictc(int c)
6290{
6291 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292}
6293
6294/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006295 * Handle:
6296 * - expr[expr], expr[expr:expr] subscript
6297 * - ".name" lookup
6298 * - function call with Funcref variable: func(expr)
6299 * - method call: var->method()
6300 *
6301 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006302 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006303 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006305handle_subscript(
6306 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006307 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006308 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006309 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006310 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006311{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006312 int evaluate = evalarg != NULL
6313 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006314 int ret = OK;
6315 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006316 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006317 int getnext;
6318 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006319
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006320 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006321 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006322 // When at the end of the line and ".name" or "->{" or "->X" follows in
6323 // the next line then consume the line break.
6324 p = eval_next_non_blank(*arg, evalarg, &getnext);
6325 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006326 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006327 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6328 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6329 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006330 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006331 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006332 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006333 check_white = FALSE;
6334 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006335
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006336 if (rettv->v_type == VAR_ANY)
6337 {
6338 char_u *exp_name;
6339 int cc;
6340 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006341 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006342 type_T *type;
6343
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006344 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006345 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006346 if (**arg != '.')
6347 {
6348 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006349 semsg(_(e_expected_dot_after_name_str),
6350 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006351 ret = FAIL;
6352 break;
6353 }
6354 ++*arg;
6355 if (IS_WHITE_OR_NUL(**arg))
6356 {
6357 if (verbose)
6358 emsg(_(e_no_white_space_allowed_after_dot));
6359 ret = FAIL;
6360 break;
6361 }
6362
6363 // isolate the name
6364 exp_name = *arg;
6365 while (eval_isnamec(**arg))
6366 ++*arg;
6367 cc = **arg;
6368 **arg = NUL;
6369
6370 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00006371 evalarg->eval_cctx, evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006372 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006373
6374 if (idx < 0 && ufunc == NULL)
6375 {
6376 ret = FAIL;
6377 break;
6378 }
6379 if (idx >= 0)
6380 {
6381 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6382 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6383
6384 copy_tv(sv->sv_tv, rettv);
6385 }
6386 else
6387 {
6388 rettv->v_type = VAR_FUNC;
6389 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6390 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006391 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006392 }
6393
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006394 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6395 || rettv->v_type == VAR_PARTIAL))
6396 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006397 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006398 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6399 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006400
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006401 // Stop the expression evaluation when immediately aborting on
6402 // error, or when an interrupt occurred or an exception was thrown
6403 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006404 if (aborting())
6405 {
6406 if (ret == OK)
6407 clear_tv(rettv);
6408 ret = FAIL;
6409 }
6410 dict_unref(selfdict);
6411 selfdict = NULL;
6412 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006413 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006414 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006415 if (in_vim9script())
6416 *arg = skipwhite(p + 2);
6417 else
6418 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006419 if (ret == OK)
6420 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006421 if (VIM_ISWHITE(**arg))
6422 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006423 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006424 ret = FAIL;
6425 }
6426 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006427 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006428 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006429 else
6430 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006431 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006432 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006433 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006434 // "." is ".name" lookup when we found a dict or when evaluating and
6435 // scriptversion is at least 2, where string concatenation is "..".
6436 else if (**arg == '['
6437 || (**arg == '.' && (rettv->v_type == VAR_DICT
6438 || (!evaluate
6439 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006440 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006441 {
6442 dict_unref(selfdict);
6443 if (rettv->v_type == VAR_DICT)
6444 {
6445 selfdict = rettv->vval.v_dict;
6446 if (selfdict != NULL)
6447 ++selfdict->dv_refcount;
6448 }
6449 else
6450 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006451 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006452 {
6453 clear_tv(rettv);
6454 ret = FAIL;
6455 }
6456 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006457 else
6458 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006459 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006460
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006461 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6462 // Don't do this when "Func" is already a partial that was bound
6463 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006464 if (selfdict != NULL
6465 && (rettv->v_type == VAR_FUNC
6466 || (rettv->v_type == VAR_PARTIAL
6467 && (rettv->vval.v_partial->pt_auto
6468 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006469 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006470
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006471 dict_unref(selfdict);
6472 return ret;
6473}
6474
6475/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006476 * Make a copy of an item.
6477 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006478 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006479 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6480 * reference to an already copied list/dict can be used.
6481 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006482 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006483 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006484item_copy(
6485 typval_T *from,
6486 typval_T *to,
6487 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006488 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006489 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006490{
6491 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006492 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006493
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006495 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006496 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006497 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006498 }
6499 ++recurse;
6500
6501 switch (from->v_type)
6502 {
6503 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006504 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006505 case VAR_STRING:
6506 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006507 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006508 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006509 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006510 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006511 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006512 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006513 copy_tv(from, to);
6514 break;
6515 case VAR_LIST:
6516 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006517 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006518 if (from->vval.v_list == NULL)
6519 to->vval.v_list = NULL;
6520 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6521 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006522 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006523 to->vval.v_list = from->vval.v_list->lv_copylist;
6524 ++to->vval.v_list->lv_refcount;
6525 }
6526 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006527 to->vval.v_list = list_copy(from->vval.v_list,
6528 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006529 if (to->vval.v_list == NULL)
6530 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006531 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006532 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006533 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006534 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535 case VAR_DICT:
6536 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006537 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006538 if (from->vval.v_dict == NULL)
6539 to->vval.v_dict = NULL;
6540 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6541 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006542 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006543 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6544 ++to->vval.v_dict->dv_refcount;
6545 }
6546 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006547 to->vval.v_dict = dict_copy(from->vval.v_dict,
6548 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006549 if (to->vval.v_dict == NULL)
6550 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006551 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006552 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006553 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006554 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006555 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006556 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006557 }
6558 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006559 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006560}
6561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006562 void
6563echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6564{
6565 char_u *tofree;
6566 char_u numbuf[NUMBUFLEN];
6567 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6568
6569 if (*atstart)
6570 {
6571 *atstart = FALSE;
6572 // Call msg_start() after eval1(), evaluating the expression
6573 // may cause a message to appear.
6574 if (with_space)
6575 {
6576 // Mark the saved text as finishing the line, so that what
6577 // follows is displayed on a new line when scrolling back
6578 // at the more prompt.
6579 msg_sb_eol();
6580 msg_start();
6581 }
6582 }
6583 else if (with_space)
6584 msg_puts_attr(" ", echo_attr);
6585
6586 if (p != NULL)
6587 for ( ; *p != NUL && !got_int; ++p)
6588 {
6589 if (*p == '\n' || *p == '\r' || *p == TAB)
6590 {
6591 if (*p != TAB && *needclr)
6592 {
6593 // remove any text still there from the command
6594 msg_clr_eos();
6595 *needclr = FALSE;
6596 }
6597 msg_putchar_attr(*p, echo_attr);
6598 }
6599 else
6600 {
6601 if (has_mbyte)
6602 {
6603 int i = (*mb_ptr2len)(p);
6604
6605 (void)msg_outtrans_len_attr(p, i, echo_attr);
6606 p += i - 1;
6607 }
6608 else
6609 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6610 }
6611 }
6612 vim_free(tofree);
6613}
6614
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 * ":echo expr1 ..." print each argument separated with a space, add a
6617 * newline at the end.
6618 * ":echon expr1 ..." print each argument plain.
6619 */
6620 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006621ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622{
6623 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006624 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006625 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 int needclr = TRUE;
6627 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006628 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006629 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006630 evalarg_T evalarg;
6631
Bram Moolenaare707c882020-07-01 13:07:37 +02006632 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633
6634 if (eap->skip)
6635 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006636 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006638 // If eval1() causes an error message the text from the command may
6639 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006640 need_clr_eos = needclr;
6641
Bram Moolenaar68db9962021-05-09 23:19:22 +02006642 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006643 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 {
6645 /*
6646 * Report the invalid expression unless the expression evaluation
6647 * has been cancelled due to an aborting error, an interrupt, or an
6648 * exception.
6649 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006650 if (!aborting() && did_emsg == did_emsg_before
6651 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006652 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006653 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 break;
6655 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006656 need_clr_eos = FALSE;
6657
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006659 {
6660 if (rettv.v_type == VAR_VOID)
6661 {
6662 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6663 break;
6664 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006665 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006666 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006667
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006668 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 arg = skipwhite(arg);
6670 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006671 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006672 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
6674 if (eap->skip)
6675 --emsg_skip;
6676 else
6677 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006678 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 if (needclr)
6680 msg_clr_eos();
6681 if (eap->cmdidx == CMD_echo)
6682 msg_end();
6683 }
6684}
6685
6686/*
6687 * ":echohl {name}".
6688 */
6689 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006690ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006692 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693}
6694
6695/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006696 * Returns the :echo attribute
6697 */
6698 int
6699get_echo_attr(void)
6700{
6701 return echo_attr;
6702}
6703
6704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 * ":execute expr1 ..." execute the result of an expression.
6706 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006707 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006709 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 * Each gets spaces around each argument and a newline at the end for
6711 * echo commands
6712 */
6713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006714ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
6716 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006717 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 int ret = OK;
6719 char_u *p;
6720 garray_T ga;
6721 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006722 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723
6724 ga_init2(&ga, 1, 80);
6725
6726 if (eap->skip)
6727 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006728 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006730 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006731 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733
6734 if (!eap->skip)
6735 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006736 char_u buf[NUMBUFLEN];
6737
6738 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006739 {
6740 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6741 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006742 semsg(_(e_using_invalid_value_as_string_str),
6743 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006744 p = NULL;
6745 }
6746 else
6747 p = tv_get_string_buf(&rettv, buf);
6748 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006749 else
6750 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006751 if (p == NULL)
6752 {
6753 clear_tv(&rettv);
6754 ret = FAIL;
6755 break;
6756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 len = (int)STRLEN(p);
6758 if (ga_grow(&ga, len + 2) == FAIL)
6759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006760 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 ret = FAIL;
6762 break;
6763 }
6764 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 ga.ga_len += len;
6768 }
6769
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006770 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 arg = skipwhite(arg);
6772 }
6773
6774 if (ret != FAIL && ga.ga_data != NULL)
6775 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006776 // use the first line of continuation lines for messages
6777 SOURCING_LNUM = start_lnum;
6778
Bram Moolenaar37fef162022-08-29 18:16:32 +01006779 if (eap->cmdidx == CMD_echomsg
6780 || eap->cmdidx == CMD_echowindow
6781 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006782 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006783 // Mark the already saved text as finishing the line, so that what
6784 // follows is displayed on a new line when scrolling back at the
6785 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006786 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006787 }
6788
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006789 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006790 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006791 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006792 out_flush();
6793 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006794 else if (eap->cmdidx == CMD_echowindow)
6795 {
6796#ifdef HAS_MESSAGE_WINDOW
6797 start_echowindow();
6798#endif
6799 msg_attr(ga.ga_data, echo_attr);
6800#ifdef HAS_MESSAGE_WINDOW
6801 end_echowindow();
6802#endif
6803 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006804 else if (eap->cmdidx == CMD_echoconsole)
6805 {
6806 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6807 ui_write((char_u *)"\r\n", 2, TRUE);
6808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 else if (eap->cmdidx == CMD_echoerr)
6810 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006811 int save_did_emsg = did_emsg;
6812
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006813 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006814 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (!force_abort)
6816 did_emsg = save_did_emsg;
6817 }
6818 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006819 {
6820 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6821
6822 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6823 sticky_cmdmod_flags = cmdmod.cmod_flags
6824 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 do_cmdline((char_u *)ga.ga_data,
6826 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006827 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 }
6830
6831 ga_clear(&ga);
6832
6833 if (eap->skip)
6834 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006835 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836}
6837
6838/*
6839 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6840 * "arg" points to the "&" or '+' when called, to "option" when returning.
6841 * Returns NULL when no option name found. Otherwise pointer to the char
6842 * after the option name.
6843 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006844 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006845find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846{
6847 char_u *p = *arg;
6848
6849 ++p;
6850 if (*p == 'g' && p[1] == ':')
6851 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006852 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 p += 2;
6854 }
6855 else if (*p == 'l' && p[1] == ':')
6856 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006857 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 p += 2;
6859 }
6860 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006861 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862
6863 if (!ASCII_ISALPHA(*p))
6864 return NULL;
6865 *arg = p;
6866
6867 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006868 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 else
6870 while (ASCII_ISALPHA(*p))
6871 ++p;
6872 return p;
6873}
6874
6875/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006876 * Display script name where an item was last set.
6877 * Should only be invoked when 'verbose' is non-zero.
6878 */
6879 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006880last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006881{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006882 char_u *p;
6883
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006884 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006885 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006886 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006887 if (p != NULL)
6888 {
6889 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006890 msg_puts(_("\n\tLast set from "));
6891 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006892 if (script_ctx.sc_lnum > 0)
6893 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006894 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006895 msg_outnum((long)script_ctx.sc_lnum);
6896 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006897 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006898 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006899 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006900 }
6901}
6902
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006903#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904
6905/*
6906 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006907 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 * "flags" can be "g" to do a global substitute.
6909 * Returns an allocated string, NULL for error.
6910 */
6911 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006912do_string_sub(
6913 char_u *str,
6914 char_u *pat,
6915 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006916 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006917 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918{
6919 int sublen;
6920 regmatch_T regmatch;
6921 int i;
6922 int do_all;
6923 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006924 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 garray_T ga;
6926 char_u *ret;
6927 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006928 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006930 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006932 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933
6934 ga_init2(&ga, 1, 200);
6935
6936 do_all = (flags[0] == 'g');
6937
6938 regmatch.rm_ic = p_ic;
6939 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6940 if (regmatch.regprog != NULL)
6941 {
6942 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006943 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6945 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006946 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006947 if (regmatch.startp[0] == regmatch.endp[0])
6948 {
6949 if (zero_width == regmatch.startp[0])
6950 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006951 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006952 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006953 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6954 (size_t)i);
6955 ga.ga_len += i;
6956 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006957 continue;
6958 }
6959 zero_width = regmatch.startp[0];
6960 }
6961
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 /*
6963 * Get some space for a temporary buffer to do the substitution
6964 * into. It will contain:
6965 * - The text up to where the match is.
6966 * - The substituted text.
6967 * - The text after the match.
6968 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006969 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006970 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6972 {
6973 ga_clear(&ga);
6974 break;
6975 }
6976
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006977 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 i = (int)(regmatch.startp[0] - tail);
6979 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006980 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01006981 (void)vim_regsub(&regmatch, sub, expr,
6982 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
6983 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006985 tail = regmatch.endp[0];
6986 if (*tail == NUL)
6987 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 if (!do_all)
6989 break;
6990 }
6991
6992 if (ga.ga_data != NULL)
6993 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6994
Bram Moolenaar473de612013-06-08 18:19:48 +02006995 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 }
6997
6998 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6999 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007000 if (p_cpo == empty_option)
7001 p_cpo = save_cpo;
7002 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007003 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007004 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007005 // If it's still empty it was changed and restored, need to restore in
7006 // the complicated way.
7007 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007008 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007009 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
7012 return ret;
7013}