blob: 6eae02b78e30b84d817186b1b3f20c7f86fc1549 [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{
55 if (varp != NULL)
56 {
57 switch (varp->v_type)
58 {
59 case VAR_FUNC:
60 func_unref(varp->vval.v_string);
61 // FALLTHROUGH
62 case VAR_STRING:
63 vim_free(varp->vval.v_string);
64 break;
65 case VAR_PARTIAL:
66 partial_unref(varp->vval.v_partial);
67 break;
68 case VAR_BLOB:
69 blob_unref(varp->vval.v_blob);
70 break;
71 case VAR_LIST:
72 list_unref(varp->vval.v_list);
73 break;
74 case VAR_DICT:
75 dict_unref(varp->vval.v_dict);
76 break;
77 case VAR_JOB:
78#ifdef FEAT_JOB_CHANNEL
79 job_unref(varp->vval.v_job);
80 break;
81#endif
82 case VAR_CHANNEL:
83#ifdef FEAT_JOB_CHANNEL
84 channel_unref(varp->vval.v_channel);
85 break;
86#endif
Bram Moolenaar00b28d62022-12-08 15:32:33 +000087 case VAR_CLASS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +000088 class_unref(varp->vval.v_class);
Bram Moolenaar00b28d62022-12-08 15:32:33 +000089 break;
90 case VAR_OBJECT:
91 object_unref(varp->vval.v_object);
92 break;
93
Bram Moolenaar367d59e2020-05-30 17:06:14 +020094 case VAR_NUMBER:
95 case VAR_FLOAT:
96 case VAR_ANY:
97 case VAR_UNKNOWN:
98 case VAR_VOID:
99 case VAR_BOOL:
100 case VAR_SPECIAL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200101 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200102 break;
103 }
104 vim_free(varp);
105 }
106}
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{
114 if (varp != NULL)
115 {
116 switch (varp->v_type)
117 {
118 case VAR_FUNC:
119 func_unref(varp->vval.v_string);
120 // FALLTHROUGH
121 case VAR_STRING:
122 VIM_CLEAR(varp->vval.v_string);
123 break;
124 case VAR_PARTIAL:
125 partial_unref(varp->vval.v_partial);
126 varp->vval.v_partial = NULL;
127 break;
128 case VAR_BLOB:
129 blob_unref(varp->vval.v_blob);
130 varp->vval.v_blob = NULL;
131 break;
132 case VAR_LIST:
133 list_unref(varp->vval.v_list);
134 varp->vval.v_list = NULL;
135 break;
136 case VAR_DICT:
137 dict_unref(varp->vval.v_dict);
138 varp->vval.v_dict = NULL;
139 break;
140 case VAR_NUMBER:
141 case VAR_BOOL:
142 case VAR_SPECIAL:
143 varp->vval.v_number = 0;
144 break;
145 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200146 varp->vval.v_float = 0.0;
147 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200148 case VAR_JOB:
149#ifdef FEAT_JOB_CHANNEL
150 job_unref(varp->vval.v_job);
151 varp->vval.v_job = NULL;
152#endif
153 break;
154 case VAR_CHANNEL:
155#ifdef FEAT_JOB_CHANNEL
156 channel_unref(varp->vval.v_channel);
157 varp->vval.v_channel = NULL;
158#endif
Bram Moolenaar24f72092021-05-07 20:43:54 +0200159 break;
160 case VAR_INSTR:
161 VIM_CLEAR(varp->vval.v_instr);
162 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000163 case VAR_CLASS:
Bram Moolenaard28d7b92022-12-08 20:42:00 +0000164 class_unref(varp->vval.v_class);
Bram Moolenaar6ef54712022-12-25 19:31:36 +0000165 varp->vval.v_class = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000166 break;
167 case VAR_OBJECT:
168 object_unref(varp->vval.v_object);
Bram Moolenaar6ef54712022-12-25 19:31:36 +0000169 varp->vval.v_object = NULL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000170 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200171 case VAR_UNKNOWN:
172 case VAR_ANY:
173 case VAR_VOID:
174 break;
175 }
176 varp->v_lock = 0;
177 }
178}
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 Moolenaar265f8112021-12-19 12:33:05 +00001313 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001314 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001315 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001316 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001317 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001318 return FAIL;
1319 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001320 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001321 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001322 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1323 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001324 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001325 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001326 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001327 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001328 return FAIL;
1329 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001330 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001331 }
1332
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001333 // If one of the two variables is a float, compare as a float.
1334 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001335 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001336 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1337 {
1338 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001339 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001340
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001341 f1 = tv_get_float_chk(tv1, &error);
1342 if (!error)
1343 f2 = tv_get_float_chk(tv2, &error);
1344 if (error)
1345 {
1346 clear_tv(tv1);
1347 return FAIL;
1348 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001349 n1 = FALSE;
1350 switch (type)
1351 {
1352 case EXPR_IS:
1353 case EXPR_EQUAL: n1 = (f1 == f2); break;
1354 case EXPR_ISNOT:
1355 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1356 case EXPR_GREATER: n1 = (f1 > f2); break;
1357 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1358 case EXPR_SMALLER: n1 = (f1 < f2); break;
1359 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1360 case EXPR_UNKNOWN:
1361 case EXPR_MATCH:
1362 default: break; // avoid gcc warning
1363 }
1364 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001365
1366 // If one of the two variables is a number, compare as a number.
1367 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001368 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001369 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1370 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001371 int error = FALSE;
1372
1373 n1 = tv_get_number_chk(tv1, &error);
1374 if (!error)
1375 n2 = tv_get_number_chk(tv2, &error);
1376 if (error)
1377 {
1378 clear_tv(tv1);
1379 return FAIL;
1380 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001381 switch (type)
1382 {
1383 case EXPR_IS:
1384 case EXPR_EQUAL: n1 = (n1 == n2); break;
1385 case EXPR_ISNOT:
1386 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1387 case EXPR_GREATER: n1 = (n1 > n2); break;
1388 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1389 case EXPR_SMALLER: n1 = (n1 < n2); break;
1390 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1391 case EXPR_UNKNOWN:
1392 case EXPR_MATCH:
1393 default: break; // avoid gcc warning
1394 }
1395 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001396 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1397 || tv2->v_type == VAR_BOOL
1398 || (tv1->v_type == VAR_SPECIAL
1399 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001400 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001401 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001402 {
1403 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001404 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1405 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001406 return FAIL;
1407 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001408 n1 = tv1->vval.v_number;
1409 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001410 switch (type)
1411 {
1412 case EXPR_IS:
1413 case EXPR_EQUAL: n1 = (n1 == n2); break;
1414 case EXPR_ISNOT:
1415 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1416 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001417 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001418 vartype_name(tv1->v_type));
1419 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001420 return FAIL;
1421 }
1422 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001423#ifdef FEAT_JOB_CHANNEL
1424 else if (tv1->v_type == tv2->v_type
1425 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1426 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1427 {
1428 if (tv1->v_type == VAR_CHANNEL)
1429 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1430 else
1431 n1 = tv1->vval.v_job == tv2->vval.v_job;
1432 if (type == EXPR_NEQUAL)
1433 n1 = !n1;
1434 }
1435#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001436 else
1437 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001438 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001439 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001440 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001441 return FAIL;
1442 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001443 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001444 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001445 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001446 if (in_vim9script())
1447 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001448 tv1->v_type = VAR_BOOL;
1449 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001450 }
1451 else
1452 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001453 tv1->v_type = VAR_NUMBER;
1454 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001455 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001456
1457 return OK;
1458}
1459
Bram Moolenaar34453202021-01-31 13:08:38 +01001460/*
dundargocc57b5bc2022-11-02 13:30:51 +00001461 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001462 * Put the result, false or true, in "res".
1463 * Return FAIL and give an error message when the comparison can't be done.
1464 */
1465 int
1466typval_compare_list(
1467 typval_T *tv1,
1468 typval_T *tv2,
1469 exprtype_T type,
1470 int ic,
1471 int *res)
1472{
1473 int val = 0;
1474
1475 if (type == EXPR_IS || type == EXPR_ISNOT)
1476 {
1477 val = (tv1->v_type == tv2->v_type
1478 && tv1->vval.v_list == tv2->vval.v_list);
1479 if (type == EXPR_ISNOT)
1480 val = !val;
1481 }
1482 else if (tv1->v_type != tv2->v_type
1483 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1484 {
1485 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001486 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001487 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001488 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001489 return FAIL;
1490 }
1491 else
1492 {
1493 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1494 ic, FALSE);
1495 if (type == EXPR_NEQUAL)
1496 val = !val;
1497 }
1498 *res = val;
1499 return OK;
1500}
1501
1502/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001503 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001504 */
1505 int
1506typval_compare_null(typval_T *tv1, typval_T *tv2)
1507{
1508 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1509 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1510 {
1511 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1512
1513 switch (tv->v_type)
1514 {
1515 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001516#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001517 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001518#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001519 case VAR_DICT: return tv->vval.v_dict == NULL;
1520 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001521#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001522 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001523#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001524 case VAR_LIST: return tv->vval.v_list == NULL;
1525 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1526 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001527
1528 case VAR_NUMBER: if (!in_vim9script())
1529 return tv->vval.v_number == 0;
1530 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001531 case VAR_FLOAT: if (!in_vim9script())
1532 return tv->vval.v_float == 0.0;
1533 break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001534 default: break;
1535 }
1536 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001537 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001538 // we won't give an error
1539 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001540}
1541
1542/*
dundargocc57b5bc2022-11-02 13:30:51 +00001543 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001544 * Put the result, false or true, in "res".
1545 * Return FAIL and give an error message when the comparison can't be done.
1546 */
1547 int
1548typval_compare_blob(
1549 typval_T *tv1,
1550 typval_T *tv2,
1551 exprtype_T type,
1552 int *res)
1553{
1554 int val = 0;
1555
1556 if (type == EXPR_IS || type == EXPR_ISNOT)
1557 {
1558 val = (tv1->v_type == tv2->v_type
1559 && tv1->vval.v_blob == tv2->vval.v_blob);
1560 if (type == EXPR_ISNOT)
1561 val = !val;
1562 }
1563 else if (tv1->v_type != tv2->v_type
1564 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1565 {
1566 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001567 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001568 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001569 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001570 return FAIL;
1571 }
1572 else
1573 {
1574 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1575 if (type == EXPR_NEQUAL)
1576 val = !val;
1577 }
1578 *res = val;
1579 return OK;
1580}
1581
1582/*
dundargocc57b5bc2022-11-02 13:30:51 +00001583 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001584 * Put the result, false or true, in "res".
1585 * Return FAIL and give an error message when the comparison can't be done.
1586 */
1587 int
1588typval_compare_dict(
1589 typval_T *tv1,
1590 typval_T *tv2,
1591 exprtype_T type,
1592 int ic,
1593 int *res)
1594{
1595 int val;
1596
1597 if (type == EXPR_IS || type == EXPR_ISNOT)
1598 {
1599 val = (tv1->v_type == tv2->v_type
1600 && tv1->vval.v_dict == tv2->vval.v_dict);
1601 if (type == EXPR_ISNOT)
1602 val = !val;
1603 }
1604 else if (tv1->v_type != tv2->v_type
1605 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1606 {
1607 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001608 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001609 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001610 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001611 return FAIL;
1612 }
1613 else
1614 {
1615 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1616 if (type == EXPR_NEQUAL)
1617 val = !val;
1618 }
1619 *res = val;
1620 return OK;
1621}
1622
1623/*
dundargocc57b5bc2022-11-02 13:30:51 +00001624 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001625 * Put the result, false or true, in "res".
1626 * Return FAIL and give an error message when the comparison can't be done.
1627 */
1628 int
1629typval_compare_func(
1630 typval_T *tv1,
1631 typval_T *tv2,
1632 exprtype_T type,
1633 int ic,
1634 int *res)
1635{
1636 int val = 0;
1637
1638 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1639 && type != EXPR_IS && type != EXPR_ISNOT)
1640 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001641 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001642 return FAIL;
1643 }
1644 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1645 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1646 // When both partials are NULL, then they are equal.
1647 // Otherwise they are not equal.
1648 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1649 else if (type == EXPR_IS || type == EXPR_ISNOT)
1650 {
1651 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1652 // strings are considered the same if their value is
1653 // the same
1654 val = tv_equal(tv1, tv2, ic, FALSE);
1655 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1656 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1657 else
1658 val = FALSE;
1659 }
1660 else
1661 val = tv_equal(tv1, tv2, ic, FALSE);
1662 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1663 val = !val;
1664 *res = val;
1665 return OK;
1666}
1667
1668/*
1669 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1670 * Put the result, false or true, in "res".
1671 * Return FAIL and give an error message when the comparison can't be done.
1672 */
1673 int
1674typval_compare_string(
1675 typval_T *tv1,
1676 typval_T *tv2,
1677 exprtype_T type,
1678 int ic,
1679 int *res)
1680{
1681 int i = 0;
1682 int val = FALSE;
1683 char_u *s1, *s2;
1684 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1685
1686 if (in_vim9script()
1687 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1688 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1689 {
1690 semsg(_(e_cannot_compare_str_with_str),
1691 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1692 return FAIL;
1693 }
1694 s1 = tv_get_string_buf(tv1, buf1);
1695 s2 = tv_get_string_buf(tv2, buf2);
1696 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1697 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1698 switch (type)
1699 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001700 case EXPR_IS: if (in_vim9script())
1701 {
1702 // Really check it is the same string, not just
1703 // the same value.
1704 val = tv1->vval.v_string == tv2->vval.v_string;
1705 break;
1706 }
1707 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001708 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001709 case EXPR_ISNOT: if (in_vim9script())
1710 {
1711 // Really check it is not the same string, not
1712 // just a different value.
1713 val = tv1->vval.v_string != tv2->vval.v_string;
1714 break;
1715 }
1716 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001717 case EXPR_NEQUAL: val = (i != 0); break;
1718 case EXPR_GREATER: val = (i > 0); break;
1719 case EXPR_GEQUAL: val = (i >= 0); break;
1720 case EXPR_SMALLER: val = (i < 0); break;
1721 case EXPR_SEQUAL: val = (i <= 0); break;
1722
1723 case EXPR_MATCH:
1724 case EXPR_NOMATCH:
1725 val = pattern_match(s2, s1, ic);
1726 if (type == EXPR_NOMATCH)
1727 val = !val;
1728 break;
1729
1730 default: break; // avoid gcc warning
1731 }
1732 *res = val;
1733 return OK;
1734}
1735/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001736 * Convert any type to a string, never give an error.
1737 * When "quotes" is TRUE add quotes to a string.
1738 * Returns an allocated string.
1739 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001740 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001741typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001742{
1743 char_u *tofree;
1744 char_u numbuf[NUMBUFLEN];
1745 char_u *ret = NULL;
1746
1747 if (arg == NULL)
1748 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001749 if (!quotes && arg->v_type == VAR_STRING)
1750 {
1751 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1752 : arg->vval.v_string);
1753 }
1754 else
1755 {
1756 ret = tv2string(arg, &tofree, numbuf, 0);
1757 // Make a copy if we have a value but it's not in allocated memory.
1758 if (ret != NULL && tofree == NULL)
1759 ret = vim_strsave(ret);
1760 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001761 return ret;
1762}
1763
1764/*
1765 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1766 * or it refers to a List or Dictionary that is locked.
1767 */
1768 int
1769tv_islocked(typval_T *tv)
1770{
1771 return (tv->v_lock & VAR_LOCKED)
1772 || (tv->v_type == VAR_LIST
1773 && tv->vval.v_list != NULL
1774 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1775 || (tv->v_type == VAR_DICT
1776 && tv->vval.v_dict != NULL
1777 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1778}
1779
1780 static int
1781func_equal(
1782 typval_T *tv1,
1783 typval_T *tv2,
1784 int ic) // ignore case
1785{
1786 char_u *s1, *s2;
1787 dict_T *d1, *d2;
1788 int a1, a2;
1789 int i;
1790
1791 // empty and NULL function name considered the same
1792 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1793 : partial_name(tv1->vval.v_partial);
1794 if (s1 != NULL && *s1 == NUL)
1795 s1 = NULL;
1796 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1797 : partial_name(tv2->vval.v_partial);
1798 if (s2 != NULL && *s2 == NUL)
1799 s2 = NULL;
1800 if (s1 == NULL || s2 == NULL)
1801 {
1802 if (s1 != s2)
1803 return FALSE;
1804 }
1805 else if (STRCMP(s1, s2) != 0)
1806 return FALSE;
1807
1808 // empty dict and NULL dict is different
1809 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1810 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1811 if (d1 == NULL || d2 == NULL)
1812 {
1813 if (d1 != d2)
1814 return FALSE;
1815 }
1816 else if (!dict_equal(d1, d2, ic, TRUE))
1817 return FALSE;
1818
1819 // empty list and no list considered the same
1820 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1821 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1822 if (a1 != a2)
1823 return FALSE;
1824 for (i = 0; i < a1; ++i)
1825 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1826 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1827 return FALSE;
1828
1829 return TRUE;
1830}
1831
1832/*
1833 * Return TRUE if "tv1" and "tv2" have the same value.
1834 * Compares the items just like "==" would compare them, but strings and
1835 * numbers are different. Floats and numbers are also different.
1836 */
1837 int
1838tv_equal(
1839 typval_T *tv1,
1840 typval_T *tv2,
1841 int ic, // ignore case
1842 int recursive) // TRUE when used recursively
1843{
1844 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1845 char_u *s1, *s2;
1846 static int recursive_cnt = 0; // catch recursive loops
1847 int r;
1848 static int tv_equal_recurse_limit;
1849
1850 // Catch lists and dicts that have an endless loop by limiting
1851 // recursiveness to a limit. We guess they are equal then.
1852 // A fixed limit has the problem of still taking an awful long time.
1853 // Reduce the limit every time running into it. That should work fine for
1854 // deeply linked structures that are not recursively linked and catch
1855 // recursiveness quickly.
1856 if (!recursive)
1857 tv_equal_recurse_limit = 1000;
1858 if (recursive_cnt >= tv_equal_recurse_limit)
1859 {
1860 --tv_equal_recurse_limit;
1861 return TRUE;
1862 }
1863
1864 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1865 // arguments.
1866 if ((tv1->v_type == VAR_FUNC
1867 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1868 && (tv2->v_type == VAR_FUNC
1869 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1870 {
1871 ++recursive_cnt;
1872 r = func_equal(tv1, tv2, ic);
1873 --recursive_cnt;
1874 return r;
1875 }
1876
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001877 if (tv1->v_type != tv2->v_type
1878 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1879 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001880 return FALSE;
1881
1882 switch (tv1->v_type)
1883 {
1884 case VAR_LIST:
1885 ++recursive_cnt;
1886 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1887 --recursive_cnt;
1888 return r;
1889
1890 case VAR_DICT:
1891 ++recursive_cnt;
1892 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1893 --recursive_cnt;
1894 return r;
1895
1896 case VAR_BLOB:
1897 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1898
1899 case VAR_NUMBER:
1900 case VAR_BOOL:
1901 case VAR_SPECIAL:
1902 return tv1->vval.v_number == tv2->vval.v_number;
1903
1904 case VAR_STRING:
1905 s1 = tv_get_string_buf(tv1, buf1);
1906 s2 = tv_get_string_buf(tv2, buf2);
1907 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1908
1909 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001910 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001911 case VAR_JOB:
1912#ifdef FEAT_JOB_CHANNEL
1913 return tv1->vval.v_job == tv2->vval.v_job;
1914#endif
1915 case VAR_CHANNEL:
1916#ifdef FEAT_JOB_CHANNEL
1917 return tv1->vval.v_channel == tv2->vval.v_channel;
1918#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001919 case VAR_INSTR:
1920 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001921
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001922 case VAR_CLASS:
1923 return tv1->vval.v_class == tv2->vval.v_class;
1924
1925 case VAR_OBJECT:
1926 // TODO: compare values
1927 return tv1->vval.v_object == tv2->vval.v_object;
1928
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001929 case VAR_PARTIAL:
1930 return tv1->vval.v_partial == tv2->vval.v_partial;
1931
1932 case VAR_FUNC:
1933 return tv1->vval.v_string == tv2->vval.v_string;
1934
1935 case VAR_UNKNOWN:
1936 case VAR_ANY:
1937 case VAR_VOID:
1938 break;
1939 }
1940
1941 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1942 // does not equal anything, not even itself.
1943 return FALSE;
1944}
1945
1946/*
1947 * Get an option value.
1948 * "arg" points to the '&' or '+' before the option name.
1949 * "arg" is advanced to character after the option name.
1950 * Return OK or FAIL.
1951 */
1952 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001953eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001954 char_u **arg,
1955 typval_T *rettv, // when NULL, only check if option exists
1956 int evaluate)
1957{
1958 char_u *option_end;
1959 long numval;
1960 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001961 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001962 int c;
1963 int working = (**arg == '+'); // has("+option")
1964 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001965 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001966
1967 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001968 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001969 if (option_end == NULL)
1970 {
1971 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001972 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001973 return FAIL;
1974 }
1975
1976 if (!evaluate)
1977 {
1978 *arg = option_end;
1979 return OK;
1980 }
1981
1982 c = *option_end;
1983 *option_end = NUL;
1984 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001985 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001986
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001987 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001988 {
1989 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001990 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001991 ret = FAIL;
1992 }
1993 else if (rettv != NULL)
1994 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001995 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001996 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001997 {
1998 rettv->v_type = VAR_STRING;
1999 rettv->vval.v_string = NULL;
2000 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002001 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002002 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002003 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2004 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002005 rettv->vval.v_number = 0;
2006 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002007 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002008 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002009 if (in_vim9script() && opt_type == gov_bool)
2010 {
2011 rettv->v_type = VAR_BOOL;
2012 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2013 }
2014 else
2015 {
2016 rettv->v_type = VAR_NUMBER;
2017 rettv->vval.v_number = numval;
2018 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002019 }
2020 else // string option
2021 {
2022 rettv->v_type = VAR_STRING;
2023 rettv->vval.v_string = stringval;
2024 }
2025 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002026 else if (working && (opt_type == gov_hidden_bool
2027 || opt_type == gov_hidden_number
2028 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002029 ret = FAIL;
2030
2031 *option_end = c; // put back for error messages
2032 *arg = option_end;
2033
2034 return ret;
2035}
2036
2037/*
2038 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2039 * Return OK or FAIL.
2040 */
2041 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002042eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002043 char_u **arg,
2044 typval_T *rettv,
2045 int evaluate,
2046 int want_string UNUSED)
2047{
2048 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002049 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002050 char_u *p;
2051 int get_float = FALSE;
2052
2053 // We accept a float when the format matches
2054 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2055 // strict to avoid backwards compatibility problems.
2056 // With script version 2 and later the leading digit can be
2057 // omitted.
2058 // Don't look for a float after the "." operator, so that
2059 // ":let vers = 1.2.3" doesn't fail.
2060 if (**arg == '.')
2061 p = *arg;
2062 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002063 {
2064 p = *arg + 1;
2065 if (skip_quotes)
2066 for (;;)
2067 {
2068 if (*p == '\'')
2069 ++p;
2070 if (!vim_isdigit(*p))
2071 break;
2072 p = skipdigits(p);
2073 }
2074 else
2075 p = skipdigits(p);
2076 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002077 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2078 {
2079 get_float = TRUE;
2080 p = skipdigits(p + 2);
2081 if (*p == 'e' || *p == 'E')
2082 {
2083 ++p;
2084 if (*p == '-' || *p == '+')
2085 ++p;
2086 if (!vim_isdigit(*p))
2087 get_float = FALSE;
2088 else
2089 p = skipdigits(p + 1);
2090 }
2091 if (ASCII_ISALPHA(*p) || *p == '.')
2092 get_float = FALSE;
2093 }
2094 if (get_float)
2095 {
2096 float_T f;
2097
Bram Moolenaar29500652021-08-08 15:43:34 +02002098 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002099 if (evaluate)
2100 {
2101 rettv->v_type = VAR_FLOAT;
2102 rettv->vval.v_float = f;
2103 }
2104 }
2105 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002106 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2107 {
2108 char_u *bp;
2109 blob_T *blob = NULL; // init for gcc
2110
2111 // Blob constant: 0z0123456789abcdef
2112 if (evaluate)
2113 blob = blob_alloc();
2114 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2115 {
2116 if (!vim_isxdigit(bp[1]))
2117 {
2118 if (blob != NULL)
2119 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002120 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002121 ga_clear(&blob->bv_ga);
2122 VIM_CLEAR(blob);
2123 }
2124 return FAIL;
2125 }
2126 if (blob != NULL)
2127 ga_append(&blob->bv_ga,
2128 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2129 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2130 ++bp;
2131 }
2132 if (blob != NULL)
2133 rettv_blob_set(rettv, blob);
2134 *arg = bp;
2135 }
2136 else
2137 {
2138 varnumber_T n;
2139
2140 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002141 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002142 ? STR2NR_NO_OCT + STR2NR_QUOTE
2143 : STR2NR_ALL, &n, NULL, 0, TRUE);
2144 if (len == 0)
2145 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002146 if (evaluate)
2147 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002148 return FAIL;
2149 }
2150 *arg += len;
2151 if (evaluate)
2152 {
2153 rettv->v_type = VAR_NUMBER;
2154 rettv->vval.v_number = n;
2155 }
2156 }
2157 return OK;
2158}
2159
2160/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002161 * Evaluate a string constant and put the result in "rettv".
2162 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2163 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2164 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002165 * Return OK or FAIL.
2166 */
2167 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002168eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002169{
2170 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002171 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002172 int extra = interpolate ? 1 : 0;
2173 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002174 int len;
2175
2176 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002177 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002178 {
2179 if (*p == '\\' && p[1] != NUL)
2180 {
2181 ++p;
2182 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002183 // to 9 characters (6 for the char and 3 for a modifier):
2184 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002185 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002186 {
2187 int modifiers = 0;
2188 int flags = FSK_KEYCODE | FSK_IN_STRING;
2189
zeertzjqdb088872022-05-02 22:53:45 +01002190 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002191
2192 // Skip to the '>' to avoid using '{' inside for string
2193 // interpolation.
2194 if (p[1] != '*')
2195 flags |= FSK_SIMPLIFY;
2196 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2197 --p; // leave "p" on the ">"
2198 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002199 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002200 else if (interpolate && (*p == '{' || *p == '}'))
2201 {
2202 if (*p == '{' && p[1] != '{') // start of expression
2203 break;
2204 ++p;
2205 if (p[-1] == '}' && *p != '}') // single '}' is an error
2206 {
2207 semsg(_(e_stray_closing_curly_str), *arg);
2208 return FAIL;
2209 }
2210 --extra; // "{{" becomes "{", "}}" becomes "}"
2211 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002212 }
2213
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002214 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002215 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002216 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002217 return FAIL;
2218 }
2219
2220 // If only parsing, set *arg and return here
2221 if (!evaluate)
2222 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002223 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002224 return OK;
2225 }
2226
2227 // Copy the string into allocated memory, handling backslashed
2228 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002229 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002230 len = (int)(p - *arg + extra);
2231 rettv->vval.v_string = alloc(len);
2232 if (rettv->vval.v_string == NULL)
2233 return FAIL;
2234 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002235
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002236 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002237 {
2238 if (*p == '\\')
2239 {
2240 switch (*++p)
2241 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002242 case 'b': *end++ = BS; ++p; break;
2243 case 'e': *end++ = ESC; ++p; break;
2244 case 'f': *end++ = FF; ++p; break;
2245 case 'n': *end++ = NL; ++p; break;
2246 case 'r': *end++ = CAR; ++p; break;
2247 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002248
2249 case 'X': // hex: "\x1", "\x12"
2250 case 'x':
2251 case 'u': // Unicode: "\u0023"
2252 case 'U':
2253 if (vim_isxdigit(p[1]))
2254 {
2255 int n, nr;
2256 int c = toupper(*p);
2257
2258 if (c == 'X')
2259 n = 2;
2260 else if (*p == 'u')
2261 n = 4;
2262 else
2263 n = 8;
2264 nr = 0;
2265 while (--n >= 0 && vim_isxdigit(p[1]))
2266 {
2267 ++p;
2268 nr = (nr << 4) + hex2nr(*p);
2269 }
2270 ++p;
2271 // For "\u" store the number according to
2272 // 'encoding'.
2273 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002274 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002275 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002276 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002277 }
2278 break;
2279
2280 // octal: "\1", "\12", "\123"
2281 case '0':
2282 case '1':
2283 case '2':
2284 case '3':
2285 case '4':
2286 case '5':
2287 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002288 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002289 if (*p >= '0' && *p <= '7')
2290 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002291 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002292 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002293 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002294 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002295 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002296 break;
2297
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002298 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002299 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002300 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002301 int flags = FSK_KEYCODE | FSK_IN_STRING;
2302
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002303 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002304 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002305 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002306 if (extra != 0)
2307 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002308 end += extra;
2309 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002310 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002311 break;
2312 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002313 }
2314 // FALLTHROUGH
2315
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002316 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002317 break;
2318 }
2319 }
2320 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002321 {
2322 if (interpolate && (*p == '{' || *p == '}'))
2323 {
2324 if (*p == '{' && p[1] != '{') // start of expression
2325 break;
2326 ++p; // reduce "{{" to "{" and "}}" to "}"
2327 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002328 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002329 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002330 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002331 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002332 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002333 ++p;
2334 *arg = p;
2335
2336 return OK;
2337}
2338
2339/*
2340 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002341 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2342 * Return OK when a "rettv" was set to the string.
2343 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002344 */
2345 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002346eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002347{
2348 char_u *p;
2349 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002350 int reduce = interpolate ? -1 : 0;
2351 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002352
2353 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002354 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002355 {
2356 if (*p == '\'')
2357 {
2358 if (p[1] != '\'')
2359 break;
2360 ++reduce;
2361 ++p;
2362 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002363 else if (interpolate)
2364 {
2365 if (*p == '{')
2366 {
2367 if (p[1] != '{')
2368 break;
2369 ++p;
2370 ++reduce;
2371 }
2372 else if (*p == '}')
2373 {
2374 ++p;
2375 if (*p != '}')
2376 {
2377 semsg(_(e_stray_closing_curly_str), *arg);
2378 return FAIL;
2379 }
2380 ++reduce;
2381 }
2382 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002383 }
2384
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002385 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002386 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002387 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002388 return FAIL;
2389 }
2390
2391 // If only parsing return after setting "*arg"
2392 if (!evaluate)
2393 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002394 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002395 return OK;
2396 }
2397
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002398 // Copy the string into allocated memory, handling '' to ' reduction and
2399 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002400 str = alloc((p - *arg) - reduce);
2401 if (str == NULL)
2402 return FAIL;
2403 rettv->v_type = VAR_STRING;
2404 rettv->vval.v_string = str;
2405
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002406 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002407 {
2408 if (*p == '\'')
2409 {
2410 if (p[1] != '\'')
2411 break;
2412 ++p;
2413 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002414 else if (interpolate && (*p == '{' || *p == '}'))
2415 {
2416 if (*p == '{' && p[1] != '{')
2417 break;
2418 ++p;
2419 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002420 MB_COPY_CHAR(p, str);
2421 }
2422 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002423 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002424
2425 return OK;
2426}
2427
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002428/*
2429 * Evaluate a single or double quoted string possibly containing expressions.
2430 * "arg" points to the '$'. The result is put in "rettv".
2431 * Returns OK or FAIL.
2432 */
LemonBoy2eaef102022-05-06 13:14:50 +01002433 int
2434eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2435{
2436 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002437 int ret = OK;
2438 int quote;
2439 garray_T ga;
2440 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002441
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002442 ga_init2(&ga, 1, 80);
2443
2444 // *arg is on the '$' character, move it to the first string character.
2445 ++*arg;
2446 quote = **arg;
2447 ++*arg;
2448
2449 for (;;)
2450 {
2451 // Get the string up to the matching quote or to a single '{'.
2452 // "arg" is advanced to either the quote or the '{'.
2453 if (quote == '"')
2454 ret = eval_string(arg, &tv, evaluate, TRUE);
2455 else
2456 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2457 if (ret == FAIL)
2458 break;
2459 if (evaluate)
2460 {
2461 ga_concat(&ga, tv.vval.v_string);
2462 clear_tv(&tv);
2463 }
2464
2465 if (**arg != '{')
2466 {
2467 // found terminating quote
2468 ++*arg;
2469 break;
2470 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002471 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002472 if (p == NULL)
2473 {
2474 ret = FAIL;
2475 break;
2476 }
2477 *arg = p;
2478 }
LemonBoy2eaef102022-05-06 13:14:50 +01002479
2480 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002481 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2482 {
2483 ga_clear(&ga);
2484 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002485 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002486 }
LemonBoy2eaef102022-05-06 13:14:50 +01002487
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002488 rettv->vval.v_string = ga.ga_data;
2489 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002490}
2491
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002492/*
2493 * Return a string with the string representation of a variable.
2494 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2495 * "numbuf" is used for a number.
2496 * Puts quotes around strings, so that they can be parsed back by eval().
2497 * May return NULL.
2498 */
2499 char_u *
2500tv2string(
2501 typval_T *tv,
2502 char_u **tofree,
2503 char_u *numbuf,
2504 int copyID)
2505{
2506 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2507}
2508
2509/*
2510 * Get the value of an environment variable.
2511 * "arg" is pointing to the '$'. It is advanced to after the name.
2512 * If the environment variable was not set, silently assume it is empty.
2513 * Return FAIL if the name is invalid.
2514 */
2515 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002516eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002517{
2518 char_u *string = NULL;
2519 int len;
2520 int cc;
2521 char_u *name;
2522 int mustfree = FALSE;
2523
2524 ++*arg;
2525 name = *arg;
2526 len = get_env_len(arg);
2527 if (evaluate)
2528 {
2529 if (len == 0)
2530 return FAIL; // invalid empty name
2531
2532 cc = name[len];
2533 name[len] = NUL;
2534 // first try vim_getenv(), fast for normal environment vars
2535 string = vim_getenv(name, &mustfree);
2536 if (string != NULL && *string != NUL)
2537 {
2538 if (!mustfree)
2539 string = vim_strsave(string);
2540 }
2541 else
2542 {
2543 if (mustfree)
2544 vim_free(string);
2545
2546 // next try expanding things like $VIM and ${HOME}
2547 string = expand_env_save(name - 1);
2548 if (string != NULL && *string == '$')
2549 VIM_CLEAR(string);
2550 }
2551 name[len] = cc;
2552
2553 rettv->v_type = VAR_STRING;
2554 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002555 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002556 }
2557
2558 return OK;
2559}
2560
2561/*
2562 * Get the lnum from the first argument.
2563 * Also accepts ".", "$", etc., but that only works for the current buffer.
2564 * Returns -1 on error.
2565 */
2566 linenr_T
2567tv_get_lnum(typval_T *argvars)
2568{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002569 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002570 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002571
Bram Moolenaar56acb092020-08-16 14:48:19 +02002572 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2573 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002574 if (lnum <= 0 && did_emsg_before == did_emsg
2575 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002576 {
2577 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002578 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002579
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002580 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002581 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002582 if (fp != NULL)
2583 lnum = fp->lnum;
2584 }
2585 return lnum;
2586}
2587
2588/*
2589 * Get the lnum from the first argument.
2590 * Also accepts "$", then "buf" is used.
2591 * Returns 0 on error.
2592 */
2593 linenr_T
2594tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2595{
2596 if (argvars[0].v_type == VAR_STRING
2597 && argvars[0].vval.v_string != NULL
2598 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002599 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002600 && buf != NULL)
2601 return buf->b_ml.ml_line_count;
2602 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2603}
2604
2605/*
2606 * Get buffer by number or pattern.
2607 */
2608 buf_T *
2609tv_get_buf(typval_T *tv, int curtab_only)
2610{
2611 char_u *name = tv->vval.v_string;
2612 buf_T *buf;
2613
2614 if (tv->v_type == VAR_NUMBER)
2615 return buflist_findnr((int)tv->vval.v_number);
2616 if (tv->v_type != VAR_STRING)
2617 return NULL;
2618 if (name == NULL || *name == NUL)
2619 return curbuf;
2620 if (name[0] == '$' && name[1] == NUL)
2621 return lastbuf;
2622
2623 buf = buflist_find_by_name(name, curtab_only);
2624
2625 // If not found, try expanding the name, like done for bufexists().
2626 if (buf == NULL)
2627 buf = find_buffer(tv);
2628
2629 return buf;
2630}
2631
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002632/*
2633 * Like tv_get_buf() but give an error message is the type is wrong.
2634 */
2635 buf_T *
2636tv_get_buf_from_arg(typval_T *tv)
2637{
2638 buf_T *buf;
2639
2640 ++emsg_off;
2641 buf = tv_get_buf(tv, FALSE);
2642 --emsg_off;
2643 if (buf == NULL
2644 && tv->v_type != VAR_NUMBER
2645 && tv->v_type != VAR_STRING)
2646 // issue errmsg for type error
2647 (void)tv_get_number(tv);
2648 return buf;
2649}
2650
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002651#endif // FEAT_EVAL