blob: b1a0060467f2cf937f8a919e9c3fddf78a104f61 [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
87 case VAR_NUMBER:
88 case VAR_FLOAT:
89 case VAR_ANY:
90 case VAR_UNKNOWN:
91 case VAR_VOID:
92 case VAR_BOOL:
93 case VAR_SPECIAL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +020094 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +020095 break;
96 }
97 vim_free(varp);
98 }
99}
100
101/*
102 * Free the memory for a variable value and set the value to NULL or 0.
103 */
104 void
105clear_tv(typval_T *varp)
106{
107 if (varp != NULL)
108 {
109 switch (varp->v_type)
110 {
111 case VAR_FUNC:
112 func_unref(varp->vval.v_string);
113 // FALLTHROUGH
114 case VAR_STRING:
115 VIM_CLEAR(varp->vval.v_string);
116 break;
117 case VAR_PARTIAL:
118 partial_unref(varp->vval.v_partial);
119 varp->vval.v_partial = NULL;
120 break;
121 case VAR_BLOB:
122 blob_unref(varp->vval.v_blob);
123 varp->vval.v_blob = NULL;
124 break;
125 case VAR_LIST:
126 list_unref(varp->vval.v_list);
127 varp->vval.v_list = NULL;
128 break;
129 case VAR_DICT:
130 dict_unref(varp->vval.v_dict);
131 varp->vval.v_dict = NULL;
132 break;
133 case VAR_NUMBER:
134 case VAR_BOOL:
135 case VAR_SPECIAL:
136 varp->vval.v_number = 0;
137 break;
138 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200139 varp->vval.v_float = 0.0;
140 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200141 case VAR_JOB:
142#ifdef FEAT_JOB_CHANNEL
143 job_unref(varp->vval.v_job);
144 varp->vval.v_job = NULL;
145#endif
146 break;
147 case VAR_CHANNEL:
148#ifdef FEAT_JOB_CHANNEL
149 channel_unref(varp->vval.v_channel);
150 varp->vval.v_channel = NULL;
151#endif
Bram Moolenaar24f72092021-05-07 20:43:54 +0200152 break;
153 case VAR_INSTR:
154 VIM_CLEAR(varp->vval.v_instr);
155 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200156 case VAR_UNKNOWN:
157 case VAR_ANY:
158 case VAR_VOID:
159 break;
160 }
161 varp->v_lock = 0;
162 }
163}
164
165/*
166 * Set the value of a variable to NULL without freeing items.
167 */
168 void
169init_tv(typval_T *varp)
170{
171 if (varp != NULL)
172 CLEAR_POINTER(varp);
173}
174
Bram Moolenaar36967b32020-08-17 21:41:02 +0200175 static varnumber_T
176tv_get_bool_or_number_chk(typval_T *varp, int *denote, int want_bool)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200177{
178 varnumber_T n = 0L;
179
180 switch (varp->v_type)
181 {
182 case VAR_NUMBER:
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200183 if (in_vim9script() && want_bool && varp->vval.v_number != 0
Bram Moolenaard70840e2020-08-22 15:06:35 +0200184 && varp->vval.v_number != 1)
185 {
186 semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
187 break;
188 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200189 return varp->vval.v_number;
190 case VAR_FLOAT:
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000191 emsg(_(e_using_float_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200192 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200193 case VAR_FUNC:
194 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000195 emsg(_(e_using_funcref_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200196 break;
197 case VAR_STRING:
Bram Moolenaar56acb092020-08-16 14:48:19 +0200198 if (in_vim9script())
199 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100200 emsg_using_string_as(varp, !want_bool);
Bram Moolenaar56acb092020-08-16 14:48:19 +0200201 break;
202 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200203 if (varp->vval.v_string != NULL)
204 vim_str2nr(varp->vval.v_string, NULL, NULL,
205 STR2NR_ALL, &n, NULL, 0, FALSE);
206 return n;
207 case VAR_LIST:
Bram Moolenaar677658a2022-01-05 16:09:06 +0000208 emsg(_(e_using_list_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200209 break;
210 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000211 emsg(_(e_using_dictionary_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200212 break;
213 case VAR_BOOL:
214 case VAR_SPECIAL:
Bram Moolenaar36967b32020-08-17 21:41:02 +0200215 if (!want_bool && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200216 {
Bram Moolenaard92cc132020-11-18 17:17:15 +0100217 if (varp->v_type == VAR_BOOL)
218 emsg(_(e_using_bool_as_number));
219 else
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000220 emsg(_(e_using_special_as_number));
Bram Moolenaar56acb092020-08-16 14:48:19 +0200221 break;
222 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200223 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
224 case VAR_JOB:
225#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000226 emsg(_(e_using_job_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200227 break;
228#endif
229 case VAR_CHANNEL:
230#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000231 emsg(_(e_using_channel_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200232 break;
233#endif
234 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000235 emsg(_(e_using_blob_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200236 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200237 case VAR_VOID:
238 emsg(_(e_cannot_use_void_value));
239 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200240 case VAR_UNKNOWN:
241 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200242 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200243 internal_error_no_abort("tv_get_number(UNKNOWN)");
244 break;
245 }
246 if (denote == NULL) // useful for values that must be unsigned
247 n = -1;
248 else
249 *denote = TRUE;
250 return n;
251}
252
Bram Moolenaar36967b32020-08-17 21:41:02 +0200253/*
254 * Get the number value of a variable.
255 * If it is a String variable, uses vim_str2nr().
256 * For incompatible types, return 0.
257 * tv_get_number_chk() is similar to tv_get_number(), but informs the
258 * caller of incompatible types: it sets *denote to TRUE if "denote"
259 * is not NULL or returns -1 otherwise.
260 */
261 varnumber_T
262tv_get_number(typval_T *varp)
263{
264 int error = FALSE;
265
266 return tv_get_number_chk(varp, &error); // return 0L on error
267}
268
269 varnumber_T
270tv_get_number_chk(typval_T *varp, int *denote)
271{
272 return tv_get_bool_or_number_chk(varp, denote, FALSE);
273}
274
275/*
276 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200277 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200278 */
279 varnumber_T
280tv_get_bool(typval_T *varp)
281{
282 return tv_get_bool_or_number_chk(varp, NULL, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200283}
284
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200285/*
286 * Get the boolean value of "varp". This is like tv_get_number_chk(),
287 * but in Vim9 script accepts Number and Bool.
288 */
289 varnumber_T
290tv_get_bool_chk(typval_T *varp, int *denote)
291{
292 return tv_get_bool_or_number_chk(varp, denote, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200293}
294
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000295 static float_T
296tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200297{
298 switch (varp->v_type)
299 {
300 case VAR_NUMBER:
301 return (float_T)(varp->vval.v_number);
302 case VAR_FLOAT:
303 return varp->vval.v_float;
304 case VAR_FUNC:
305 case VAR_PARTIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000306 emsg(_(e_using_funcref_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200307 break;
308 case VAR_STRING:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000309 emsg(_(e_using_string_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200310 break;
311 case VAR_LIST:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000312 emsg(_(e_using_list_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200313 break;
314 case VAR_DICT:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000315 emsg(_(e_using_dictionary_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200316 break;
317 case VAR_BOOL:
Bram Moolenaarb2810f12022-01-08 21:38:52 +0000318 emsg(_(e_using_boolean_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200319 break;
320 case VAR_SPECIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000321 emsg(_(e_using_special_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200322 break;
323 case VAR_JOB:
324# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000325 emsg(_(e_using_job_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200326 break;
327# endif
328 case VAR_CHANNEL:
329# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000330 emsg(_(e_using_channel_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200331 break;
332# endif
333 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000334 emsg(_(e_using_blob_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200335 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200336 case VAR_VOID:
337 emsg(_(e_cannot_use_void_value));
338 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200339 case VAR_UNKNOWN:
340 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200341 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200342 internal_error_no_abort("tv_get_float(UNKNOWN)");
343 break;
344 }
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000345 if (error != NULL)
346 *error = TRUE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200347 return 0;
348}
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000349
350 float_T
351tv_get_float(typval_T *varp)
352{
353 return tv_get_float_chk(varp, NULL);
354}
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200355
356/*
Ernie Rael51d04d12022-05-04 15:40:22 +0100357 * Give an error and return FAIL unless "args[idx]" is unknown
358 */
359 int
360check_for_unknown_arg(typval_T *args, int idx)
361{
362 if (args[idx].v_type != VAR_UNKNOWN)
363 {
364 semsg(_(e_too_many_arguments), idx + 1);
365 return FAIL;
366 }
367 return OK;
368}
369
370/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200371 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100372 */
373 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100374check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100375{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100376 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100377 {
rbtnnddc80af2021-12-17 18:01:31 +0000378 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100379 return FAIL;
380 }
381 return OK;
382}
383
384/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100385 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100386 */
387 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100388check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100389{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100390 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100391 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100392 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100393 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200394 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100395 return FAIL;
396 }
397 return OK;
398}
399
400/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200401 * Check for an optional string argument at 'idx'
402 */
403 int
404check_for_opt_string_arg(typval_T *args, int idx)
405{
406 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100407 || check_for_string_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200408}
409
410/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200411 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200412 */
413 int
414check_for_number_arg(typval_T *args, int idx)
415{
416 if (args[idx].v_type != VAR_NUMBER)
417 {
rbtnnddc80af2021-12-17 18:01:31 +0000418 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200419 return FAIL;
420 }
421 return OK;
422}
423
424/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200425 * Check for an optional number argument at 'idx'
426 */
427 int
428check_for_opt_number_arg(typval_T *args, int idx)
429{
430 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100431 || check_for_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200432}
433
434/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200435 * Give an error and return FAIL unless "args[idx]" is a float or a number.
436 */
437 int
438check_for_float_or_nr_arg(typval_T *args, int idx)
439{
440 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
441 {
rbtnnddc80af2021-12-17 18:01:31 +0000442 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200443 return FAIL;
444 }
445 return OK;
446}
447
448/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200449 * Give an error and return FAIL unless "args[idx]" is a bool.
450 */
451 int
452check_for_bool_arg(typval_T *args, int idx)
453{
454 if (args[idx].v_type != VAR_BOOL
455 && !(args[idx].v_type == VAR_NUMBER
456 && (args[idx].vval.v_number == 0
457 || args[idx].vval.v_number == 1)))
458 {
rbtnnddc80af2021-12-17 18:01:31 +0000459 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200460 return FAIL;
461 }
462 return OK;
463}
464
465/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200466 * Check for an optional bool argument at 'idx'.
467 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200468 */
469 int
470check_for_opt_bool_arg(typval_T *args, int idx)
471{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200472 if (args[idx].v_type == VAR_UNKNOWN)
473 return OK;
474 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200475}
476
477/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200478 * Give an error and return FAIL unless "args[idx]" is a blob.
479 */
480 int
481check_for_blob_arg(typval_T *args, int idx)
482{
483 if (args[idx].v_type != VAR_BLOB)
484 {
rbtnnddc80af2021-12-17 18:01:31 +0000485 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200486 return FAIL;
487 }
488 return OK;
489}
490
491/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200492 * Give an error and return FAIL unless "args[idx]" is a list.
493 */
494 int
495check_for_list_arg(typval_T *args, int idx)
496{
497 if (args[idx].v_type != VAR_LIST)
498 {
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200499 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200500 return FAIL;
501 }
502 return OK;
503}
504
505/*
Bram Moolenaard83392a2022-09-01 12:22:46 +0100506 * Give an error and return FAIL unless "args[idx]" is a non-NULL list.
507 */
508 int
509check_for_nonnull_list_arg(typval_T *args, int idx)
510{
511 if (check_for_list_arg(args, idx) == FAIL)
512 return FAIL;
513
514 if (args[idx].vval.v_list == NULL)
515 {
516 semsg(_(e_non_null_list_required_for_argument_nr), idx + 1);
517 return FAIL;
518 }
519 return OK;
520}
521
522/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200523 * Check for an optional list argument at 'idx'
524 */
525 int
526check_for_opt_list_arg(typval_T *args, int idx)
527{
528 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100529 || check_for_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200530}
531
532/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200533 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200534 */
535 int
536check_for_dict_arg(typval_T *args, int idx)
537{
538 if (args[idx].v_type != VAR_DICT)
539 {
rbtnnddc80af2021-12-17 18:01:31 +0000540 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200541 return FAIL;
542 }
543 return OK;
544}
545
546/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100547 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
548 */
549 int
550check_for_nonnull_dict_arg(typval_T *args, int idx)
551{
552 if (check_for_dict_arg(args, idx) == FAIL)
553 return FAIL;
554
555 if (args[idx].vval.v_dict == NULL)
556 {
557 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
558 return FAIL;
559 }
560 return OK;
561}
562
563/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200564 * Check for an optional dict argument at 'idx'
565 */
566 int
567check_for_opt_dict_arg(typval_T *args, int idx)
568{
569 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100570 || check_for_dict_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200571}
572
Dominique Pelle748b3082022-01-08 12:41:16 +0000573#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200574/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200575 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
576 */
577 int
578check_for_chan_or_job_arg(typval_T *args, int idx)
579{
580 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
581 {
rbtnnddc80af2021-12-17 18:01:31 +0000582 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200583 return FAIL;
584 }
585 return OK;
586}
587
588/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200589 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
590 * job.
591 */
592 int
593check_for_opt_chan_or_job_arg(typval_T *args, int idx)
594{
595 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100596 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200597}
598
599/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200600 * Give an error and return FAIL unless "args[idx]" is a job.
601 */
602 int
603check_for_job_arg(typval_T *args, int idx)
604{
605 if (args[idx].v_type != VAR_JOB)
606 {
rbtnnddc80af2021-12-17 18:01:31 +0000607 semsg(_(e_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 Lakshmanan4490ec42021-07-27 22:00:44 +0200614 * Check for an optional job argument at 'idx'.
615 */
616 int
617check_for_opt_job_arg(typval_T *args, int idx)
618{
619 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100620 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200621}
Bram Moolenaar3b8c7082022-11-30 20:20:56 +0000622#else
623/*
624 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
625 * job. Used without the +channel feature, thus only VAR_UNKNOWN is accepted.
626 */
627 int
628check_for_opt_chan_or_job_arg(typval_T *args, int idx)
629{
630 return args[idx].v_type == VAR_UNKNOWN ? OK : FAIL;
631}
Dominique Pelle748b3082022-01-08 12:41:16 +0000632#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200633
634/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200635 * Give an error and return FAIL unless "args[idx]" is a string or
636 * a number.
637 */
638 int
639check_for_string_or_number_arg(typval_T *args, int idx)
640{
641 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
642 {
rbtnnddc80af2021-12-17 18:01:31 +0000643 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200644 return FAIL;
645 }
646 return OK;
647}
648
649/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200650 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200651 */
652 int
653check_for_opt_string_or_number_arg(typval_T *args, int idx)
654{
655 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100656 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200657}
658
659/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200660 * Give an error and return FAIL unless "args[idx]" is a buffer number.
661 * Buffer number can be a number or a string.
662 */
663 int
664check_for_buffer_arg(typval_T *args, int idx)
665{
666 return check_for_string_or_number_arg(args, idx);
667}
668
669/*
670 * Check for an optional buffer argument at 'idx'
671 */
672 int
673check_for_opt_buffer_arg(typval_T *args, int idx)
674{
675 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100676 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200677}
678
679/*
680 * Give an error and return FAIL unless "args[idx]" is a line number.
681 * Line number can be a number or a string.
682 */
683 int
684check_for_lnum_arg(typval_T *args, int idx)
685{
686 return check_for_string_or_number_arg(args, idx);
687}
688
689/*
690 * Check for an optional line number argument at 'idx'
691 */
692 int
693check_for_opt_lnum_arg(typval_T *args, int idx)
694{
695 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100696 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200697}
698
Dominique Pelle748b3082022-01-08 12:41:16 +0000699#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200700/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200701 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200702 */
703 int
704check_for_string_or_blob_arg(typval_T *args, int idx)
705{
706 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
707 {
rbtnnddc80af2021-12-17 18:01:31 +0000708 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200709 return FAIL;
710 }
711 return OK;
712}
Dominique Pelle748b3082022-01-08 12:41:16 +0000713#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200714
715/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200716 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200717 */
718 int
719check_for_string_or_list_arg(typval_T *args, int idx)
720{
721 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
722 {
rbtnnddc80af2021-12-17 18:01:31 +0000723 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200724 return FAIL;
725 }
726 return OK;
727}
728
729/*
rbtnn0ccb5842021-12-18 18:33:46 +0000730 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
731 * blob.
732 */
733 int
734check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
735{
736 if (args[idx].v_type != VAR_STRING
737 && args[idx].v_type != VAR_LIST
738 && args[idx].v_type != VAR_BLOB)
739 {
740 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
741 return FAIL;
742 }
743 return OK;
744}
745
746/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200747 * Check for an optional string or list argument at 'idx'
748 */
749 int
750check_for_opt_string_or_list_arg(typval_T *args, int idx)
751{
752 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100753 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200754}
755
756/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200757 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
758 */
759 int
760check_for_string_or_dict_arg(typval_T *args, int idx)
761{
762 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
763 {
rbtnnddc80af2021-12-17 18:01:31 +0000764 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200765 return FAIL;
766 }
767 return OK;
768}
769
770/*
771 * Give an error and return FAIL unless "args[idx]" is a string or a number
772 * or a list.
773 */
774 int
775check_for_string_or_number_or_list_arg(typval_T *args, int idx)
776{
777 if (args[idx].v_type != VAR_STRING
778 && args[idx].v_type != VAR_NUMBER
779 && args[idx].v_type != VAR_LIST)
780 {
rbtnn0ccb5842021-12-18 18:33:46 +0000781 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200782 return FAIL;
783 }
784 return OK;
785}
786
787/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200788 * Give an error and return FAIL unless "args[idx]" is an optional string
789 * or number or a list
790 */
791 int
792check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
793{
794 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100795 || check_for_string_or_number_or_list_arg(args, idx)
796 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200797}
798
799/*
Bakudankun375141e2022-09-09 18:46:47 +0100800 * Give an error and return FAIL unless "args[idx]" is a string or a number
801 * or a list or a blob.
802 */
803 int
804check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
805{
806 if (args[idx].v_type != VAR_STRING
807 && args[idx].v_type != VAR_NUMBER
808 && args[idx].v_type != VAR_LIST
809 && args[idx].v_type != VAR_BLOB)
810 {
811 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
812 return FAIL;
813 }
814 return OK;
815}
816
817/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200818 * Give an error and return FAIL unless "args[idx]" is a string or a list
819 * or a dict.
820 */
821 int
822check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
823{
824 if (args[idx].v_type != VAR_STRING
825 && args[idx].v_type != VAR_LIST
826 && args[idx].v_type != VAR_DICT)
827 {
rbtnnddc80af2021-12-17 18:01:31 +0000828 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200829 return FAIL;
830 }
831 return OK;
832}
833
834/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000835 * Give an error and return FAIL unless "args[idx]" is a string
836 * or a function reference.
837 */
838 int
839check_for_string_or_func_arg(typval_T *args, int idx)
840{
841 if (args[idx].v_type != VAR_PARTIAL
842 && args[idx].v_type != VAR_FUNC
843 && args[idx].v_type != VAR_STRING)
844 {
845 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
846 return FAIL;
847 }
848 return OK;
849}
850
851/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200852 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
853 */
854 int
855check_for_list_or_blob_arg(typval_T *args, int idx)
856{
857 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
858 {
rbtnn0ccb5842021-12-18 18:33:46 +0000859 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200860 return FAIL;
861 }
862 return OK;
863}
864
865/*
866 * Give an error and return FAIL unless "args[idx]" is a list or dict
867 */
868 int
869check_for_list_or_dict_arg(typval_T *args, int idx)
870{
871 if (args[idx].v_type != VAR_LIST
872 && args[idx].v_type != VAR_DICT)
873 {
rbtnnddc80af2021-12-17 18:01:31 +0000874 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200875 return FAIL;
876 }
877 return OK;
878}
879
880/*
881 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
882 * blob.
883 */
884 int
885check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
886{
887 if (args[idx].v_type != VAR_LIST
888 && args[idx].v_type != VAR_DICT
889 && args[idx].v_type != VAR_BLOB)
890 {
rbtnnddc80af2021-12-17 18:01:31 +0000891 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200892 return FAIL;
893 }
894 return OK;
895}
896
897/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000898 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
899 * blob or a string.
900 */
901 int
902check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
903{
904 if (args[idx].v_type != VAR_LIST
905 && args[idx].v_type != VAR_DICT
906 && args[idx].v_type != VAR_BLOB
907 && args[idx].v_type != VAR_STRING)
908 {
rbtnnddc80af2021-12-17 18:01:31 +0000909 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000910 return FAIL;
911 }
912 return OK;
913}
914
915/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200916 * Give an error and return FAIL unless "args[idx]" is an optional buffer
917 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200918 */
919 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200920check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200921{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200922 if (args[idx].v_type != VAR_UNKNOWN
923 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200924 && args[idx].v_type != VAR_NUMBER
925 && args[idx].v_type != VAR_DICT)
926 {
rbtnnddc80af2021-12-17 18:01:31 +0000927 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200928 return FAIL;
929 }
930 return OK;
931}
932
933/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200934 * Get the string value of a variable.
935 * If it is a Number variable, the number is converted into a string.
936 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
937 * tv_get_string_buf() uses a given buffer.
938 * If the String variable has never been set, return an empty string.
939 * Never returns NULL;
940 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
941 * NULL on error.
942 */
943 char_u *
944tv_get_string(typval_T *varp)
945{
946 static char_u mybuf[NUMBUFLEN];
947
948 return tv_get_string_buf(varp, mybuf);
949}
950
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100951/*
952 * Like tv_get_string() but don't allow number to string conversion for Vim9.
953 */
954 char_u *
955tv_get_string_strict(typval_T *varp)
956{
957 static char_u mybuf[NUMBUFLEN];
958 char_u *res = tv_get_string_buf_chk_strict(
959 varp, mybuf, in_vim9script());
960
961 return res != NULL ? res : (char_u *)"";
962}
963
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200964 char_u *
965tv_get_string_buf(typval_T *varp, char_u *buf)
966{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200967 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200968
969 return res != NULL ? res : (char_u *)"";
970}
971
972/*
973 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
974 */
975 char_u *
976tv_get_string_chk(typval_T *varp)
977{
978 static char_u mybuf[NUMBUFLEN];
979
980 return tv_get_string_buf_chk(varp, mybuf);
981}
982
983 char_u *
984tv_get_string_buf_chk(typval_T *varp, char_u *buf)
985{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100986 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
987}
988
989 char_u *
990tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
991{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200992 switch (varp->v_type)
993 {
994 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100995 if (strict)
996 {
997 emsg(_(e_using_number_as_string));
998 break;
999 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001000 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1001 (varnumber_T)varp->vval.v_number);
1002 return buf;
1003 case VAR_FUNC:
1004 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001005 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001006 break;
1007 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001008 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001009 break;
1010 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001011 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001012 break;
1013 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001014 if (strict)
1015 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001016 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001017 break;
1018 }
1019 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1020 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001021 case VAR_STRING:
1022 if (varp->vval.v_string != NULL)
1023 return varp->vval.v_string;
1024 return (char_u *)"";
1025 case VAR_BOOL:
1026 case VAR_SPECIAL:
1027 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1028 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001029 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001030 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001031 break;
1032 case VAR_JOB:
1033#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001034 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001035 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001036 semsg(_(e_using_invalid_value_as_string_str), "job");
1037 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001038 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001039 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001040#endif
1041 break;
1042 case VAR_CHANNEL:
1043#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001044 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001045 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001046 semsg(_(e_using_invalid_value_as_string_str), "channel");
1047 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001048 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001049 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001050#endif
1051 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001052 case VAR_VOID:
1053 emsg(_(e_cannot_use_void_value));
1054 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001055 case VAR_UNKNOWN:
1056 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001057 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001058 semsg(_(e_using_invalid_value_as_string_str),
1059 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001060 break;
1061 }
1062 return NULL;
1063}
1064
1065/*
1066 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1067 * string() on Dict, List, etc.
1068 */
1069 char_u *
1070tv_stringify(typval_T *varp, char_u *buf)
1071{
1072 if (varp->v_type == VAR_LIST
1073 || varp->v_type == VAR_DICT
1074 || varp->v_type == VAR_BLOB
1075 || varp->v_type == VAR_FUNC
1076 || varp->v_type == VAR_PARTIAL
1077 || varp->v_type == VAR_FLOAT)
1078 {
1079 typval_T tmp;
1080
1081 f_string(varp, &tmp);
1082 tv_get_string_buf(&tmp, buf);
1083 clear_tv(varp);
1084 *varp = tmp;
1085 return tmp.vval.v_string;
1086 }
1087 return tv_get_string_buf(varp, buf);
1088}
1089
1090/*
1091 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1092 * Also give an error message, using "name" or _("name") when use_gettext is
1093 * TRUE.
1094 */
1095 int
1096tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1097{
1098 int lock = 0;
1099
1100 switch (tv->v_type)
1101 {
1102 case VAR_BLOB:
1103 if (tv->vval.v_blob != NULL)
1104 lock = tv->vval.v_blob->bv_lock;
1105 break;
1106 case VAR_LIST:
1107 if (tv->vval.v_list != NULL)
1108 lock = tv->vval.v_list->lv_lock;
1109 break;
1110 case VAR_DICT:
1111 if (tv->vval.v_dict != NULL)
1112 lock = tv->vval.v_dict->dv_lock;
1113 break;
1114 default:
1115 break;
1116 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001117 return value_check_lock(tv->v_lock, name, use_gettext)
1118 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001119}
1120
1121/*
1122 * Copy the values from typval_T "from" to typval_T "to".
1123 * When needed allocates string or increases reference count.
1124 * Does not make a copy of a list, blob or dict but copies the reference!
1125 * It is OK for "from" and "to" to point to the same item. This is used to
1126 * make a copy later.
1127 */
1128 void
1129copy_tv(typval_T *from, typval_T *to)
1130{
1131 to->v_type = from->v_type;
1132 to->v_lock = 0;
1133 switch (from->v_type)
1134 {
1135 case VAR_NUMBER:
1136 case VAR_BOOL:
1137 case VAR_SPECIAL:
1138 to->vval.v_number = from->vval.v_number;
1139 break;
1140 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001141 to->vval.v_float = from->vval.v_float;
1142 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001143 case VAR_JOB:
1144#ifdef FEAT_JOB_CHANNEL
1145 to->vval.v_job = from->vval.v_job;
1146 if (to->vval.v_job != NULL)
1147 ++to->vval.v_job->jv_refcount;
1148 break;
1149#endif
1150 case VAR_CHANNEL:
1151#ifdef FEAT_JOB_CHANNEL
1152 to->vval.v_channel = from->vval.v_channel;
1153 if (to->vval.v_channel != NULL)
1154 ++to->vval.v_channel->ch_refcount;
1155 break;
1156#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001157 case VAR_INSTR:
1158 to->vval.v_instr = from->vval.v_instr;
1159 break;
1160
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001161 case VAR_STRING:
1162 case VAR_FUNC:
1163 if (from->vval.v_string == NULL)
1164 to->vval.v_string = NULL;
1165 else
1166 {
1167 to->vval.v_string = vim_strsave(from->vval.v_string);
1168 if (from->v_type == VAR_FUNC)
1169 func_ref(to->vval.v_string);
1170 }
1171 break;
1172 case VAR_PARTIAL:
1173 if (from->vval.v_partial == NULL)
1174 to->vval.v_partial = NULL;
1175 else
1176 {
1177 to->vval.v_partial = from->vval.v_partial;
1178 ++to->vval.v_partial->pt_refcount;
1179 }
1180 break;
1181 case VAR_BLOB:
1182 if (from->vval.v_blob == NULL)
1183 to->vval.v_blob = NULL;
1184 else
1185 {
1186 to->vval.v_blob = from->vval.v_blob;
1187 ++to->vval.v_blob->bv_refcount;
1188 }
1189 break;
1190 case VAR_LIST:
1191 if (from->vval.v_list == NULL)
1192 to->vval.v_list = NULL;
1193 else
1194 {
1195 to->vval.v_list = from->vval.v_list;
1196 ++to->vval.v_list->lv_refcount;
1197 }
1198 break;
1199 case VAR_DICT:
1200 if (from->vval.v_dict == NULL)
1201 to->vval.v_dict = NULL;
1202 else
1203 {
1204 to->vval.v_dict = from->vval.v_dict;
1205 ++to->vval.v_dict->dv_refcount;
1206 }
1207 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001208 case VAR_VOID:
1209 emsg(_(e_cannot_use_void_value));
1210 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001211 case VAR_UNKNOWN:
1212 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001213 internal_error_no_abort("copy_tv(UNKNOWN)");
1214 break;
1215 }
1216}
1217
1218/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001219 * Compare "tv1" and "tv2".
1220 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001221 */
1222 int
1223typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001224 typval_T *tv1, // first operand
1225 typval_T *tv2, // second operand
1226 exprtype_T type, // operator
1227 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001228{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001229 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001230 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001231 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1232
Bram Moolenaar265f8112021-12-19 12:33:05 +00001233 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001234 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001235 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001236 // it means TRUE.
1237 n1 = (type == EXPR_ISNOT);
1238 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001239 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1240 || (tv2->v_type == VAR_SPECIAL
1241 && tv2->vval.v_number == VVAL_NULL))
1242 && tv1->v_type != tv2->v_type
1243 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1244 {
1245 n1 = typval_compare_null(tv1, tv2);
1246 if (n1 == MAYBE)
1247 {
1248 clear_tv(tv1);
1249 return FAIL;
1250 }
1251 if (type == EXPR_NEQUAL)
1252 n1 = !n1;
1253 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001254 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001255 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001256 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001257 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001258 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001259 return FAIL;
1260 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001261 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001262 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001263 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001264 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001265 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001266 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001267 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001268 return FAIL;
1269 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001270 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001271 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001272 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001273 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001274 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001275 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001276 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001277 return FAIL;
1278 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001279 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001280 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001281 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1282 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001283 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001284 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001285 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001286 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001287 return FAIL;
1288 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001289 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001290 }
1291
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001292 // If one of the two variables is a float, compare as a float.
1293 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001294 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001295 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1296 {
1297 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001298 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001299
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001300 f1 = tv_get_float_chk(tv1, &error);
1301 if (!error)
1302 f2 = tv_get_float_chk(tv2, &error);
1303 if (error)
1304 {
1305 clear_tv(tv1);
1306 return FAIL;
1307 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001308 n1 = FALSE;
1309 switch (type)
1310 {
1311 case EXPR_IS:
1312 case EXPR_EQUAL: n1 = (f1 == f2); break;
1313 case EXPR_ISNOT:
1314 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1315 case EXPR_GREATER: n1 = (f1 > f2); break;
1316 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1317 case EXPR_SMALLER: n1 = (f1 < f2); break;
1318 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1319 case EXPR_UNKNOWN:
1320 case EXPR_MATCH:
1321 default: break; // avoid gcc warning
1322 }
1323 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001324
1325 // If one of the two variables is a number, compare as a number.
1326 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001327 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001328 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1329 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001330 int error = FALSE;
1331
1332 n1 = tv_get_number_chk(tv1, &error);
1333 if (!error)
1334 n2 = tv_get_number_chk(tv2, &error);
1335 if (error)
1336 {
1337 clear_tv(tv1);
1338 return FAIL;
1339 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001340 switch (type)
1341 {
1342 case EXPR_IS:
1343 case EXPR_EQUAL: n1 = (n1 == n2); break;
1344 case EXPR_ISNOT:
1345 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1346 case EXPR_GREATER: n1 = (n1 > n2); break;
1347 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1348 case EXPR_SMALLER: n1 = (n1 < n2); break;
1349 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1350 case EXPR_UNKNOWN:
1351 case EXPR_MATCH:
1352 default: break; // avoid gcc warning
1353 }
1354 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001355 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1356 || tv2->v_type == VAR_BOOL
1357 || (tv1->v_type == VAR_SPECIAL
1358 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001359 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001360 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001361 {
1362 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001363 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1364 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001365 return FAIL;
1366 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001367 n1 = tv1->vval.v_number;
1368 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001369 switch (type)
1370 {
1371 case EXPR_IS:
1372 case EXPR_EQUAL: n1 = (n1 == n2); break;
1373 case EXPR_ISNOT:
1374 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1375 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001376 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001377 vartype_name(tv1->v_type));
1378 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001379 return FAIL;
1380 }
1381 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001382#ifdef FEAT_JOB_CHANNEL
1383 else if (tv1->v_type == tv2->v_type
1384 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1385 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1386 {
1387 if (tv1->v_type == VAR_CHANNEL)
1388 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1389 else
1390 n1 = tv1->vval.v_job == tv2->vval.v_job;
1391 if (type == EXPR_NEQUAL)
1392 n1 = !n1;
1393 }
1394#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001395 else
1396 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001397 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001398 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001399 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001400 return FAIL;
1401 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001402 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001403 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001404 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001405 if (in_vim9script())
1406 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001407 tv1->v_type = VAR_BOOL;
1408 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001409 }
1410 else
1411 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001412 tv1->v_type = VAR_NUMBER;
1413 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001414 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001415
1416 return OK;
1417}
1418
Bram Moolenaar34453202021-01-31 13:08:38 +01001419/*
dundargocc57b5bc2022-11-02 13:30:51 +00001420 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001421 * Put the result, false or true, in "res".
1422 * Return FAIL and give an error message when the comparison can't be done.
1423 */
1424 int
1425typval_compare_list(
1426 typval_T *tv1,
1427 typval_T *tv2,
1428 exprtype_T type,
1429 int ic,
1430 int *res)
1431{
1432 int val = 0;
1433
1434 if (type == EXPR_IS || type == EXPR_ISNOT)
1435 {
1436 val = (tv1->v_type == tv2->v_type
1437 && tv1->vval.v_list == tv2->vval.v_list);
1438 if (type == EXPR_ISNOT)
1439 val = !val;
1440 }
1441 else if (tv1->v_type != tv2->v_type
1442 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1443 {
1444 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001445 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001446 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001447 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001448 return FAIL;
1449 }
1450 else
1451 {
1452 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1453 ic, FALSE);
1454 if (type == EXPR_NEQUAL)
1455 val = !val;
1456 }
1457 *res = val;
1458 return OK;
1459}
1460
1461/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001462 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001463 */
1464 int
1465typval_compare_null(typval_T *tv1, typval_T *tv2)
1466{
1467 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1468 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1469 {
1470 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1471
1472 switch (tv->v_type)
1473 {
1474 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001475#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001476 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001477#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001478 case VAR_DICT: return tv->vval.v_dict == NULL;
1479 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001480#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001481 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001482#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001483 case VAR_LIST: return tv->vval.v_list == NULL;
1484 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1485 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001486
1487 case VAR_NUMBER: if (!in_vim9script())
1488 return tv->vval.v_number == 0;
1489 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001490 case VAR_FLOAT: if (!in_vim9script())
1491 return tv->vval.v_float == 0.0;
1492 break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001493 default: break;
1494 }
1495 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001496 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001497 // we won't give an error
1498 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001499}
1500
1501/*
dundargocc57b5bc2022-11-02 13:30:51 +00001502 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001503 * Put the result, false or true, in "res".
1504 * Return FAIL and give an error message when the comparison can't be done.
1505 */
1506 int
1507typval_compare_blob(
1508 typval_T *tv1,
1509 typval_T *tv2,
1510 exprtype_T type,
1511 int *res)
1512{
1513 int val = 0;
1514
1515 if (type == EXPR_IS || type == EXPR_ISNOT)
1516 {
1517 val = (tv1->v_type == tv2->v_type
1518 && tv1->vval.v_blob == tv2->vval.v_blob);
1519 if (type == EXPR_ISNOT)
1520 val = !val;
1521 }
1522 else if (tv1->v_type != tv2->v_type
1523 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1524 {
1525 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001526 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001527 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001528 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001529 return FAIL;
1530 }
1531 else
1532 {
1533 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1534 if (type == EXPR_NEQUAL)
1535 val = !val;
1536 }
1537 *res = val;
1538 return OK;
1539}
1540
1541/*
dundargocc57b5bc2022-11-02 13:30:51 +00001542 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001543 * Put the result, false or true, in "res".
1544 * Return FAIL and give an error message when the comparison can't be done.
1545 */
1546 int
1547typval_compare_dict(
1548 typval_T *tv1,
1549 typval_T *tv2,
1550 exprtype_T type,
1551 int ic,
1552 int *res)
1553{
1554 int val;
1555
1556 if (type == EXPR_IS || type == EXPR_ISNOT)
1557 {
1558 val = (tv1->v_type == tv2->v_type
1559 && tv1->vval.v_dict == tv2->vval.v_dict);
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 Moolenaara6f79292022-01-04 21:30:47 +00001567 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001568 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001569 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001570 return FAIL;
1571 }
1572 else
1573 {
1574 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
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 funcrefs 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_func(
1589 typval_T *tv1,
1590 typval_T *tv2,
1591 exprtype_T type,
1592 int ic,
1593 int *res)
1594{
1595 int val = 0;
1596
1597 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1598 && type != EXPR_IS && type != EXPR_ISNOT)
1599 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001600 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001601 return FAIL;
1602 }
1603 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1604 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1605 // When both partials are NULL, then they are equal.
1606 // Otherwise they are not equal.
1607 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1608 else if (type == EXPR_IS || type == EXPR_ISNOT)
1609 {
1610 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1611 // strings are considered the same if their value is
1612 // the same
1613 val = tv_equal(tv1, tv2, ic, FALSE);
1614 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1615 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1616 else
1617 val = FALSE;
1618 }
1619 else
1620 val = tv_equal(tv1, tv2, ic, FALSE);
1621 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1622 val = !val;
1623 *res = val;
1624 return OK;
1625}
1626
1627/*
1628 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1629 * Put the result, false or true, in "res".
1630 * Return FAIL and give an error message when the comparison can't be done.
1631 */
1632 int
1633typval_compare_string(
1634 typval_T *tv1,
1635 typval_T *tv2,
1636 exprtype_T type,
1637 int ic,
1638 int *res)
1639{
1640 int i = 0;
1641 int val = FALSE;
1642 char_u *s1, *s2;
1643 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1644
1645 if (in_vim9script()
1646 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1647 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1648 {
1649 semsg(_(e_cannot_compare_str_with_str),
1650 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1651 return FAIL;
1652 }
1653 s1 = tv_get_string_buf(tv1, buf1);
1654 s2 = tv_get_string_buf(tv2, buf2);
1655 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1656 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1657 switch (type)
1658 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001659 case EXPR_IS: if (in_vim9script())
1660 {
1661 // Really check it is the same string, not just
1662 // the same value.
1663 val = tv1->vval.v_string == tv2->vval.v_string;
1664 break;
1665 }
1666 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001667 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001668 case EXPR_ISNOT: if (in_vim9script())
1669 {
1670 // Really check it is not the same string, not
1671 // just a different value.
1672 val = tv1->vval.v_string != tv2->vval.v_string;
1673 break;
1674 }
1675 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001676 case EXPR_NEQUAL: val = (i != 0); break;
1677 case EXPR_GREATER: val = (i > 0); break;
1678 case EXPR_GEQUAL: val = (i >= 0); break;
1679 case EXPR_SMALLER: val = (i < 0); break;
1680 case EXPR_SEQUAL: val = (i <= 0); break;
1681
1682 case EXPR_MATCH:
1683 case EXPR_NOMATCH:
1684 val = pattern_match(s2, s1, ic);
1685 if (type == EXPR_NOMATCH)
1686 val = !val;
1687 break;
1688
1689 default: break; // avoid gcc warning
1690 }
1691 *res = val;
1692 return OK;
1693}
1694/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001695 * Convert any type to a string, never give an error.
1696 * When "quotes" is TRUE add quotes to a string.
1697 * Returns an allocated string.
1698 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001699 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001700typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001701{
1702 char_u *tofree;
1703 char_u numbuf[NUMBUFLEN];
1704 char_u *ret = NULL;
1705
1706 if (arg == NULL)
1707 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001708 if (!quotes && arg->v_type == VAR_STRING)
1709 {
1710 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1711 : arg->vval.v_string);
1712 }
1713 else
1714 {
1715 ret = tv2string(arg, &tofree, numbuf, 0);
1716 // Make a copy if we have a value but it's not in allocated memory.
1717 if (ret != NULL && tofree == NULL)
1718 ret = vim_strsave(ret);
1719 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001720 return ret;
1721}
1722
1723/*
1724 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1725 * or it refers to a List or Dictionary that is locked.
1726 */
1727 int
1728tv_islocked(typval_T *tv)
1729{
1730 return (tv->v_lock & VAR_LOCKED)
1731 || (tv->v_type == VAR_LIST
1732 && tv->vval.v_list != NULL
1733 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1734 || (tv->v_type == VAR_DICT
1735 && tv->vval.v_dict != NULL
1736 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1737}
1738
1739 static int
1740func_equal(
1741 typval_T *tv1,
1742 typval_T *tv2,
1743 int ic) // ignore case
1744{
1745 char_u *s1, *s2;
1746 dict_T *d1, *d2;
1747 int a1, a2;
1748 int i;
1749
1750 // empty and NULL function name considered the same
1751 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1752 : partial_name(tv1->vval.v_partial);
1753 if (s1 != NULL && *s1 == NUL)
1754 s1 = NULL;
1755 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1756 : partial_name(tv2->vval.v_partial);
1757 if (s2 != NULL && *s2 == NUL)
1758 s2 = NULL;
1759 if (s1 == NULL || s2 == NULL)
1760 {
1761 if (s1 != s2)
1762 return FALSE;
1763 }
1764 else if (STRCMP(s1, s2) != 0)
1765 return FALSE;
1766
1767 // empty dict and NULL dict is different
1768 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1769 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1770 if (d1 == NULL || d2 == NULL)
1771 {
1772 if (d1 != d2)
1773 return FALSE;
1774 }
1775 else if (!dict_equal(d1, d2, ic, TRUE))
1776 return FALSE;
1777
1778 // empty list and no list considered the same
1779 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1780 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1781 if (a1 != a2)
1782 return FALSE;
1783 for (i = 0; i < a1; ++i)
1784 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1785 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1786 return FALSE;
1787
1788 return TRUE;
1789}
1790
1791/*
1792 * Return TRUE if "tv1" and "tv2" have the same value.
1793 * Compares the items just like "==" would compare them, but strings and
1794 * numbers are different. Floats and numbers are also different.
1795 */
1796 int
1797tv_equal(
1798 typval_T *tv1,
1799 typval_T *tv2,
1800 int ic, // ignore case
1801 int recursive) // TRUE when used recursively
1802{
1803 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1804 char_u *s1, *s2;
1805 static int recursive_cnt = 0; // catch recursive loops
1806 int r;
1807 static int tv_equal_recurse_limit;
1808
1809 // Catch lists and dicts that have an endless loop by limiting
1810 // recursiveness to a limit. We guess they are equal then.
1811 // A fixed limit has the problem of still taking an awful long time.
1812 // Reduce the limit every time running into it. That should work fine for
1813 // deeply linked structures that are not recursively linked and catch
1814 // recursiveness quickly.
1815 if (!recursive)
1816 tv_equal_recurse_limit = 1000;
1817 if (recursive_cnt >= tv_equal_recurse_limit)
1818 {
1819 --tv_equal_recurse_limit;
1820 return TRUE;
1821 }
1822
1823 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1824 // arguments.
1825 if ((tv1->v_type == VAR_FUNC
1826 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1827 && (tv2->v_type == VAR_FUNC
1828 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1829 {
1830 ++recursive_cnt;
1831 r = func_equal(tv1, tv2, ic);
1832 --recursive_cnt;
1833 return r;
1834 }
1835
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001836 if (tv1->v_type != tv2->v_type
1837 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1838 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001839 return FALSE;
1840
1841 switch (tv1->v_type)
1842 {
1843 case VAR_LIST:
1844 ++recursive_cnt;
1845 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1846 --recursive_cnt;
1847 return r;
1848
1849 case VAR_DICT:
1850 ++recursive_cnt;
1851 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1852 --recursive_cnt;
1853 return r;
1854
1855 case VAR_BLOB:
1856 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1857
1858 case VAR_NUMBER:
1859 case VAR_BOOL:
1860 case VAR_SPECIAL:
1861 return tv1->vval.v_number == tv2->vval.v_number;
1862
1863 case VAR_STRING:
1864 s1 = tv_get_string_buf(tv1, buf1);
1865 s2 = tv_get_string_buf(tv2, buf2);
1866 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1867
1868 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001869 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001870 case VAR_JOB:
1871#ifdef FEAT_JOB_CHANNEL
1872 return tv1->vval.v_job == tv2->vval.v_job;
1873#endif
1874 case VAR_CHANNEL:
1875#ifdef FEAT_JOB_CHANNEL
1876 return tv1->vval.v_channel == tv2->vval.v_channel;
1877#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001878 case VAR_INSTR:
1879 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001880
1881 case VAR_PARTIAL:
1882 return tv1->vval.v_partial == tv2->vval.v_partial;
1883
1884 case VAR_FUNC:
1885 return tv1->vval.v_string == tv2->vval.v_string;
1886
1887 case VAR_UNKNOWN:
1888 case VAR_ANY:
1889 case VAR_VOID:
1890 break;
1891 }
1892
1893 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1894 // does not equal anything, not even itself.
1895 return FALSE;
1896}
1897
1898/*
1899 * Get an option value.
1900 * "arg" points to the '&' or '+' before the option name.
1901 * "arg" is advanced to character after the option name.
1902 * Return OK or FAIL.
1903 */
1904 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001905eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001906 char_u **arg,
1907 typval_T *rettv, // when NULL, only check if option exists
1908 int evaluate)
1909{
1910 char_u *option_end;
1911 long numval;
1912 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001913 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001914 int c;
1915 int working = (**arg == '+'); // has("+option")
1916 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001917 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001918
1919 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001920 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001921 if (option_end == NULL)
1922 {
1923 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001924 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001925 return FAIL;
1926 }
1927
1928 if (!evaluate)
1929 {
1930 *arg = option_end;
1931 return OK;
1932 }
1933
1934 c = *option_end;
1935 *option_end = NUL;
1936 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001937 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001938
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001939 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001940 {
1941 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001942 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001943 ret = FAIL;
1944 }
1945 else if (rettv != NULL)
1946 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001947 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001948 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001949 {
1950 rettv->v_type = VAR_STRING;
1951 rettv->vval.v_string = NULL;
1952 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001953 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001954 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001955 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1956 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001957 rettv->vval.v_number = 0;
1958 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001959 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001960 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001961 if (in_vim9script() && opt_type == gov_bool)
1962 {
1963 rettv->v_type = VAR_BOOL;
1964 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1965 }
1966 else
1967 {
1968 rettv->v_type = VAR_NUMBER;
1969 rettv->vval.v_number = numval;
1970 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001971 }
1972 else // string option
1973 {
1974 rettv->v_type = VAR_STRING;
1975 rettv->vval.v_string = stringval;
1976 }
1977 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001978 else if (working && (opt_type == gov_hidden_bool
1979 || opt_type == gov_hidden_number
1980 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001981 ret = FAIL;
1982
1983 *option_end = c; // put back for error messages
1984 *arg = option_end;
1985
1986 return ret;
1987}
1988
1989/*
1990 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1991 * Return OK or FAIL.
1992 */
1993 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001994eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001995 char_u **arg,
1996 typval_T *rettv,
1997 int evaluate,
1998 int want_string UNUSED)
1999{
2000 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002001 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002002 char_u *p;
2003 int get_float = FALSE;
2004
2005 // We accept a float when the format matches
2006 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2007 // strict to avoid backwards compatibility problems.
2008 // With script version 2 and later the leading digit can be
2009 // omitted.
2010 // Don't look for a float after the "." operator, so that
2011 // ":let vers = 1.2.3" doesn't fail.
2012 if (**arg == '.')
2013 p = *arg;
2014 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002015 {
2016 p = *arg + 1;
2017 if (skip_quotes)
2018 for (;;)
2019 {
2020 if (*p == '\'')
2021 ++p;
2022 if (!vim_isdigit(*p))
2023 break;
2024 p = skipdigits(p);
2025 }
2026 else
2027 p = skipdigits(p);
2028 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002029 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2030 {
2031 get_float = TRUE;
2032 p = skipdigits(p + 2);
2033 if (*p == 'e' || *p == 'E')
2034 {
2035 ++p;
2036 if (*p == '-' || *p == '+')
2037 ++p;
2038 if (!vim_isdigit(*p))
2039 get_float = FALSE;
2040 else
2041 p = skipdigits(p + 1);
2042 }
2043 if (ASCII_ISALPHA(*p) || *p == '.')
2044 get_float = FALSE;
2045 }
2046 if (get_float)
2047 {
2048 float_T f;
2049
Bram Moolenaar29500652021-08-08 15:43:34 +02002050 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002051 if (evaluate)
2052 {
2053 rettv->v_type = VAR_FLOAT;
2054 rettv->vval.v_float = f;
2055 }
2056 }
2057 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002058 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2059 {
2060 char_u *bp;
2061 blob_T *blob = NULL; // init for gcc
2062
2063 // Blob constant: 0z0123456789abcdef
2064 if (evaluate)
2065 blob = blob_alloc();
2066 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2067 {
2068 if (!vim_isxdigit(bp[1]))
2069 {
2070 if (blob != NULL)
2071 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002072 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002073 ga_clear(&blob->bv_ga);
2074 VIM_CLEAR(blob);
2075 }
2076 return FAIL;
2077 }
2078 if (blob != NULL)
2079 ga_append(&blob->bv_ga,
2080 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2081 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2082 ++bp;
2083 }
2084 if (blob != NULL)
2085 rettv_blob_set(rettv, blob);
2086 *arg = bp;
2087 }
2088 else
2089 {
2090 varnumber_T n;
2091
2092 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002093 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002094 ? STR2NR_NO_OCT + STR2NR_QUOTE
2095 : STR2NR_ALL, &n, NULL, 0, TRUE);
2096 if (len == 0)
2097 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002098 if (evaluate)
2099 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002100 return FAIL;
2101 }
2102 *arg += len;
2103 if (evaluate)
2104 {
2105 rettv->v_type = VAR_NUMBER;
2106 rettv->vval.v_number = n;
2107 }
2108 }
2109 return OK;
2110}
2111
2112/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002113 * Evaluate a string constant and put the result in "rettv".
2114 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2115 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2116 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002117 * Return OK or FAIL.
2118 */
2119 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002120eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002121{
2122 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002123 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002124 int extra = interpolate ? 1 : 0;
2125 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002126 int len;
2127
2128 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002129 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002130 {
2131 if (*p == '\\' && p[1] != NUL)
2132 {
2133 ++p;
2134 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002135 // to 9 characters (6 for the char and 3 for a modifier):
2136 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002137 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002138 {
2139 int modifiers = 0;
2140 int flags = FSK_KEYCODE | FSK_IN_STRING;
2141
zeertzjqdb088872022-05-02 22:53:45 +01002142 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002143
2144 // Skip to the '>' to avoid using '{' inside for string
2145 // interpolation.
2146 if (p[1] != '*')
2147 flags |= FSK_SIMPLIFY;
2148 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2149 --p; // leave "p" on the ">"
2150 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002151 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002152 else if (interpolate && (*p == '{' || *p == '}'))
2153 {
2154 if (*p == '{' && p[1] != '{') // start of expression
2155 break;
2156 ++p;
2157 if (p[-1] == '}' && *p != '}') // single '}' is an error
2158 {
2159 semsg(_(e_stray_closing_curly_str), *arg);
2160 return FAIL;
2161 }
2162 --extra; // "{{" becomes "{", "}}" becomes "}"
2163 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002164 }
2165
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002166 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002167 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002168 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002169 return FAIL;
2170 }
2171
2172 // If only parsing, set *arg and return here
2173 if (!evaluate)
2174 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002175 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002176 return OK;
2177 }
2178
2179 // Copy the string into allocated memory, handling backslashed
2180 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002181 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002182 len = (int)(p - *arg + extra);
2183 rettv->vval.v_string = alloc(len);
2184 if (rettv->vval.v_string == NULL)
2185 return FAIL;
2186 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002187
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002188 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002189 {
2190 if (*p == '\\')
2191 {
2192 switch (*++p)
2193 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002194 case 'b': *end++ = BS; ++p; break;
2195 case 'e': *end++ = ESC; ++p; break;
2196 case 'f': *end++ = FF; ++p; break;
2197 case 'n': *end++ = NL; ++p; break;
2198 case 'r': *end++ = CAR; ++p; break;
2199 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002200
2201 case 'X': // hex: "\x1", "\x12"
2202 case 'x':
2203 case 'u': // Unicode: "\u0023"
2204 case 'U':
2205 if (vim_isxdigit(p[1]))
2206 {
2207 int n, nr;
2208 int c = toupper(*p);
2209
2210 if (c == 'X')
2211 n = 2;
2212 else if (*p == 'u')
2213 n = 4;
2214 else
2215 n = 8;
2216 nr = 0;
2217 while (--n >= 0 && vim_isxdigit(p[1]))
2218 {
2219 ++p;
2220 nr = (nr << 4) + hex2nr(*p);
2221 }
2222 ++p;
2223 // For "\u" store the number according to
2224 // 'encoding'.
2225 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002226 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002227 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002228 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002229 }
2230 break;
2231
2232 // octal: "\1", "\12", "\123"
2233 case '0':
2234 case '1':
2235 case '2':
2236 case '3':
2237 case '4':
2238 case '5':
2239 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002240 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002241 if (*p >= '0' && *p <= '7')
2242 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002243 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002244 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002245 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002246 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002247 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002248 break;
2249
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002250 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002251 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002252 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002253 int flags = FSK_KEYCODE | FSK_IN_STRING;
2254
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002255 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002256 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002257 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002258 if (extra != 0)
2259 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002260 end += extra;
2261 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002262 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002263 break;
2264 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002265 }
2266 // FALLTHROUGH
2267
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002268 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002269 break;
2270 }
2271 }
2272 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002273 {
2274 if (interpolate && (*p == '{' || *p == '}'))
2275 {
2276 if (*p == '{' && p[1] != '{') // start of expression
2277 break;
2278 ++p; // reduce "{{" to "{" and "}}" to "}"
2279 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002280 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002281 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002282 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002283 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002284 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002285 ++p;
2286 *arg = p;
2287
2288 return OK;
2289}
2290
2291/*
2292 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002293 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2294 * Return OK when a "rettv" was set to the string.
2295 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002296 */
2297 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002298eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002299{
2300 char_u *p;
2301 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002302 int reduce = interpolate ? -1 : 0;
2303 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002304
2305 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002306 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002307 {
2308 if (*p == '\'')
2309 {
2310 if (p[1] != '\'')
2311 break;
2312 ++reduce;
2313 ++p;
2314 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002315 else if (interpolate)
2316 {
2317 if (*p == '{')
2318 {
2319 if (p[1] != '{')
2320 break;
2321 ++p;
2322 ++reduce;
2323 }
2324 else if (*p == '}')
2325 {
2326 ++p;
2327 if (*p != '}')
2328 {
2329 semsg(_(e_stray_closing_curly_str), *arg);
2330 return FAIL;
2331 }
2332 ++reduce;
2333 }
2334 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002335 }
2336
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002337 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002338 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002339 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002340 return FAIL;
2341 }
2342
2343 // If only parsing return after setting "*arg"
2344 if (!evaluate)
2345 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002346 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002347 return OK;
2348 }
2349
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002350 // Copy the string into allocated memory, handling '' to ' reduction and
2351 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002352 str = alloc((p - *arg) - reduce);
2353 if (str == NULL)
2354 return FAIL;
2355 rettv->v_type = VAR_STRING;
2356 rettv->vval.v_string = str;
2357
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002358 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002359 {
2360 if (*p == '\'')
2361 {
2362 if (p[1] != '\'')
2363 break;
2364 ++p;
2365 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002366 else if (interpolate && (*p == '{' || *p == '}'))
2367 {
2368 if (*p == '{' && p[1] != '{')
2369 break;
2370 ++p;
2371 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002372 MB_COPY_CHAR(p, str);
2373 }
2374 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002375 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002376
2377 return OK;
2378}
2379
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002380/*
2381 * Evaluate a single or double quoted string possibly containing expressions.
2382 * "arg" points to the '$'. The result is put in "rettv".
2383 * Returns OK or FAIL.
2384 */
LemonBoy2eaef102022-05-06 13:14:50 +01002385 int
2386eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2387{
2388 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002389 int ret = OK;
2390 int quote;
2391 garray_T ga;
2392 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002393
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002394 ga_init2(&ga, 1, 80);
2395
2396 // *arg is on the '$' character, move it to the first string character.
2397 ++*arg;
2398 quote = **arg;
2399 ++*arg;
2400
2401 for (;;)
2402 {
2403 // Get the string up to the matching quote or to a single '{'.
2404 // "arg" is advanced to either the quote or the '{'.
2405 if (quote == '"')
2406 ret = eval_string(arg, &tv, evaluate, TRUE);
2407 else
2408 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2409 if (ret == FAIL)
2410 break;
2411 if (evaluate)
2412 {
2413 ga_concat(&ga, tv.vval.v_string);
2414 clear_tv(&tv);
2415 }
2416
2417 if (**arg != '{')
2418 {
2419 // found terminating quote
2420 ++*arg;
2421 break;
2422 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002423 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002424 if (p == NULL)
2425 {
2426 ret = FAIL;
2427 break;
2428 }
2429 *arg = p;
2430 }
LemonBoy2eaef102022-05-06 13:14:50 +01002431
2432 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002433 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2434 {
2435 ga_clear(&ga);
2436 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002437 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002438 }
LemonBoy2eaef102022-05-06 13:14:50 +01002439
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002440 rettv->vval.v_string = ga.ga_data;
2441 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002442}
2443
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002444/*
2445 * Return a string with the string representation of a variable.
2446 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2447 * "numbuf" is used for a number.
2448 * Puts quotes around strings, so that they can be parsed back by eval().
2449 * May return NULL.
2450 */
2451 char_u *
2452tv2string(
2453 typval_T *tv,
2454 char_u **tofree,
2455 char_u *numbuf,
2456 int copyID)
2457{
2458 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2459}
2460
2461/*
2462 * Get the value of an environment variable.
2463 * "arg" is pointing to the '$'. It is advanced to after the name.
2464 * If the environment variable was not set, silently assume it is empty.
2465 * Return FAIL if the name is invalid.
2466 */
2467 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002468eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002469{
2470 char_u *string = NULL;
2471 int len;
2472 int cc;
2473 char_u *name;
2474 int mustfree = FALSE;
2475
2476 ++*arg;
2477 name = *arg;
2478 len = get_env_len(arg);
2479 if (evaluate)
2480 {
2481 if (len == 0)
2482 return FAIL; // invalid empty name
2483
2484 cc = name[len];
2485 name[len] = NUL;
2486 // first try vim_getenv(), fast for normal environment vars
2487 string = vim_getenv(name, &mustfree);
2488 if (string != NULL && *string != NUL)
2489 {
2490 if (!mustfree)
2491 string = vim_strsave(string);
2492 }
2493 else
2494 {
2495 if (mustfree)
2496 vim_free(string);
2497
2498 // next try expanding things like $VIM and ${HOME}
2499 string = expand_env_save(name - 1);
2500 if (string != NULL && *string == '$')
2501 VIM_CLEAR(string);
2502 }
2503 name[len] = cc;
2504
2505 rettv->v_type = VAR_STRING;
2506 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002507 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002508 }
2509
2510 return OK;
2511}
2512
2513/*
2514 * Get the lnum from the first argument.
2515 * Also accepts ".", "$", etc., but that only works for the current buffer.
2516 * Returns -1 on error.
2517 */
2518 linenr_T
2519tv_get_lnum(typval_T *argvars)
2520{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002521 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002522 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002523
Bram Moolenaar56acb092020-08-16 14:48:19 +02002524 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2525 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002526 if (lnum <= 0 && did_emsg_before == did_emsg
2527 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002528 {
2529 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002530 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002531
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002532 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002533 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002534 if (fp != NULL)
2535 lnum = fp->lnum;
2536 }
2537 return lnum;
2538}
2539
2540/*
2541 * Get the lnum from the first argument.
2542 * Also accepts "$", then "buf" is used.
2543 * Returns 0 on error.
2544 */
2545 linenr_T
2546tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2547{
2548 if (argvars[0].v_type == VAR_STRING
2549 && argvars[0].vval.v_string != NULL
2550 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002551 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002552 && buf != NULL)
2553 return buf->b_ml.ml_line_count;
2554 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2555}
2556
2557/*
2558 * Get buffer by number or pattern.
2559 */
2560 buf_T *
2561tv_get_buf(typval_T *tv, int curtab_only)
2562{
2563 char_u *name = tv->vval.v_string;
2564 buf_T *buf;
2565
2566 if (tv->v_type == VAR_NUMBER)
2567 return buflist_findnr((int)tv->vval.v_number);
2568 if (tv->v_type != VAR_STRING)
2569 return NULL;
2570 if (name == NULL || *name == NUL)
2571 return curbuf;
2572 if (name[0] == '$' && name[1] == NUL)
2573 return lastbuf;
2574
2575 buf = buflist_find_by_name(name, curtab_only);
2576
2577 // If not found, try expanding the name, like done for bufexists().
2578 if (buf == NULL)
2579 buf = find_buffer(tv);
2580
2581 return buf;
2582}
2583
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002584/*
2585 * Like tv_get_buf() but give an error message is the type is wrong.
2586 */
2587 buf_T *
2588tv_get_buf_from_arg(typval_T *tv)
2589{
2590 buf_T *buf;
2591
2592 ++emsg_off;
2593 buf = tv_get_buf(tv, FALSE);
2594 --emsg_off;
2595 if (buf == NULL
2596 && tv->v_type != VAR_NUMBER
2597 && tv->v_type != VAR_STRING)
2598 // issue errmsg for type error
2599 (void)tv_get_number(tv);
2600 return buf;
2601}
2602
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002603#endif // FEAT_EVAL