blob: 95abe212e3b5d44f9866a943ec6c0e6819929087 [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/*
Ernie Rael51d04d12022-05-04 15:40:22 +0100363 * Give an error and return FAIL unless "args[idx]" is unknown
364 */
365 int
366check_for_unknown_arg(typval_T *args, int idx)
367{
368 if (args[idx].v_type != VAR_UNKNOWN)
369 {
370 semsg(_(e_too_many_arguments), idx + 1);
371 return FAIL;
372 }
373 return OK;
374}
375
376/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200377 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100378 */
379 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100380check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100381{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100382 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100383 {
rbtnnddc80af2021-12-17 18:01:31 +0000384 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100385 return FAIL;
386 }
387 return OK;
388}
389
390/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100391 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100392 */
393 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100394check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100395{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100396 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100397 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100398 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100399 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200400 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100401 return FAIL;
402 }
403 return OK;
404}
405
406/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200407 * Check for an optional string argument at 'idx'
408 */
409 int
410check_for_opt_string_arg(typval_T *args, int idx)
411{
412 return (args[idx].v_type == VAR_UNKNOWN
413 || check_for_string_arg(args, idx) != FAIL);
414}
415
416/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200417 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200418 */
419 int
420check_for_number_arg(typval_T *args, int idx)
421{
422 if (args[idx].v_type != VAR_NUMBER)
423 {
rbtnnddc80af2021-12-17 18:01:31 +0000424 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200425 return FAIL;
426 }
427 return OK;
428}
429
430/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200431 * Check for an optional number argument at 'idx'
432 */
433 int
434check_for_opt_number_arg(typval_T *args, int idx)
435{
436 return (args[idx].v_type == VAR_UNKNOWN
437 || check_for_number_arg(args, idx) != FAIL);
438}
439
440/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200441 * Give an error and return FAIL unless "args[idx]" is a float or a number.
442 */
443 int
444check_for_float_or_nr_arg(typval_T *args, int idx)
445{
446 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
447 {
rbtnnddc80af2021-12-17 18:01:31 +0000448 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200449 return FAIL;
450 }
451 return OK;
452}
453
454/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200455 * Give an error and return FAIL unless "args[idx]" is a bool.
456 */
457 int
458check_for_bool_arg(typval_T *args, int idx)
459{
460 if (args[idx].v_type != VAR_BOOL
461 && !(args[idx].v_type == VAR_NUMBER
462 && (args[idx].vval.v_number == 0
463 || args[idx].vval.v_number == 1)))
464 {
rbtnnddc80af2021-12-17 18:01:31 +0000465 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200466 return FAIL;
467 }
468 return OK;
469}
470
471/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200472 * Check for an optional bool argument at 'idx'.
473 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200474 */
475 int
476check_for_opt_bool_arg(typval_T *args, int idx)
477{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200478 if (args[idx].v_type == VAR_UNKNOWN)
479 return OK;
480 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200481}
482
483/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200484 * Give an error and return FAIL unless "args[idx]" is a blob.
485 */
486 int
487check_for_blob_arg(typval_T *args, int idx)
488{
489 if (args[idx].v_type != VAR_BLOB)
490 {
rbtnnddc80af2021-12-17 18:01:31 +0000491 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200492 return FAIL;
493 }
494 return OK;
495}
496
497/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200498 * Give an error and return FAIL unless "args[idx]" is a list.
499 */
500 int
501check_for_list_arg(typval_T *args, int idx)
502{
503 if (args[idx].v_type != VAR_LIST)
504 {
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200505 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200506 return FAIL;
507 }
508 return OK;
509}
510
511/*
Bram Moolenaard83392a2022-09-01 12:22:46 +0100512 * Give an error and return FAIL unless "args[idx]" is a non-NULL list.
513 */
514 int
515check_for_nonnull_list_arg(typval_T *args, int idx)
516{
517 if (check_for_list_arg(args, idx) == FAIL)
518 return FAIL;
519
520 if (args[idx].vval.v_list == NULL)
521 {
522 semsg(_(e_non_null_list_required_for_argument_nr), idx + 1);
523 return FAIL;
524 }
525 return OK;
526}
527
528/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200529 * Check for an optional list argument at 'idx'
530 */
531 int
532check_for_opt_list_arg(typval_T *args, int idx)
533{
534 return (args[idx].v_type == VAR_UNKNOWN
535 || check_for_list_arg(args, idx) != FAIL);
536}
537
538/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200539 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200540 */
541 int
542check_for_dict_arg(typval_T *args, int idx)
543{
544 if (args[idx].v_type != VAR_DICT)
545 {
rbtnnddc80af2021-12-17 18:01:31 +0000546 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200547 return FAIL;
548 }
549 return OK;
550}
551
552/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100553 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
554 */
555 int
556check_for_nonnull_dict_arg(typval_T *args, int idx)
557{
558 if (check_for_dict_arg(args, idx) == FAIL)
559 return FAIL;
560
561 if (args[idx].vval.v_dict == NULL)
562 {
563 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
564 return FAIL;
565 }
566 return OK;
567}
568
569/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200570 * Check for an optional dict argument at 'idx'
571 */
572 int
573check_for_opt_dict_arg(typval_T *args, int idx)
574{
575 return (args[idx].v_type == VAR_UNKNOWN
576 || check_for_dict_arg(args, idx) != FAIL);
577}
578
Dominique Pelle748b3082022-01-08 12:41:16 +0000579#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200580/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200581 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
582 */
583 int
584check_for_chan_or_job_arg(typval_T *args, int idx)
585{
586 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
587 {
rbtnnddc80af2021-12-17 18:01:31 +0000588 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200589 return FAIL;
590 }
591 return OK;
592}
593
594/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200595 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
596 * job.
597 */
598 int
599check_for_opt_chan_or_job_arg(typval_T *args, int idx)
600{
601 return (args[idx].v_type == VAR_UNKNOWN
602 || check_for_chan_or_job_arg(args, idx) != FAIL);
603}
604
605/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200606 * Give an error and return FAIL unless "args[idx]" is a job.
607 */
608 int
609check_for_job_arg(typval_T *args, int idx)
610{
611 if (args[idx].v_type != VAR_JOB)
612 {
rbtnnddc80af2021-12-17 18:01:31 +0000613 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200614 return FAIL;
615 }
616 return OK;
617}
618
619/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200620 * Check for an optional job argument at 'idx'.
621 */
622 int
623check_for_opt_job_arg(typval_T *args, int idx)
624{
625 return (args[idx].v_type == VAR_UNKNOWN
626 || check_for_job_arg(args, idx) != FAIL);
627}
Dominique Pelle748b3082022-01-08 12:41:16 +0000628#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200629
630/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200631 * Give an error and return FAIL unless "args[idx]" is a string or
632 * a number.
633 */
634 int
635check_for_string_or_number_arg(typval_T *args, int idx)
636{
637 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
638 {
rbtnnddc80af2021-12-17 18:01:31 +0000639 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200640 return FAIL;
641 }
642 return OK;
643}
644
645/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200646 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200647 */
648 int
649check_for_opt_string_or_number_arg(typval_T *args, int idx)
650{
651 return (args[idx].v_type == VAR_UNKNOWN
652 || check_for_string_or_number_arg(args, idx) != FAIL);
653}
654
655/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200656 * Give an error and return FAIL unless "args[idx]" is a buffer number.
657 * Buffer number can be a number or a string.
658 */
659 int
660check_for_buffer_arg(typval_T *args, int idx)
661{
662 return check_for_string_or_number_arg(args, idx);
663}
664
665/*
666 * Check for an optional buffer argument at 'idx'
667 */
668 int
669check_for_opt_buffer_arg(typval_T *args, int idx)
670{
671 return (args[idx].v_type == VAR_UNKNOWN
672 || check_for_buffer_arg(args, idx));
673}
674
675/*
676 * Give an error and return FAIL unless "args[idx]" is a line number.
677 * Line number can be a number or a string.
678 */
679 int
680check_for_lnum_arg(typval_T *args, int idx)
681{
682 return check_for_string_or_number_arg(args, idx);
683}
684
685/*
686 * Check for an optional line number argument at 'idx'
687 */
688 int
689check_for_opt_lnum_arg(typval_T *args, int idx)
690{
691 return (args[idx].v_type == VAR_UNKNOWN
692 || check_for_lnum_arg(args, idx));
693}
694
Dominique Pelle748b3082022-01-08 12:41:16 +0000695#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200696/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200697 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200698 */
699 int
700check_for_string_or_blob_arg(typval_T *args, int idx)
701{
702 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
703 {
rbtnnddc80af2021-12-17 18:01:31 +0000704 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200705 return FAIL;
706 }
707 return OK;
708}
Dominique Pelle748b3082022-01-08 12:41:16 +0000709#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200710
711/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200712 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200713 */
714 int
715check_for_string_or_list_arg(typval_T *args, int idx)
716{
717 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
718 {
rbtnnddc80af2021-12-17 18:01:31 +0000719 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200720 return FAIL;
721 }
722 return OK;
723}
724
725/*
rbtnn0ccb5842021-12-18 18:33:46 +0000726 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
727 * blob.
728 */
729 int
730check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
731{
732 if (args[idx].v_type != VAR_STRING
733 && args[idx].v_type != VAR_LIST
734 && args[idx].v_type != VAR_BLOB)
735 {
736 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
737 return FAIL;
738 }
739 return OK;
740}
741
742/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200743 * Check for an optional string or list argument at 'idx'
744 */
745 int
746check_for_opt_string_or_list_arg(typval_T *args, int idx)
747{
748 return (args[idx].v_type == VAR_UNKNOWN
749 || check_for_string_or_list_arg(args, idx));
750}
751
752/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200753 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
754 */
755 int
756check_for_string_or_dict_arg(typval_T *args, int idx)
757{
758 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
759 {
rbtnnddc80af2021-12-17 18:01:31 +0000760 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200761 return FAIL;
762 }
763 return OK;
764}
765
766/*
767 * Give an error and return FAIL unless "args[idx]" is a string or a number
768 * or a list.
769 */
770 int
771check_for_string_or_number_or_list_arg(typval_T *args, int idx)
772{
773 if (args[idx].v_type != VAR_STRING
774 && args[idx].v_type != VAR_NUMBER
775 && args[idx].v_type != VAR_LIST)
776 {
rbtnn0ccb5842021-12-18 18:33:46 +0000777 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200778 return FAIL;
779 }
780 return OK;
781}
782
783/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200784 * Give an error and return FAIL unless "args[idx]" is an optional string
785 * or number or a list
786 */
787 int
788check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
789{
790 return (args[idx].v_type == VAR_UNKNOWN
791 || check_for_string_or_number_or_list_arg(args, idx) != FAIL);
792}
793
794/*
Bakudankun375141e2022-09-09 18:46:47 +0100795 * Give an error and return FAIL unless "args[idx]" is a string or a number
796 * or a list or a blob.
797 */
798 int
799check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
800{
801 if (args[idx].v_type != VAR_STRING
802 && args[idx].v_type != VAR_NUMBER
803 && args[idx].v_type != VAR_LIST
804 && args[idx].v_type != VAR_BLOB)
805 {
806 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
807 return FAIL;
808 }
809 return OK;
810}
811
812/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200813 * Give an error and return FAIL unless "args[idx]" is a string or a list
814 * or a dict.
815 */
816 int
817check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
818{
819 if (args[idx].v_type != VAR_STRING
820 && args[idx].v_type != VAR_LIST
821 && args[idx].v_type != VAR_DICT)
822 {
rbtnnddc80af2021-12-17 18:01:31 +0000823 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200824 return FAIL;
825 }
826 return OK;
827}
828
829/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000830 * Give an error and return FAIL unless "args[idx]" is a string
831 * or a function reference.
832 */
833 int
834check_for_string_or_func_arg(typval_T *args, int idx)
835{
836 if (args[idx].v_type != VAR_PARTIAL
837 && args[idx].v_type != VAR_FUNC
838 && args[idx].v_type != VAR_STRING)
839 {
840 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
841 return FAIL;
842 }
843 return OK;
844}
845
846/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200847 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
848 */
849 int
850check_for_list_or_blob_arg(typval_T *args, int idx)
851{
852 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
853 {
rbtnn0ccb5842021-12-18 18:33:46 +0000854 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200855 return FAIL;
856 }
857 return OK;
858}
859
860/*
861 * Give an error and return FAIL unless "args[idx]" is a list or dict
862 */
863 int
864check_for_list_or_dict_arg(typval_T *args, int idx)
865{
866 if (args[idx].v_type != VAR_LIST
867 && args[idx].v_type != VAR_DICT)
868 {
rbtnnddc80af2021-12-17 18:01:31 +0000869 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200870 return FAIL;
871 }
872 return OK;
873}
874
875/*
876 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
877 * blob.
878 */
879 int
880check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
881{
882 if (args[idx].v_type != VAR_LIST
883 && args[idx].v_type != VAR_DICT
884 && args[idx].v_type != VAR_BLOB)
885 {
rbtnnddc80af2021-12-17 18:01:31 +0000886 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200887 return FAIL;
888 }
889 return OK;
890}
891
892/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000893 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
894 * blob or a string.
895 */
896 int
897check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
898{
899 if (args[idx].v_type != VAR_LIST
900 && args[idx].v_type != VAR_DICT
901 && args[idx].v_type != VAR_BLOB
902 && args[idx].v_type != VAR_STRING)
903 {
rbtnnddc80af2021-12-17 18:01:31 +0000904 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000905 return FAIL;
906 }
907 return OK;
908}
909
910/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200911 * Give an error and return FAIL unless "args[idx]" is an optional buffer
912 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200913 */
914 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200915check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200916{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200917 if (args[idx].v_type != VAR_UNKNOWN
918 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200919 && args[idx].v_type != VAR_NUMBER
920 && args[idx].v_type != VAR_DICT)
921 {
rbtnnddc80af2021-12-17 18:01:31 +0000922 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200923 return FAIL;
924 }
925 return OK;
926}
927
928/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200929 * Get the string value of a variable.
930 * If it is a Number variable, the number is converted into a string.
931 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
932 * tv_get_string_buf() uses a given buffer.
933 * If the String variable has never been set, return an empty string.
934 * Never returns NULL;
935 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
936 * NULL on error.
937 */
938 char_u *
939tv_get_string(typval_T *varp)
940{
941 static char_u mybuf[NUMBUFLEN];
942
943 return tv_get_string_buf(varp, mybuf);
944}
945
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100946/*
947 * Like tv_get_string() but don't allow number to string conversion for Vim9.
948 */
949 char_u *
950tv_get_string_strict(typval_T *varp)
951{
952 static char_u mybuf[NUMBUFLEN];
953 char_u *res = tv_get_string_buf_chk_strict(
954 varp, mybuf, in_vim9script());
955
956 return res != NULL ? res : (char_u *)"";
957}
958
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200959 char_u *
960tv_get_string_buf(typval_T *varp, char_u *buf)
961{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200962 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200963
964 return res != NULL ? res : (char_u *)"";
965}
966
967/*
968 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
969 */
970 char_u *
971tv_get_string_chk(typval_T *varp)
972{
973 static char_u mybuf[NUMBUFLEN];
974
975 return tv_get_string_buf_chk(varp, mybuf);
976}
977
978 char_u *
979tv_get_string_buf_chk(typval_T *varp, char_u *buf)
980{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100981 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
982}
983
984 char_u *
985tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
986{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200987 switch (varp->v_type)
988 {
989 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100990 if (strict)
991 {
992 emsg(_(e_using_number_as_string));
993 break;
994 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200995 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
996 (varnumber_T)varp->vval.v_number);
997 return buf;
998 case VAR_FUNC:
999 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001000 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001001 break;
1002 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001003 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001004 break;
1005 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001006 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001007 break;
1008 case VAR_FLOAT:
1009#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001010 if (strict)
1011 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001012 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001013 break;
1014 }
1015 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1016 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001017#endif
1018 case VAR_STRING:
1019 if (varp->vval.v_string != NULL)
1020 return varp->vval.v_string;
1021 return (char_u *)"";
1022 case VAR_BOOL:
1023 case VAR_SPECIAL:
1024 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1025 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001026 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001027 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001028 break;
1029 case VAR_JOB:
1030#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001031 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001032 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001033 semsg(_(e_using_invalid_value_as_string_str), "job");
1034 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001035 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001036 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001037#endif
1038 break;
1039 case VAR_CHANNEL:
1040#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001041 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001042 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001043 semsg(_(e_using_invalid_value_as_string_str), "channel");
1044 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001045 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001046 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001047#endif
1048 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001049 case VAR_VOID:
1050 emsg(_(e_cannot_use_void_value));
1051 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001052 case VAR_UNKNOWN:
1053 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001054 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001055 semsg(_(e_using_invalid_value_as_string_str),
1056 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001057 break;
1058 }
1059 return NULL;
1060}
1061
1062/*
1063 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1064 * string() on Dict, List, etc.
1065 */
1066 char_u *
1067tv_stringify(typval_T *varp, char_u *buf)
1068{
1069 if (varp->v_type == VAR_LIST
1070 || varp->v_type == VAR_DICT
1071 || varp->v_type == VAR_BLOB
1072 || varp->v_type == VAR_FUNC
1073 || varp->v_type == VAR_PARTIAL
1074 || varp->v_type == VAR_FLOAT)
1075 {
1076 typval_T tmp;
1077
1078 f_string(varp, &tmp);
1079 tv_get_string_buf(&tmp, buf);
1080 clear_tv(varp);
1081 *varp = tmp;
1082 return tmp.vval.v_string;
1083 }
1084 return tv_get_string_buf(varp, buf);
1085}
1086
1087/*
1088 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1089 * Also give an error message, using "name" or _("name") when use_gettext is
1090 * TRUE.
1091 */
1092 int
1093tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1094{
1095 int lock = 0;
1096
1097 switch (tv->v_type)
1098 {
1099 case VAR_BLOB:
1100 if (tv->vval.v_blob != NULL)
1101 lock = tv->vval.v_blob->bv_lock;
1102 break;
1103 case VAR_LIST:
1104 if (tv->vval.v_list != NULL)
1105 lock = tv->vval.v_list->lv_lock;
1106 break;
1107 case VAR_DICT:
1108 if (tv->vval.v_dict != NULL)
1109 lock = tv->vval.v_dict->dv_lock;
1110 break;
1111 default:
1112 break;
1113 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001114 return value_check_lock(tv->v_lock, name, use_gettext)
1115 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001116}
1117
1118/*
1119 * Copy the values from typval_T "from" to typval_T "to".
1120 * When needed allocates string or increases reference count.
1121 * Does not make a copy of a list, blob or dict but copies the reference!
1122 * It is OK for "from" and "to" to point to the same item. This is used to
1123 * make a copy later.
1124 */
1125 void
1126copy_tv(typval_T *from, typval_T *to)
1127{
1128 to->v_type = from->v_type;
1129 to->v_lock = 0;
1130 switch (from->v_type)
1131 {
1132 case VAR_NUMBER:
1133 case VAR_BOOL:
1134 case VAR_SPECIAL:
1135 to->vval.v_number = from->vval.v_number;
1136 break;
1137 case VAR_FLOAT:
1138#ifdef FEAT_FLOAT
1139 to->vval.v_float = from->vval.v_float;
1140 break;
1141#endif
1142 case VAR_JOB:
1143#ifdef FEAT_JOB_CHANNEL
1144 to->vval.v_job = from->vval.v_job;
1145 if (to->vval.v_job != NULL)
1146 ++to->vval.v_job->jv_refcount;
1147 break;
1148#endif
1149 case VAR_CHANNEL:
1150#ifdef FEAT_JOB_CHANNEL
1151 to->vval.v_channel = from->vval.v_channel;
1152 if (to->vval.v_channel != NULL)
1153 ++to->vval.v_channel->ch_refcount;
1154 break;
1155#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001156 case VAR_INSTR:
1157 to->vval.v_instr = from->vval.v_instr;
1158 break;
1159
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001160 case VAR_STRING:
1161 case VAR_FUNC:
1162 if (from->vval.v_string == NULL)
1163 to->vval.v_string = NULL;
1164 else
1165 {
1166 to->vval.v_string = vim_strsave(from->vval.v_string);
1167 if (from->v_type == VAR_FUNC)
1168 func_ref(to->vval.v_string);
1169 }
1170 break;
1171 case VAR_PARTIAL:
1172 if (from->vval.v_partial == NULL)
1173 to->vval.v_partial = NULL;
1174 else
1175 {
1176 to->vval.v_partial = from->vval.v_partial;
1177 ++to->vval.v_partial->pt_refcount;
1178 }
1179 break;
1180 case VAR_BLOB:
1181 if (from->vval.v_blob == NULL)
1182 to->vval.v_blob = NULL;
1183 else
1184 {
1185 to->vval.v_blob = from->vval.v_blob;
1186 ++to->vval.v_blob->bv_refcount;
1187 }
1188 break;
1189 case VAR_LIST:
1190 if (from->vval.v_list == NULL)
1191 to->vval.v_list = NULL;
1192 else
1193 {
1194 to->vval.v_list = from->vval.v_list;
1195 ++to->vval.v_list->lv_refcount;
1196 }
1197 break;
1198 case VAR_DICT:
1199 if (from->vval.v_dict == NULL)
1200 to->vval.v_dict = NULL;
1201 else
1202 {
1203 to->vval.v_dict = from->vval.v_dict;
1204 ++to->vval.v_dict->dv_refcount;
1205 }
1206 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001207 case VAR_VOID:
1208 emsg(_(e_cannot_use_void_value));
1209 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001210 case VAR_UNKNOWN:
1211 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001212 internal_error_no_abort("copy_tv(UNKNOWN)");
1213 break;
1214 }
1215}
1216
1217/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001218 * Compare "tv1" and "tv2".
1219 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001220 */
1221 int
1222typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001223 typval_T *tv1, // first operand
1224 typval_T *tv2, // second operand
1225 exprtype_T type, // operator
1226 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001227{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001228 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001229 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001230 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1231
Bram Moolenaar265f8112021-12-19 12:33:05 +00001232 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001233 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001234 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001235 // it means TRUE.
1236 n1 = (type == EXPR_ISNOT);
1237 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001238 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1239 || (tv2->v_type == VAR_SPECIAL
1240 && tv2->vval.v_number == VVAL_NULL))
1241 && tv1->v_type != tv2->v_type
1242 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1243 {
1244 n1 = typval_compare_null(tv1, tv2);
1245 if (n1 == MAYBE)
1246 {
1247 clear_tv(tv1);
1248 return FAIL;
1249 }
1250 if (type == EXPR_NEQUAL)
1251 n1 = !n1;
1252 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001253 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001254 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001255 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001256 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001257 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001258 return FAIL;
1259 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001260 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001261 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001262 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001263 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001264 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001265 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001266 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001267 return FAIL;
1268 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001269 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001270 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001271 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001272 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001273 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001274 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001275 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001276 return FAIL;
1277 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001278 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001279 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001280 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1281 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001282 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001283 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001284 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001285 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001286 return FAIL;
1287 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001288 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001289 }
1290
1291#ifdef FEAT_FLOAT
1292 // 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 }
1324#endif
1325
1326 // If one of the two variables is a number, compare as a number.
1327 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001328 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001329 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1330 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001331 int error = FALSE;
1332
1333 n1 = tv_get_number_chk(tv1, &error);
1334 if (!error)
1335 n2 = tv_get_number_chk(tv2, &error);
1336 if (error)
1337 {
1338 clear_tv(tv1);
1339 return FAIL;
1340 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001341 switch (type)
1342 {
1343 case EXPR_IS:
1344 case EXPR_EQUAL: n1 = (n1 == n2); break;
1345 case EXPR_ISNOT:
1346 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1347 case EXPR_GREATER: n1 = (n1 > n2); break;
1348 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1349 case EXPR_SMALLER: n1 = (n1 < n2); break;
1350 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1351 case EXPR_UNKNOWN:
1352 case EXPR_MATCH:
1353 default: break; // avoid gcc warning
1354 }
1355 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001356 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1357 || tv2->v_type == VAR_BOOL
1358 || (tv1->v_type == VAR_SPECIAL
1359 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001360 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001361 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001362 {
1363 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001364 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1365 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001366 return FAIL;
1367 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001368 n1 = tv1->vval.v_number;
1369 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001370 switch (type)
1371 {
1372 case EXPR_IS:
1373 case EXPR_EQUAL: n1 = (n1 == n2); break;
1374 case EXPR_ISNOT:
1375 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1376 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001377 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001378 vartype_name(tv1->v_type));
1379 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001380 return FAIL;
1381 }
1382 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001383#ifdef FEAT_JOB_CHANNEL
1384 else if (tv1->v_type == tv2->v_type
1385 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1386 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1387 {
1388 if (tv1->v_type == VAR_CHANNEL)
1389 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1390 else
1391 n1 = tv1->vval.v_job == tv2->vval.v_job;
1392 if (type == EXPR_NEQUAL)
1393 n1 = !n1;
1394 }
1395#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001396 else
1397 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001398 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001399 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001400 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001401 return FAIL;
1402 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001403 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001404 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001405 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001406 if (in_vim9script())
1407 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001408 tv1->v_type = VAR_BOOL;
1409 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001410 }
1411 else
1412 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001413 tv1->v_type = VAR_NUMBER;
1414 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001415 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001416
1417 return OK;
1418}
1419
Bram Moolenaar34453202021-01-31 13:08:38 +01001420/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001421 * Compare "tv1" to "tv2" as lists acording to "type" and "ic".
1422 * Put the result, false or true, in "res".
1423 * Return FAIL and give an error message when the comparison can't be done.
1424 */
1425 int
1426typval_compare_list(
1427 typval_T *tv1,
1428 typval_T *tv2,
1429 exprtype_T type,
1430 int ic,
1431 int *res)
1432{
1433 int val = 0;
1434
1435 if (type == EXPR_IS || type == EXPR_ISNOT)
1436 {
1437 val = (tv1->v_type == tv2->v_type
1438 && tv1->vval.v_list == tv2->vval.v_list);
1439 if (type == EXPR_ISNOT)
1440 val = !val;
1441 }
1442 else if (tv1->v_type != tv2->v_type
1443 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1444 {
1445 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001446 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001447 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001448 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001449 return FAIL;
1450 }
1451 else
1452 {
1453 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1454 ic, FALSE);
1455 if (type == EXPR_NEQUAL)
1456 val = !val;
1457 }
1458 *res = val;
1459 return OK;
1460}
1461
1462/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001463 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001464 */
1465 int
1466typval_compare_null(typval_T *tv1, typval_T *tv2)
1467{
1468 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1469 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1470 {
1471 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1472
1473 switch (tv->v_type)
1474 {
1475 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001476#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001477 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001478#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001479 case VAR_DICT: return tv->vval.v_dict == NULL;
1480 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001481#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001482 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001483#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001484 case VAR_LIST: return tv->vval.v_list == NULL;
1485 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1486 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001487
1488 case VAR_NUMBER: if (!in_vim9script())
1489 return tv->vval.v_number == 0;
1490 break;
1491#ifdef FEAT_FLOAT
1492 case VAR_FLOAT: if (!in_vim9script())
1493 return tv->vval.v_float == 0.0;
1494 break;
1495#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001496 default: break;
1497 }
1498 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001499 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001500 // we won't give an error
1501 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001502}
1503
1504/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001505 * Compare "tv1" to "tv2" as blobs acording to "type".
1506 * Put the result, false or true, in "res".
1507 * Return FAIL and give an error message when the comparison can't be done.
1508 */
1509 int
1510typval_compare_blob(
1511 typval_T *tv1,
1512 typval_T *tv2,
1513 exprtype_T type,
1514 int *res)
1515{
1516 int val = 0;
1517
1518 if (type == EXPR_IS || type == EXPR_ISNOT)
1519 {
1520 val = (tv1->v_type == tv2->v_type
1521 && tv1->vval.v_blob == tv2->vval.v_blob);
1522 if (type == EXPR_ISNOT)
1523 val = !val;
1524 }
1525 else if (tv1->v_type != tv2->v_type
1526 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1527 {
1528 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001529 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001530 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001531 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001532 return FAIL;
1533 }
1534 else
1535 {
1536 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1537 if (type == EXPR_NEQUAL)
1538 val = !val;
1539 }
1540 *res = val;
1541 return OK;
1542}
1543
1544/*
1545 * Compare "tv1" to "tv2" as dictionaries acording to "type" and "ic".
1546 * Put the result, false or true, in "res".
1547 * Return FAIL and give an error message when the comparison can't be done.
1548 */
1549 int
1550typval_compare_dict(
1551 typval_T *tv1,
1552 typval_T *tv2,
1553 exprtype_T type,
1554 int ic,
1555 int *res)
1556{
1557 int val;
1558
1559 if (type == EXPR_IS || type == EXPR_ISNOT)
1560 {
1561 val = (tv1->v_type == tv2->v_type
1562 && tv1->vval.v_dict == tv2->vval.v_dict);
1563 if (type == EXPR_ISNOT)
1564 val = !val;
1565 }
1566 else if (tv1->v_type != tv2->v_type
1567 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1568 {
1569 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001570 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001571 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001572 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001573 return FAIL;
1574 }
1575 else
1576 {
1577 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1578 if (type == EXPR_NEQUAL)
1579 val = !val;
1580 }
1581 *res = val;
1582 return OK;
1583}
1584
1585/*
1586 * Compare "tv1" to "tv2" as funcrefs acording to "type" and "ic".
1587 * Put the result, false or true, in "res".
1588 * Return FAIL and give an error message when the comparison can't be done.
1589 */
1590 int
1591typval_compare_func(
1592 typval_T *tv1,
1593 typval_T *tv2,
1594 exprtype_T type,
1595 int ic,
1596 int *res)
1597{
1598 int val = 0;
1599
1600 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1601 && type != EXPR_IS && type != EXPR_ISNOT)
1602 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001603 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001604 return FAIL;
1605 }
1606 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1607 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1608 // When both partials are NULL, then they are equal.
1609 // Otherwise they are not equal.
1610 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1611 else if (type == EXPR_IS || type == EXPR_ISNOT)
1612 {
1613 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1614 // strings are considered the same if their value is
1615 // the same
1616 val = tv_equal(tv1, tv2, ic, FALSE);
1617 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1618 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1619 else
1620 val = FALSE;
1621 }
1622 else
1623 val = tv_equal(tv1, tv2, ic, FALSE);
1624 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1625 val = !val;
1626 *res = val;
1627 return OK;
1628}
1629
1630/*
1631 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1632 * Put the result, false or true, in "res".
1633 * Return FAIL and give an error message when the comparison can't be done.
1634 */
1635 int
1636typval_compare_string(
1637 typval_T *tv1,
1638 typval_T *tv2,
1639 exprtype_T type,
1640 int ic,
1641 int *res)
1642{
1643 int i = 0;
1644 int val = FALSE;
1645 char_u *s1, *s2;
1646 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1647
1648 if (in_vim9script()
1649 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1650 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1651 {
1652 semsg(_(e_cannot_compare_str_with_str),
1653 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1654 return FAIL;
1655 }
1656 s1 = tv_get_string_buf(tv1, buf1);
1657 s2 = tv_get_string_buf(tv2, buf2);
1658 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1659 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1660 switch (type)
1661 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001662 case EXPR_IS: if (in_vim9script())
1663 {
1664 // Really check it is the same string, not just
1665 // the same value.
1666 val = tv1->vval.v_string == tv2->vval.v_string;
1667 break;
1668 }
1669 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001670 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001671 case EXPR_ISNOT: if (in_vim9script())
1672 {
1673 // Really check it is not the same string, not
1674 // just a different value.
1675 val = tv1->vval.v_string != tv2->vval.v_string;
1676 break;
1677 }
1678 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001679 case EXPR_NEQUAL: val = (i != 0); break;
1680 case EXPR_GREATER: val = (i > 0); break;
1681 case EXPR_GEQUAL: val = (i >= 0); break;
1682 case EXPR_SMALLER: val = (i < 0); break;
1683 case EXPR_SEQUAL: val = (i <= 0); break;
1684
1685 case EXPR_MATCH:
1686 case EXPR_NOMATCH:
1687 val = pattern_match(s2, s1, ic);
1688 if (type == EXPR_NOMATCH)
1689 val = !val;
1690 break;
1691
1692 default: break; // avoid gcc warning
1693 }
1694 *res = val;
1695 return OK;
1696}
1697/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001698 * Convert any type to a string, never give an error.
1699 * When "quotes" is TRUE add quotes to a string.
1700 * Returns an allocated string.
1701 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001702 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001703typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001704{
1705 char_u *tofree;
1706 char_u numbuf[NUMBUFLEN];
1707 char_u *ret = NULL;
1708
1709 if (arg == NULL)
1710 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001711 if (!quotes && arg->v_type == VAR_STRING)
1712 {
1713 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1714 : arg->vval.v_string);
1715 }
1716 else
1717 {
1718 ret = tv2string(arg, &tofree, numbuf, 0);
1719 // Make a copy if we have a value but it's not in allocated memory.
1720 if (ret != NULL && tofree == NULL)
1721 ret = vim_strsave(ret);
1722 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001723 return ret;
1724}
1725
1726/*
1727 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1728 * or it refers to a List or Dictionary that is locked.
1729 */
1730 int
1731tv_islocked(typval_T *tv)
1732{
1733 return (tv->v_lock & VAR_LOCKED)
1734 || (tv->v_type == VAR_LIST
1735 && tv->vval.v_list != NULL
1736 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1737 || (tv->v_type == VAR_DICT
1738 && tv->vval.v_dict != NULL
1739 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1740}
1741
1742 static int
1743func_equal(
1744 typval_T *tv1,
1745 typval_T *tv2,
1746 int ic) // ignore case
1747{
1748 char_u *s1, *s2;
1749 dict_T *d1, *d2;
1750 int a1, a2;
1751 int i;
1752
1753 // empty and NULL function name considered the same
1754 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1755 : partial_name(tv1->vval.v_partial);
1756 if (s1 != NULL && *s1 == NUL)
1757 s1 = NULL;
1758 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1759 : partial_name(tv2->vval.v_partial);
1760 if (s2 != NULL && *s2 == NUL)
1761 s2 = NULL;
1762 if (s1 == NULL || s2 == NULL)
1763 {
1764 if (s1 != s2)
1765 return FALSE;
1766 }
1767 else if (STRCMP(s1, s2) != 0)
1768 return FALSE;
1769
1770 // empty dict and NULL dict is different
1771 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1772 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1773 if (d1 == NULL || d2 == NULL)
1774 {
1775 if (d1 != d2)
1776 return FALSE;
1777 }
1778 else if (!dict_equal(d1, d2, ic, TRUE))
1779 return FALSE;
1780
1781 // empty list and no list considered the same
1782 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1783 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1784 if (a1 != a2)
1785 return FALSE;
1786 for (i = 0; i < a1; ++i)
1787 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1788 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1789 return FALSE;
1790
1791 return TRUE;
1792}
1793
1794/*
1795 * Return TRUE if "tv1" and "tv2" have the same value.
1796 * Compares the items just like "==" would compare them, but strings and
1797 * numbers are different. Floats and numbers are also different.
1798 */
1799 int
1800tv_equal(
1801 typval_T *tv1,
1802 typval_T *tv2,
1803 int ic, // ignore case
1804 int recursive) // TRUE when used recursively
1805{
1806 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1807 char_u *s1, *s2;
1808 static int recursive_cnt = 0; // catch recursive loops
1809 int r;
1810 static int tv_equal_recurse_limit;
1811
1812 // Catch lists and dicts that have an endless loop by limiting
1813 // recursiveness to a limit. We guess they are equal then.
1814 // A fixed limit has the problem of still taking an awful long time.
1815 // Reduce the limit every time running into it. That should work fine for
1816 // deeply linked structures that are not recursively linked and catch
1817 // recursiveness quickly.
1818 if (!recursive)
1819 tv_equal_recurse_limit = 1000;
1820 if (recursive_cnt >= tv_equal_recurse_limit)
1821 {
1822 --tv_equal_recurse_limit;
1823 return TRUE;
1824 }
1825
1826 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1827 // arguments.
1828 if ((tv1->v_type == VAR_FUNC
1829 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1830 && (tv2->v_type == VAR_FUNC
1831 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1832 {
1833 ++recursive_cnt;
1834 r = func_equal(tv1, tv2, ic);
1835 --recursive_cnt;
1836 return r;
1837 }
1838
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001839 if (tv1->v_type != tv2->v_type
1840 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1841 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001842 return FALSE;
1843
1844 switch (tv1->v_type)
1845 {
1846 case VAR_LIST:
1847 ++recursive_cnt;
1848 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1849 --recursive_cnt;
1850 return r;
1851
1852 case VAR_DICT:
1853 ++recursive_cnt;
1854 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1855 --recursive_cnt;
1856 return r;
1857
1858 case VAR_BLOB:
1859 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1860
1861 case VAR_NUMBER:
1862 case VAR_BOOL:
1863 case VAR_SPECIAL:
1864 return tv1->vval.v_number == tv2->vval.v_number;
1865
1866 case VAR_STRING:
1867 s1 = tv_get_string_buf(tv1, buf1);
1868 s2 = tv_get_string_buf(tv2, buf2);
1869 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1870
1871 case VAR_FLOAT:
1872#ifdef FEAT_FLOAT
1873 return tv1->vval.v_float == tv2->vval.v_float;
1874#endif
1875 case VAR_JOB:
1876#ifdef FEAT_JOB_CHANNEL
1877 return tv1->vval.v_job == tv2->vval.v_job;
1878#endif
1879 case VAR_CHANNEL:
1880#ifdef FEAT_JOB_CHANNEL
1881 return tv1->vval.v_channel == tv2->vval.v_channel;
1882#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001883 case VAR_INSTR:
1884 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001885
1886 case VAR_PARTIAL:
1887 return tv1->vval.v_partial == tv2->vval.v_partial;
1888
1889 case VAR_FUNC:
1890 return tv1->vval.v_string == tv2->vval.v_string;
1891
1892 case VAR_UNKNOWN:
1893 case VAR_ANY:
1894 case VAR_VOID:
1895 break;
1896 }
1897
1898 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1899 // does not equal anything, not even itself.
1900 return FALSE;
1901}
1902
1903/*
1904 * Get an option value.
1905 * "arg" points to the '&' or '+' before the option name.
1906 * "arg" is advanced to character after the option name.
1907 * Return OK or FAIL.
1908 */
1909 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001910eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001911 char_u **arg,
1912 typval_T *rettv, // when NULL, only check if option exists
1913 int evaluate)
1914{
1915 char_u *option_end;
1916 long numval;
1917 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001918 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001919 int c;
1920 int working = (**arg == '+'); // has("+option")
1921 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001922 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001923
1924 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001925 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001926 if (option_end == NULL)
1927 {
1928 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001929 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001930 return FAIL;
1931 }
1932
1933 if (!evaluate)
1934 {
1935 *arg = option_end;
1936 return OK;
1937 }
1938
1939 c = *option_end;
1940 *option_end = NUL;
1941 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001942 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001943
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001944 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001945 {
1946 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001947 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001948 ret = FAIL;
1949 }
1950 else if (rettv != NULL)
1951 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001952 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001953 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001954 {
1955 rettv->v_type = VAR_STRING;
1956 rettv->vval.v_string = NULL;
1957 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001958 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001959 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001960 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1961 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001962 rettv->vval.v_number = 0;
1963 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001964 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001965 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001966 if (in_vim9script() && opt_type == gov_bool)
1967 {
1968 rettv->v_type = VAR_BOOL;
1969 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1970 }
1971 else
1972 {
1973 rettv->v_type = VAR_NUMBER;
1974 rettv->vval.v_number = numval;
1975 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001976 }
1977 else // string option
1978 {
1979 rettv->v_type = VAR_STRING;
1980 rettv->vval.v_string = stringval;
1981 }
1982 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001983 else if (working && (opt_type == gov_hidden_bool
1984 || opt_type == gov_hidden_number
1985 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001986 ret = FAIL;
1987
1988 *option_end = c; // put back for error messages
1989 *arg = option_end;
1990
1991 return ret;
1992}
1993
1994/*
1995 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1996 * Return OK or FAIL.
1997 */
1998 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001999eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002000 char_u **arg,
2001 typval_T *rettv,
2002 int evaluate,
2003 int want_string UNUSED)
2004{
2005 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002006 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002007#ifdef FEAT_FLOAT
2008 char_u *p;
2009 int get_float = FALSE;
2010
2011 // We accept a float when the format matches
2012 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2013 // strict to avoid backwards compatibility problems.
2014 // With script version 2 and later the leading digit can be
2015 // omitted.
2016 // Don't look for a float after the "." operator, so that
2017 // ":let vers = 1.2.3" doesn't fail.
2018 if (**arg == '.')
2019 p = *arg;
2020 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002021 {
2022 p = *arg + 1;
2023 if (skip_quotes)
2024 for (;;)
2025 {
2026 if (*p == '\'')
2027 ++p;
2028 if (!vim_isdigit(*p))
2029 break;
2030 p = skipdigits(p);
2031 }
2032 else
2033 p = skipdigits(p);
2034 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002035 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2036 {
2037 get_float = TRUE;
2038 p = skipdigits(p + 2);
2039 if (*p == 'e' || *p == 'E')
2040 {
2041 ++p;
2042 if (*p == '-' || *p == '+')
2043 ++p;
2044 if (!vim_isdigit(*p))
2045 get_float = FALSE;
2046 else
2047 p = skipdigits(p + 1);
2048 }
2049 if (ASCII_ISALPHA(*p) || *p == '.')
2050 get_float = FALSE;
2051 }
2052 if (get_float)
2053 {
2054 float_T f;
2055
Bram Moolenaar29500652021-08-08 15:43:34 +02002056 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002057 if (evaluate)
2058 {
2059 rettv->v_type = VAR_FLOAT;
2060 rettv->vval.v_float = f;
2061 }
2062 }
2063 else
2064#endif
2065 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2066 {
2067 char_u *bp;
2068 blob_T *blob = NULL; // init for gcc
2069
2070 // Blob constant: 0z0123456789abcdef
2071 if (evaluate)
2072 blob = blob_alloc();
2073 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2074 {
2075 if (!vim_isxdigit(bp[1]))
2076 {
2077 if (blob != NULL)
2078 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002079 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002080 ga_clear(&blob->bv_ga);
2081 VIM_CLEAR(blob);
2082 }
2083 return FAIL;
2084 }
2085 if (blob != NULL)
2086 ga_append(&blob->bv_ga,
2087 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2088 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2089 ++bp;
2090 }
2091 if (blob != NULL)
2092 rettv_blob_set(rettv, blob);
2093 *arg = bp;
2094 }
2095 else
2096 {
2097 varnumber_T n;
2098
2099 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002100 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002101 ? STR2NR_NO_OCT + STR2NR_QUOTE
2102 : STR2NR_ALL, &n, NULL, 0, TRUE);
2103 if (len == 0)
2104 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002105 if (evaluate)
2106 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002107 return FAIL;
2108 }
2109 *arg += len;
2110 if (evaluate)
2111 {
2112 rettv->v_type = VAR_NUMBER;
2113 rettv->vval.v_number = n;
2114 }
2115 }
2116 return OK;
2117}
2118
2119/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002120 * Evaluate a string constant and put the result in "rettv".
2121 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2122 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2123 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002124 * Return OK or FAIL.
2125 */
2126 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002127eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002128{
2129 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002130 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002131 int extra = interpolate ? 1 : 0;
2132 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002133 int len;
2134
2135 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002136 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002137 {
2138 if (*p == '\\' && p[1] != NUL)
2139 {
2140 ++p;
2141 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002142 // to 9 characters (6 for the char and 3 for a modifier):
2143 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002144 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002145 {
2146 int modifiers = 0;
2147 int flags = FSK_KEYCODE | FSK_IN_STRING;
2148
zeertzjqdb088872022-05-02 22:53:45 +01002149 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002150
2151 // Skip to the '>' to avoid using '{' inside for string
2152 // interpolation.
2153 if (p[1] != '*')
2154 flags |= FSK_SIMPLIFY;
2155 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2156 --p; // leave "p" on the ">"
2157 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002158 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002159 else if (interpolate && (*p == '{' || *p == '}'))
2160 {
2161 if (*p == '{' && p[1] != '{') // start of expression
2162 break;
2163 ++p;
2164 if (p[-1] == '}' && *p != '}') // single '}' is an error
2165 {
2166 semsg(_(e_stray_closing_curly_str), *arg);
2167 return FAIL;
2168 }
2169 --extra; // "{{" becomes "{", "}}" becomes "}"
2170 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002171 }
2172
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002173 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002174 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002175 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002176 return FAIL;
2177 }
2178
2179 // If only parsing, set *arg and return here
2180 if (!evaluate)
2181 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002182 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002183 return OK;
2184 }
2185
2186 // Copy the string into allocated memory, handling backslashed
2187 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002188 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002189 len = (int)(p - *arg + extra);
2190 rettv->vval.v_string = alloc(len);
2191 if (rettv->vval.v_string == NULL)
2192 return FAIL;
2193 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002194
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002195 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002196 {
2197 if (*p == '\\')
2198 {
2199 switch (*++p)
2200 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002201 case 'b': *end++ = BS; ++p; break;
2202 case 'e': *end++ = ESC; ++p; break;
2203 case 'f': *end++ = FF; ++p; break;
2204 case 'n': *end++ = NL; ++p; break;
2205 case 'r': *end++ = CAR; ++p; break;
2206 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002207
2208 case 'X': // hex: "\x1", "\x12"
2209 case 'x':
2210 case 'u': // Unicode: "\u0023"
2211 case 'U':
2212 if (vim_isxdigit(p[1]))
2213 {
2214 int n, nr;
2215 int c = toupper(*p);
2216
2217 if (c == 'X')
2218 n = 2;
2219 else if (*p == 'u')
2220 n = 4;
2221 else
2222 n = 8;
2223 nr = 0;
2224 while (--n >= 0 && vim_isxdigit(p[1]))
2225 {
2226 ++p;
2227 nr = (nr << 4) + hex2nr(*p);
2228 }
2229 ++p;
2230 // For "\u" store the number according to
2231 // 'encoding'.
2232 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002233 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002234 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002235 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002236 }
2237 break;
2238
2239 // octal: "\1", "\12", "\123"
2240 case '0':
2241 case '1':
2242 case '2':
2243 case '3':
2244 case '4':
2245 case '5':
2246 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002247 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002248 if (*p >= '0' && *p <= '7')
2249 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002250 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002251 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002252 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002253 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002254 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002255 break;
2256
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002257 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002258 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002259 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002260 int flags = FSK_KEYCODE | FSK_IN_STRING;
2261
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002262 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002263 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002264 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002265 if (extra != 0)
2266 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002267 end += extra;
2268 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002269 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002270 break;
2271 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002272 }
2273 // FALLTHROUGH
2274
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002275 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002276 break;
2277 }
2278 }
2279 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002280 {
2281 if (interpolate && (*p == '{' || *p == '}'))
2282 {
2283 if (*p == '{' && p[1] != '{') // start of expression
2284 break;
2285 ++p; // reduce "{{" to "{" and "}}" to "}"
2286 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002287 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002288 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002289 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002290 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002291 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002292 ++p;
2293 *arg = p;
2294
2295 return OK;
2296}
2297
2298/*
2299 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002300 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2301 * Return OK when a "rettv" was set to the string.
2302 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002303 */
2304 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002305eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002306{
2307 char_u *p;
2308 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002309 int reduce = interpolate ? -1 : 0;
2310 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002311
2312 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002313 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002314 {
2315 if (*p == '\'')
2316 {
2317 if (p[1] != '\'')
2318 break;
2319 ++reduce;
2320 ++p;
2321 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002322 else if (interpolate)
2323 {
2324 if (*p == '{')
2325 {
2326 if (p[1] != '{')
2327 break;
2328 ++p;
2329 ++reduce;
2330 }
2331 else if (*p == '}')
2332 {
2333 ++p;
2334 if (*p != '}')
2335 {
2336 semsg(_(e_stray_closing_curly_str), *arg);
2337 return FAIL;
2338 }
2339 ++reduce;
2340 }
2341 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002342 }
2343
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002344 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002345 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002346 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002347 return FAIL;
2348 }
2349
2350 // If only parsing return after setting "*arg"
2351 if (!evaluate)
2352 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002353 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002354 return OK;
2355 }
2356
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002357 // Copy the string into allocated memory, handling '' to ' reduction and
2358 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002359 str = alloc((p - *arg) - reduce);
2360 if (str == NULL)
2361 return FAIL;
2362 rettv->v_type = VAR_STRING;
2363 rettv->vval.v_string = str;
2364
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002365 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002366 {
2367 if (*p == '\'')
2368 {
2369 if (p[1] != '\'')
2370 break;
2371 ++p;
2372 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002373 else if (interpolate && (*p == '{' || *p == '}'))
2374 {
2375 if (*p == '{' && p[1] != '{')
2376 break;
2377 ++p;
2378 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002379 MB_COPY_CHAR(p, str);
2380 }
2381 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002382 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002383
2384 return OK;
2385}
2386
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002387/*
2388 * Evaluate a single or double quoted string possibly containing expressions.
2389 * "arg" points to the '$'. The result is put in "rettv".
2390 * Returns OK or FAIL.
2391 */
LemonBoy2eaef102022-05-06 13:14:50 +01002392 int
2393eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2394{
2395 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002396 int ret = OK;
2397 int quote;
2398 garray_T ga;
2399 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002400
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002401 ga_init2(&ga, 1, 80);
2402
2403 // *arg is on the '$' character, move it to the first string character.
2404 ++*arg;
2405 quote = **arg;
2406 ++*arg;
2407
2408 for (;;)
2409 {
2410 // Get the string up to the matching quote or to a single '{'.
2411 // "arg" is advanced to either the quote or the '{'.
2412 if (quote == '"')
2413 ret = eval_string(arg, &tv, evaluate, TRUE);
2414 else
2415 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2416 if (ret == FAIL)
2417 break;
2418 if (evaluate)
2419 {
2420 ga_concat(&ga, tv.vval.v_string);
2421 clear_tv(&tv);
2422 }
2423
2424 if (**arg != '{')
2425 {
2426 // found terminating quote
2427 ++*arg;
2428 break;
2429 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002430 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002431 if (p == NULL)
2432 {
2433 ret = FAIL;
2434 break;
2435 }
2436 *arg = p;
2437 }
LemonBoy2eaef102022-05-06 13:14:50 +01002438
2439 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002440 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2441 {
2442 ga_clear(&ga);
2443 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002444 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002445 }
LemonBoy2eaef102022-05-06 13:14:50 +01002446
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002447 rettv->vval.v_string = ga.ga_data;
2448 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002449}
2450
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002451/*
2452 * Return a string with the string representation of a variable.
2453 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2454 * "numbuf" is used for a number.
2455 * Puts quotes around strings, so that they can be parsed back by eval().
2456 * May return NULL.
2457 */
2458 char_u *
2459tv2string(
2460 typval_T *tv,
2461 char_u **tofree,
2462 char_u *numbuf,
2463 int copyID)
2464{
2465 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2466}
2467
2468/*
2469 * Get the value of an environment variable.
2470 * "arg" is pointing to the '$'. It is advanced to after the name.
2471 * If the environment variable was not set, silently assume it is empty.
2472 * Return FAIL if the name is invalid.
2473 */
2474 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002475eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002476{
2477 char_u *string = NULL;
2478 int len;
2479 int cc;
2480 char_u *name;
2481 int mustfree = FALSE;
2482
2483 ++*arg;
2484 name = *arg;
2485 len = get_env_len(arg);
2486 if (evaluate)
2487 {
2488 if (len == 0)
2489 return FAIL; // invalid empty name
2490
2491 cc = name[len];
2492 name[len] = NUL;
2493 // first try vim_getenv(), fast for normal environment vars
2494 string = vim_getenv(name, &mustfree);
2495 if (string != NULL && *string != NUL)
2496 {
2497 if (!mustfree)
2498 string = vim_strsave(string);
2499 }
2500 else
2501 {
2502 if (mustfree)
2503 vim_free(string);
2504
2505 // next try expanding things like $VIM and ${HOME}
2506 string = expand_env_save(name - 1);
2507 if (string != NULL && *string == '$')
2508 VIM_CLEAR(string);
2509 }
2510 name[len] = cc;
2511
2512 rettv->v_type = VAR_STRING;
2513 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002514 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002515 }
2516
2517 return OK;
2518}
2519
2520/*
2521 * Get the lnum from the first argument.
2522 * Also accepts ".", "$", etc., but that only works for the current buffer.
2523 * Returns -1 on error.
2524 */
2525 linenr_T
2526tv_get_lnum(typval_T *argvars)
2527{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002528 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002529
Bram Moolenaar56acb092020-08-16 14:48:19 +02002530 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2531 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002532 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002533 {
2534 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002535 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002536
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002537 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002538 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002539 if (fp != NULL)
2540 lnum = fp->lnum;
2541 }
2542 return lnum;
2543}
2544
2545/*
2546 * Get the lnum from the first argument.
2547 * Also accepts "$", then "buf" is used.
2548 * Returns 0 on error.
2549 */
2550 linenr_T
2551tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2552{
2553 if (argvars[0].v_type == VAR_STRING
2554 && argvars[0].vval.v_string != NULL
2555 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002556 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002557 && buf != NULL)
2558 return buf->b_ml.ml_line_count;
2559 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2560}
2561
2562/*
2563 * Get buffer by number or pattern.
2564 */
2565 buf_T *
2566tv_get_buf(typval_T *tv, int curtab_only)
2567{
2568 char_u *name = tv->vval.v_string;
2569 buf_T *buf;
2570
2571 if (tv->v_type == VAR_NUMBER)
2572 return buflist_findnr((int)tv->vval.v_number);
2573 if (tv->v_type != VAR_STRING)
2574 return NULL;
2575 if (name == NULL || *name == NUL)
2576 return curbuf;
2577 if (name[0] == '$' && name[1] == NUL)
2578 return lastbuf;
2579
2580 buf = buflist_find_by_name(name, curtab_only);
2581
2582 // If not found, try expanding the name, like done for bufexists().
2583 if (buf == NULL)
2584 buf = find_buffer(tv);
2585
2586 return buf;
2587}
2588
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002589/*
2590 * Like tv_get_buf() but give an error message is the type is wrong.
2591 */
2592 buf_T *
2593tv_get_buf_from_arg(typval_T *tv)
2594{
2595 buf_T *buf;
2596
2597 ++emsg_off;
2598 buf = tv_get_buf(tv, FALSE);
2599 --emsg_off;
2600 if (buf == NULL
2601 && tv->v_type != VAR_NUMBER
2602 && tv->v_type != VAR_STRING)
2603 // issue errmsg for type error
2604 (void)tv_get_number(tv);
2605 return buf;
2606}
2607
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002608#endif // FEAT_EVAL