blob: 98915ccca9022ef4d2c226c634c50cc3958be517 [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 Moolenaar00b28d62022-12-08 15:32:33 +0000165 break;
166 case VAR_OBJECT:
167 object_unref(varp->vval.v_object);
168 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200169 case VAR_UNKNOWN:
170 case VAR_ANY:
171 case VAR_VOID:
172 break;
173 }
174 varp->v_lock = 0;
175 }
176}
177
178/*
179 * Set the value of a variable to NULL without freeing items.
180 */
181 void
182init_tv(typval_T *varp)
183{
184 if (varp != NULL)
185 CLEAR_POINTER(varp);
186}
187
Bram Moolenaar36967b32020-08-17 21:41:02 +0200188 static varnumber_T
189tv_get_bool_or_number_chk(typval_T *varp, int *denote, int want_bool)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200190{
191 varnumber_T n = 0L;
192
193 switch (varp->v_type)
194 {
195 case VAR_NUMBER:
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200196 if (in_vim9script() && want_bool && varp->vval.v_number != 0
Bram Moolenaard70840e2020-08-22 15:06:35 +0200197 && varp->vval.v_number != 1)
198 {
199 semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
200 break;
201 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200202 return varp->vval.v_number;
203 case VAR_FLOAT:
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000204 emsg(_(e_using_float_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200205 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200206 case VAR_FUNC:
207 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000208 emsg(_(e_using_funcref_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200209 break;
210 case VAR_STRING:
Bram Moolenaar56acb092020-08-16 14:48:19 +0200211 if (in_vim9script())
212 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100213 emsg_using_string_as(varp, !want_bool);
Bram Moolenaar56acb092020-08-16 14:48:19 +0200214 break;
215 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200216 if (varp->vval.v_string != NULL)
217 vim_str2nr(varp->vval.v_string, NULL, NULL,
218 STR2NR_ALL, &n, NULL, 0, FALSE);
219 return n;
220 case VAR_LIST:
Bram Moolenaar677658a2022-01-05 16:09:06 +0000221 emsg(_(e_using_list_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200222 break;
223 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000224 emsg(_(e_using_dictionary_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200225 break;
226 case VAR_BOOL:
227 case VAR_SPECIAL:
Bram Moolenaar36967b32020-08-17 21:41:02 +0200228 if (!want_bool && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200229 {
Bram Moolenaard92cc132020-11-18 17:17:15 +0100230 if (varp->v_type == VAR_BOOL)
231 emsg(_(e_using_bool_as_number));
232 else
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000233 emsg(_(e_using_special_as_number));
Bram Moolenaar56acb092020-08-16 14:48:19 +0200234 break;
235 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200236 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
237 case VAR_JOB:
238#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000239 emsg(_(e_using_job_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200240 break;
241#endif
242 case VAR_CHANNEL:
243#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000244 emsg(_(e_using_channel_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200245 break;
246#endif
247 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000248 emsg(_(e_using_blob_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200249 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000250 case VAR_CLASS:
251 emsg(_(e_using_class_as_number));
252 break;
253 case VAR_OBJECT:
254 emsg(_(e_using_object_as_number));
255 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200256 case VAR_VOID:
257 emsg(_(e_cannot_use_void_value));
258 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200259 case VAR_UNKNOWN:
260 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200261 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200262 internal_error_no_abort("tv_get_number(UNKNOWN)");
263 break;
264 }
265 if (denote == NULL) // useful for values that must be unsigned
266 n = -1;
267 else
268 *denote = TRUE;
269 return n;
270}
271
Bram Moolenaar36967b32020-08-17 21:41:02 +0200272/*
273 * Get the number value of a variable.
274 * If it is a String variable, uses vim_str2nr().
275 * For incompatible types, return 0.
276 * tv_get_number_chk() is similar to tv_get_number(), but informs the
277 * caller of incompatible types: it sets *denote to TRUE if "denote"
278 * is not NULL or returns -1 otherwise.
279 */
280 varnumber_T
281tv_get_number(typval_T *varp)
282{
283 int error = FALSE;
284
285 return tv_get_number_chk(varp, &error); // return 0L on error
286}
287
288 varnumber_T
289tv_get_number_chk(typval_T *varp, int *denote)
290{
291 return tv_get_bool_or_number_chk(varp, denote, FALSE);
292}
293
294/*
295 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200296 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200297 */
298 varnumber_T
299tv_get_bool(typval_T *varp)
300{
301 return tv_get_bool_or_number_chk(varp, NULL, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200302}
303
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200304/*
305 * Get the boolean value of "varp". This is like tv_get_number_chk(),
306 * but in Vim9 script accepts Number and Bool.
307 */
308 varnumber_T
309tv_get_bool_chk(typval_T *varp, int *denote)
310{
311 return tv_get_bool_or_number_chk(varp, denote, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200312}
313
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000314 static float_T
315tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200316{
317 switch (varp->v_type)
318 {
319 case VAR_NUMBER:
320 return (float_T)(varp->vval.v_number);
321 case VAR_FLOAT:
322 return varp->vval.v_float;
323 case VAR_FUNC:
324 case VAR_PARTIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000325 emsg(_(e_using_funcref_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200326 break;
327 case VAR_STRING:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000328 emsg(_(e_using_string_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200329 break;
330 case VAR_LIST:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000331 emsg(_(e_using_list_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200332 break;
333 case VAR_DICT:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000334 emsg(_(e_using_dictionary_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200335 break;
336 case VAR_BOOL:
Bram Moolenaarb2810f12022-01-08 21:38:52 +0000337 emsg(_(e_using_boolean_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200338 break;
339 case VAR_SPECIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000340 emsg(_(e_using_special_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200341 break;
342 case VAR_JOB:
343# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000344 emsg(_(e_using_job_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200345 break;
346# endif
347 case VAR_CHANNEL:
348# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000349 emsg(_(e_using_channel_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200350 break;
351# endif
352 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000353 emsg(_(e_using_blob_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200354 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000355 case VAR_CLASS:
356 emsg(_(e_using_class_as_float));
357 break;
358 case VAR_OBJECT:
359 emsg(_(e_using_object_as_float));
360 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200361 case VAR_VOID:
362 emsg(_(e_cannot_use_void_value));
363 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200364 case VAR_UNKNOWN:
365 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200366 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200367 internal_error_no_abort("tv_get_float(UNKNOWN)");
368 break;
369 }
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000370 if (error != NULL)
371 *error = TRUE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200372 return 0;
373}
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000374
375 float_T
376tv_get_float(typval_T *varp)
377{
378 return tv_get_float_chk(varp, NULL);
379}
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200380
381/*
Ernie Rael51d04d12022-05-04 15:40:22 +0100382 * Give an error and return FAIL unless "args[idx]" is unknown
383 */
384 int
385check_for_unknown_arg(typval_T *args, int idx)
386{
387 if (args[idx].v_type != VAR_UNKNOWN)
388 {
389 semsg(_(e_too_many_arguments), idx + 1);
390 return FAIL;
391 }
392 return OK;
393}
394
395/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200396 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100397 */
398 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100399check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100400{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100401 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100402 {
rbtnnddc80af2021-12-17 18:01:31 +0000403 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100404 return FAIL;
405 }
406 return OK;
407}
408
409/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100410 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100411 */
412 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100413check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100414{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100415 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100416 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100417 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100418 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200419 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100420 return FAIL;
421 }
422 return OK;
423}
424
425/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200426 * Check for an optional string argument at 'idx'
427 */
428 int
429check_for_opt_string_arg(typval_T *args, int idx)
430{
431 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100432 || check_for_string_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200433}
434
435/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200436 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200437 */
438 int
439check_for_number_arg(typval_T *args, int idx)
440{
441 if (args[idx].v_type != VAR_NUMBER)
442 {
rbtnnddc80af2021-12-17 18:01:31 +0000443 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200444 return FAIL;
445 }
446 return OK;
447}
448
449/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200450 * Check for an optional number argument at 'idx'
451 */
452 int
453check_for_opt_number_arg(typval_T *args, int idx)
454{
455 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100456 || check_for_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200457}
458
459/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200460 * Give an error and return FAIL unless "args[idx]" is a float or a number.
461 */
462 int
463check_for_float_or_nr_arg(typval_T *args, int idx)
464{
465 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
466 {
rbtnnddc80af2021-12-17 18:01:31 +0000467 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200468 return FAIL;
469 }
470 return OK;
471}
472
473/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200474 * Give an error and return FAIL unless "args[idx]" is a bool.
475 */
476 int
477check_for_bool_arg(typval_T *args, int idx)
478{
479 if (args[idx].v_type != VAR_BOOL
480 && !(args[idx].v_type == VAR_NUMBER
481 && (args[idx].vval.v_number == 0
482 || args[idx].vval.v_number == 1)))
483 {
rbtnnddc80af2021-12-17 18:01:31 +0000484 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200485 return FAIL;
486 }
487 return OK;
488}
489
490/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200491 * Check for an optional bool argument at 'idx'.
492 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200493 */
494 int
495check_for_opt_bool_arg(typval_T *args, int idx)
496{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200497 if (args[idx].v_type == VAR_UNKNOWN)
498 return OK;
499 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200500}
501
502/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200503 * Give an error and return FAIL unless "args[idx]" is a blob.
504 */
505 int
506check_for_blob_arg(typval_T *args, int idx)
507{
508 if (args[idx].v_type != VAR_BLOB)
509 {
rbtnnddc80af2021-12-17 18:01:31 +0000510 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200511 return FAIL;
512 }
513 return OK;
514}
515
516/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200517 * Give an error and return FAIL unless "args[idx]" is a list.
518 */
519 int
520check_for_list_arg(typval_T *args, int idx)
521{
522 if (args[idx].v_type != VAR_LIST)
523 {
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200524 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200525 return FAIL;
526 }
527 return OK;
528}
529
530/*
Bram Moolenaard83392a2022-09-01 12:22:46 +0100531 * Give an error and return FAIL unless "args[idx]" is a non-NULL list.
532 */
533 int
534check_for_nonnull_list_arg(typval_T *args, int idx)
535{
536 if (check_for_list_arg(args, idx) == FAIL)
537 return FAIL;
538
539 if (args[idx].vval.v_list == NULL)
540 {
541 semsg(_(e_non_null_list_required_for_argument_nr), idx + 1);
542 return FAIL;
543 }
544 return OK;
545}
546
547/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200548 * Check for an optional list argument at 'idx'
549 */
550 int
551check_for_opt_list_arg(typval_T *args, int idx)
552{
553 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100554 || check_for_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200555}
556
557/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200558 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200559 */
560 int
561check_for_dict_arg(typval_T *args, int idx)
562{
563 if (args[idx].v_type != VAR_DICT)
564 {
rbtnnddc80af2021-12-17 18:01:31 +0000565 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200566 return FAIL;
567 }
568 return OK;
569}
570
571/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100572 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
573 */
574 int
575check_for_nonnull_dict_arg(typval_T *args, int idx)
576{
577 if (check_for_dict_arg(args, idx) == FAIL)
578 return FAIL;
579
580 if (args[idx].vval.v_dict == NULL)
581 {
582 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
583 return FAIL;
584 }
585 return OK;
586}
587
588/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200589 * Check for an optional dict argument at 'idx'
590 */
591 int
592check_for_opt_dict_arg(typval_T *args, int idx)
593{
594 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100595 || check_for_dict_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200596}
597
Dominique Pelle748b3082022-01-08 12:41:16 +0000598#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200599/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200600 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
601 */
602 int
603check_for_chan_or_job_arg(typval_T *args, int idx)
604{
605 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
606 {
rbtnnddc80af2021-12-17 18:01:31 +0000607 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200608 return FAIL;
609 }
610 return OK;
611}
612
613/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200614 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
615 * job.
616 */
617 int
618check_for_opt_chan_or_job_arg(typval_T *args, int idx)
619{
620 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100621 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200622}
623
624/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200625 * Give an error and return FAIL unless "args[idx]" is a job.
626 */
627 int
628check_for_job_arg(typval_T *args, int idx)
629{
630 if (args[idx].v_type != VAR_JOB)
631 {
rbtnnddc80af2021-12-17 18:01:31 +0000632 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200633 return FAIL;
634 }
635 return OK;
636}
637
638/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200639 * Check for an optional job argument at 'idx'.
640 */
641 int
642check_for_opt_job_arg(typval_T *args, int idx)
643{
644 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100645 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200646}
Bram Moolenaar3b8c7082022-11-30 20:20:56 +0000647#else
648/*
649 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
650 * job. Used without the +channel feature, thus only VAR_UNKNOWN is accepted.
651 */
652 int
653check_for_opt_chan_or_job_arg(typval_T *args, int idx)
654{
655 return args[idx].v_type == VAR_UNKNOWN ? OK : FAIL;
656}
Dominique Pelle748b3082022-01-08 12:41:16 +0000657#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200658
659/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200660 * Give an error and return FAIL unless "args[idx]" is a string or
661 * a number.
662 */
663 int
664check_for_string_or_number_arg(typval_T *args, int idx)
665{
666 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
667 {
rbtnnddc80af2021-12-17 18:01:31 +0000668 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200669 return FAIL;
670 }
671 return OK;
672}
673
674/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200675 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200676 */
677 int
678check_for_opt_string_or_number_arg(typval_T *args, int idx)
679{
680 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100681 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200682}
683
684/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200685 * Give an error and return FAIL unless "args[idx]" is a buffer number.
686 * Buffer number can be a number or a string.
687 */
688 int
689check_for_buffer_arg(typval_T *args, int idx)
690{
691 return check_for_string_or_number_arg(args, idx);
692}
693
694/*
695 * Check for an optional buffer argument at 'idx'
696 */
697 int
698check_for_opt_buffer_arg(typval_T *args, int idx)
699{
700 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100701 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200702}
703
704/*
705 * Give an error and return FAIL unless "args[idx]" is a line number.
706 * Line number can be a number or a string.
707 */
708 int
709check_for_lnum_arg(typval_T *args, int idx)
710{
711 return check_for_string_or_number_arg(args, idx);
712}
713
714/*
715 * Check for an optional line number argument at 'idx'
716 */
717 int
718check_for_opt_lnum_arg(typval_T *args, int idx)
719{
720 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100721 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200722}
723
Dominique Pelle748b3082022-01-08 12:41:16 +0000724#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200725/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200726 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200727 */
728 int
729check_for_string_or_blob_arg(typval_T *args, int idx)
730{
731 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
732 {
rbtnnddc80af2021-12-17 18:01:31 +0000733 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200734 return FAIL;
735 }
736 return OK;
737}
Dominique Pelle748b3082022-01-08 12:41:16 +0000738#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200739
740/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200741 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200742 */
743 int
744check_for_string_or_list_arg(typval_T *args, int idx)
745{
746 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
747 {
rbtnnddc80af2021-12-17 18:01:31 +0000748 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200749 return FAIL;
750 }
751 return OK;
752}
753
754/*
rbtnn0ccb5842021-12-18 18:33:46 +0000755 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
756 * blob.
757 */
758 int
759check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
760{
761 if (args[idx].v_type != VAR_STRING
762 && args[idx].v_type != VAR_LIST
763 && args[idx].v_type != VAR_BLOB)
764 {
765 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
766 return FAIL;
767 }
768 return OK;
769}
770
771/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200772 * Check for an optional string or list argument at 'idx'
773 */
774 int
775check_for_opt_string_or_list_arg(typval_T *args, int idx)
776{
777 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100778 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200779}
780
781/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200782 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
783 */
784 int
785check_for_string_or_dict_arg(typval_T *args, int idx)
786{
787 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
788 {
rbtnnddc80af2021-12-17 18:01:31 +0000789 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200790 return FAIL;
791 }
792 return OK;
793}
794
795/*
796 * Give an error and return FAIL unless "args[idx]" is a string or a number
797 * or a list.
798 */
799 int
800check_for_string_or_number_or_list_arg(typval_T *args, int idx)
801{
802 if (args[idx].v_type != VAR_STRING
803 && args[idx].v_type != VAR_NUMBER
804 && args[idx].v_type != VAR_LIST)
805 {
rbtnn0ccb5842021-12-18 18:33:46 +0000806 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200807 return FAIL;
808 }
809 return OK;
810}
811
812/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200813 * Give an error and return FAIL unless "args[idx]" is an optional string
814 * or number or a list
815 */
816 int
817check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
818{
819 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100820 || check_for_string_or_number_or_list_arg(args, idx)
821 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200822}
823
824/*
Bakudankun375141e2022-09-09 18:46:47 +0100825 * Give an error and return FAIL unless "args[idx]" is a string or a number
826 * or a list or a blob.
827 */
828 int
829check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
830{
831 if (args[idx].v_type != VAR_STRING
832 && args[idx].v_type != VAR_NUMBER
833 && args[idx].v_type != VAR_LIST
834 && args[idx].v_type != VAR_BLOB)
835 {
836 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
837 return FAIL;
838 }
839 return OK;
840}
841
842/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200843 * Give an error and return FAIL unless "args[idx]" is a string or a list
844 * or a dict.
845 */
846 int
847check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
848{
849 if (args[idx].v_type != VAR_STRING
850 && args[idx].v_type != VAR_LIST
851 && args[idx].v_type != VAR_DICT)
852 {
rbtnnddc80af2021-12-17 18:01:31 +0000853 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200854 return FAIL;
855 }
856 return OK;
857}
858
859/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000860 * Give an error and return FAIL unless "args[idx]" is a string
861 * or a function reference.
862 */
863 int
864check_for_string_or_func_arg(typval_T *args, int idx)
865{
866 if (args[idx].v_type != VAR_PARTIAL
867 && args[idx].v_type != VAR_FUNC
868 && args[idx].v_type != VAR_STRING)
869 {
870 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
871 return FAIL;
872 }
873 return OK;
874}
875
876/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200877 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
878 */
879 int
880check_for_list_or_blob_arg(typval_T *args, int idx)
881{
882 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
883 {
rbtnn0ccb5842021-12-18 18:33:46 +0000884 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200885 return FAIL;
886 }
887 return OK;
888}
889
890/*
891 * Give an error and return FAIL unless "args[idx]" is a list or dict
892 */
893 int
894check_for_list_or_dict_arg(typval_T *args, int idx)
895{
896 if (args[idx].v_type != VAR_LIST
897 && args[idx].v_type != VAR_DICT)
898 {
rbtnnddc80af2021-12-17 18:01:31 +0000899 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200900 return FAIL;
901 }
902 return OK;
903}
904
905/*
906 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
907 * blob.
908 */
909 int
910check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
911{
912 if (args[idx].v_type != VAR_LIST
913 && args[idx].v_type != VAR_DICT
914 && args[idx].v_type != VAR_BLOB)
915 {
rbtnnddc80af2021-12-17 18:01:31 +0000916 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200917 return FAIL;
918 }
919 return OK;
920}
921
922/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000923 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
924 * blob or a string.
925 */
926 int
927check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
928{
929 if (args[idx].v_type != VAR_LIST
930 && args[idx].v_type != VAR_DICT
931 && args[idx].v_type != VAR_BLOB
932 && args[idx].v_type != VAR_STRING)
933 {
rbtnnddc80af2021-12-17 18:01:31 +0000934 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000935 return FAIL;
936 }
937 return OK;
938}
939
940/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200941 * Give an error and return FAIL unless "args[idx]" is an optional buffer
942 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200943 */
944 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200945check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200946{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200947 if (args[idx].v_type != VAR_UNKNOWN
948 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200949 && args[idx].v_type != VAR_NUMBER
950 && args[idx].v_type != VAR_DICT)
951 {
rbtnnddc80af2021-12-17 18:01:31 +0000952 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200953 return FAIL;
954 }
955 return OK;
956}
957
958/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200959 * Get the string value of a variable.
960 * If it is a Number variable, the number is converted into a string.
961 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
962 * tv_get_string_buf() uses a given buffer.
963 * If the String variable has never been set, return an empty string.
964 * Never returns NULL;
965 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
966 * NULL on error.
967 */
968 char_u *
969tv_get_string(typval_T *varp)
970{
971 static char_u mybuf[NUMBUFLEN];
972
973 return tv_get_string_buf(varp, mybuf);
974}
975
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100976/*
977 * Like tv_get_string() but don't allow number to string conversion for Vim9.
978 */
979 char_u *
980tv_get_string_strict(typval_T *varp)
981{
982 static char_u mybuf[NUMBUFLEN];
983 char_u *res = tv_get_string_buf_chk_strict(
984 varp, mybuf, in_vim9script());
985
986 return res != NULL ? res : (char_u *)"";
987}
988
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200989 char_u *
990tv_get_string_buf(typval_T *varp, char_u *buf)
991{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200992 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200993
994 return res != NULL ? res : (char_u *)"";
995}
996
997/*
998 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
999 */
1000 char_u *
1001tv_get_string_chk(typval_T *varp)
1002{
1003 static char_u mybuf[NUMBUFLEN];
1004
1005 return tv_get_string_buf_chk(varp, mybuf);
1006}
1007
1008 char_u *
1009tv_get_string_buf_chk(typval_T *varp, char_u *buf)
1010{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001011 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
1012}
1013
1014 char_u *
1015tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
1016{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001017 switch (varp->v_type)
1018 {
1019 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001020 if (strict)
1021 {
1022 emsg(_(e_using_number_as_string));
1023 break;
1024 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001025 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1026 (varnumber_T)varp->vval.v_number);
1027 return buf;
1028 case VAR_FUNC:
1029 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001030 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001031 break;
1032 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001033 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001034 break;
1035 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001036 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001037 break;
1038 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001039 if (strict)
1040 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001041 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001042 break;
1043 }
1044 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1045 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001046 case VAR_STRING:
1047 if (varp->vval.v_string != NULL)
1048 return varp->vval.v_string;
1049 return (char_u *)"";
1050 case VAR_BOOL:
1051 case VAR_SPECIAL:
1052 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1053 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001054 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001055 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001056 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001057 case VAR_CLASS:
1058 emsg(_(e_using_class_as_string));
1059 break;
1060 case VAR_OBJECT:
1061 emsg(_(e_using_object_as_string));
1062 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001063 case VAR_JOB:
1064#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001065 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001066 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001067 semsg(_(e_using_invalid_value_as_string_str), "job");
1068 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001069 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001070 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001071#endif
1072 break;
1073 case VAR_CHANNEL:
1074#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001075 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001076 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001077 semsg(_(e_using_invalid_value_as_string_str), "channel");
1078 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001079 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001080 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001081#endif
1082 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001083 case VAR_VOID:
1084 emsg(_(e_cannot_use_void_value));
1085 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001086 case VAR_UNKNOWN:
1087 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001088 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001089 semsg(_(e_using_invalid_value_as_string_str),
1090 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001091 break;
1092 }
1093 return NULL;
1094}
1095
1096/*
1097 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1098 * string() on Dict, List, etc.
1099 */
1100 char_u *
1101tv_stringify(typval_T *varp, char_u *buf)
1102{
1103 if (varp->v_type == VAR_LIST
1104 || varp->v_type == VAR_DICT
1105 || varp->v_type == VAR_BLOB
1106 || varp->v_type == VAR_FUNC
1107 || varp->v_type == VAR_PARTIAL
1108 || varp->v_type == VAR_FLOAT)
1109 {
1110 typval_T tmp;
1111
1112 f_string(varp, &tmp);
1113 tv_get_string_buf(&tmp, buf);
1114 clear_tv(varp);
1115 *varp = tmp;
1116 return tmp.vval.v_string;
1117 }
1118 return tv_get_string_buf(varp, buf);
1119}
1120
1121/*
1122 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1123 * Also give an error message, using "name" or _("name") when use_gettext is
1124 * TRUE.
1125 */
1126 int
1127tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1128{
1129 int lock = 0;
1130
1131 switch (tv->v_type)
1132 {
1133 case VAR_BLOB:
1134 if (tv->vval.v_blob != NULL)
1135 lock = tv->vval.v_blob->bv_lock;
1136 break;
1137 case VAR_LIST:
1138 if (tv->vval.v_list != NULL)
1139 lock = tv->vval.v_list->lv_lock;
1140 break;
1141 case VAR_DICT:
1142 if (tv->vval.v_dict != NULL)
1143 lock = tv->vval.v_dict->dv_lock;
1144 break;
1145 default:
1146 break;
1147 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001148 return value_check_lock(tv->v_lock, name, use_gettext)
1149 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001150}
1151
1152/*
1153 * Copy the values from typval_T "from" to typval_T "to".
1154 * When needed allocates string or increases reference count.
1155 * Does not make a copy of a list, blob or dict but copies the reference!
1156 * It is OK for "from" and "to" to point to the same item. This is used to
1157 * make a copy later.
1158 */
1159 void
1160copy_tv(typval_T *from, typval_T *to)
1161{
1162 to->v_type = from->v_type;
1163 to->v_lock = 0;
1164 switch (from->v_type)
1165 {
1166 case VAR_NUMBER:
1167 case VAR_BOOL:
1168 case VAR_SPECIAL:
1169 to->vval.v_number = from->vval.v_number;
1170 break;
1171 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001172 to->vval.v_float = from->vval.v_float;
1173 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001174 case VAR_JOB:
1175#ifdef FEAT_JOB_CHANNEL
1176 to->vval.v_job = from->vval.v_job;
1177 if (to->vval.v_job != NULL)
1178 ++to->vval.v_job->jv_refcount;
1179 break;
1180#endif
1181 case VAR_CHANNEL:
1182#ifdef FEAT_JOB_CHANNEL
1183 to->vval.v_channel = from->vval.v_channel;
1184 if (to->vval.v_channel != NULL)
1185 ++to->vval.v_channel->ch_refcount;
1186 break;
1187#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001188 case VAR_INSTR:
1189 to->vval.v_instr = from->vval.v_instr;
1190 break;
1191
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001192 case VAR_CLASS:
1193 copy_class(from, to);
1194 break;
1195
1196 case VAR_OBJECT:
1197 copy_object(from, to);
1198 break;
1199
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001200 case VAR_STRING:
1201 case VAR_FUNC:
1202 if (from->vval.v_string == NULL)
1203 to->vval.v_string = NULL;
1204 else
1205 {
1206 to->vval.v_string = vim_strsave(from->vval.v_string);
1207 if (from->v_type == VAR_FUNC)
1208 func_ref(to->vval.v_string);
1209 }
1210 break;
1211 case VAR_PARTIAL:
1212 if (from->vval.v_partial == NULL)
1213 to->vval.v_partial = NULL;
1214 else
1215 {
1216 to->vval.v_partial = from->vval.v_partial;
1217 ++to->vval.v_partial->pt_refcount;
1218 }
1219 break;
1220 case VAR_BLOB:
1221 if (from->vval.v_blob == NULL)
1222 to->vval.v_blob = NULL;
1223 else
1224 {
1225 to->vval.v_blob = from->vval.v_blob;
1226 ++to->vval.v_blob->bv_refcount;
1227 }
1228 break;
1229 case VAR_LIST:
1230 if (from->vval.v_list == NULL)
1231 to->vval.v_list = NULL;
1232 else
1233 {
1234 to->vval.v_list = from->vval.v_list;
1235 ++to->vval.v_list->lv_refcount;
1236 }
1237 break;
1238 case VAR_DICT:
1239 if (from->vval.v_dict == NULL)
1240 to->vval.v_dict = NULL;
1241 else
1242 {
1243 to->vval.v_dict = from->vval.v_dict;
1244 ++to->vval.v_dict->dv_refcount;
1245 }
1246 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001247 case VAR_VOID:
1248 emsg(_(e_cannot_use_void_value));
1249 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001250 case VAR_UNKNOWN:
1251 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001252 internal_error_no_abort("copy_tv(UNKNOWN)");
1253 break;
1254 }
1255}
1256
1257/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001258 * Compare "tv1" and "tv2".
1259 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001260 */
1261 int
1262typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001263 typval_T *tv1, // first operand
1264 typval_T *tv2, // second operand
1265 exprtype_T type, // operator
1266 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001267{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001268 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001269 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001270 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1271
Bram Moolenaar265f8112021-12-19 12:33:05 +00001272 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001273 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001274 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001275 // it means TRUE.
1276 n1 = (type == EXPR_ISNOT);
1277 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001278 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1279 || (tv2->v_type == VAR_SPECIAL
1280 && tv2->vval.v_number == VVAL_NULL))
1281 && tv1->v_type != tv2->v_type
1282 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1283 {
1284 n1 = typval_compare_null(tv1, tv2);
1285 if (n1 == MAYBE)
1286 {
1287 clear_tv(tv1);
1288 return FAIL;
1289 }
1290 if (type == EXPR_NEQUAL)
1291 n1 = !n1;
1292 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001293 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001294 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001295 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001296 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001297 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001298 return FAIL;
1299 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001300 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001301 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001302 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001303 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001304 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001305 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001306 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001307 return FAIL;
1308 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001309 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001310 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001311 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001312 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001313 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001314 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001315 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001316 return FAIL;
1317 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001318 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001319 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001320 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1321 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001322 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001323 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001324 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001325 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001326 return FAIL;
1327 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001328 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001329 }
1330
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001331 // If one of the two variables is a float, compare as a float.
1332 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001333 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001334 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1335 {
1336 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001337 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001338
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001339 f1 = tv_get_float_chk(tv1, &error);
1340 if (!error)
1341 f2 = tv_get_float_chk(tv2, &error);
1342 if (error)
1343 {
1344 clear_tv(tv1);
1345 return FAIL;
1346 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001347 n1 = FALSE;
1348 switch (type)
1349 {
1350 case EXPR_IS:
1351 case EXPR_EQUAL: n1 = (f1 == f2); break;
1352 case EXPR_ISNOT:
1353 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1354 case EXPR_GREATER: n1 = (f1 > f2); break;
1355 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1356 case EXPR_SMALLER: n1 = (f1 < f2); break;
1357 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1358 case EXPR_UNKNOWN:
1359 case EXPR_MATCH:
1360 default: break; // avoid gcc warning
1361 }
1362 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001363
1364 // If one of the two variables is a number, compare as a number.
1365 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001366 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001367 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1368 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001369 int error = FALSE;
1370
1371 n1 = tv_get_number_chk(tv1, &error);
1372 if (!error)
1373 n2 = tv_get_number_chk(tv2, &error);
1374 if (error)
1375 {
1376 clear_tv(tv1);
1377 return FAIL;
1378 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001379 switch (type)
1380 {
1381 case EXPR_IS:
1382 case EXPR_EQUAL: n1 = (n1 == n2); break;
1383 case EXPR_ISNOT:
1384 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1385 case EXPR_GREATER: n1 = (n1 > n2); break;
1386 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1387 case EXPR_SMALLER: n1 = (n1 < n2); break;
1388 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1389 case EXPR_UNKNOWN:
1390 case EXPR_MATCH:
1391 default: break; // avoid gcc warning
1392 }
1393 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001394 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1395 || tv2->v_type == VAR_BOOL
1396 || (tv1->v_type == VAR_SPECIAL
1397 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001398 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001399 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001400 {
1401 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001402 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1403 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001404 return FAIL;
1405 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001406 n1 = tv1->vval.v_number;
1407 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001408 switch (type)
1409 {
1410 case EXPR_IS:
1411 case EXPR_EQUAL: n1 = (n1 == n2); break;
1412 case EXPR_ISNOT:
1413 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1414 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001415 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001416 vartype_name(tv1->v_type));
1417 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001418 return FAIL;
1419 }
1420 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001421#ifdef FEAT_JOB_CHANNEL
1422 else if (tv1->v_type == tv2->v_type
1423 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1424 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1425 {
1426 if (tv1->v_type == VAR_CHANNEL)
1427 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1428 else
1429 n1 = tv1->vval.v_job == tv2->vval.v_job;
1430 if (type == EXPR_NEQUAL)
1431 n1 = !n1;
1432 }
1433#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001434 else
1435 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001436 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001437 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001438 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001439 return FAIL;
1440 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001441 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001442 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001443 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001444 if (in_vim9script())
1445 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001446 tv1->v_type = VAR_BOOL;
1447 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001448 }
1449 else
1450 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001451 tv1->v_type = VAR_NUMBER;
1452 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001453 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001454
1455 return OK;
1456}
1457
Bram Moolenaar34453202021-01-31 13:08:38 +01001458/*
dundargocc57b5bc2022-11-02 13:30:51 +00001459 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001460 * Put the result, false or true, in "res".
1461 * Return FAIL and give an error message when the comparison can't be done.
1462 */
1463 int
1464typval_compare_list(
1465 typval_T *tv1,
1466 typval_T *tv2,
1467 exprtype_T type,
1468 int ic,
1469 int *res)
1470{
1471 int val = 0;
1472
1473 if (type == EXPR_IS || type == EXPR_ISNOT)
1474 {
1475 val = (tv1->v_type == tv2->v_type
1476 && tv1->vval.v_list == tv2->vval.v_list);
1477 if (type == EXPR_ISNOT)
1478 val = !val;
1479 }
1480 else if (tv1->v_type != tv2->v_type
1481 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1482 {
1483 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001484 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001485 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001486 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001487 return FAIL;
1488 }
1489 else
1490 {
1491 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1492 ic, FALSE);
1493 if (type == EXPR_NEQUAL)
1494 val = !val;
1495 }
1496 *res = val;
1497 return OK;
1498}
1499
1500/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001501 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001502 */
1503 int
1504typval_compare_null(typval_T *tv1, typval_T *tv2)
1505{
1506 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1507 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1508 {
1509 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1510
1511 switch (tv->v_type)
1512 {
1513 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001514#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001515 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001516#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001517 case VAR_DICT: return tv->vval.v_dict == NULL;
1518 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001519#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001520 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001521#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001522 case VAR_LIST: return tv->vval.v_list == NULL;
1523 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1524 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001525
1526 case VAR_NUMBER: if (!in_vim9script())
1527 return tv->vval.v_number == 0;
1528 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001529 case VAR_FLOAT: if (!in_vim9script())
1530 return tv->vval.v_float == 0.0;
1531 break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001532 default: break;
1533 }
1534 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001535 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001536 // we won't give an error
1537 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001538}
1539
1540/*
dundargocc57b5bc2022-11-02 13:30:51 +00001541 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001542 * Put the result, false or true, in "res".
1543 * Return FAIL and give an error message when the comparison can't be done.
1544 */
1545 int
1546typval_compare_blob(
1547 typval_T *tv1,
1548 typval_T *tv2,
1549 exprtype_T type,
1550 int *res)
1551{
1552 int val = 0;
1553
1554 if (type == EXPR_IS || type == EXPR_ISNOT)
1555 {
1556 val = (tv1->v_type == tv2->v_type
1557 && tv1->vval.v_blob == tv2->vval.v_blob);
1558 if (type == EXPR_ISNOT)
1559 val = !val;
1560 }
1561 else if (tv1->v_type != tv2->v_type
1562 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1563 {
1564 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001565 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001566 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001567 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001568 return FAIL;
1569 }
1570 else
1571 {
1572 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1573 if (type == EXPR_NEQUAL)
1574 val = !val;
1575 }
1576 *res = val;
1577 return OK;
1578}
1579
1580/*
dundargocc57b5bc2022-11-02 13:30:51 +00001581 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001582 * Put the result, false or true, in "res".
1583 * Return FAIL and give an error message when the comparison can't be done.
1584 */
1585 int
1586typval_compare_dict(
1587 typval_T *tv1,
1588 typval_T *tv2,
1589 exprtype_T type,
1590 int ic,
1591 int *res)
1592{
1593 int val;
1594
1595 if (type == EXPR_IS || type == EXPR_ISNOT)
1596 {
1597 val = (tv1->v_type == tv2->v_type
1598 && tv1->vval.v_dict == tv2->vval.v_dict);
1599 if (type == EXPR_ISNOT)
1600 val = !val;
1601 }
1602 else if (tv1->v_type != tv2->v_type
1603 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1604 {
1605 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001606 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001607 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001608 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001609 return FAIL;
1610 }
1611 else
1612 {
1613 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1614 if (type == EXPR_NEQUAL)
1615 val = !val;
1616 }
1617 *res = val;
1618 return OK;
1619}
1620
1621/*
dundargocc57b5bc2022-11-02 13:30:51 +00001622 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001623 * Put the result, false or true, in "res".
1624 * Return FAIL and give an error message when the comparison can't be done.
1625 */
1626 int
1627typval_compare_func(
1628 typval_T *tv1,
1629 typval_T *tv2,
1630 exprtype_T type,
1631 int ic,
1632 int *res)
1633{
1634 int val = 0;
1635
1636 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1637 && type != EXPR_IS && type != EXPR_ISNOT)
1638 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001639 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001640 return FAIL;
1641 }
1642 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1643 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1644 // When both partials are NULL, then they are equal.
1645 // Otherwise they are not equal.
1646 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1647 else if (type == EXPR_IS || type == EXPR_ISNOT)
1648 {
1649 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1650 // strings are considered the same if their value is
1651 // the same
1652 val = tv_equal(tv1, tv2, ic, FALSE);
1653 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1654 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1655 else
1656 val = FALSE;
1657 }
1658 else
1659 val = tv_equal(tv1, tv2, ic, FALSE);
1660 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1661 val = !val;
1662 *res = val;
1663 return OK;
1664}
1665
1666/*
1667 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1668 * Put the result, false or true, in "res".
1669 * Return FAIL and give an error message when the comparison can't be done.
1670 */
1671 int
1672typval_compare_string(
1673 typval_T *tv1,
1674 typval_T *tv2,
1675 exprtype_T type,
1676 int ic,
1677 int *res)
1678{
1679 int i = 0;
1680 int val = FALSE;
1681 char_u *s1, *s2;
1682 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1683
1684 if (in_vim9script()
1685 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1686 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1687 {
1688 semsg(_(e_cannot_compare_str_with_str),
1689 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1690 return FAIL;
1691 }
1692 s1 = tv_get_string_buf(tv1, buf1);
1693 s2 = tv_get_string_buf(tv2, buf2);
1694 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1695 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1696 switch (type)
1697 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001698 case EXPR_IS: if (in_vim9script())
1699 {
1700 // Really check it is the same string, not just
1701 // the same value.
1702 val = tv1->vval.v_string == tv2->vval.v_string;
1703 break;
1704 }
1705 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001706 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001707 case EXPR_ISNOT: if (in_vim9script())
1708 {
1709 // Really check it is not the same string, not
1710 // just a different value.
1711 val = tv1->vval.v_string != tv2->vval.v_string;
1712 break;
1713 }
1714 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001715 case EXPR_NEQUAL: val = (i != 0); break;
1716 case EXPR_GREATER: val = (i > 0); break;
1717 case EXPR_GEQUAL: val = (i >= 0); break;
1718 case EXPR_SMALLER: val = (i < 0); break;
1719 case EXPR_SEQUAL: val = (i <= 0); break;
1720
1721 case EXPR_MATCH:
1722 case EXPR_NOMATCH:
1723 val = pattern_match(s2, s1, ic);
1724 if (type == EXPR_NOMATCH)
1725 val = !val;
1726 break;
1727
1728 default: break; // avoid gcc warning
1729 }
1730 *res = val;
1731 return OK;
1732}
1733/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001734 * Convert any type to a string, never give an error.
1735 * When "quotes" is TRUE add quotes to a string.
1736 * Returns an allocated string.
1737 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001738 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001739typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001740{
1741 char_u *tofree;
1742 char_u numbuf[NUMBUFLEN];
1743 char_u *ret = NULL;
1744
1745 if (arg == NULL)
1746 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001747 if (!quotes && arg->v_type == VAR_STRING)
1748 {
1749 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1750 : arg->vval.v_string);
1751 }
1752 else
1753 {
1754 ret = tv2string(arg, &tofree, numbuf, 0);
1755 // Make a copy if we have a value but it's not in allocated memory.
1756 if (ret != NULL && tofree == NULL)
1757 ret = vim_strsave(ret);
1758 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001759 return ret;
1760}
1761
1762/*
1763 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1764 * or it refers to a List or Dictionary that is locked.
1765 */
1766 int
1767tv_islocked(typval_T *tv)
1768{
1769 return (tv->v_lock & VAR_LOCKED)
1770 || (tv->v_type == VAR_LIST
1771 && tv->vval.v_list != NULL
1772 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1773 || (tv->v_type == VAR_DICT
1774 && tv->vval.v_dict != NULL
1775 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1776}
1777
1778 static int
1779func_equal(
1780 typval_T *tv1,
1781 typval_T *tv2,
1782 int ic) // ignore case
1783{
1784 char_u *s1, *s2;
1785 dict_T *d1, *d2;
1786 int a1, a2;
1787 int i;
1788
1789 // empty and NULL function name considered the same
1790 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1791 : partial_name(tv1->vval.v_partial);
1792 if (s1 != NULL && *s1 == NUL)
1793 s1 = NULL;
1794 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1795 : partial_name(tv2->vval.v_partial);
1796 if (s2 != NULL && *s2 == NUL)
1797 s2 = NULL;
1798 if (s1 == NULL || s2 == NULL)
1799 {
1800 if (s1 != s2)
1801 return FALSE;
1802 }
1803 else if (STRCMP(s1, s2) != 0)
1804 return FALSE;
1805
1806 // empty dict and NULL dict is different
1807 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1808 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1809 if (d1 == NULL || d2 == NULL)
1810 {
1811 if (d1 != d2)
1812 return FALSE;
1813 }
1814 else if (!dict_equal(d1, d2, ic, TRUE))
1815 return FALSE;
1816
1817 // empty list and no list considered the same
1818 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1819 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1820 if (a1 != a2)
1821 return FALSE;
1822 for (i = 0; i < a1; ++i)
1823 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1824 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1825 return FALSE;
1826
1827 return TRUE;
1828}
1829
1830/*
1831 * Return TRUE if "tv1" and "tv2" have the same value.
1832 * Compares the items just like "==" would compare them, but strings and
1833 * numbers are different. Floats and numbers are also different.
1834 */
1835 int
1836tv_equal(
1837 typval_T *tv1,
1838 typval_T *tv2,
1839 int ic, // ignore case
1840 int recursive) // TRUE when used recursively
1841{
1842 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1843 char_u *s1, *s2;
1844 static int recursive_cnt = 0; // catch recursive loops
1845 int r;
1846 static int tv_equal_recurse_limit;
1847
1848 // Catch lists and dicts that have an endless loop by limiting
1849 // recursiveness to a limit. We guess they are equal then.
1850 // A fixed limit has the problem of still taking an awful long time.
1851 // Reduce the limit every time running into it. That should work fine for
1852 // deeply linked structures that are not recursively linked and catch
1853 // recursiveness quickly.
1854 if (!recursive)
1855 tv_equal_recurse_limit = 1000;
1856 if (recursive_cnt >= tv_equal_recurse_limit)
1857 {
1858 --tv_equal_recurse_limit;
1859 return TRUE;
1860 }
1861
1862 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1863 // arguments.
1864 if ((tv1->v_type == VAR_FUNC
1865 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1866 && (tv2->v_type == VAR_FUNC
1867 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1868 {
1869 ++recursive_cnt;
1870 r = func_equal(tv1, tv2, ic);
1871 --recursive_cnt;
1872 return r;
1873 }
1874
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001875 if (tv1->v_type != tv2->v_type
1876 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1877 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001878 return FALSE;
1879
1880 switch (tv1->v_type)
1881 {
1882 case VAR_LIST:
1883 ++recursive_cnt;
1884 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1885 --recursive_cnt;
1886 return r;
1887
1888 case VAR_DICT:
1889 ++recursive_cnt;
1890 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1891 --recursive_cnt;
1892 return r;
1893
1894 case VAR_BLOB:
1895 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1896
1897 case VAR_NUMBER:
1898 case VAR_BOOL:
1899 case VAR_SPECIAL:
1900 return tv1->vval.v_number == tv2->vval.v_number;
1901
1902 case VAR_STRING:
1903 s1 = tv_get_string_buf(tv1, buf1);
1904 s2 = tv_get_string_buf(tv2, buf2);
1905 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1906
1907 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001908 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001909 case VAR_JOB:
1910#ifdef FEAT_JOB_CHANNEL
1911 return tv1->vval.v_job == tv2->vval.v_job;
1912#endif
1913 case VAR_CHANNEL:
1914#ifdef FEAT_JOB_CHANNEL
1915 return tv1->vval.v_channel == tv2->vval.v_channel;
1916#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001917 case VAR_INSTR:
1918 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001919
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001920 case VAR_CLASS:
1921 return tv1->vval.v_class == tv2->vval.v_class;
1922
1923 case VAR_OBJECT:
1924 // TODO: compare values
1925 return tv1->vval.v_object == tv2->vval.v_object;
1926
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001927 case VAR_PARTIAL:
1928 return tv1->vval.v_partial == tv2->vval.v_partial;
1929
1930 case VAR_FUNC:
1931 return tv1->vval.v_string == tv2->vval.v_string;
1932
1933 case VAR_UNKNOWN:
1934 case VAR_ANY:
1935 case VAR_VOID:
1936 break;
1937 }
1938
1939 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1940 // does not equal anything, not even itself.
1941 return FALSE;
1942}
1943
1944/*
1945 * Get an option value.
1946 * "arg" points to the '&' or '+' before the option name.
1947 * "arg" is advanced to character after the option name.
1948 * Return OK or FAIL.
1949 */
1950 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001951eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001952 char_u **arg,
1953 typval_T *rettv, // when NULL, only check if option exists
1954 int evaluate)
1955{
1956 char_u *option_end;
1957 long numval;
1958 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001959 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001960 int c;
1961 int working = (**arg == '+'); // has("+option")
1962 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001963 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001964
1965 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001966 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001967 if (option_end == NULL)
1968 {
1969 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001970 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001971 return FAIL;
1972 }
1973
1974 if (!evaluate)
1975 {
1976 *arg = option_end;
1977 return OK;
1978 }
1979
1980 c = *option_end;
1981 *option_end = NUL;
1982 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001983 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001984
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001985 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001986 {
1987 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001988 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001989 ret = FAIL;
1990 }
1991 else if (rettv != NULL)
1992 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001993 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001994 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001995 {
1996 rettv->v_type = VAR_STRING;
1997 rettv->vval.v_string = NULL;
1998 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001999 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002000 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002001 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2002 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002003 rettv->vval.v_number = 0;
2004 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002005 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002006 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002007 if (in_vim9script() && opt_type == gov_bool)
2008 {
2009 rettv->v_type = VAR_BOOL;
2010 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2011 }
2012 else
2013 {
2014 rettv->v_type = VAR_NUMBER;
2015 rettv->vval.v_number = numval;
2016 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002017 }
2018 else // string option
2019 {
2020 rettv->v_type = VAR_STRING;
2021 rettv->vval.v_string = stringval;
2022 }
2023 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002024 else if (working && (opt_type == gov_hidden_bool
2025 || opt_type == gov_hidden_number
2026 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002027 ret = FAIL;
2028
2029 *option_end = c; // put back for error messages
2030 *arg = option_end;
2031
2032 return ret;
2033}
2034
2035/*
2036 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2037 * Return OK or FAIL.
2038 */
2039 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002040eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002041 char_u **arg,
2042 typval_T *rettv,
2043 int evaluate,
2044 int want_string UNUSED)
2045{
2046 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002047 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002048 char_u *p;
2049 int get_float = FALSE;
2050
2051 // We accept a float when the format matches
2052 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2053 // strict to avoid backwards compatibility problems.
2054 // With script version 2 and later the leading digit can be
2055 // omitted.
2056 // Don't look for a float after the "." operator, so that
2057 // ":let vers = 1.2.3" doesn't fail.
2058 if (**arg == '.')
2059 p = *arg;
2060 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002061 {
2062 p = *arg + 1;
2063 if (skip_quotes)
2064 for (;;)
2065 {
2066 if (*p == '\'')
2067 ++p;
2068 if (!vim_isdigit(*p))
2069 break;
2070 p = skipdigits(p);
2071 }
2072 else
2073 p = skipdigits(p);
2074 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002075 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2076 {
2077 get_float = TRUE;
2078 p = skipdigits(p + 2);
2079 if (*p == 'e' || *p == 'E')
2080 {
2081 ++p;
2082 if (*p == '-' || *p == '+')
2083 ++p;
2084 if (!vim_isdigit(*p))
2085 get_float = FALSE;
2086 else
2087 p = skipdigits(p + 1);
2088 }
2089 if (ASCII_ISALPHA(*p) || *p == '.')
2090 get_float = FALSE;
2091 }
2092 if (get_float)
2093 {
2094 float_T f;
2095
Bram Moolenaar29500652021-08-08 15:43:34 +02002096 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002097 if (evaluate)
2098 {
2099 rettv->v_type = VAR_FLOAT;
2100 rettv->vval.v_float = f;
2101 }
2102 }
2103 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002104 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2105 {
2106 char_u *bp;
2107 blob_T *blob = NULL; // init for gcc
2108
2109 // Blob constant: 0z0123456789abcdef
2110 if (evaluate)
2111 blob = blob_alloc();
2112 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2113 {
2114 if (!vim_isxdigit(bp[1]))
2115 {
2116 if (blob != NULL)
2117 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002118 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002119 ga_clear(&blob->bv_ga);
2120 VIM_CLEAR(blob);
2121 }
2122 return FAIL;
2123 }
2124 if (blob != NULL)
2125 ga_append(&blob->bv_ga,
2126 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2127 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2128 ++bp;
2129 }
2130 if (blob != NULL)
2131 rettv_blob_set(rettv, blob);
2132 *arg = bp;
2133 }
2134 else
2135 {
2136 varnumber_T n;
2137
2138 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002139 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002140 ? STR2NR_NO_OCT + STR2NR_QUOTE
2141 : STR2NR_ALL, &n, NULL, 0, TRUE);
2142 if (len == 0)
2143 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002144 if (evaluate)
2145 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002146 return FAIL;
2147 }
2148 *arg += len;
2149 if (evaluate)
2150 {
2151 rettv->v_type = VAR_NUMBER;
2152 rettv->vval.v_number = n;
2153 }
2154 }
2155 return OK;
2156}
2157
2158/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002159 * Evaluate a string constant and put the result in "rettv".
2160 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2161 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2162 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002163 * Return OK or FAIL.
2164 */
2165 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002166eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002167{
2168 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002169 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002170 int extra = interpolate ? 1 : 0;
2171 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002172 int len;
2173
2174 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002175 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002176 {
2177 if (*p == '\\' && p[1] != NUL)
2178 {
2179 ++p;
2180 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002181 // to 9 characters (6 for the char and 3 for a modifier):
2182 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002183 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002184 {
2185 int modifiers = 0;
2186 int flags = FSK_KEYCODE | FSK_IN_STRING;
2187
zeertzjqdb088872022-05-02 22:53:45 +01002188 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002189
2190 // Skip to the '>' to avoid using '{' inside for string
2191 // interpolation.
2192 if (p[1] != '*')
2193 flags |= FSK_SIMPLIFY;
2194 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2195 --p; // leave "p" on the ">"
2196 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002197 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002198 else if (interpolate && (*p == '{' || *p == '}'))
2199 {
2200 if (*p == '{' && p[1] != '{') // start of expression
2201 break;
2202 ++p;
2203 if (p[-1] == '}' && *p != '}') // single '}' is an error
2204 {
2205 semsg(_(e_stray_closing_curly_str), *arg);
2206 return FAIL;
2207 }
2208 --extra; // "{{" becomes "{", "}}" becomes "}"
2209 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002210 }
2211
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002212 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002213 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002214 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002215 return FAIL;
2216 }
2217
2218 // If only parsing, set *arg and return here
2219 if (!evaluate)
2220 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002221 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002222 return OK;
2223 }
2224
2225 // Copy the string into allocated memory, handling backslashed
2226 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002227 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002228 len = (int)(p - *arg + extra);
2229 rettv->vval.v_string = alloc(len);
2230 if (rettv->vval.v_string == NULL)
2231 return FAIL;
2232 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002233
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002234 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002235 {
2236 if (*p == '\\')
2237 {
2238 switch (*++p)
2239 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002240 case 'b': *end++ = BS; ++p; break;
2241 case 'e': *end++ = ESC; ++p; break;
2242 case 'f': *end++ = FF; ++p; break;
2243 case 'n': *end++ = NL; ++p; break;
2244 case 'r': *end++ = CAR; ++p; break;
2245 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002246
2247 case 'X': // hex: "\x1", "\x12"
2248 case 'x':
2249 case 'u': // Unicode: "\u0023"
2250 case 'U':
2251 if (vim_isxdigit(p[1]))
2252 {
2253 int n, nr;
2254 int c = toupper(*p);
2255
2256 if (c == 'X')
2257 n = 2;
2258 else if (*p == 'u')
2259 n = 4;
2260 else
2261 n = 8;
2262 nr = 0;
2263 while (--n >= 0 && vim_isxdigit(p[1]))
2264 {
2265 ++p;
2266 nr = (nr << 4) + hex2nr(*p);
2267 }
2268 ++p;
2269 // For "\u" store the number according to
2270 // 'encoding'.
2271 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002272 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002273 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002274 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002275 }
2276 break;
2277
2278 // octal: "\1", "\12", "\123"
2279 case '0':
2280 case '1':
2281 case '2':
2282 case '3':
2283 case '4':
2284 case '5':
2285 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002286 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002287 if (*p >= '0' && *p <= '7')
2288 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002289 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002290 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002291 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002292 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002293 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002294 break;
2295
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002296 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002297 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002298 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002299 int flags = FSK_KEYCODE | FSK_IN_STRING;
2300
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002301 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002302 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002303 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002304 if (extra != 0)
2305 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002306 end += extra;
2307 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002308 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002309 break;
2310 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002311 }
2312 // FALLTHROUGH
2313
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002314 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002315 break;
2316 }
2317 }
2318 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002319 {
2320 if (interpolate && (*p == '{' || *p == '}'))
2321 {
2322 if (*p == '{' && p[1] != '{') // start of expression
2323 break;
2324 ++p; // reduce "{{" to "{" and "}}" to "}"
2325 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002326 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002327 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002328 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002329 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002330 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002331 ++p;
2332 *arg = p;
2333
2334 return OK;
2335}
2336
2337/*
2338 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002339 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2340 * Return OK when a "rettv" was set to the string.
2341 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002342 */
2343 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002344eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002345{
2346 char_u *p;
2347 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002348 int reduce = interpolate ? -1 : 0;
2349 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002350
2351 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002352 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002353 {
2354 if (*p == '\'')
2355 {
2356 if (p[1] != '\'')
2357 break;
2358 ++reduce;
2359 ++p;
2360 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002361 else if (interpolate)
2362 {
2363 if (*p == '{')
2364 {
2365 if (p[1] != '{')
2366 break;
2367 ++p;
2368 ++reduce;
2369 }
2370 else if (*p == '}')
2371 {
2372 ++p;
2373 if (*p != '}')
2374 {
2375 semsg(_(e_stray_closing_curly_str), *arg);
2376 return FAIL;
2377 }
2378 ++reduce;
2379 }
2380 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002381 }
2382
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002383 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002384 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002385 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002386 return FAIL;
2387 }
2388
2389 // If only parsing return after setting "*arg"
2390 if (!evaluate)
2391 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002392 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002393 return OK;
2394 }
2395
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002396 // Copy the string into allocated memory, handling '' to ' reduction and
2397 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002398 str = alloc((p - *arg) - reduce);
2399 if (str == NULL)
2400 return FAIL;
2401 rettv->v_type = VAR_STRING;
2402 rettv->vval.v_string = str;
2403
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002404 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002405 {
2406 if (*p == '\'')
2407 {
2408 if (p[1] != '\'')
2409 break;
2410 ++p;
2411 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002412 else if (interpolate && (*p == '{' || *p == '}'))
2413 {
2414 if (*p == '{' && p[1] != '{')
2415 break;
2416 ++p;
2417 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002418 MB_COPY_CHAR(p, str);
2419 }
2420 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002421 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002422
2423 return OK;
2424}
2425
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002426/*
2427 * Evaluate a single or double quoted string possibly containing expressions.
2428 * "arg" points to the '$'. The result is put in "rettv".
2429 * Returns OK or FAIL.
2430 */
LemonBoy2eaef102022-05-06 13:14:50 +01002431 int
2432eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2433{
2434 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002435 int ret = OK;
2436 int quote;
2437 garray_T ga;
2438 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002439
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002440 ga_init2(&ga, 1, 80);
2441
2442 // *arg is on the '$' character, move it to the first string character.
2443 ++*arg;
2444 quote = **arg;
2445 ++*arg;
2446
2447 for (;;)
2448 {
2449 // Get the string up to the matching quote or to a single '{'.
2450 // "arg" is advanced to either the quote or the '{'.
2451 if (quote == '"')
2452 ret = eval_string(arg, &tv, evaluate, TRUE);
2453 else
2454 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2455 if (ret == FAIL)
2456 break;
2457 if (evaluate)
2458 {
2459 ga_concat(&ga, tv.vval.v_string);
2460 clear_tv(&tv);
2461 }
2462
2463 if (**arg != '{')
2464 {
2465 // found terminating quote
2466 ++*arg;
2467 break;
2468 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002469 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002470 if (p == NULL)
2471 {
2472 ret = FAIL;
2473 break;
2474 }
2475 *arg = p;
2476 }
LemonBoy2eaef102022-05-06 13:14:50 +01002477
2478 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002479 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2480 {
2481 ga_clear(&ga);
2482 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002483 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002484 }
LemonBoy2eaef102022-05-06 13:14:50 +01002485
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002486 rettv->vval.v_string = ga.ga_data;
2487 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002488}
2489
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002490/*
2491 * Return a string with the string representation of a variable.
2492 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2493 * "numbuf" is used for a number.
2494 * Puts quotes around strings, so that they can be parsed back by eval().
2495 * May return NULL.
2496 */
2497 char_u *
2498tv2string(
2499 typval_T *tv,
2500 char_u **tofree,
2501 char_u *numbuf,
2502 int copyID)
2503{
2504 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2505}
2506
2507/*
2508 * Get the value of an environment variable.
2509 * "arg" is pointing to the '$'. It is advanced to after the name.
2510 * If the environment variable was not set, silently assume it is empty.
2511 * Return FAIL if the name is invalid.
2512 */
2513 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002514eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002515{
2516 char_u *string = NULL;
2517 int len;
2518 int cc;
2519 char_u *name;
2520 int mustfree = FALSE;
2521
2522 ++*arg;
2523 name = *arg;
2524 len = get_env_len(arg);
2525 if (evaluate)
2526 {
2527 if (len == 0)
2528 return FAIL; // invalid empty name
2529
2530 cc = name[len];
2531 name[len] = NUL;
2532 // first try vim_getenv(), fast for normal environment vars
2533 string = vim_getenv(name, &mustfree);
2534 if (string != NULL && *string != NUL)
2535 {
2536 if (!mustfree)
2537 string = vim_strsave(string);
2538 }
2539 else
2540 {
2541 if (mustfree)
2542 vim_free(string);
2543
2544 // next try expanding things like $VIM and ${HOME}
2545 string = expand_env_save(name - 1);
2546 if (string != NULL && *string == '$')
2547 VIM_CLEAR(string);
2548 }
2549 name[len] = cc;
2550
2551 rettv->v_type = VAR_STRING;
2552 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002553 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002554 }
2555
2556 return OK;
2557}
2558
2559/*
2560 * Get the lnum from the first argument.
2561 * Also accepts ".", "$", etc., but that only works for the current buffer.
2562 * Returns -1 on error.
2563 */
2564 linenr_T
2565tv_get_lnum(typval_T *argvars)
2566{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002567 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002568 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002569
Bram Moolenaar56acb092020-08-16 14:48:19 +02002570 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2571 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002572 if (lnum <= 0 && did_emsg_before == did_emsg
2573 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002574 {
2575 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002576 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002577
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002578 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002579 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002580 if (fp != NULL)
2581 lnum = fp->lnum;
2582 }
2583 return lnum;
2584}
2585
2586/*
2587 * Get the lnum from the first argument.
2588 * Also accepts "$", then "buf" is used.
2589 * Returns 0 on error.
2590 */
2591 linenr_T
2592tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2593{
2594 if (argvars[0].v_type == VAR_STRING
2595 && argvars[0].vval.v_string != NULL
2596 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002597 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002598 && buf != NULL)
2599 return buf->b_ml.ml_line_count;
2600 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2601}
2602
2603/*
2604 * Get buffer by number or pattern.
2605 */
2606 buf_T *
2607tv_get_buf(typval_T *tv, int curtab_only)
2608{
2609 char_u *name = tv->vval.v_string;
2610 buf_T *buf;
2611
2612 if (tv->v_type == VAR_NUMBER)
2613 return buflist_findnr((int)tv->vval.v_number);
2614 if (tv->v_type != VAR_STRING)
2615 return NULL;
2616 if (name == NULL || *name == NUL)
2617 return curbuf;
2618 if (name[0] == '$' && name[1] == NUL)
2619 return lastbuf;
2620
2621 buf = buflist_find_by_name(name, curtab_only);
2622
2623 // If not found, try expanding the name, like done for bufexists().
2624 if (buf == NULL)
2625 buf = find_buffer(tv);
2626
2627 return buf;
2628}
2629
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002630/*
2631 * Like tv_get_buf() but give an error message is the type is wrong.
2632 */
2633 buf_T *
2634tv_get_buf_from_arg(typval_T *tv)
2635{
2636 buf_T *buf;
2637
2638 ++emsg_off;
2639 buf = tv_get_buf(tv, FALSE);
2640 --emsg_off;
2641 if (buf == NULL
2642 && tv->v_type != VAR_NUMBER
2643 && tv->v_type != VAR_STRING)
2644 // issue errmsg for type error
2645 (void)tv_get_number(tv);
2646 return buf;
2647}
2648
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002649#endif // FEAT_EVAL