blob: 4033c07ccd734f6d73302f822ab8400ab86c501f [file] [log] [blame]
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
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 * typval.c: functions that deal with a typval
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
18/*
19 * Allocate memory for a variable type-value, and make it empty (0 or NULL
20 * value).
21 */
22 typval_T *
23alloc_tv(void)
24{
25 return ALLOC_CLEAR_ONE(typval_T);
26}
27
28/*
29 * Allocate memory for a variable type-value, and assign a string to it.
30 * The string "s" must have been allocated, it is consumed.
31 * Return NULL for out of memory, the variable otherwise.
32 */
33 typval_T *
34alloc_string_tv(char_u *s)
35{
36 typval_T *rettv;
37
38 rettv = alloc_tv();
39 if (rettv != NULL)
40 {
41 rettv->v_type = VAR_STRING;
42 rettv->vval.v_string = s;
43 }
44 else
45 vim_free(s);
46 return rettv;
47}
48
49/*
50 * Free the memory for a variable type-value.
51 */
52 void
53free_tv(typval_T *varp)
54{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +000055 if (varp == NULL)
56 return;
Bram Moolenaar00b28d62022-12-08 15:32:33 +000057
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +000058 switch (varp->v_type)
59 {
60 case VAR_FUNC:
61 func_unref(varp->vval.v_string);
62 // FALLTHROUGH
63 case VAR_STRING:
64 vim_free(varp->vval.v_string);
65 break;
66 case VAR_PARTIAL:
67 partial_unref(varp->vval.v_partial);
68 break;
69 case VAR_BLOB:
70 blob_unref(varp->vval.v_blob);
71 break;
72 case VAR_LIST:
73 list_unref(varp->vval.v_list);
74 break;
75 case VAR_DICT:
76 dict_unref(varp->vval.v_dict);
77 break;
78 case VAR_JOB:
79#ifdef FEAT_JOB_CHANNEL
80 job_unref(varp->vval.v_job);
81 break;
82#endif
83 case VAR_CHANNEL:
84#ifdef FEAT_JOB_CHANNEL
85 channel_unref(varp->vval.v_channel);
86 break;
87#endif
88 case VAR_CLASS:
89 class_unref(varp->vval.v_class);
90 break;
91 case VAR_OBJECT:
92 object_unref(varp->vval.v_object);
93 break;
94
95 case VAR_NUMBER:
96 case VAR_FLOAT:
97 case VAR_ANY:
98 case VAR_UNKNOWN:
99 case VAR_VOID:
100 case VAR_BOOL:
101 case VAR_SPECIAL:
102 case VAR_INSTR:
103 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200104 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000105 vim_free(varp);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200106}
107
108/*
109 * Free the memory for a variable value and set the value to NULL or 0.
110 */
111 void
112clear_tv(typval_T *varp)
113{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000114 if (varp == NULL)
115 return;
116
117 switch (varp->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200118 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000119 case VAR_FUNC:
120 func_unref(varp->vval.v_string);
121 // FALLTHROUGH
122 case VAR_STRING:
123 VIM_CLEAR(varp->vval.v_string);
124 break;
125 case VAR_PARTIAL:
126 partial_unref(varp->vval.v_partial);
127 varp->vval.v_partial = NULL;
128 break;
129 case VAR_BLOB:
130 blob_unref(varp->vval.v_blob);
131 varp->vval.v_blob = NULL;
132 break;
133 case VAR_LIST:
134 list_unref(varp->vval.v_list);
135 varp->vval.v_list = NULL;
136 break;
137 case VAR_DICT:
138 dict_unref(varp->vval.v_dict);
139 varp->vval.v_dict = NULL;
140 break;
141 case VAR_NUMBER:
142 case VAR_BOOL:
143 case VAR_SPECIAL:
144 varp->vval.v_number = 0;
145 break;
146 case VAR_FLOAT:
147 varp->vval.v_float = 0.0;
148 break;
149 case VAR_JOB:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200150#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000151 job_unref(varp->vval.v_job);
152 varp->vval.v_job = NULL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200153#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000154 break;
155 case VAR_CHANNEL:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200156#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000157 channel_unref(varp->vval.v_channel);
158 varp->vval.v_channel = NULL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200159#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000160 break;
161 case VAR_INSTR:
162 VIM_CLEAR(varp->vval.v_instr);
163 break;
164 case VAR_CLASS:
165 class_unref(varp->vval.v_class);
166 varp->vval.v_class = NULL;
167 break;
168 case VAR_OBJECT:
169 object_unref(varp->vval.v_object);
170 varp->vval.v_object = NULL;
171 break;
172 case VAR_UNKNOWN:
173 case VAR_ANY:
174 case VAR_VOID:
175 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200176 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000177 varp->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200178}
179
180/*
181 * Set the value of a variable to NULL without freeing items.
182 */
183 void
184init_tv(typval_T *varp)
185{
186 if (varp != NULL)
187 CLEAR_POINTER(varp);
188}
189
Bram Moolenaar36967b32020-08-17 21:41:02 +0200190 static varnumber_T
191tv_get_bool_or_number_chk(typval_T *varp, int *denote, int want_bool)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200192{
193 varnumber_T n = 0L;
194
195 switch (varp->v_type)
196 {
197 case VAR_NUMBER:
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200198 if (in_vim9script() && want_bool && varp->vval.v_number != 0
Bram Moolenaard70840e2020-08-22 15:06:35 +0200199 && varp->vval.v_number != 1)
200 {
201 semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
202 break;
203 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200204 return varp->vval.v_number;
205 case VAR_FLOAT:
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000206 emsg(_(e_using_float_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200207 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200208 case VAR_FUNC:
209 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000210 emsg(_(e_using_funcref_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200211 break;
212 case VAR_STRING:
Bram Moolenaar56acb092020-08-16 14:48:19 +0200213 if (in_vim9script())
214 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100215 emsg_using_string_as(varp, !want_bool);
Bram Moolenaar56acb092020-08-16 14:48:19 +0200216 break;
217 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200218 if (varp->vval.v_string != NULL)
219 vim_str2nr(varp->vval.v_string, NULL, NULL,
220 STR2NR_ALL, &n, NULL, 0, FALSE);
221 return n;
222 case VAR_LIST:
Bram Moolenaar677658a2022-01-05 16:09:06 +0000223 emsg(_(e_using_list_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200224 break;
225 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000226 emsg(_(e_using_dictionary_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200227 break;
228 case VAR_BOOL:
229 case VAR_SPECIAL:
Bram Moolenaar36967b32020-08-17 21:41:02 +0200230 if (!want_bool && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200231 {
Bram Moolenaard92cc132020-11-18 17:17:15 +0100232 if (varp->v_type == VAR_BOOL)
233 emsg(_(e_using_bool_as_number));
234 else
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000235 emsg(_(e_using_special_as_number));
Bram Moolenaar56acb092020-08-16 14:48:19 +0200236 break;
237 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200238 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
239 case VAR_JOB:
240#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000241 emsg(_(e_using_job_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200242 break;
243#endif
244 case VAR_CHANNEL:
245#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000246 emsg(_(e_using_channel_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200247 break;
248#endif
249 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000250 emsg(_(e_using_blob_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200251 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000252 case VAR_CLASS:
253 emsg(_(e_using_class_as_number));
254 break;
255 case VAR_OBJECT:
256 emsg(_(e_using_object_as_number));
257 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200258 case VAR_VOID:
259 emsg(_(e_cannot_use_void_value));
260 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200261 case VAR_UNKNOWN:
262 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200263 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200264 internal_error_no_abort("tv_get_number(UNKNOWN)");
265 break;
266 }
267 if (denote == NULL) // useful for values that must be unsigned
268 n = -1;
269 else
270 *denote = TRUE;
271 return n;
272}
273
Bram Moolenaar36967b32020-08-17 21:41:02 +0200274/*
275 * Get the number value of a variable.
276 * If it is a String variable, uses vim_str2nr().
277 * For incompatible types, return 0.
278 * tv_get_number_chk() is similar to tv_get_number(), but informs the
279 * caller of incompatible types: it sets *denote to TRUE if "denote"
280 * is not NULL or returns -1 otherwise.
281 */
282 varnumber_T
283tv_get_number(typval_T *varp)
284{
285 int error = FALSE;
286
287 return tv_get_number_chk(varp, &error); // return 0L on error
288}
289
290 varnumber_T
291tv_get_number_chk(typval_T *varp, int *denote)
292{
293 return tv_get_bool_or_number_chk(varp, denote, FALSE);
294}
295
296/*
297 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200298 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200299 */
300 varnumber_T
301tv_get_bool(typval_T *varp)
302{
303 return tv_get_bool_or_number_chk(varp, NULL, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200304}
305
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200306/*
307 * Get the boolean value of "varp". This is like tv_get_number_chk(),
308 * but in Vim9 script accepts Number and Bool.
309 */
310 varnumber_T
311tv_get_bool_chk(typval_T *varp, int *denote)
312{
313 return tv_get_bool_or_number_chk(varp, denote, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200314}
315
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000316 static float_T
317tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200318{
319 switch (varp->v_type)
320 {
321 case VAR_NUMBER:
322 return (float_T)(varp->vval.v_number);
323 case VAR_FLOAT:
324 return varp->vval.v_float;
325 case VAR_FUNC:
326 case VAR_PARTIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000327 emsg(_(e_using_funcref_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200328 break;
329 case VAR_STRING:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000330 emsg(_(e_using_string_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200331 break;
332 case VAR_LIST:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000333 emsg(_(e_using_list_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200334 break;
335 case VAR_DICT:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000336 emsg(_(e_using_dictionary_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200337 break;
338 case VAR_BOOL:
Bram Moolenaarb2810f12022-01-08 21:38:52 +0000339 emsg(_(e_using_boolean_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200340 break;
341 case VAR_SPECIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000342 emsg(_(e_using_special_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200343 break;
344 case VAR_JOB:
345# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000346 emsg(_(e_using_job_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200347 break;
348# endif
349 case VAR_CHANNEL:
350# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000351 emsg(_(e_using_channel_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200352 break;
353# endif
354 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000355 emsg(_(e_using_blob_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200356 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000357 case VAR_CLASS:
358 emsg(_(e_using_class_as_float));
359 break;
360 case VAR_OBJECT:
361 emsg(_(e_using_object_as_float));
362 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200363 case VAR_VOID:
364 emsg(_(e_cannot_use_void_value));
365 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200366 case VAR_UNKNOWN:
367 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200368 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200369 internal_error_no_abort("tv_get_float(UNKNOWN)");
370 break;
371 }
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000372 if (error != NULL)
373 *error = TRUE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200374 return 0;
375}
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000376
377 float_T
378tv_get_float(typval_T *varp)
379{
380 return tv_get_float_chk(varp, NULL);
381}
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200382
383/*
Ernie Rael51d04d12022-05-04 15:40:22 +0100384 * Give an error and return FAIL unless "args[idx]" is unknown
385 */
386 int
387check_for_unknown_arg(typval_T *args, int idx)
388{
389 if (args[idx].v_type != VAR_UNKNOWN)
390 {
391 semsg(_(e_too_many_arguments), idx + 1);
392 return FAIL;
393 }
394 return OK;
395}
396
397/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200398 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100399 */
400 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100401check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100402{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100403 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100404 {
rbtnnddc80af2021-12-17 18:01:31 +0000405 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100406 return FAIL;
407 }
408 return OK;
409}
410
411/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100412 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100413 */
414 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100415check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100416{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100417 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100418 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100419 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100420 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200421 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100422 return FAIL;
423 }
424 return OK;
425}
426
427/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200428 * Check for an optional string argument at 'idx'
429 */
430 int
431check_for_opt_string_arg(typval_T *args, int idx)
432{
433 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100434 || check_for_string_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200435}
436
437/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200438 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200439 */
440 int
441check_for_number_arg(typval_T *args, int idx)
442{
443 if (args[idx].v_type != VAR_NUMBER)
444 {
rbtnnddc80af2021-12-17 18:01:31 +0000445 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200446 return FAIL;
447 }
448 return OK;
449}
450
451/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200452 * Check for an optional number argument at 'idx'
453 */
454 int
455check_for_opt_number_arg(typval_T *args, int idx)
456{
457 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100458 || check_for_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200459}
460
461/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200462 * Give an error and return FAIL unless "args[idx]" is a float or a number.
463 */
464 int
465check_for_float_or_nr_arg(typval_T *args, int idx)
466{
467 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
468 {
rbtnnddc80af2021-12-17 18:01:31 +0000469 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200470 return FAIL;
471 }
472 return OK;
473}
474
475/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200476 * Give an error and return FAIL unless "args[idx]" is a bool.
477 */
478 int
479check_for_bool_arg(typval_T *args, int idx)
480{
481 if (args[idx].v_type != VAR_BOOL
482 && !(args[idx].v_type == VAR_NUMBER
483 && (args[idx].vval.v_number == 0
484 || args[idx].vval.v_number == 1)))
485 {
rbtnnddc80af2021-12-17 18:01:31 +0000486 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200487 return FAIL;
488 }
489 return OK;
490}
491
492/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200493 * Check for an optional bool argument at 'idx'.
494 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200495 */
496 int
497check_for_opt_bool_arg(typval_T *args, int idx)
498{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200499 if (args[idx].v_type == VAR_UNKNOWN)
500 return OK;
501 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200502}
503
504/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200505 * Give an error and return FAIL unless "args[idx]" is a blob.
506 */
507 int
508check_for_blob_arg(typval_T *args, int idx)
509{
510 if (args[idx].v_type != VAR_BLOB)
511 {
rbtnnddc80af2021-12-17 18:01:31 +0000512 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200513 return FAIL;
514 }
515 return OK;
516}
517
518/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200519 * Give an error and return FAIL unless "args[idx]" is a list.
520 */
521 int
522check_for_list_arg(typval_T *args, int idx)
523{
524 if (args[idx].v_type != VAR_LIST)
525 {
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200526 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200527 return FAIL;
528 }
529 return OK;
530}
531
532/*
Bram Moolenaard83392a2022-09-01 12:22:46 +0100533 * Give an error and return FAIL unless "args[idx]" is a non-NULL list.
534 */
535 int
536check_for_nonnull_list_arg(typval_T *args, int idx)
537{
538 if (check_for_list_arg(args, idx) == FAIL)
539 return FAIL;
540
541 if (args[idx].vval.v_list == NULL)
542 {
543 semsg(_(e_non_null_list_required_for_argument_nr), idx + 1);
544 return FAIL;
545 }
546 return OK;
547}
548
549/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200550 * Check for an optional list argument at 'idx'
551 */
552 int
553check_for_opt_list_arg(typval_T *args, int idx)
554{
555 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100556 || check_for_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200557}
558
559/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200560 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200561 */
562 int
563check_for_dict_arg(typval_T *args, int idx)
564{
565 if (args[idx].v_type != VAR_DICT)
566 {
rbtnnddc80af2021-12-17 18:01:31 +0000567 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200568 return FAIL;
569 }
570 return OK;
571}
572
573/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100574 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
575 */
576 int
577check_for_nonnull_dict_arg(typval_T *args, int idx)
578{
579 if (check_for_dict_arg(args, idx) == FAIL)
580 return FAIL;
581
582 if (args[idx].vval.v_dict == NULL)
583 {
584 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
585 return FAIL;
586 }
587 return OK;
588}
589
590/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200591 * Check for an optional dict argument at 'idx'
592 */
593 int
594check_for_opt_dict_arg(typval_T *args, int idx)
595{
596 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100597 || check_for_dict_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200598}
599
Dominique Pelle748b3082022-01-08 12:41:16 +0000600#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200601/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200602 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
603 */
604 int
605check_for_chan_or_job_arg(typval_T *args, int idx)
606{
607 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
608 {
rbtnnddc80af2021-12-17 18:01:31 +0000609 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200610 return FAIL;
611 }
612 return OK;
613}
614
615/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200616 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
617 * job.
618 */
619 int
620check_for_opt_chan_or_job_arg(typval_T *args, int idx)
621{
622 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100623 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200624}
625
626/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200627 * Give an error and return FAIL unless "args[idx]" is a job.
628 */
629 int
630check_for_job_arg(typval_T *args, int idx)
631{
632 if (args[idx].v_type != VAR_JOB)
633 {
rbtnnddc80af2021-12-17 18:01:31 +0000634 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200635 return FAIL;
636 }
637 return OK;
638}
639
640/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200641 * Check for an optional job argument at 'idx'.
642 */
643 int
644check_for_opt_job_arg(typval_T *args, int idx)
645{
646 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100647 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200648}
Bram Moolenaar3b8c7082022-11-30 20:20:56 +0000649#else
650/*
651 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
652 * job. Used without the +channel feature, thus only VAR_UNKNOWN is accepted.
653 */
654 int
655check_for_opt_chan_or_job_arg(typval_T *args, int idx)
656{
657 return args[idx].v_type == VAR_UNKNOWN ? OK : FAIL;
658}
Dominique Pelle748b3082022-01-08 12:41:16 +0000659#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200660
661/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200662 * Give an error and return FAIL unless "args[idx]" is a string or
663 * a number.
664 */
665 int
666check_for_string_or_number_arg(typval_T *args, int idx)
667{
668 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
669 {
rbtnnddc80af2021-12-17 18:01:31 +0000670 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200671 return FAIL;
672 }
673 return OK;
674}
675
676/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200677 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200678 */
679 int
680check_for_opt_string_or_number_arg(typval_T *args, int idx)
681{
682 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100683 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200684}
685
686/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200687 * Give an error and return FAIL unless "args[idx]" is a buffer number.
688 * Buffer number can be a number or a string.
689 */
690 int
691check_for_buffer_arg(typval_T *args, int idx)
692{
693 return check_for_string_or_number_arg(args, idx);
694}
695
696/*
697 * Check for an optional buffer argument at 'idx'
698 */
699 int
700check_for_opt_buffer_arg(typval_T *args, int idx)
701{
702 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100703 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200704}
705
706/*
707 * Give an error and return FAIL unless "args[idx]" is a line number.
708 * Line number can be a number or a string.
709 */
710 int
711check_for_lnum_arg(typval_T *args, int idx)
712{
713 return check_for_string_or_number_arg(args, idx);
714}
715
716/*
717 * Check for an optional line number argument at 'idx'
718 */
719 int
720check_for_opt_lnum_arg(typval_T *args, int idx)
721{
722 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100723 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200724}
725
Dominique Pelle748b3082022-01-08 12:41:16 +0000726#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200727/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200728 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200729 */
730 int
731check_for_string_or_blob_arg(typval_T *args, int idx)
732{
733 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
734 {
rbtnnddc80af2021-12-17 18:01:31 +0000735 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200736 return FAIL;
737 }
738 return OK;
739}
Dominique Pelle748b3082022-01-08 12:41:16 +0000740#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200741
742/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200743 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200744 */
745 int
746check_for_string_or_list_arg(typval_T *args, int idx)
747{
748 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
749 {
rbtnnddc80af2021-12-17 18:01:31 +0000750 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200751 return FAIL;
752 }
753 return OK;
754}
755
756/*
rbtnn0ccb5842021-12-18 18:33:46 +0000757 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
758 * blob.
759 */
760 int
761check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
762{
763 if (args[idx].v_type != VAR_STRING
764 && args[idx].v_type != VAR_LIST
765 && args[idx].v_type != VAR_BLOB)
766 {
767 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
768 return FAIL;
769 }
770 return OK;
771}
772
773/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200774 * Check for an optional string or list argument at 'idx'
775 */
776 int
777check_for_opt_string_or_list_arg(typval_T *args, int idx)
778{
779 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100780 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200781}
782
783/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200784 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
785 */
786 int
787check_for_string_or_dict_arg(typval_T *args, int idx)
788{
789 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
790 {
rbtnnddc80af2021-12-17 18:01:31 +0000791 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200792 return FAIL;
793 }
794 return OK;
795}
796
797/*
798 * Give an error and return FAIL unless "args[idx]" is a string or a number
799 * or a list.
800 */
801 int
802check_for_string_or_number_or_list_arg(typval_T *args, int idx)
803{
804 if (args[idx].v_type != VAR_STRING
805 && args[idx].v_type != VAR_NUMBER
806 && args[idx].v_type != VAR_LIST)
807 {
rbtnn0ccb5842021-12-18 18:33:46 +0000808 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200809 return FAIL;
810 }
811 return OK;
812}
813
814/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200815 * Give an error and return FAIL unless "args[idx]" is an optional string
816 * or number or a list
817 */
818 int
819check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
820{
821 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100822 || check_for_string_or_number_or_list_arg(args, idx)
823 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200824}
825
826/*
Bakudankun375141e2022-09-09 18:46:47 +0100827 * Give an error and return FAIL unless "args[idx]" is a string or a number
828 * or a list or a blob.
829 */
830 int
831check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
832{
833 if (args[idx].v_type != VAR_STRING
834 && args[idx].v_type != VAR_NUMBER
835 && args[idx].v_type != VAR_LIST
836 && args[idx].v_type != VAR_BLOB)
837 {
838 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
839 return FAIL;
840 }
841 return OK;
842}
843
844/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200845 * Give an error and return FAIL unless "args[idx]" is a string or a list
846 * or a dict.
847 */
848 int
849check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
850{
851 if (args[idx].v_type != VAR_STRING
852 && args[idx].v_type != VAR_LIST
853 && args[idx].v_type != VAR_DICT)
854 {
rbtnnddc80af2021-12-17 18:01:31 +0000855 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200856 return FAIL;
857 }
858 return OK;
859}
860
861/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000862 * Give an error and return FAIL unless "args[idx]" is a string
863 * or a function reference.
864 */
865 int
866check_for_string_or_func_arg(typval_T *args, int idx)
867{
868 if (args[idx].v_type != VAR_PARTIAL
869 && args[idx].v_type != VAR_FUNC
870 && args[idx].v_type != VAR_STRING)
871 {
872 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
873 return FAIL;
874 }
875 return OK;
876}
877
878/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200879 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
880 */
881 int
882check_for_list_or_blob_arg(typval_T *args, int idx)
883{
884 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
885 {
rbtnn0ccb5842021-12-18 18:33:46 +0000886 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200887 return FAIL;
888 }
889 return OK;
890}
891
892/*
893 * Give an error and return FAIL unless "args[idx]" is a list or dict
894 */
895 int
896check_for_list_or_dict_arg(typval_T *args, int idx)
897{
898 if (args[idx].v_type != VAR_LIST
899 && args[idx].v_type != VAR_DICT)
900 {
rbtnnddc80af2021-12-17 18:01:31 +0000901 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200902 return FAIL;
903 }
904 return OK;
905}
906
907/*
908 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
909 * blob.
910 */
911 int
912check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
913{
914 if (args[idx].v_type != VAR_LIST
915 && args[idx].v_type != VAR_DICT
916 && args[idx].v_type != VAR_BLOB)
917 {
rbtnnddc80af2021-12-17 18:01:31 +0000918 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200919 return FAIL;
920 }
921 return OK;
922}
923
924/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000925 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
926 * blob or a string.
927 */
928 int
929check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
930{
931 if (args[idx].v_type != VAR_LIST
932 && args[idx].v_type != VAR_DICT
933 && args[idx].v_type != VAR_BLOB
934 && args[idx].v_type != VAR_STRING)
935 {
rbtnnddc80af2021-12-17 18:01:31 +0000936 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000937 return FAIL;
938 }
939 return OK;
940}
941
942/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200943 * Give an error and return FAIL unless "args[idx]" is an optional buffer
944 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200945 */
946 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200947check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200948{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200949 if (args[idx].v_type != VAR_UNKNOWN
950 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200951 && args[idx].v_type != VAR_NUMBER
952 && args[idx].v_type != VAR_DICT)
953 {
rbtnnddc80af2021-12-17 18:01:31 +0000954 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200955 return FAIL;
956 }
957 return OK;
958}
959
960/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200961 * Get the string value of a variable.
962 * If it is a Number variable, the number is converted into a string.
963 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
964 * tv_get_string_buf() uses a given buffer.
965 * If the String variable has never been set, return an empty string.
966 * Never returns NULL;
967 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
968 * NULL on error.
969 */
970 char_u *
971tv_get_string(typval_T *varp)
972{
973 static char_u mybuf[NUMBUFLEN];
974
975 return tv_get_string_buf(varp, mybuf);
976}
977
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100978/*
979 * Like tv_get_string() but don't allow number to string conversion for Vim9.
980 */
981 char_u *
982tv_get_string_strict(typval_T *varp)
983{
984 static char_u mybuf[NUMBUFLEN];
985 char_u *res = tv_get_string_buf_chk_strict(
986 varp, mybuf, in_vim9script());
987
988 return res != NULL ? res : (char_u *)"";
989}
990
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200991 char_u *
992tv_get_string_buf(typval_T *varp, char_u *buf)
993{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200994 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200995
996 return res != NULL ? res : (char_u *)"";
997}
998
999/*
1000 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1001 */
1002 char_u *
1003tv_get_string_chk(typval_T *varp)
1004{
1005 static char_u mybuf[NUMBUFLEN];
1006
1007 return tv_get_string_buf_chk(varp, mybuf);
1008}
1009
1010 char_u *
1011tv_get_string_buf_chk(typval_T *varp, char_u *buf)
1012{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001013 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
1014}
1015
1016 char_u *
1017tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
1018{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001019 switch (varp->v_type)
1020 {
1021 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001022 if (strict)
1023 {
1024 emsg(_(e_using_number_as_string));
1025 break;
1026 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001027 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1028 (varnumber_T)varp->vval.v_number);
1029 return buf;
1030 case VAR_FUNC:
1031 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001032 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001033 break;
1034 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001035 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001036 break;
1037 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001038 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001039 break;
1040 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001041 if (strict)
1042 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001043 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001044 break;
1045 }
1046 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1047 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001048 case VAR_STRING:
1049 if (varp->vval.v_string != NULL)
1050 return varp->vval.v_string;
1051 return (char_u *)"";
1052 case VAR_BOOL:
1053 case VAR_SPECIAL:
1054 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1055 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001056 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001057 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001058 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001059 case VAR_CLASS:
1060 emsg(_(e_using_class_as_string));
1061 break;
1062 case VAR_OBJECT:
1063 emsg(_(e_using_object_as_string));
1064 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001065 case VAR_JOB:
1066#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001067 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001068 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001069 semsg(_(e_using_invalid_value_as_string_str), "job");
1070 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001071 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001072 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001073#endif
1074 break;
1075 case VAR_CHANNEL:
1076#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001077 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001078 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001079 semsg(_(e_using_invalid_value_as_string_str), "channel");
1080 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001081 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001082 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001083#endif
1084 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001085 case VAR_VOID:
1086 emsg(_(e_cannot_use_void_value));
1087 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001088 case VAR_UNKNOWN:
1089 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001090 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001091 semsg(_(e_using_invalid_value_as_string_str),
1092 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001093 break;
1094 }
1095 return NULL;
1096}
1097
1098/*
1099 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1100 * string() on Dict, List, etc.
1101 */
1102 char_u *
1103tv_stringify(typval_T *varp, char_u *buf)
1104{
1105 if (varp->v_type == VAR_LIST
1106 || varp->v_type == VAR_DICT
1107 || varp->v_type == VAR_BLOB
1108 || varp->v_type == VAR_FUNC
1109 || varp->v_type == VAR_PARTIAL
1110 || varp->v_type == VAR_FLOAT)
1111 {
1112 typval_T tmp;
1113
1114 f_string(varp, &tmp);
1115 tv_get_string_buf(&tmp, buf);
1116 clear_tv(varp);
1117 *varp = tmp;
1118 return tmp.vval.v_string;
1119 }
1120 return tv_get_string_buf(varp, buf);
1121}
1122
1123/*
1124 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1125 * Also give an error message, using "name" or _("name") when use_gettext is
1126 * TRUE.
1127 */
1128 int
1129tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1130{
1131 int lock = 0;
1132
1133 switch (tv->v_type)
1134 {
1135 case VAR_BLOB:
1136 if (tv->vval.v_blob != NULL)
1137 lock = tv->vval.v_blob->bv_lock;
1138 break;
1139 case VAR_LIST:
1140 if (tv->vval.v_list != NULL)
1141 lock = tv->vval.v_list->lv_lock;
1142 break;
1143 case VAR_DICT:
1144 if (tv->vval.v_dict != NULL)
1145 lock = tv->vval.v_dict->dv_lock;
1146 break;
1147 default:
1148 break;
1149 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001150 return value_check_lock(tv->v_lock, name, use_gettext)
1151 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001152}
1153
1154/*
1155 * Copy the values from typval_T "from" to typval_T "to".
1156 * When needed allocates string or increases reference count.
1157 * Does not make a copy of a list, blob or dict but copies the reference!
1158 * It is OK for "from" and "to" to point to the same item. This is used to
1159 * make a copy later.
1160 */
1161 void
1162copy_tv(typval_T *from, typval_T *to)
1163{
1164 to->v_type = from->v_type;
1165 to->v_lock = 0;
1166 switch (from->v_type)
1167 {
1168 case VAR_NUMBER:
1169 case VAR_BOOL:
1170 case VAR_SPECIAL:
1171 to->vval.v_number = from->vval.v_number;
1172 break;
1173 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001174 to->vval.v_float = from->vval.v_float;
1175 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001176 case VAR_JOB:
1177#ifdef FEAT_JOB_CHANNEL
1178 to->vval.v_job = from->vval.v_job;
1179 if (to->vval.v_job != NULL)
1180 ++to->vval.v_job->jv_refcount;
1181 break;
1182#endif
1183 case VAR_CHANNEL:
1184#ifdef FEAT_JOB_CHANNEL
1185 to->vval.v_channel = from->vval.v_channel;
1186 if (to->vval.v_channel != NULL)
1187 ++to->vval.v_channel->ch_refcount;
1188 break;
1189#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001190 case VAR_INSTR:
1191 to->vval.v_instr = from->vval.v_instr;
1192 break;
1193
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001194 case VAR_CLASS:
1195 copy_class(from, to);
1196 break;
1197
1198 case VAR_OBJECT:
1199 copy_object(from, to);
1200 break;
1201
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001202 case VAR_STRING:
1203 case VAR_FUNC:
1204 if (from->vval.v_string == NULL)
1205 to->vval.v_string = NULL;
1206 else
1207 {
1208 to->vval.v_string = vim_strsave(from->vval.v_string);
1209 if (from->v_type == VAR_FUNC)
1210 func_ref(to->vval.v_string);
1211 }
1212 break;
1213 case VAR_PARTIAL:
1214 if (from->vval.v_partial == NULL)
1215 to->vval.v_partial = NULL;
1216 else
1217 {
1218 to->vval.v_partial = from->vval.v_partial;
1219 ++to->vval.v_partial->pt_refcount;
1220 }
1221 break;
1222 case VAR_BLOB:
1223 if (from->vval.v_blob == NULL)
1224 to->vval.v_blob = NULL;
1225 else
1226 {
1227 to->vval.v_blob = from->vval.v_blob;
1228 ++to->vval.v_blob->bv_refcount;
1229 }
1230 break;
1231 case VAR_LIST:
1232 if (from->vval.v_list == NULL)
1233 to->vval.v_list = NULL;
1234 else
1235 {
1236 to->vval.v_list = from->vval.v_list;
1237 ++to->vval.v_list->lv_refcount;
1238 }
1239 break;
1240 case VAR_DICT:
1241 if (from->vval.v_dict == NULL)
1242 to->vval.v_dict = NULL;
1243 else
1244 {
1245 to->vval.v_dict = from->vval.v_dict;
1246 ++to->vval.v_dict->dv_refcount;
1247 }
1248 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001249 case VAR_VOID:
1250 emsg(_(e_cannot_use_void_value));
1251 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001252 case VAR_UNKNOWN:
1253 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001254 internal_error_no_abort("copy_tv(UNKNOWN)");
1255 break;
1256 }
1257}
1258
1259/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001260 * Compare "tv1" and "tv2".
1261 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001262 */
1263 int
1264typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001265 typval_T *tv1, // first operand
1266 typval_T *tv2, // second operand
1267 exprtype_T type, // operator
1268 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001269{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001270 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001271 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001272 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1273
Bram Moolenaar265f8112021-12-19 12:33:05 +00001274 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001275 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001276 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001277 // it means TRUE.
1278 n1 = (type == EXPR_ISNOT);
1279 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001280 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1281 || (tv2->v_type == VAR_SPECIAL
1282 && tv2->vval.v_number == VVAL_NULL))
1283 && tv1->v_type != tv2->v_type
1284 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1285 {
1286 n1 = typval_compare_null(tv1, tv2);
1287 if (n1 == MAYBE)
1288 {
1289 clear_tv(tv1);
1290 return FAIL;
1291 }
1292 if (type == EXPR_NEQUAL)
1293 n1 = !n1;
1294 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001295 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001296 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001297 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001298 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001299 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001300 return FAIL;
1301 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001302 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001303 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001304 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001305 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001306 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001307 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001308 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001309 return FAIL;
1310 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001311 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001312 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001313 else if (tv1->v_type == VAR_CLASS || tv2->v_type == VAR_CLASS)
1314 {
1315 if (typval_compare_class(tv1, tv2, type, ic, &res) == FAIL)
1316 {
1317 clear_tv(tv1);
1318 return FAIL;
1319 }
1320 n1 = res;
1321 }
1322 else if (tv1->v_type == VAR_OBJECT || tv2->v_type == VAR_OBJECT)
1323 {
1324 if (typval_compare_object(tv1, tv2, type, ic, &res) == FAIL)
1325 {
1326 clear_tv(tv1);
1327 return FAIL;
1328 }
1329 n1 = res;
1330 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001331 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001332 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001333 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001334 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001335 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001336 return FAIL;
1337 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001338 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001339 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001340 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1341 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001342 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001343 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001344 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001345 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001346 return FAIL;
1347 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001348 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001349 }
1350
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001351 // If one of the two variables is a float, compare as a float.
1352 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001353 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001354 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1355 {
1356 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001357 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001358
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001359 f1 = tv_get_float_chk(tv1, &error);
1360 if (!error)
1361 f2 = tv_get_float_chk(tv2, &error);
1362 if (error)
1363 {
1364 clear_tv(tv1);
1365 return FAIL;
1366 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001367 n1 = FALSE;
1368 switch (type)
1369 {
1370 case EXPR_IS:
1371 case EXPR_EQUAL: n1 = (f1 == f2); break;
1372 case EXPR_ISNOT:
1373 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1374 case EXPR_GREATER: n1 = (f1 > f2); break;
1375 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1376 case EXPR_SMALLER: n1 = (f1 < f2); break;
1377 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1378 case EXPR_UNKNOWN:
1379 case EXPR_MATCH:
1380 default: break; // avoid gcc warning
1381 }
1382 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001383
1384 // If one of the two variables is a number, compare as a number.
1385 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001386 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001387 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1388 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001389 int error = FALSE;
1390
1391 n1 = tv_get_number_chk(tv1, &error);
1392 if (!error)
1393 n2 = tv_get_number_chk(tv2, &error);
1394 if (error)
1395 {
1396 clear_tv(tv1);
1397 return FAIL;
1398 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001399 switch (type)
1400 {
1401 case EXPR_IS:
1402 case EXPR_EQUAL: n1 = (n1 == n2); break;
1403 case EXPR_ISNOT:
1404 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1405 case EXPR_GREATER: n1 = (n1 > n2); break;
1406 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1407 case EXPR_SMALLER: n1 = (n1 < n2); break;
1408 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1409 case EXPR_UNKNOWN:
1410 case EXPR_MATCH:
1411 default: break; // avoid gcc warning
1412 }
1413 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001414 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1415 || tv2->v_type == VAR_BOOL
1416 || (tv1->v_type == VAR_SPECIAL
1417 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001418 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001419 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001420 {
1421 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001422 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1423 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001424 return FAIL;
1425 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001426 n1 = tv1->vval.v_number;
1427 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001428 switch (type)
1429 {
1430 case EXPR_IS:
1431 case EXPR_EQUAL: n1 = (n1 == n2); break;
1432 case EXPR_ISNOT:
1433 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1434 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001435 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001436 vartype_name(tv1->v_type));
1437 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001438 return FAIL;
1439 }
1440 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001441#ifdef FEAT_JOB_CHANNEL
1442 else if (tv1->v_type == tv2->v_type
1443 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1444 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1445 {
1446 if (tv1->v_type == VAR_CHANNEL)
1447 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1448 else
1449 n1 = tv1->vval.v_job == tv2->vval.v_job;
1450 if (type == EXPR_NEQUAL)
1451 n1 = !n1;
1452 }
1453#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001454 else
1455 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001456 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001457 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001458 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001459 return FAIL;
1460 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001461 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001462 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001463 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001464 if (in_vim9script())
1465 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001466 tv1->v_type = VAR_BOOL;
1467 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001468 }
1469 else
1470 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001471 tv1->v_type = VAR_NUMBER;
1472 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001473 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001474
1475 return OK;
1476}
1477
Bram Moolenaar34453202021-01-31 13:08:38 +01001478/*
dundargocc57b5bc2022-11-02 13:30:51 +00001479 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001480 * Put the result, false or true, in "res".
1481 * Return FAIL and give an error message when the comparison can't be done.
1482 */
1483 int
1484typval_compare_list(
1485 typval_T *tv1,
1486 typval_T *tv2,
1487 exprtype_T type,
1488 int ic,
1489 int *res)
1490{
1491 int val = 0;
1492
1493 if (type == EXPR_IS || type == EXPR_ISNOT)
1494 {
1495 val = (tv1->v_type == tv2->v_type
1496 && tv1->vval.v_list == tv2->vval.v_list);
1497 if (type == EXPR_ISNOT)
1498 val = !val;
1499 }
1500 else if (tv1->v_type != tv2->v_type
1501 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1502 {
1503 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001504 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001505 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001506 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001507 return FAIL;
1508 }
1509 else
1510 {
1511 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1512 ic, FALSE);
1513 if (type == EXPR_NEQUAL)
1514 val = !val;
1515 }
1516 *res = val;
1517 return OK;
1518}
1519
1520/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001521 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001522 */
1523 int
1524typval_compare_null(typval_T *tv1, typval_T *tv2)
1525{
1526 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1527 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1528 {
1529 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1530
1531 switch (tv->v_type)
1532 {
1533 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001534#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001535 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001536#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001537 case VAR_DICT: return tv->vval.v_dict == NULL;
1538 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001539#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001540 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001541#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001542 case VAR_LIST: return tv->vval.v_list == NULL;
1543 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1544 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001545
1546 case VAR_NUMBER: if (!in_vim9script())
1547 return tv->vval.v_number == 0;
1548 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001549 case VAR_FLOAT: if (!in_vim9script())
1550 return tv->vval.v_float == 0.0;
1551 break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001552 default: break;
1553 }
1554 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001555 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001556 // we won't give an error
1557 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001558}
1559
1560/*
dundargocc57b5bc2022-11-02 13:30:51 +00001561 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001562 * Put the result, false or true, in "res".
1563 * Return FAIL and give an error message when the comparison can't be done.
1564 */
1565 int
1566typval_compare_blob(
1567 typval_T *tv1,
1568 typval_T *tv2,
1569 exprtype_T type,
1570 int *res)
1571{
1572 int val = 0;
1573
1574 if (type == EXPR_IS || type == EXPR_ISNOT)
1575 {
1576 val = (tv1->v_type == tv2->v_type
1577 && tv1->vval.v_blob == tv2->vval.v_blob);
1578 if (type == EXPR_ISNOT)
1579 val = !val;
1580 }
1581 else if (tv1->v_type != tv2->v_type
1582 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1583 {
1584 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001585 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001586 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001587 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001588 return FAIL;
1589 }
1590 else
1591 {
1592 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1593 if (type == EXPR_NEQUAL)
1594 val = !val;
1595 }
1596 *res = val;
1597 return OK;
1598}
1599
1600/*
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001601 * Compare "tv1" to "tv2" as classes according to "type".
1602 * Put the result, false or true, in "res".
1603 * Return FAIL and give an error message when the comparison can't be done.
1604 */
1605 int
1606typval_compare_class(
1607 typval_T *tv1,
1608 typval_T *tv2,
1609 exprtype_T type UNUSED,
1610 int ic UNUSED,
1611 int *res)
1612{
1613 // TODO: use "type"
1614 *res = tv1->vval.v_class == tv2->vval.v_class;
1615 return OK;
1616}
1617
1618/*
1619 * Compare "tv1" to "tv2" as objects according to "type".
1620 * Put the result, false or true, in "res".
1621 * Return FAIL and give an error message when the comparison can't be done.
1622 */
1623 int
1624typval_compare_object(
1625 typval_T *tv1,
1626 typval_T *tv2,
1627 exprtype_T type,
1628 int ic,
1629 int *res)
1630{
1631 int res_match = type == EXPR_EQUAL || type == EXPR_IS ? TRUE : FALSE;
1632
1633 if (tv1->vval.v_object == NULL && tv2->vval.v_object == NULL)
1634 {
1635 *res = res_match;
1636 return OK;
1637 }
1638 if (tv1->vval.v_object == NULL || tv2->vval.v_object == NULL)
1639 {
1640 *res = !res_match;
1641 return OK;
1642 }
1643
1644 class_T *cl1 = tv1->vval.v_object->obj_class;
1645 class_T *cl2 = tv2->vval.v_object->obj_class;
1646 if (cl1 != cl2 || cl1 == NULL || cl2 == NULL)
1647 {
1648 *res = !res_match;
1649 return OK;
1650 }
1651
1652 object_T *obj1 = tv1->vval.v_object;
1653 object_T *obj2 = tv2->vval.v_object;
1654 if (type == EXPR_IS || type == EXPR_ISNOT)
1655 {
1656 *res = obj1 == obj2 ? res_match : !res_match;
1657 return OK;
1658 }
1659
1660 for (int i = 0; i < cl1->class_obj_member_count; ++i)
1661 if (!tv_equal((typval_T *)(obj1 + 1) + i,
1662 (typval_T *)(obj2 + 1) + i, ic, TRUE))
1663 {
1664 *res = !res_match;
1665 return OK;
1666 }
1667 *res = res_match;
1668 return OK;
1669}
1670
1671/*
dundargocc57b5bc2022-11-02 13:30:51 +00001672 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001673 * Put the result, false or true, in "res".
1674 * Return FAIL and give an error message when the comparison can't be done.
1675 */
1676 int
1677typval_compare_dict(
1678 typval_T *tv1,
1679 typval_T *tv2,
1680 exprtype_T type,
1681 int ic,
1682 int *res)
1683{
1684 int val;
1685
1686 if (type == EXPR_IS || type == EXPR_ISNOT)
1687 {
1688 val = (tv1->v_type == tv2->v_type
1689 && tv1->vval.v_dict == tv2->vval.v_dict);
1690 if (type == EXPR_ISNOT)
1691 val = !val;
1692 }
1693 else if (tv1->v_type != tv2->v_type
1694 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1695 {
1696 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001697 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001698 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001699 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001700 return FAIL;
1701 }
1702 else
1703 {
1704 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1705 if (type == EXPR_NEQUAL)
1706 val = !val;
1707 }
1708 *res = val;
1709 return OK;
1710}
1711
1712/*
dundargocc57b5bc2022-11-02 13:30:51 +00001713 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001714 * Put the result, false or true, in "res".
1715 * Return FAIL and give an error message when the comparison can't be done.
1716 */
1717 int
1718typval_compare_func(
1719 typval_T *tv1,
1720 typval_T *tv2,
1721 exprtype_T type,
1722 int ic,
1723 int *res)
1724{
1725 int val = 0;
1726
1727 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1728 && type != EXPR_IS && type != EXPR_ISNOT)
1729 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001730 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001731 return FAIL;
1732 }
1733 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1734 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1735 // When both partials are NULL, then they are equal.
1736 // Otherwise they are not equal.
1737 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1738 else if (type == EXPR_IS || type == EXPR_ISNOT)
1739 {
1740 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1741 // strings are considered the same if their value is
1742 // the same
1743 val = tv_equal(tv1, tv2, ic, FALSE);
1744 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1745 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1746 else
1747 val = FALSE;
1748 }
1749 else
1750 val = tv_equal(tv1, tv2, ic, FALSE);
1751 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1752 val = !val;
1753 *res = val;
1754 return OK;
1755}
1756
1757/*
1758 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1759 * Put the result, false or true, in "res".
1760 * Return FAIL and give an error message when the comparison can't be done.
1761 */
1762 int
1763typval_compare_string(
1764 typval_T *tv1,
1765 typval_T *tv2,
1766 exprtype_T type,
1767 int ic,
1768 int *res)
1769{
1770 int i = 0;
1771 int val = FALSE;
1772 char_u *s1, *s2;
1773 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1774
1775 if (in_vim9script()
1776 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1777 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1778 {
1779 semsg(_(e_cannot_compare_str_with_str),
1780 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1781 return FAIL;
1782 }
1783 s1 = tv_get_string_buf(tv1, buf1);
1784 s2 = tv_get_string_buf(tv2, buf2);
1785 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1786 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1787 switch (type)
1788 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001789 case EXPR_IS: if (in_vim9script())
1790 {
1791 // Really check it is the same string, not just
1792 // the same value.
1793 val = tv1->vval.v_string == tv2->vval.v_string;
1794 break;
1795 }
1796 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001797 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001798 case EXPR_ISNOT: if (in_vim9script())
1799 {
1800 // Really check it is not the same string, not
1801 // just a different value.
1802 val = tv1->vval.v_string != tv2->vval.v_string;
1803 break;
1804 }
1805 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001806 case EXPR_NEQUAL: val = (i != 0); break;
1807 case EXPR_GREATER: val = (i > 0); break;
1808 case EXPR_GEQUAL: val = (i >= 0); break;
1809 case EXPR_SMALLER: val = (i < 0); break;
1810 case EXPR_SEQUAL: val = (i <= 0); break;
1811
1812 case EXPR_MATCH:
1813 case EXPR_NOMATCH:
1814 val = pattern_match(s2, s1, ic);
1815 if (type == EXPR_NOMATCH)
1816 val = !val;
1817 break;
1818
1819 default: break; // avoid gcc warning
1820 }
1821 *res = val;
1822 return OK;
1823}
1824/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001825 * Convert any type to a string, never give an error.
1826 * When "quotes" is TRUE add quotes to a string.
1827 * Returns an allocated string.
1828 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001829 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001830typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001831{
1832 char_u *tofree;
1833 char_u numbuf[NUMBUFLEN];
1834 char_u *ret = NULL;
1835
1836 if (arg == NULL)
1837 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001838 if (!quotes && arg->v_type == VAR_STRING)
1839 {
1840 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1841 : arg->vval.v_string);
1842 }
1843 else
1844 {
1845 ret = tv2string(arg, &tofree, numbuf, 0);
1846 // Make a copy if we have a value but it's not in allocated memory.
1847 if (ret != NULL && tofree == NULL)
1848 ret = vim_strsave(ret);
1849 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001850 return ret;
1851}
1852
1853/*
1854 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1855 * or it refers to a List or Dictionary that is locked.
1856 */
1857 int
1858tv_islocked(typval_T *tv)
1859{
1860 return (tv->v_lock & VAR_LOCKED)
1861 || (tv->v_type == VAR_LIST
1862 && tv->vval.v_list != NULL
1863 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1864 || (tv->v_type == VAR_DICT
1865 && tv->vval.v_dict != NULL
1866 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1867}
1868
1869 static int
1870func_equal(
1871 typval_T *tv1,
1872 typval_T *tv2,
1873 int ic) // ignore case
1874{
1875 char_u *s1, *s2;
1876 dict_T *d1, *d2;
1877 int a1, a2;
1878 int i;
1879
1880 // empty and NULL function name considered the same
1881 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1882 : partial_name(tv1->vval.v_partial);
1883 if (s1 != NULL && *s1 == NUL)
1884 s1 = NULL;
1885 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1886 : partial_name(tv2->vval.v_partial);
1887 if (s2 != NULL && *s2 == NUL)
1888 s2 = NULL;
1889 if (s1 == NULL || s2 == NULL)
1890 {
1891 if (s1 != s2)
1892 return FALSE;
1893 }
1894 else if (STRCMP(s1, s2) != 0)
1895 return FALSE;
1896
1897 // empty dict and NULL dict is different
1898 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1899 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1900 if (d1 == NULL || d2 == NULL)
1901 {
1902 if (d1 != d2)
1903 return FALSE;
1904 }
1905 else if (!dict_equal(d1, d2, ic, TRUE))
1906 return FALSE;
1907
1908 // empty list and no list considered the same
1909 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1910 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1911 if (a1 != a2)
1912 return FALSE;
1913 for (i = 0; i < a1; ++i)
1914 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1915 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1916 return FALSE;
1917
1918 return TRUE;
1919}
1920
1921/*
1922 * Return TRUE if "tv1" and "tv2" have the same value.
1923 * Compares the items just like "==" would compare them, but strings and
1924 * numbers are different. Floats and numbers are also different.
1925 */
1926 int
1927tv_equal(
1928 typval_T *tv1,
1929 typval_T *tv2,
1930 int ic, // ignore case
1931 int recursive) // TRUE when used recursively
1932{
1933 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1934 char_u *s1, *s2;
1935 static int recursive_cnt = 0; // catch recursive loops
1936 int r;
1937 static int tv_equal_recurse_limit;
1938
1939 // Catch lists and dicts that have an endless loop by limiting
1940 // recursiveness to a limit. We guess they are equal then.
1941 // A fixed limit has the problem of still taking an awful long time.
1942 // Reduce the limit every time running into it. That should work fine for
1943 // deeply linked structures that are not recursively linked and catch
1944 // recursiveness quickly.
1945 if (!recursive)
1946 tv_equal_recurse_limit = 1000;
1947 if (recursive_cnt >= tv_equal_recurse_limit)
1948 {
1949 --tv_equal_recurse_limit;
1950 return TRUE;
1951 }
1952
1953 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1954 // arguments.
1955 if ((tv1->v_type == VAR_FUNC
1956 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1957 && (tv2->v_type == VAR_FUNC
1958 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1959 {
1960 ++recursive_cnt;
1961 r = func_equal(tv1, tv2, ic);
1962 --recursive_cnt;
1963 return r;
1964 }
1965
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001966 if (tv1->v_type != tv2->v_type
1967 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1968 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001969 return FALSE;
1970
1971 switch (tv1->v_type)
1972 {
1973 case VAR_LIST:
1974 ++recursive_cnt;
1975 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1976 --recursive_cnt;
1977 return r;
1978
1979 case VAR_DICT:
1980 ++recursive_cnt;
1981 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1982 --recursive_cnt;
1983 return r;
1984
1985 case VAR_BLOB:
1986 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1987
1988 case VAR_NUMBER:
1989 case VAR_BOOL:
1990 case VAR_SPECIAL:
1991 return tv1->vval.v_number == tv2->vval.v_number;
1992
1993 case VAR_STRING:
1994 s1 = tv_get_string_buf(tv1, buf1);
1995 s2 = tv_get_string_buf(tv2, buf2);
1996 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1997
1998 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001999 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002000 case VAR_JOB:
2001#ifdef FEAT_JOB_CHANNEL
2002 return tv1->vval.v_job == tv2->vval.v_job;
2003#endif
2004 case VAR_CHANNEL:
2005#ifdef FEAT_JOB_CHANNEL
2006 return tv1->vval.v_channel == tv2->vval.v_channel;
2007#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002008 case VAR_INSTR:
2009 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002010
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002011 case VAR_CLASS:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002012 // A class only exists once, equality is identity.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002013 return tv1->vval.v_class == tv2->vval.v_class;
2014
2015 case VAR_OBJECT:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002016 (void)typval_compare_object(tv1, tv2, EXPR_EQUAL, ic, &r);
2017 return r;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002018
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002019 case VAR_PARTIAL:
2020 return tv1->vval.v_partial == tv2->vval.v_partial;
2021
2022 case VAR_FUNC:
2023 return tv1->vval.v_string == tv2->vval.v_string;
2024
2025 case VAR_UNKNOWN:
2026 case VAR_ANY:
2027 case VAR_VOID:
2028 break;
2029 }
2030
2031 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
2032 // does not equal anything, not even itself.
2033 return FALSE;
2034}
2035
2036/*
2037 * Get an option value.
2038 * "arg" points to the '&' or '+' before the option name.
2039 * "arg" is advanced to character after the option name.
2040 * Return OK or FAIL.
2041 */
2042 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002043eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002044 char_u **arg,
2045 typval_T *rettv, // when NULL, only check if option exists
2046 int evaluate)
2047{
2048 char_u *option_end;
2049 long numval;
2050 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002051 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002052 int c;
2053 int working = (**arg == '+'); // has("+option")
2054 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002055 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002056
2057 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002058 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002059 if (option_end == NULL)
2060 {
2061 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002062 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002063 return FAIL;
2064 }
2065
2066 if (!evaluate)
2067 {
2068 *arg = option_end;
2069 return OK;
2070 }
2071
2072 c = *option_end;
2073 *option_end = NUL;
2074 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002075 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002076
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002077 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002078 {
2079 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002080 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002081 ret = FAIL;
2082 }
2083 else if (rettv != NULL)
2084 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01002085 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002086 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002087 {
2088 rettv->v_type = VAR_STRING;
2089 rettv->vval.v_string = NULL;
2090 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002091 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002092 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002093 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2094 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002095 rettv->vval.v_number = 0;
2096 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002097 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002098 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002099 if (in_vim9script() && opt_type == gov_bool)
2100 {
2101 rettv->v_type = VAR_BOOL;
2102 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2103 }
2104 else
2105 {
2106 rettv->v_type = VAR_NUMBER;
2107 rettv->vval.v_number = numval;
2108 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002109 }
2110 else // string option
2111 {
2112 rettv->v_type = VAR_STRING;
2113 rettv->vval.v_string = stringval;
2114 }
2115 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002116 else if (working && (opt_type == gov_hidden_bool
2117 || opt_type == gov_hidden_number
2118 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002119 ret = FAIL;
2120
2121 *option_end = c; // put back for error messages
2122 *arg = option_end;
2123
2124 return ret;
2125}
2126
2127/*
2128 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2129 * Return OK or FAIL.
2130 */
2131 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002132eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002133 char_u **arg,
2134 typval_T *rettv,
2135 int evaluate,
2136 int want_string UNUSED)
2137{
2138 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002139 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002140 char_u *p;
2141 int get_float = FALSE;
2142
2143 // We accept a float when the format matches
2144 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2145 // strict to avoid backwards compatibility problems.
2146 // With script version 2 and later the leading digit can be
2147 // omitted.
2148 // Don't look for a float after the "." operator, so that
2149 // ":let vers = 1.2.3" doesn't fail.
2150 if (**arg == '.')
2151 p = *arg;
2152 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002153 {
2154 p = *arg + 1;
2155 if (skip_quotes)
2156 for (;;)
2157 {
2158 if (*p == '\'')
2159 ++p;
2160 if (!vim_isdigit(*p))
2161 break;
2162 p = skipdigits(p);
2163 }
2164 else
2165 p = skipdigits(p);
2166 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002167 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2168 {
2169 get_float = TRUE;
2170 p = skipdigits(p + 2);
2171 if (*p == 'e' || *p == 'E')
2172 {
2173 ++p;
2174 if (*p == '-' || *p == '+')
2175 ++p;
2176 if (!vim_isdigit(*p))
2177 get_float = FALSE;
2178 else
2179 p = skipdigits(p + 1);
2180 }
2181 if (ASCII_ISALPHA(*p) || *p == '.')
2182 get_float = FALSE;
2183 }
2184 if (get_float)
2185 {
2186 float_T f;
2187
Bram Moolenaar29500652021-08-08 15:43:34 +02002188 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002189 if (evaluate)
2190 {
2191 rettv->v_type = VAR_FLOAT;
2192 rettv->vval.v_float = f;
2193 }
2194 }
2195 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002196 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2197 {
2198 char_u *bp;
2199 blob_T *blob = NULL; // init for gcc
2200
2201 // Blob constant: 0z0123456789abcdef
2202 if (evaluate)
2203 blob = blob_alloc();
2204 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2205 {
2206 if (!vim_isxdigit(bp[1]))
2207 {
2208 if (blob != NULL)
2209 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002210 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002211 ga_clear(&blob->bv_ga);
2212 VIM_CLEAR(blob);
2213 }
2214 return FAIL;
2215 }
2216 if (blob != NULL)
2217 ga_append(&blob->bv_ga,
2218 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2219 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2220 ++bp;
2221 }
2222 if (blob != NULL)
2223 rettv_blob_set(rettv, blob);
2224 *arg = bp;
2225 }
2226 else
2227 {
2228 varnumber_T n;
2229
2230 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002231 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002232 ? STR2NR_NO_OCT + STR2NR_QUOTE
2233 : STR2NR_ALL, &n, NULL, 0, TRUE);
2234 if (len == 0)
2235 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002236 if (evaluate)
2237 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002238 return FAIL;
2239 }
2240 *arg += len;
2241 if (evaluate)
2242 {
2243 rettv->v_type = VAR_NUMBER;
2244 rettv->vval.v_number = n;
2245 }
2246 }
2247 return OK;
2248}
2249
2250/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002251 * Evaluate a string constant and put the result in "rettv".
2252 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2253 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2254 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002255 * Return OK or FAIL.
2256 */
2257 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002258eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002259{
2260 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002261 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002262 int extra = interpolate ? 1 : 0;
2263 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002264 int len;
2265
2266 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002267 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002268 {
2269 if (*p == '\\' && p[1] != NUL)
2270 {
2271 ++p;
2272 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002273 // to 9 characters (6 for the char and 3 for a modifier):
2274 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002275 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002276 {
2277 int modifiers = 0;
2278 int flags = FSK_KEYCODE | FSK_IN_STRING;
2279
zeertzjqdb088872022-05-02 22:53:45 +01002280 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002281
2282 // Skip to the '>' to avoid using '{' inside for string
2283 // interpolation.
2284 if (p[1] != '*')
2285 flags |= FSK_SIMPLIFY;
2286 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2287 --p; // leave "p" on the ">"
2288 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002289 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002290 else if (interpolate && (*p == '{' || *p == '}'))
2291 {
2292 if (*p == '{' && p[1] != '{') // start of expression
2293 break;
2294 ++p;
2295 if (p[-1] == '}' && *p != '}') // single '}' is an error
2296 {
2297 semsg(_(e_stray_closing_curly_str), *arg);
2298 return FAIL;
2299 }
2300 --extra; // "{{" becomes "{", "}}" becomes "}"
2301 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002302 }
2303
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002304 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002305 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002306 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002307 return FAIL;
2308 }
2309
2310 // If only parsing, set *arg and return here
2311 if (!evaluate)
2312 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002313 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002314 return OK;
2315 }
2316
2317 // Copy the string into allocated memory, handling backslashed
2318 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002319 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002320 len = (int)(p - *arg + extra);
2321 rettv->vval.v_string = alloc(len);
2322 if (rettv->vval.v_string == NULL)
2323 return FAIL;
2324 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002325
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002326 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002327 {
2328 if (*p == '\\')
2329 {
2330 switch (*++p)
2331 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002332 case 'b': *end++ = BS; ++p; break;
2333 case 'e': *end++ = ESC; ++p; break;
2334 case 'f': *end++ = FF; ++p; break;
2335 case 'n': *end++ = NL; ++p; break;
2336 case 'r': *end++ = CAR; ++p; break;
2337 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002338
2339 case 'X': // hex: "\x1", "\x12"
2340 case 'x':
2341 case 'u': // Unicode: "\u0023"
2342 case 'U':
2343 if (vim_isxdigit(p[1]))
2344 {
2345 int n, nr;
2346 int c = toupper(*p);
2347
2348 if (c == 'X')
2349 n = 2;
2350 else if (*p == 'u')
2351 n = 4;
2352 else
2353 n = 8;
2354 nr = 0;
2355 while (--n >= 0 && vim_isxdigit(p[1]))
2356 {
2357 ++p;
2358 nr = (nr << 4) + hex2nr(*p);
2359 }
2360 ++p;
2361 // For "\u" store the number according to
2362 // 'encoding'.
2363 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002364 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002365 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002366 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002367 }
2368 break;
2369
2370 // octal: "\1", "\12", "\123"
2371 case '0':
2372 case '1':
2373 case '2':
2374 case '3':
2375 case '4':
2376 case '5':
2377 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002378 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002379 if (*p >= '0' && *p <= '7')
2380 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002381 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002382 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002383 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002384 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002385 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002386 break;
2387
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002388 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002389 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002390 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002391 int flags = FSK_KEYCODE | FSK_IN_STRING;
2392
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002393 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002394 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002395 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002396 if (extra != 0)
2397 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002398 end += extra;
2399 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002400 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002401 break;
2402 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002403 }
2404 // FALLTHROUGH
2405
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002406 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002407 break;
2408 }
2409 }
2410 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002411 {
2412 if (interpolate && (*p == '{' || *p == '}'))
2413 {
2414 if (*p == '{' && p[1] != '{') // start of expression
2415 break;
2416 ++p; // reduce "{{" to "{" and "}}" to "}"
2417 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002418 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002419 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002420 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002421 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002422 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002423 ++p;
2424 *arg = p;
2425
2426 return OK;
2427}
2428
2429/*
2430 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002431 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2432 * Return OK when a "rettv" was set to the string.
2433 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002434 */
2435 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002436eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002437{
2438 char_u *p;
2439 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002440 int reduce = interpolate ? -1 : 0;
2441 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002442
2443 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002444 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002445 {
2446 if (*p == '\'')
2447 {
2448 if (p[1] != '\'')
2449 break;
2450 ++reduce;
2451 ++p;
2452 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002453 else if (interpolate)
2454 {
2455 if (*p == '{')
2456 {
2457 if (p[1] != '{')
2458 break;
2459 ++p;
2460 ++reduce;
2461 }
2462 else if (*p == '}')
2463 {
2464 ++p;
2465 if (*p != '}')
2466 {
2467 semsg(_(e_stray_closing_curly_str), *arg);
2468 return FAIL;
2469 }
2470 ++reduce;
2471 }
2472 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002473 }
2474
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002475 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002476 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002477 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002478 return FAIL;
2479 }
2480
2481 // If only parsing return after setting "*arg"
2482 if (!evaluate)
2483 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002484 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002485 return OK;
2486 }
2487
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002488 // Copy the string into allocated memory, handling '' to ' reduction and
2489 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002490 str = alloc((p - *arg) - reduce);
2491 if (str == NULL)
2492 return FAIL;
2493 rettv->v_type = VAR_STRING;
2494 rettv->vval.v_string = str;
2495
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002496 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002497 {
2498 if (*p == '\'')
2499 {
2500 if (p[1] != '\'')
2501 break;
2502 ++p;
2503 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002504 else if (interpolate && (*p == '{' || *p == '}'))
2505 {
2506 if (*p == '{' && p[1] != '{')
2507 break;
2508 ++p;
2509 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002510 MB_COPY_CHAR(p, str);
2511 }
2512 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002513 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002514
2515 return OK;
2516}
2517
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002518/*
2519 * Evaluate a single or double quoted string possibly containing expressions.
2520 * "arg" points to the '$'. The result is put in "rettv".
2521 * Returns OK or FAIL.
2522 */
LemonBoy2eaef102022-05-06 13:14:50 +01002523 int
2524eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2525{
2526 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002527 int ret = OK;
2528 int quote;
2529 garray_T ga;
2530 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002531
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002532 ga_init2(&ga, 1, 80);
2533
2534 // *arg is on the '$' character, move it to the first string character.
2535 ++*arg;
2536 quote = **arg;
2537 ++*arg;
2538
2539 for (;;)
2540 {
2541 // Get the string up to the matching quote or to a single '{'.
2542 // "arg" is advanced to either the quote or the '{'.
2543 if (quote == '"')
2544 ret = eval_string(arg, &tv, evaluate, TRUE);
2545 else
2546 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2547 if (ret == FAIL)
2548 break;
2549 if (evaluate)
2550 {
2551 ga_concat(&ga, tv.vval.v_string);
2552 clear_tv(&tv);
2553 }
2554
2555 if (**arg != '{')
2556 {
2557 // found terminating quote
2558 ++*arg;
2559 break;
2560 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002561 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002562 if (p == NULL)
2563 {
2564 ret = FAIL;
2565 break;
2566 }
2567 *arg = p;
2568 }
LemonBoy2eaef102022-05-06 13:14:50 +01002569
2570 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002571 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2572 {
2573 ga_clear(&ga);
2574 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002575 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002576 }
LemonBoy2eaef102022-05-06 13:14:50 +01002577
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002578 rettv->vval.v_string = ga.ga_data;
2579 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002580}
2581
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002582/*
2583 * Return a string with the string representation of a variable.
2584 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2585 * "numbuf" is used for a number.
2586 * Puts quotes around strings, so that they can be parsed back by eval().
2587 * May return NULL.
2588 */
2589 char_u *
2590tv2string(
2591 typval_T *tv,
2592 char_u **tofree,
2593 char_u *numbuf,
2594 int copyID)
2595{
2596 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2597}
2598
2599/*
2600 * Get the value of an environment variable.
2601 * "arg" is pointing to the '$'. It is advanced to after the name.
2602 * If the environment variable was not set, silently assume it is empty.
2603 * Return FAIL if the name is invalid.
2604 */
2605 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002606eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002607{
2608 char_u *string = NULL;
2609 int len;
2610 int cc;
2611 char_u *name;
2612 int mustfree = FALSE;
2613
2614 ++*arg;
2615 name = *arg;
2616 len = get_env_len(arg);
2617 if (evaluate)
2618 {
2619 if (len == 0)
2620 return FAIL; // invalid empty name
2621
2622 cc = name[len];
2623 name[len] = NUL;
2624 // first try vim_getenv(), fast for normal environment vars
2625 string = vim_getenv(name, &mustfree);
2626 if (string != NULL && *string != NUL)
2627 {
2628 if (!mustfree)
2629 string = vim_strsave(string);
2630 }
2631 else
2632 {
2633 if (mustfree)
2634 vim_free(string);
2635
2636 // next try expanding things like $VIM and ${HOME}
2637 string = expand_env_save(name - 1);
2638 if (string != NULL && *string == '$')
2639 VIM_CLEAR(string);
2640 }
2641 name[len] = cc;
2642
2643 rettv->v_type = VAR_STRING;
2644 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002645 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002646 }
2647
2648 return OK;
2649}
2650
2651/*
2652 * Get the lnum from the first argument.
2653 * Also accepts ".", "$", etc., but that only works for the current buffer.
2654 * Returns -1 on error.
2655 */
2656 linenr_T
2657tv_get_lnum(typval_T *argvars)
2658{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002659 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002660 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002661
Bram Moolenaar56acb092020-08-16 14:48:19 +02002662 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2663 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002664 if (lnum <= 0 && did_emsg_before == did_emsg
2665 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002666 {
2667 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002668 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002669
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002670 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002671 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002672 if (fp != NULL)
2673 lnum = fp->lnum;
2674 }
2675 return lnum;
2676}
2677
2678/*
2679 * Get the lnum from the first argument.
2680 * Also accepts "$", then "buf" is used.
2681 * Returns 0 on error.
2682 */
2683 linenr_T
2684tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2685{
2686 if (argvars[0].v_type == VAR_STRING
2687 && argvars[0].vval.v_string != NULL
2688 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002689 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002690 && buf != NULL)
2691 return buf->b_ml.ml_line_count;
2692 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2693}
2694
2695/*
2696 * Get buffer by number or pattern.
2697 */
2698 buf_T *
2699tv_get_buf(typval_T *tv, int curtab_only)
2700{
2701 char_u *name = tv->vval.v_string;
2702 buf_T *buf;
2703
2704 if (tv->v_type == VAR_NUMBER)
2705 return buflist_findnr((int)tv->vval.v_number);
2706 if (tv->v_type != VAR_STRING)
2707 return NULL;
2708 if (name == NULL || *name == NUL)
2709 return curbuf;
2710 if (name[0] == '$' && name[1] == NUL)
2711 return lastbuf;
2712
2713 buf = buflist_find_by_name(name, curtab_only);
2714
2715 // If not found, try expanding the name, like done for bufexists().
2716 if (buf == NULL)
2717 buf = find_buffer(tv);
2718
2719 return buf;
2720}
2721
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002722/*
2723 * Like tv_get_buf() but give an error message is the type is wrong.
2724 */
2725 buf_T *
2726tv_get_buf_from_arg(typval_T *tv)
2727{
2728 buf_T *buf;
2729
2730 ++emsg_off;
2731 buf = tv_get_buf(tv, FALSE);
2732 --emsg_off;
2733 if (buf == NULL
2734 && tv->v_type != VAR_NUMBER
2735 && tv->v_type != VAR_STRING)
2736 // issue errmsg for type error
2737 (void)tv_get_number(tv);
2738 return buf;
2739}
2740
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002741#endif // FEAT_EVAL