blob: 4f6e41dfdd6c99709225e9c72863bb1bddfd13e9 [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:
139#ifdef FEAT_FLOAT
140 varp->vval.v_float = 0.0;
141 break;
142#endif
143 case VAR_JOB:
144#ifdef FEAT_JOB_CHANNEL
145 job_unref(varp->vval.v_job);
146 varp->vval.v_job = NULL;
147#endif
148 break;
149 case VAR_CHANNEL:
150#ifdef FEAT_JOB_CHANNEL
151 channel_unref(varp->vval.v_channel);
152 varp->vval.v_channel = NULL;
153#endif
Bram Moolenaar24f72092021-05-07 20:43:54 +0200154 break;
155 case VAR_INSTR:
156 VIM_CLEAR(varp->vval.v_instr);
157 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200158 case VAR_UNKNOWN:
159 case VAR_ANY:
160 case VAR_VOID:
161 break;
162 }
163 varp->v_lock = 0;
164 }
165}
166
167/*
168 * Set the value of a variable to NULL without freeing items.
169 */
170 void
171init_tv(typval_T *varp)
172{
173 if (varp != NULL)
174 CLEAR_POINTER(varp);
175}
176
Bram Moolenaar36967b32020-08-17 21:41:02 +0200177 static varnumber_T
178tv_get_bool_or_number_chk(typval_T *varp, int *denote, int want_bool)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200179{
180 varnumber_T n = 0L;
181
182 switch (varp->v_type)
183 {
184 case VAR_NUMBER:
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200185 if (in_vim9script() && want_bool && varp->vval.v_number != 0
Bram Moolenaard70840e2020-08-22 15:06:35 +0200186 && varp->vval.v_number != 1)
187 {
188 semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
189 break;
190 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200191 return varp->vval.v_number;
192 case VAR_FLOAT:
193#ifdef FEAT_FLOAT
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000194 emsg(_(e_using_float_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200195 break;
196#endif
197 case VAR_FUNC:
198 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000199 emsg(_(e_using_funcref_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200200 break;
201 case VAR_STRING:
Bram Moolenaar56acb092020-08-16 14:48:19 +0200202 if (in_vim9script())
203 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100204 emsg_using_string_as(varp, !want_bool);
Bram Moolenaar56acb092020-08-16 14:48:19 +0200205 break;
206 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200207 if (varp->vval.v_string != NULL)
208 vim_str2nr(varp->vval.v_string, NULL, NULL,
209 STR2NR_ALL, &n, NULL, 0, FALSE);
210 return n;
211 case VAR_LIST:
Bram Moolenaar677658a2022-01-05 16:09:06 +0000212 emsg(_(e_using_list_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200213 break;
214 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000215 emsg(_(e_using_dictionary_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200216 break;
217 case VAR_BOOL:
218 case VAR_SPECIAL:
Bram Moolenaar36967b32020-08-17 21:41:02 +0200219 if (!want_bool && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200220 {
Bram Moolenaard92cc132020-11-18 17:17:15 +0100221 if (varp->v_type == VAR_BOOL)
222 emsg(_(e_using_bool_as_number));
223 else
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000224 emsg(_(e_using_special_as_number));
Bram Moolenaar56acb092020-08-16 14:48:19 +0200225 break;
226 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200227 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
228 case VAR_JOB:
229#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000230 emsg(_(e_using_job_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200231 break;
232#endif
233 case VAR_CHANNEL:
234#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000235 emsg(_(e_using_channel_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200236 break;
237#endif
238 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000239 emsg(_(e_using_blob_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200240 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200241 case VAR_VOID:
242 emsg(_(e_cannot_use_void_value));
243 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200244 case VAR_UNKNOWN:
245 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200246 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200247 internal_error_no_abort("tv_get_number(UNKNOWN)");
248 break;
249 }
250 if (denote == NULL) // useful for values that must be unsigned
251 n = -1;
252 else
253 *denote = TRUE;
254 return n;
255}
256
Bram Moolenaar36967b32020-08-17 21:41:02 +0200257/*
258 * Get the number value of a variable.
259 * If it is a String variable, uses vim_str2nr().
260 * For incompatible types, return 0.
261 * tv_get_number_chk() is similar to tv_get_number(), but informs the
262 * caller of incompatible types: it sets *denote to TRUE if "denote"
263 * is not NULL or returns -1 otherwise.
264 */
265 varnumber_T
266tv_get_number(typval_T *varp)
267{
268 int error = FALSE;
269
270 return tv_get_number_chk(varp, &error); // return 0L on error
271}
272
273 varnumber_T
274tv_get_number_chk(typval_T *varp, int *denote)
275{
276 return tv_get_bool_or_number_chk(varp, denote, FALSE);
277}
278
279/*
280 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200281 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200282 */
283 varnumber_T
284tv_get_bool(typval_T *varp)
285{
286 return tv_get_bool_or_number_chk(varp, NULL, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200287}
288
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200289/*
290 * Get the boolean value of "varp". This is like tv_get_number_chk(),
291 * but in Vim9 script accepts Number and Bool.
292 */
293 varnumber_T
294tv_get_bool_chk(typval_T *varp, int *denote)
295{
296 return tv_get_bool_or_number_chk(varp, denote, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200297}
298
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200299#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000300 static float_T
301tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200302{
303 switch (varp->v_type)
304 {
305 case VAR_NUMBER:
306 return (float_T)(varp->vval.v_number);
307 case VAR_FLOAT:
308 return varp->vval.v_float;
309 case VAR_FUNC:
310 case VAR_PARTIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000311 emsg(_(e_using_funcref_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200312 break;
313 case VAR_STRING:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000314 emsg(_(e_using_string_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200315 break;
316 case VAR_LIST:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000317 emsg(_(e_using_list_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200318 break;
319 case VAR_DICT:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000320 emsg(_(e_using_dictionary_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200321 break;
322 case VAR_BOOL:
Bram Moolenaarb2810f12022-01-08 21:38:52 +0000323 emsg(_(e_using_boolean_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200324 break;
325 case VAR_SPECIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000326 emsg(_(e_using_special_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200327 break;
328 case VAR_JOB:
329# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000330 emsg(_(e_using_job_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200331 break;
332# endif
333 case VAR_CHANNEL:
334# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000335 emsg(_(e_using_channel_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200336 break;
337# endif
338 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000339 emsg(_(e_using_blob_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200340 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200341 case VAR_VOID:
342 emsg(_(e_cannot_use_void_value));
343 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200344 case VAR_UNKNOWN:
345 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200346 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200347 internal_error_no_abort("tv_get_float(UNKNOWN)");
348 break;
349 }
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000350 if (error != NULL)
351 *error = TRUE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200352 return 0;
353}
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000354
355 float_T
356tv_get_float(typval_T *varp)
357{
358 return tv_get_float_chk(varp, NULL);
359}
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200360#endif
361
362/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200363 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100364 */
365 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100366check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100367{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100368 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100369 {
rbtnnddc80af2021-12-17 18:01:31 +0000370 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100371 return FAIL;
372 }
373 return OK;
374}
375
376/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100377 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100378 */
379 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100380check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100381{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100382 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100383 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100384 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100385 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200386 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100387 return FAIL;
388 }
389 return OK;
390}
391
392/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200393 * Check for an optional string argument at 'idx'
394 */
395 int
396check_for_opt_string_arg(typval_T *args, int idx)
397{
398 return (args[idx].v_type == VAR_UNKNOWN
399 || check_for_string_arg(args, idx) != FAIL);
400}
401
402/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200403 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200404 */
405 int
406check_for_number_arg(typval_T *args, int idx)
407{
408 if (args[idx].v_type != VAR_NUMBER)
409 {
rbtnnddc80af2021-12-17 18:01:31 +0000410 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200411 return FAIL;
412 }
413 return OK;
414}
415
416/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200417 * Check for an optional number argument at 'idx'
418 */
419 int
420check_for_opt_number_arg(typval_T *args, int idx)
421{
422 return (args[idx].v_type == VAR_UNKNOWN
423 || check_for_number_arg(args, idx) != FAIL);
424}
425
426/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200427 * Give an error and return FAIL unless "args[idx]" is a float or a number.
428 */
429 int
430check_for_float_or_nr_arg(typval_T *args, int idx)
431{
432 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
433 {
rbtnnddc80af2021-12-17 18:01:31 +0000434 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200435 return FAIL;
436 }
437 return OK;
438}
439
440/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200441 * Give an error and return FAIL unless "args[idx]" is a bool.
442 */
443 int
444check_for_bool_arg(typval_T *args, int idx)
445{
446 if (args[idx].v_type != VAR_BOOL
447 && !(args[idx].v_type == VAR_NUMBER
448 && (args[idx].vval.v_number == 0
449 || args[idx].vval.v_number == 1)))
450 {
rbtnnddc80af2021-12-17 18:01:31 +0000451 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200452 return FAIL;
453 }
454 return OK;
455}
456
457/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200458 * Check for an optional bool argument at 'idx'.
459 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200460 */
461 int
462check_for_opt_bool_arg(typval_T *args, int idx)
463{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200464 if (args[idx].v_type == VAR_UNKNOWN)
465 return OK;
466 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200467}
468
469/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200470 * Give an error and return FAIL unless "args[idx]" is a blob.
471 */
472 int
473check_for_blob_arg(typval_T *args, int idx)
474{
475 if (args[idx].v_type != VAR_BLOB)
476 {
rbtnnddc80af2021-12-17 18:01:31 +0000477 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200478 return FAIL;
479 }
480 return OK;
481}
482
483/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200484 * Give an error and return FAIL unless "args[idx]" is a list.
485 */
486 int
487check_for_list_arg(typval_T *args, int idx)
488{
489 if (args[idx].v_type != VAR_LIST)
490 {
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200491 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200492 return FAIL;
493 }
494 return OK;
495}
496
497/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200498 * Check for an optional list argument at 'idx'
499 */
500 int
501check_for_opt_list_arg(typval_T *args, int idx)
502{
503 return (args[idx].v_type == VAR_UNKNOWN
504 || check_for_list_arg(args, idx) != FAIL);
505}
506
507/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200508 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200509 */
510 int
511check_for_dict_arg(typval_T *args, int idx)
512{
513 if (args[idx].v_type != VAR_DICT)
514 {
rbtnnddc80af2021-12-17 18:01:31 +0000515 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200516 return FAIL;
517 }
518 return OK;
519}
520
521/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200522 * Check for an optional dict argument at 'idx'
523 */
524 int
525check_for_opt_dict_arg(typval_T *args, int idx)
526{
527 return (args[idx].v_type == VAR_UNKNOWN
528 || check_for_dict_arg(args, idx) != FAIL);
529}
530
Dominique Pelle748b3082022-01-08 12:41:16 +0000531#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200532/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200533 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
534 */
535 int
536check_for_chan_or_job_arg(typval_T *args, int idx)
537{
538 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
539 {
rbtnnddc80af2021-12-17 18:01:31 +0000540 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200541 return FAIL;
542 }
543 return OK;
544}
545
546/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200547 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
548 * job.
549 */
550 int
551check_for_opt_chan_or_job_arg(typval_T *args, int idx)
552{
553 return (args[idx].v_type == VAR_UNKNOWN
554 || check_for_chan_or_job_arg(args, idx) != FAIL);
555}
556
557/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200558 * Give an error and return FAIL unless "args[idx]" is a job.
559 */
560 int
561check_for_job_arg(typval_T *args, int idx)
562{
563 if (args[idx].v_type != VAR_JOB)
564 {
rbtnnddc80af2021-12-17 18:01:31 +0000565 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200566 return FAIL;
567 }
568 return OK;
569}
570
571/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200572 * Check for an optional job argument at 'idx'.
573 */
574 int
575check_for_opt_job_arg(typval_T *args, int idx)
576{
577 return (args[idx].v_type == VAR_UNKNOWN
578 || check_for_job_arg(args, idx) != FAIL);
579}
Dominique Pelle748b3082022-01-08 12:41:16 +0000580#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200581
582/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200583 * Give an error and return FAIL unless "args[idx]" is a string or
584 * a number.
585 */
586 int
587check_for_string_or_number_arg(typval_T *args, int idx)
588{
589 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
590 {
rbtnnddc80af2021-12-17 18:01:31 +0000591 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200592 return FAIL;
593 }
594 return OK;
595}
596
597/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200598 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200599 */
600 int
601check_for_opt_string_or_number_arg(typval_T *args, int idx)
602{
603 return (args[idx].v_type == VAR_UNKNOWN
604 || check_for_string_or_number_arg(args, idx) != FAIL);
605}
606
607/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200608 * Give an error and return FAIL unless "args[idx]" is a buffer number.
609 * Buffer number can be a number or a string.
610 */
611 int
612check_for_buffer_arg(typval_T *args, int idx)
613{
614 return check_for_string_or_number_arg(args, idx);
615}
616
617/*
618 * Check for an optional buffer argument at 'idx'
619 */
620 int
621check_for_opt_buffer_arg(typval_T *args, int idx)
622{
623 return (args[idx].v_type == VAR_UNKNOWN
624 || check_for_buffer_arg(args, idx));
625}
626
627/*
628 * Give an error and return FAIL unless "args[idx]" is a line number.
629 * Line number can be a number or a string.
630 */
631 int
632check_for_lnum_arg(typval_T *args, int idx)
633{
634 return check_for_string_or_number_arg(args, idx);
635}
636
637/*
638 * Check for an optional line number argument at 'idx'
639 */
640 int
641check_for_opt_lnum_arg(typval_T *args, int idx)
642{
643 return (args[idx].v_type == VAR_UNKNOWN
644 || check_for_lnum_arg(args, idx));
645}
646
Dominique Pelle748b3082022-01-08 12:41:16 +0000647#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200648/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200649 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200650 */
651 int
652check_for_string_or_blob_arg(typval_T *args, int idx)
653{
654 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
655 {
rbtnnddc80af2021-12-17 18:01:31 +0000656 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200657 return FAIL;
658 }
659 return OK;
660}
Dominique Pelle748b3082022-01-08 12:41:16 +0000661#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200662
663/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200664 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200665 */
666 int
667check_for_string_or_list_arg(typval_T *args, int idx)
668{
669 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
670 {
rbtnnddc80af2021-12-17 18:01:31 +0000671 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200672 return FAIL;
673 }
674 return OK;
675}
676
677/*
rbtnn0ccb5842021-12-18 18:33:46 +0000678 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
679 * blob.
680 */
681 int
682check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
683{
684 if (args[idx].v_type != VAR_STRING
685 && args[idx].v_type != VAR_LIST
686 && args[idx].v_type != VAR_BLOB)
687 {
688 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
689 return FAIL;
690 }
691 return OK;
692}
693
694/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200695 * Check for an optional string or list argument at 'idx'
696 */
697 int
698check_for_opt_string_or_list_arg(typval_T *args, int idx)
699{
700 return (args[idx].v_type == VAR_UNKNOWN
701 || check_for_string_or_list_arg(args, idx));
702}
703
704/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200705 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
706 */
707 int
708check_for_string_or_dict_arg(typval_T *args, int idx)
709{
710 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
711 {
rbtnnddc80af2021-12-17 18:01:31 +0000712 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200713 return FAIL;
714 }
715 return OK;
716}
717
718/*
719 * Give an error and return FAIL unless "args[idx]" is a string or a number
720 * or a list.
721 */
722 int
723check_for_string_or_number_or_list_arg(typval_T *args, int idx)
724{
725 if (args[idx].v_type != VAR_STRING
726 && args[idx].v_type != VAR_NUMBER
727 && args[idx].v_type != VAR_LIST)
728 {
rbtnn0ccb5842021-12-18 18:33:46 +0000729 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200730 return FAIL;
731 }
732 return OK;
733}
734
735/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200736 * Give an error and return FAIL unless "args[idx]" is an optional string
737 * or number or a list
738 */
739 int
740check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
741{
742 return (args[idx].v_type == VAR_UNKNOWN
743 || check_for_string_or_number_or_list_arg(args, idx) != FAIL);
744}
745
746/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200747 * Give an error and return FAIL unless "args[idx]" is a string or a list
748 * or a dict.
749 */
750 int
751check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
752{
753 if (args[idx].v_type != VAR_STRING
754 && args[idx].v_type != VAR_LIST
755 && args[idx].v_type != VAR_DICT)
756 {
rbtnnddc80af2021-12-17 18:01:31 +0000757 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200758 return FAIL;
759 }
760 return OK;
761}
762
763/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000764 * Give an error and return FAIL unless "args[idx]" is a string
765 * or a function reference.
766 */
767 int
768check_for_string_or_func_arg(typval_T *args, int idx)
769{
770 if (args[idx].v_type != VAR_PARTIAL
771 && args[idx].v_type != VAR_FUNC
772 && args[idx].v_type != VAR_STRING)
773 {
774 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
775 return FAIL;
776 }
777 return OK;
778}
779
780/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200781 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
782 */
783 int
784check_for_list_or_blob_arg(typval_T *args, int idx)
785{
786 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
787 {
rbtnn0ccb5842021-12-18 18:33:46 +0000788 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200789 return FAIL;
790 }
791 return OK;
792}
793
794/*
795 * Give an error and return FAIL unless "args[idx]" is a list or dict
796 */
797 int
798check_for_list_or_dict_arg(typval_T *args, int idx)
799{
800 if (args[idx].v_type != VAR_LIST
801 && args[idx].v_type != VAR_DICT)
802 {
rbtnnddc80af2021-12-17 18:01:31 +0000803 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200804 return FAIL;
805 }
806 return OK;
807}
808
809/*
810 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
811 * blob.
812 */
813 int
814check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
815{
816 if (args[idx].v_type != VAR_LIST
817 && args[idx].v_type != VAR_DICT
818 && args[idx].v_type != VAR_BLOB)
819 {
rbtnnddc80af2021-12-17 18:01:31 +0000820 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200821 return FAIL;
822 }
823 return OK;
824}
825
826/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000827 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
828 * blob or a string.
829 */
830 int
831check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
832{
833 if (args[idx].v_type != VAR_LIST
834 && args[idx].v_type != VAR_DICT
835 && args[idx].v_type != VAR_BLOB
836 && args[idx].v_type != VAR_STRING)
837 {
rbtnnddc80af2021-12-17 18:01:31 +0000838 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000839 return FAIL;
840 }
841 return OK;
842}
843
844/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200845 * Give an error and return FAIL unless "args[idx]" is an optional buffer
846 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200847 */
848 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200849check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200850{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200851 if (args[idx].v_type != VAR_UNKNOWN
852 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200853 && args[idx].v_type != VAR_NUMBER
854 && args[idx].v_type != VAR_DICT)
855 {
rbtnnddc80af2021-12-17 18:01:31 +0000856 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200857 return FAIL;
858 }
859 return OK;
860}
861
862/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200863 * Get the string value of a variable.
864 * If it is a Number variable, the number is converted into a string.
865 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
866 * tv_get_string_buf() uses a given buffer.
867 * If the String variable has never been set, return an empty string.
868 * Never returns NULL;
869 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
870 * NULL on error.
871 */
872 char_u *
873tv_get_string(typval_T *varp)
874{
875 static char_u mybuf[NUMBUFLEN];
876
877 return tv_get_string_buf(varp, mybuf);
878}
879
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100880/*
881 * Like tv_get_string() but don't allow number to string conversion for Vim9.
882 */
883 char_u *
884tv_get_string_strict(typval_T *varp)
885{
886 static char_u mybuf[NUMBUFLEN];
887 char_u *res = tv_get_string_buf_chk_strict(
888 varp, mybuf, in_vim9script());
889
890 return res != NULL ? res : (char_u *)"";
891}
892
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200893 char_u *
894tv_get_string_buf(typval_T *varp, char_u *buf)
895{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200896 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200897
898 return res != NULL ? res : (char_u *)"";
899}
900
901/*
902 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
903 */
904 char_u *
905tv_get_string_chk(typval_T *varp)
906{
907 static char_u mybuf[NUMBUFLEN];
908
909 return tv_get_string_buf_chk(varp, mybuf);
910}
911
912 char_u *
913tv_get_string_buf_chk(typval_T *varp, char_u *buf)
914{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100915 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
916}
917
918 char_u *
919tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
920{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200921 switch (varp->v_type)
922 {
923 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100924 if (strict)
925 {
926 emsg(_(e_using_number_as_string));
927 break;
928 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200929 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
930 (varnumber_T)varp->vval.v_number);
931 return buf;
932 case VAR_FUNC:
933 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000934 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200935 break;
936 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000937 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200938 break;
939 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000940 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200941 break;
942 case VAR_FLOAT:
943#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200944 if (strict)
945 {
Bram Moolenaar0699b042022-01-01 16:01:23 +0000946 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200947 break;
948 }
949 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
950 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200951#endif
952 case VAR_STRING:
953 if (varp->vval.v_string != NULL)
954 return varp->vval.v_string;
955 return (char_u *)"";
956 case VAR_BOOL:
957 case VAR_SPECIAL:
958 STRCPY(buf, get_var_special_name(varp->vval.v_number));
959 return buf;
960 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000961 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200962 break;
963 case VAR_JOB:
964#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200965 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200966 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200967 semsg(_(e_using_invalid_value_as_string_str), "job");
968 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200969 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200970 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200971#endif
972 break;
973 case VAR_CHANNEL:
974#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200975 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200976 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200977 semsg(_(e_using_invalid_value_as_string_str), "channel");
978 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200979 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200980 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200981#endif
982 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200983 case VAR_VOID:
984 emsg(_(e_cannot_use_void_value));
985 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200986 case VAR_UNKNOWN:
987 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200988 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200989 semsg(_(e_using_invalid_value_as_string_str),
990 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200991 break;
992 }
993 return NULL;
994}
995
996/*
997 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
998 * string() on Dict, List, etc.
999 */
1000 char_u *
1001tv_stringify(typval_T *varp, char_u *buf)
1002{
1003 if (varp->v_type == VAR_LIST
1004 || varp->v_type == VAR_DICT
1005 || varp->v_type == VAR_BLOB
1006 || varp->v_type == VAR_FUNC
1007 || varp->v_type == VAR_PARTIAL
1008 || varp->v_type == VAR_FLOAT)
1009 {
1010 typval_T tmp;
1011
1012 f_string(varp, &tmp);
1013 tv_get_string_buf(&tmp, buf);
1014 clear_tv(varp);
1015 *varp = tmp;
1016 return tmp.vval.v_string;
1017 }
1018 return tv_get_string_buf(varp, buf);
1019}
1020
1021/*
1022 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1023 * Also give an error message, using "name" or _("name") when use_gettext is
1024 * TRUE.
1025 */
1026 int
1027tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1028{
1029 int lock = 0;
1030
1031 switch (tv->v_type)
1032 {
1033 case VAR_BLOB:
1034 if (tv->vval.v_blob != NULL)
1035 lock = tv->vval.v_blob->bv_lock;
1036 break;
1037 case VAR_LIST:
1038 if (tv->vval.v_list != NULL)
1039 lock = tv->vval.v_list->lv_lock;
1040 break;
1041 case VAR_DICT:
1042 if (tv->vval.v_dict != NULL)
1043 lock = tv->vval.v_dict->dv_lock;
1044 break;
1045 default:
1046 break;
1047 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001048 return value_check_lock(tv->v_lock, name, use_gettext)
1049 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001050}
1051
1052/*
1053 * Copy the values from typval_T "from" to typval_T "to".
1054 * When needed allocates string or increases reference count.
1055 * Does not make a copy of a list, blob or dict but copies the reference!
1056 * It is OK for "from" and "to" to point to the same item. This is used to
1057 * make a copy later.
1058 */
1059 void
1060copy_tv(typval_T *from, typval_T *to)
1061{
1062 to->v_type = from->v_type;
1063 to->v_lock = 0;
1064 switch (from->v_type)
1065 {
1066 case VAR_NUMBER:
1067 case VAR_BOOL:
1068 case VAR_SPECIAL:
1069 to->vval.v_number = from->vval.v_number;
1070 break;
1071 case VAR_FLOAT:
1072#ifdef FEAT_FLOAT
1073 to->vval.v_float = from->vval.v_float;
1074 break;
1075#endif
1076 case VAR_JOB:
1077#ifdef FEAT_JOB_CHANNEL
1078 to->vval.v_job = from->vval.v_job;
1079 if (to->vval.v_job != NULL)
1080 ++to->vval.v_job->jv_refcount;
1081 break;
1082#endif
1083 case VAR_CHANNEL:
1084#ifdef FEAT_JOB_CHANNEL
1085 to->vval.v_channel = from->vval.v_channel;
1086 if (to->vval.v_channel != NULL)
1087 ++to->vval.v_channel->ch_refcount;
1088 break;
1089#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001090 case VAR_INSTR:
1091 to->vval.v_instr = from->vval.v_instr;
1092 break;
1093
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001094 case VAR_STRING:
1095 case VAR_FUNC:
1096 if (from->vval.v_string == NULL)
1097 to->vval.v_string = NULL;
1098 else
1099 {
1100 to->vval.v_string = vim_strsave(from->vval.v_string);
1101 if (from->v_type == VAR_FUNC)
1102 func_ref(to->vval.v_string);
1103 }
1104 break;
1105 case VAR_PARTIAL:
1106 if (from->vval.v_partial == NULL)
1107 to->vval.v_partial = NULL;
1108 else
1109 {
1110 to->vval.v_partial = from->vval.v_partial;
1111 ++to->vval.v_partial->pt_refcount;
1112 }
1113 break;
1114 case VAR_BLOB:
1115 if (from->vval.v_blob == NULL)
1116 to->vval.v_blob = NULL;
1117 else
1118 {
1119 to->vval.v_blob = from->vval.v_blob;
1120 ++to->vval.v_blob->bv_refcount;
1121 }
1122 break;
1123 case VAR_LIST:
1124 if (from->vval.v_list == NULL)
1125 to->vval.v_list = NULL;
1126 else
1127 {
1128 to->vval.v_list = from->vval.v_list;
1129 ++to->vval.v_list->lv_refcount;
1130 }
1131 break;
1132 case VAR_DICT:
1133 if (from->vval.v_dict == NULL)
1134 to->vval.v_dict = NULL;
1135 else
1136 {
1137 to->vval.v_dict = from->vval.v_dict;
1138 ++to->vval.v_dict->dv_refcount;
1139 }
1140 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001141 case VAR_VOID:
1142 emsg(_(e_cannot_use_void_value));
1143 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001144 case VAR_UNKNOWN:
1145 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001146 internal_error_no_abort("copy_tv(UNKNOWN)");
1147 break;
1148 }
1149}
1150
1151/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001152 * Compare "tv1" and "tv2".
1153 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001154 */
1155 int
1156typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001157 typval_T *tv1, // first operand
1158 typval_T *tv2, // second operand
1159 exprtype_T type, // operator
1160 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001161{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001162 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001163 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001164 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1165
Bram Moolenaar265f8112021-12-19 12:33:05 +00001166 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001167 {
1168 // For "is" a different type always means FALSE, for "notis"
1169 // it means TRUE.
1170 n1 = (type == EXPR_ISNOT);
1171 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001172 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1173 || (tv2->v_type == VAR_SPECIAL
1174 && tv2->vval.v_number == VVAL_NULL))
1175 && tv1->v_type != tv2->v_type
1176 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1177 {
1178 n1 = typval_compare_null(tv1, tv2);
1179 if (n1 == MAYBE)
1180 {
1181 clear_tv(tv1);
1182 return FAIL;
1183 }
1184 if (type == EXPR_NEQUAL)
1185 n1 = !n1;
1186 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001187 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001188 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001189 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001190 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001191 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001192 return FAIL;
1193 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001194 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001195 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001196 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001197 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001198 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001199 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001200 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001201 return FAIL;
1202 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001203 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001204 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001205 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001206 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001207 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001208 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001209 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001210 return FAIL;
1211 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001212 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001213 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001214 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1215 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001216 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001217 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001218 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001219 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001220 return FAIL;
1221 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001222 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001223 }
1224
1225#ifdef FEAT_FLOAT
1226 // If one of the two variables is a float, compare as a float.
1227 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001228 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001229 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1230 {
1231 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001232 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001233
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001234 f1 = tv_get_float_chk(tv1, &error);
1235 if (!error)
1236 f2 = tv_get_float_chk(tv2, &error);
1237 if (error)
1238 {
1239 clear_tv(tv1);
1240 return FAIL;
1241 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001242 n1 = FALSE;
1243 switch (type)
1244 {
1245 case EXPR_IS:
1246 case EXPR_EQUAL: n1 = (f1 == f2); break;
1247 case EXPR_ISNOT:
1248 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1249 case EXPR_GREATER: n1 = (f1 > f2); break;
1250 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1251 case EXPR_SMALLER: n1 = (f1 < f2); break;
1252 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1253 case EXPR_UNKNOWN:
1254 case EXPR_MATCH:
1255 default: break; // avoid gcc warning
1256 }
1257 }
1258#endif
1259
1260 // If one of the two variables is a number, compare as a number.
1261 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001262 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001263 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1264 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001265 int error = FALSE;
1266
1267 n1 = tv_get_number_chk(tv1, &error);
1268 if (!error)
1269 n2 = tv_get_number_chk(tv2, &error);
1270 if (error)
1271 {
1272 clear_tv(tv1);
1273 return FAIL;
1274 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001275 switch (type)
1276 {
1277 case EXPR_IS:
1278 case EXPR_EQUAL: n1 = (n1 == n2); break;
1279 case EXPR_ISNOT:
1280 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1281 case EXPR_GREATER: n1 = (n1 > n2); break;
1282 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1283 case EXPR_SMALLER: n1 = (n1 < n2); break;
1284 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1285 case EXPR_UNKNOWN:
1286 case EXPR_MATCH:
1287 default: break; // avoid gcc warning
1288 }
1289 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001290 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1291 || tv2->v_type == VAR_BOOL
1292 || (tv1->v_type == VAR_SPECIAL
1293 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001294 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001295 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001296 {
1297 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001298 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1299 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001300 return FAIL;
1301 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001302 n1 = tv1->vval.v_number;
1303 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001304 switch (type)
1305 {
1306 case EXPR_IS:
1307 case EXPR_EQUAL: n1 = (n1 == n2); break;
1308 case EXPR_ISNOT:
1309 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1310 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001311 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001312 vartype_name(tv1->v_type));
1313 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001314 return FAIL;
1315 }
1316 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001317#ifdef FEAT_JOB_CHANNEL
1318 else if (tv1->v_type == tv2->v_type
1319 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1320 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1321 {
1322 if (tv1->v_type == VAR_CHANNEL)
1323 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1324 else
1325 n1 = tv1->vval.v_job == tv2->vval.v_job;
1326 if (type == EXPR_NEQUAL)
1327 n1 = !n1;
1328 }
1329#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001330 else
1331 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001332 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001333 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001334 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001335 return FAIL;
1336 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001337 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001338 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001339 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001340 if (in_vim9script())
1341 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001342 tv1->v_type = VAR_BOOL;
1343 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001344 }
1345 else
1346 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001347 tv1->v_type = VAR_NUMBER;
1348 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001349 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001350
1351 return OK;
1352}
1353
Bram Moolenaar34453202021-01-31 13:08:38 +01001354/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001355 * Compare "tv1" to "tv2" as lists acording to "type" and "ic".
1356 * Put the result, false or true, in "res".
1357 * Return FAIL and give an error message when the comparison can't be done.
1358 */
1359 int
1360typval_compare_list(
1361 typval_T *tv1,
1362 typval_T *tv2,
1363 exprtype_T type,
1364 int ic,
1365 int *res)
1366{
1367 int val = 0;
1368
1369 if (type == EXPR_IS || type == EXPR_ISNOT)
1370 {
1371 val = (tv1->v_type == tv2->v_type
1372 && tv1->vval.v_list == tv2->vval.v_list);
1373 if (type == EXPR_ISNOT)
1374 val = !val;
1375 }
1376 else if (tv1->v_type != tv2->v_type
1377 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1378 {
1379 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001380 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001381 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001382 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001383 return FAIL;
1384 }
1385 else
1386 {
1387 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1388 ic, FALSE);
1389 if (type == EXPR_NEQUAL)
1390 val = !val;
1391 }
1392 *res = val;
1393 return OK;
1394}
1395
1396/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001397 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001398 */
1399 int
1400typval_compare_null(typval_T *tv1, typval_T *tv2)
1401{
1402 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1403 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1404 {
1405 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1406
1407 switch (tv->v_type)
1408 {
1409 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001410#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001411 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001412#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001413 case VAR_DICT: return tv->vval.v_dict == NULL;
1414 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001415#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001416 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001417#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001418 case VAR_LIST: return tv->vval.v_list == NULL;
1419 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1420 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001421
1422 case VAR_NUMBER: if (!in_vim9script())
1423 return tv->vval.v_number == 0;
1424 break;
1425#ifdef FEAT_FLOAT
1426 case VAR_FLOAT: if (!in_vim9script())
1427 return tv->vval.v_float == 0.0;
1428 break;
1429#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001430 default: break;
1431 }
1432 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001433 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001434 // we won't give an error
1435 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001436}
1437
1438/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001439 * Compare "tv1" to "tv2" as blobs acording to "type".
1440 * Put the result, false or true, in "res".
1441 * Return FAIL and give an error message when the comparison can't be done.
1442 */
1443 int
1444typval_compare_blob(
1445 typval_T *tv1,
1446 typval_T *tv2,
1447 exprtype_T type,
1448 int *res)
1449{
1450 int val = 0;
1451
1452 if (type == EXPR_IS || type == EXPR_ISNOT)
1453 {
1454 val = (tv1->v_type == tv2->v_type
1455 && tv1->vval.v_blob == tv2->vval.v_blob);
1456 if (type == EXPR_ISNOT)
1457 val = !val;
1458 }
1459 else if (tv1->v_type != tv2->v_type
1460 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1461 {
1462 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001463 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001464 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001465 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001466 return FAIL;
1467 }
1468 else
1469 {
1470 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1471 if (type == EXPR_NEQUAL)
1472 val = !val;
1473 }
1474 *res = val;
1475 return OK;
1476}
1477
1478/*
1479 * Compare "tv1" to "tv2" as dictionaries acording to "type" and "ic".
1480 * Put the result, false or true, in "res".
1481 * Return FAIL and give an error message when the comparison can't be done.
1482 */
1483 int
1484typval_compare_dict(
1485 typval_T *tv1,
1486 typval_T *tv2,
1487 exprtype_T type,
1488 int ic,
1489 int *res)
1490{
1491 int val;
1492
1493 if (type == EXPR_IS || type == EXPR_ISNOT)
1494 {
1495 val = (tv1->v_type == tv2->v_type
1496 && tv1->vval.v_dict == tv2->vval.v_dict);
1497 if (type == EXPR_ISNOT)
1498 val = !val;
1499 }
1500 else if (tv1->v_type != tv2->v_type
1501 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1502 {
1503 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001504 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001505 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001506 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001507 return FAIL;
1508 }
1509 else
1510 {
1511 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1512 if (type == EXPR_NEQUAL)
1513 val = !val;
1514 }
1515 *res = val;
1516 return OK;
1517}
1518
1519/*
1520 * Compare "tv1" to "tv2" as funcrefs acording to "type" and "ic".
1521 * Put the result, false or true, in "res".
1522 * Return FAIL and give an error message when the comparison can't be done.
1523 */
1524 int
1525typval_compare_func(
1526 typval_T *tv1,
1527 typval_T *tv2,
1528 exprtype_T type,
1529 int ic,
1530 int *res)
1531{
1532 int val = 0;
1533
1534 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1535 && type != EXPR_IS && type != EXPR_ISNOT)
1536 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001537 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001538 return FAIL;
1539 }
1540 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1541 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1542 // When both partials are NULL, then they are equal.
1543 // Otherwise they are not equal.
1544 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1545 else if (type == EXPR_IS || type == EXPR_ISNOT)
1546 {
1547 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1548 // strings are considered the same if their value is
1549 // the same
1550 val = tv_equal(tv1, tv2, ic, FALSE);
1551 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1552 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1553 else
1554 val = FALSE;
1555 }
1556 else
1557 val = tv_equal(tv1, tv2, ic, FALSE);
1558 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1559 val = !val;
1560 *res = val;
1561 return OK;
1562}
1563
1564/*
1565 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1566 * Put the result, false or true, in "res".
1567 * Return FAIL and give an error message when the comparison can't be done.
1568 */
1569 int
1570typval_compare_string(
1571 typval_T *tv1,
1572 typval_T *tv2,
1573 exprtype_T type,
1574 int ic,
1575 int *res)
1576{
1577 int i = 0;
1578 int val = FALSE;
1579 char_u *s1, *s2;
1580 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1581
1582 if (in_vim9script()
1583 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1584 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1585 {
1586 semsg(_(e_cannot_compare_str_with_str),
1587 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1588 return FAIL;
1589 }
1590 s1 = tv_get_string_buf(tv1, buf1);
1591 s2 = tv_get_string_buf(tv2, buf2);
1592 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1593 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1594 switch (type)
1595 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001596 case EXPR_IS: if (in_vim9script())
1597 {
1598 // Really check it is the same string, not just
1599 // the same value.
1600 val = tv1->vval.v_string == tv2->vval.v_string;
1601 break;
1602 }
1603 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001604 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001605 case EXPR_ISNOT: if (in_vim9script())
1606 {
1607 // Really check it is not the same string, not
1608 // just a different value.
1609 val = tv1->vval.v_string != tv2->vval.v_string;
1610 break;
1611 }
1612 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001613 case EXPR_NEQUAL: val = (i != 0); break;
1614 case EXPR_GREATER: val = (i > 0); break;
1615 case EXPR_GEQUAL: val = (i >= 0); break;
1616 case EXPR_SMALLER: val = (i < 0); break;
1617 case EXPR_SEQUAL: val = (i <= 0); break;
1618
1619 case EXPR_MATCH:
1620 case EXPR_NOMATCH:
1621 val = pattern_match(s2, s1, ic);
1622 if (type == EXPR_NOMATCH)
1623 val = !val;
1624 break;
1625
1626 default: break; // avoid gcc warning
1627 }
1628 *res = val;
1629 return OK;
1630}
1631/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001632 * Convert any type to a string, never give an error.
1633 * When "quotes" is TRUE add quotes to a string.
1634 * Returns an allocated string.
1635 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001636 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001637typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001638{
1639 char_u *tofree;
1640 char_u numbuf[NUMBUFLEN];
1641 char_u *ret = NULL;
1642
1643 if (arg == NULL)
1644 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001645 if (!quotes && arg->v_type == VAR_STRING)
1646 {
1647 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1648 : arg->vval.v_string);
1649 }
1650 else
1651 {
1652 ret = tv2string(arg, &tofree, numbuf, 0);
1653 // Make a copy if we have a value but it's not in allocated memory.
1654 if (ret != NULL && tofree == NULL)
1655 ret = vim_strsave(ret);
1656 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001657 return ret;
1658}
1659
1660/*
1661 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1662 * or it refers to a List or Dictionary that is locked.
1663 */
1664 int
1665tv_islocked(typval_T *tv)
1666{
1667 return (tv->v_lock & VAR_LOCKED)
1668 || (tv->v_type == VAR_LIST
1669 && tv->vval.v_list != NULL
1670 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1671 || (tv->v_type == VAR_DICT
1672 && tv->vval.v_dict != NULL
1673 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1674}
1675
1676 static int
1677func_equal(
1678 typval_T *tv1,
1679 typval_T *tv2,
1680 int ic) // ignore case
1681{
1682 char_u *s1, *s2;
1683 dict_T *d1, *d2;
1684 int a1, a2;
1685 int i;
1686
1687 // empty and NULL function name considered the same
1688 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1689 : partial_name(tv1->vval.v_partial);
1690 if (s1 != NULL && *s1 == NUL)
1691 s1 = NULL;
1692 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1693 : partial_name(tv2->vval.v_partial);
1694 if (s2 != NULL && *s2 == NUL)
1695 s2 = NULL;
1696 if (s1 == NULL || s2 == NULL)
1697 {
1698 if (s1 != s2)
1699 return FALSE;
1700 }
1701 else if (STRCMP(s1, s2) != 0)
1702 return FALSE;
1703
1704 // empty dict and NULL dict is different
1705 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1706 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1707 if (d1 == NULL || d2 == NULL)
1708 {
1709 if (d1 != d2)
1710 return FALSE;
1711 }
1712 else if (!dict_equal(d1, d2, ic, TRUE))
1713 return FALSE;
1714
1715 // empty list and no list considered the same
1716 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1717 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1718 if (a1 != a2)
1719 return FALSE;
1720 for (i = 0; i < a1; ++i)
1721 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1722 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1723 return FALSE;
1724
1725 return TRUE;
1726}
1727
1728/*
1729 * Return TRUE if "tv1" and "tv2" have the same value.
1730 * Compares the items just like "==" would compare them, but strings and
1731 * numbers are different. Floats and numbers are also different.
1732 */
1733 int
1734tv_equal(
1735 typval_T *tv1,
1736 typval_T *tv2,
1737 int ic, // ignore case
1738 int recursive) // TRUE when used recursively
1739{
1740 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1741 char_u *s1, *s2;
1742 static int recursive_cnt = 0; // catch recursive loops
1743 int r;
1744 static int tv_equal_recurse_limit;
1745
1746 // Catch lists and dicts that have an endless loop by limiting
1747 // recursiveness to a limit. We guess they are equal then.
1748 // A fixed limit has the problem of still taking an awful long time.
1749 // Reduce the limit every time running into it. That should work fine for
1750 // deeply linked structures that are not recursively linked and catch
1751 // recursiveness quickly.
1752 if (!recursive)
1753 tv_equal_recurse_limit = 1000;
1754 if (recursive_cnt >= tv_equal_recurse_limit)
1755 {
1756 --tv_equal_recurse_limit;
1757 return TRUE;
1758 }
1759
1760 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1761 // arguments.
1762 if ((tv1->v_type == VAR_FUNC
1763 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1764 && (tv2->v_type == VAR_FUNC
1765 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1766 {
1767 ++recursive_cnt;
1768 r = func_equal(tv1, tv2, ic);
1769 --recursive_cnt;
1770 return r;
1771 }
1772
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001773 if (tv1->v_type != tv2->v_type
1774 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1775 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001776 return FALSE;
1777
1778 switch (tv1->v_type)
1779 {
1780 case VAR_LIST:
1781 ++recursive_cnt;
1782 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1783 --recursive_cnt;
1784 return r;
1785
1786 case VAR_DICT:
1787 ++recursive_cnt;
1788 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1789 --recursive_cnt;
1790 return r;
1791
1792 case VAR_BLOB:
1793 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1794
1795 case VAR_NUMBER:
1796 case VAR_BOOL:
1797 case VAR_SPECIAL:
1798 return tv1->vval.v_number == tv2->vval.v_number;
1799
1800 case VAR_STRING:
1801 s1 = tv_get_string_buf(tv1, buf1);
1802 s2 = tv_get_string_buf(tv2, buf2);
1803 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1804
1805 case VAR_FLOAT:
1806#ifdef FEAT_FLOAT
1807 return tv1->vval.v_float == tv2->vval.v_float;
1808#endif
1809 case VAR_JOB:
1810#ifdef FEAT_JOB_CHANNEL
1811 return tv1->vval.v_job == tv2->vval.v_job;
1812#endif
1813 case VAR_CHANNEL:
1814#ifdef FEAT_JOB_CHANNEL
1815 return tv1->vval.v_channel == tv2->vval.v_channel;
1816#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001817 case VAR_INSTR:
1818 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001819
1820 case VAR_PARTIAL:
1821 return tv1->vval.v_partial == tv2->vval.v_partial;
1822
1823 case VAR_FUNC:
1824 return tv1->vval.v_string == tv2->vval.v_string;
1825
1826 case VAR_UNKNOWN:
1827 case VAR_ANY:
1828 case VAR_VOID:
1829 break;
1830 }
1831
1832 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1833 // does not equal anything, not even itself.
1834 return FALSE;
1835}
1836
1837/*
1838 * Get an option value.
1839 * "arg" points to the '&' or '+' before the option name.
1840 * "arg" is advanced to character after the option name.
1841 * Return OK or FAIL.
1842 */
1843 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001844eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001845 char_u **arg,
1846 typval_T *rettv, // when NULL, only check if option exists
1847 int evaluate)
1848{
1849 char_u *option_end;
1850 long numval;
1851 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001852 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001853 int c;
1854 int working = (**arg == '+'); // has("+option")
1855 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001856 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001857
1858 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001859 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001860 if (option_end == NULL)
1861 {
1862 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001863 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001864 return FAIL;
1865 }
1866
1867 if (!evaluate)
1868 {
1869 *arg = option_end;
1870 return OK;
1871 }
1872
1873 c = *option_end;
1874 *option_end = NUL;
1875 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001876 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001877
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001878 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001879 {
1880 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001881 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001882 ret = FAIL;
1883 }
1884 else if (rettv != NULL)
1885 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001886 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001887 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001888 {
1889 rettv->v_type = VAR_STRING;
1890 rettv->vval.v_string = NULL;
1891 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001892 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001893 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001894 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1895 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001896 rettv->vval.v_number = 0;
1897 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001898 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001899 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001900 if (in_vim9script() && opt_type == gov_bool)
1901 {
1902 rettv->v_type = VAR_BOOL;
1903 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1904 }
1905 else
1906 {
1907 rettv->v_type = VAR_NUMBER;
1908 rettv->vval.v_number = numval;
1909 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001910 }
1911 else // string option
1912 {
1913 rettv->v_type = VAR_STRING;
1914 rettv->vval.v_string = stringval;
1915 }
1916 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001917 else if (working && (opt_type == gov_hidden_bool
1918 || opt_type == gov_hidden_number
1919 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001920 ret = FAIL;
1921
1922 *option_end = c; // put back for error messages
1923 *arg = option_end;
1924
1925 return ret;
1926}
1927
1928/*
1929 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1930 * Return OK or FAIL.
1931 */
1932 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001933eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001934 char_u **arg,
1935 typval_T *rettv,
1936 int evaluate,
1937 int want_string UNUSED)
1938{
1939 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02001940 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001941#ifdef FEAT_FLOAT
1942 char_u *p;
1943 int get_float = FALSE;
1944
1945 // We accept a float when the format matches
1946 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1947 // strict to avoid backwards compatibility problems.
1948 // With script version 2 and later the leading digit can be
1949 // omitted.
1950 // Don't look for a float after the "." operator, so that
1951 // ":let vers = 1.2.3" doesn't fail.
1952 if (**arg == '.')
1953 p = *arg;
1954 else
Bram Moolenaar29500652021-08-08 15:43:34 +02001955 {
1956 p = *arg + 1;
1957 if (skip_quotes)
1958 for (;;)
1959 {
1960 if (*p == '\'')
1961 ++p;
1962 if (!vim_isdigit(*p))
1963 break;
1964 p = skipdigits(p);
1965 }
1966 else
1967 p = skipdigits(p);
1968 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001969 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1970 {
1971 get_float = TRUE;
1972 p = skipdigits(p + 2);
1973 if (*p == 'e' || *p == 'E')
1974 {
1975 ++p;
1976 if (*p == '-' || *p == '+')
1977 ++p;
1978 if (!vim_isdigit(*p))
1979 get_float = FALSE;
1980 else
1981 p = skipdigits(p + 1);
1982 }
1983 if (ASCII_ISALPHA(*p) || *p == '.')
1984 get_float = FALSE;
1985 }
1986 if (get_float)
1987 {
1988 float_T f;
1989
Bram Moolenaar29500652021-08-08 15:43:34 +02001990 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001991 if (evaluate)
1992 {
1993 rettv->v_type = VAR_FLOAT;
1994 rettv->vval.v_float = f;
1995 }
1996 }
1997 else
1998#endif
1999 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2000 {
2001 char_u *bp;
2002 blob_T *blob = NULL; // init for gcc
2003
2004 // Blob constant: 0z0123456789abcdef
2005 if (evaluate)
2006 blob = blob_alloc();
2007 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2008 {
2009 if (!vim_isxdigit(bp[1]))
2010 {
2011 if (blob != NULL)
2012 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002013 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002014 ga_clear(&blob->bv_ga);
2015 VIM_CLEAR(blob);
2016 }
2017 return FAIL;
2018 }
2019 if (blob != NULL)
2020 ga_append(&blob->bv_ga,
2021 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2022 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2023 ++bp;
2024 }
2025 if (blob != NULL)
2026 rettv_blob_set(rettv, blob);
2027 *arg = bp;
2028 }
2029 else
2030 {
2031 varnumber_T n;
2032
2033 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002034 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002035 ? STR2NR_NO_OCT + STR2NR_QUOTE
2036 : STR2NR_ALL, &n, NULL, 0, TRUE);
2037 if (len == 0)
2038 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002039 if (evaluate)
2040 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002041 return FAIL;
2042 }
2043 *arg += len;
2044 if (evaluate)
2045 {
2046 rettv->v_type = VAR_NUMBER;
2047 rettv->vval.v_number = n;
2048 }
2049 }
2050 return OK;
2051}
2052
2053/*
2054 * Allocate a variable for a string constant.
2055 * Return OK or FAIL.
2056 */
2057 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002058eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002059{
2060 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002061 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002062 int extra = 0;
2063 int len;
2064
2065 // Find the end of the string, skipping backslashed characters.
2066 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
2067 {
2068 if (*p == '\\' && p[1] != NUL)
2069 {
2070 ++p;
2071 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002072 // to 9 characters (6 for the char and 3 for a modifier):
2073 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002074 if (*p == '<')
zeertzjqdb088872022-05-02 22:53:45 +01002075 extra += 5;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002076 }
2077 }
2078
2079 if (*p != '"')
2080 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002081 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002082 return FAIL;
2083 }
2084
2085 // If only parsing, set *arg and return here
2086 if (!evaluate)
2087 {
2088 *arg = p + 1;
2089 return OK;
2090 }
2091
2092 // Copy the string into allocated memory, handling backslashed
2093 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002094 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002095 len = (int)(p - *arg + extra);
2096 rettv->vval.v_string = alloc(len);
2097 if (rettv->vval.v_string == NULL)
2098 return FAIL;
2099 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002100
2101 for (p = *arg + 1; *p != NUL && *p != '"'; )
2102 {
2103 if (*p == '\\')
2104 {
2105 switch (*++p)
2106 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002107 case 'b': *end++ = BS; ++p; break;
2108 case 'e': *end++ = ESC; ++p; break;
2109 case 'f': *end++ = FF; ++p; break;
2110 case 'n': *end++ = NL; ++p; break;
2111 case 'r': *end++ = CAR; ++p; break;
2112 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002113
2114 case 'X': // hex: "\x1", "\x12"
2115 case 'x':
2116 case 'u': // Unicode: "\u0023"
2117 case 'U':
2118 if (vim_isxdigit(p[1]))
2119 {
2120 int n, nr;
2121 int c = toupper(*p);
2122
2123 if (c == 'X')
2124 n = 2;
2125 else if (*p == 'u')
2126 n = 4;
2127 else
2128 n = 8;
2129 nr = 0;
2130 while (--n >= 0 && vim_isxdigit(p[1]))
2131 {
2132 ++p;
2133 nr = (nr << 4) + hex2nr(*p);
2134 }
2135 ++p;
2136 // For "\u" store the number according to
2137 // 'encoding'.
2138 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002139 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002140 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002141 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002142 }
2143 break;
2144
2145 // octal: "\1", "\12", "\123"
2146 case '0':
2147 case '1':
2148 case '2':
2149 case '3':
2150 case '4':
2151 case '5':
2152 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002153 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002154 if (*p >= '0' && *p <= '7')
2155 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002156 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002157 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002158 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002159 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002160 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002161 break;
2162
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002163 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002164 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002165 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002166 int flags = FSK_KEYCODE | FSK_IN_STRING;
2167
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002168 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002169 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002170 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002171 if (extra != 0)
2172 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002173 end += extra;
2174 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002175 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002176 break;
2177 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002178 }
2179 // FALLTHROUGH
2180
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002181 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002182 break;
2183 }
2184 }
2185 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002186 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002187 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002188 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002189 if (*p != NUL) // just in case
2190 ++p;
2191 *arg = p;
2192
2193 return OK;
2194}
2195
2196/*
2197 * Allocate a variable for a 'str''ing' constant.
2198 * Return OK or FAIL.
2199 */
2200 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002201eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002202{
2203 char_u *p;
2204 char_u *str;
2205 int reduce = 0;
2206
2207 // Find the end of the string, skipping ''.
2208 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
2209 {
2210 if (*p == '\'')
2211 {
2212 if (p[1] != '\'')
2213 break;
2214 ++reduce;
2215 ++p;
2216 }
2217 }
2218
2219 if (*p != '\'')
2220 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002221 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002222 return FAIL;
2223 }
2224
2225 // If only parsing return after setting "*arg"
2226 if (!evaluate)
2227 {
2228 *arg = p + 1;
2229 return OK;
2230 }
2231
2232 // Copy the string into allocated memory, handling '' to ' reduction.
2233 str = alloc((p - *arg) - reduce);
2234 if (str == NULL)
2235 return FAIL;
2236 rettv->v_type = VAR_STRING;
2237 rettv->vval.v_string = str;
2238
2239 for (p = *arg + 1; *p != NUL; )
2240 {
2241 if (*p == '\'')
2242 {
2243 if (p[1] != '\'')
2244 break;
2245 ++p;
2246 }
2247 MB_COPY_CHAR(p, str);
2248 }
2249 *str = NUL;
2250 *arg = p + 1;
2251
2252 return OK;
2253}
2254
2255/*
2256 * Return a string with the string representation of a variable.
2257 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2258 * "numbuf" is used for a number.
2259 * Puts quotes around strings, so that they can be parsed back by eval().
2260 * May return NULL.
2261 */
2262 char_u *
2263tv2string(
2264 typval_T *tv,
2265 char_u **tofree,
2266 char_u *numbuf,
2267 int copyID)
2268{
2269 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2270}
2271
2272/*
2273 * Get the value of an environment variable.
2274 * "arg" is pointing to the '$'. It is advanced to after the name.
2275 * If the environment variable was not set, silently assume it is empty.
2276 * Return FAIL if the name is invalid.
2277 */
2278 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002279eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002280{
2281 char_u *string = NULL;
2282 int len;
2283 int cc;
2284 char_u *name;
2285 int mustfree = FALSE;
2286
2287 ++*arg;
2288 name = *arg;
2289 len = get_env_len(arg);
2290 if (evaluate)
2291 {
2292 if (len == 0)
2293 return FAIL; // invalid empty name
2294
2295 cc = name[len];
2296 name[len] = NUL;
2297 // first try vim_getenv(), fast for normal environment vars
2298 string = vim_getenv(name, &mustfree);
2299 if (string != NULL && *string != NUL)
2300 {
2301 if (!mustfree)
2302 string = vim_strsave(string);
2303 }
2304 else
2305 {
2306 if (mustfree)
2307 vim_free(string);
2308
2309 // next try expanding things like $VIM and ${HOME}
2310 string = expand_env_save(name - 1);
2311 if (string != NULL && *string == '$')
2312 VIM_CLEAR(string);
2313 }
2314 name[len] = cc;
2315
2316 rettv->v_type = VAR_STRING;
2317 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002318 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002319 }
2320
2321 return OK;
2322}
2323
2324/*
2325 * Get the lnum from the first argument.
2326 * Also accepts ".", "$", etc., but that only works for the current buffer.
2327 * Returns -1 on error.
2328 */
2329 linenr_T
2330tv_get_lnum(typval_T *argvars)
2331{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002332 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002333
Bram Moolenaar56acb092020-08-16 14:48:19 +02002334 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2335 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002336 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002337 {
2338 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002339 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002340
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002341 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002342 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002343 if (fp != NULL)
2344 lnum = fp->lnum;
2345 }
2346 return lnum;
2347}
2348
2349/*
2350 * Get the lnum from the first argument.
2351 * Also accepts "$", then "buf" is used.
2352 * Returns 0 on error.
2353 */
2354 linenr_T
2355tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2356{
2357 if (argvars[0].v_type == VAR_STRING
2358 && argvars[0].vval.v_string != NULL
2359 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002360 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002361 && buf != NULL)
2362 return buf->b_ml.ml_line_count;
2363 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2364}
2365
2366/*
2367 * Get buffer by number or pattern.
2368 */
2369 buf_T *
2370tv_get_buf(typval_T *tv, int curtab_only)
2371{
2372 char_u *name = tv->vval.v_string;
2373 buf_T *buf;
2374
2375 if (tv->v_type == VAR_NUMBER)
2376 return buflist_findnr((int)tv->vval.v_number);
2377 if (tv->v_type != VAR_STRING)
2378 return NULL;
2379 if (name == NULL || *name == NUL)
2380 return curbuf;
2381 if (name[0] == '$' && name[1] == NUL)
2382 return lastbuf;
2383
2384 buf = buflist_find_by_name(name, curtab_only);
2385
2386 // If not found, try expanding the name, like done for bufexists().
2387 if (buf == NULL)
2388 buf = find_buffer(tv);
2389
2390 return buf;
2391}
2392
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002393/*
2394 * Like tv_get_buf() but give an error message is the type is wrong.
2395 */
2396 buf_T *
2397tv_get_buf_from_arg(typval_T *tv)
2398{
2399 buf_T *buf;
2400
2401 ++emsg_off;
2402 buf = tv_get_buf(tv, FALSE);
2403 --emsg_off;
2404 if (buf == NULL
2405 && tv->v_type != VAR_NUMBER
2406 && tv->v_type != VAR_STRING)
2407 // issue errmsg for type error
2408 (void)tv_get_number(tv);
2409 return buf;
2410}
2411
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002412#endif // FEAT_EVAL