blob: a4fdb782f1e6fef272d495f9e5dec6039c616e2e [file] [log] [blame]
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * typval.c: functions that deal with a typval
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
18/*
19 * Allocate memory for a variable type-value, and make it empty (0 or NULL
20 * value).
21 */
22 typval_T *
23alloc_tv(void)
24{
25 return ALLOC_CLEAR_ONE(typval_T);
26}
27
28/*
29 * Allocate memory for a variable type-value, and assign a string to it.
30 * The string "s" must have been allocated, it is consumed.
31 * Return NULL for out of memory, the variable otherwise.
32 */
33 typval_T *
34alloc_string_tv(char_u *s)
35{
36 typval_T *rettv;
37
38 rettv = alloc_tv();
39 if (rettv != NULL)
40 {
41 rettv->v_type = VAR_STRING;
42 rettv->vval.v_string = s;
43 }
44 else
45 vim_free(s);
46 return rettv;
47}
48
49/*
50 * Free the memory for a variable type-value.
51 */
52 void
53free_tv(typval_T *varp)
54{
55 if (varp != NULL)
56 {
57 switch (varp->v_type)
58 {
59 case VAR_FUNC:
60 func_unref(varp->vval.v_string);
61 // FALLTHROUGH
62 case VAR_STRING:
63 vim_free(varp->vval.v_string);
64 break;
65 case VAR_PARTIAL:
66 partial_unref(varp->vval.v_partial);
67 break;
68 case VAR_BLOB:
69 blob_unref(varp->vval.v_blob);
70 break;
71 case VAR_LIST:
72 list_unref(varp->vval.v_list);
73 break;
74 case VAR_DICT:
75 dict_unref(varp->vval.v_dict);
76 break;
77 case VAR_JOB:
78#ifdef FEAT_JOB_CHANNEL
79 job_unref(varp->vval.v_job);
80 break;
81#endif
82 case VAR_CHANNEL:
83#ifdef FEAT_JOB_CHANNEL
84 channel_unref(varp->vval.v_channel);
85 break;
86#endif
87 case VAR_NUMBER:
88 case VAR_FLOAT:
89 case VAR_ANY:
90 case VAR_UNKNOWN:
91 case VAR_VOID:
92 case VAR_BOOL:
93 case VAR_SPECIAL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +020094 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +020095 break;
96 }
97 vim_free(varp);
98 }
99}
100
101/*
102 * Free the memory for a variable value and set the value to NULL or 0.
103 */
104 void
105clear_tv(typval_T *varp)
106{
107 if (varp != NULL)
108 {
109 switch (varp->v_type)
110 {
111 case VAR_FUNC:
112 func_unref(varp->vval.v_string);
113 // FALLTHROUGH
114 case VAR_STRING:
115 VIM_CLEAR(varp->vval.v_string);
116 break;
117 case VAR_PARTIAL:
118 partial_unref(varp->vval.v_partial);
119 varp->vval.v_partial = NULL;
120 break;
121 case VAR_BLOB:
122 blob_unref(varp->vval.v_blob);
123 varp->vval.v_blob = NULL;
124 break;
125 case VAR_LIST:
126 list_unref(varp->vval.v_list);
127 varp->vval.v_list = NULL;
128 break;
129 case VAR_DICT:
130 dict_unref(varp->vval.v_dict);
131 varp->vval.v_dict = NULL;
132 break;
133 case VAR_NUMBER:
134 case VAR_BOOL:
135 case VAR_SPECIAL:
136 varp->vval.v_number = 0;
137 break;
138 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200139 varp->vval.v_float = 0.0;
140 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200141 case VAR_JOB:
142#ifdef FEAT_JOB_CHANNEL
143 job_unref(varp->vval.v_job);
144 varp->vval.v_job = NULL;
145#endif
146 break;
147 case VAR_CHANNEL:
148#ifdef FEAT_JOB_CHANNEL
149 channel_unref(varp->vval.v_channel);
150 varp->vval.v_channel = NULL;
151#endif
Bram Moolenaar24f72092021-05-07 20:43:54 +0200152 break;
153 case VAR_INSTR:
154 VIM_CLEAR(varp->vval.v_instr);
155 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200156 case VAR_UNKNOWN:
157 case VAR_ANY:
158 case VAR_VOID:
159 break;
160 }
161 varp->v_lock = 0;
162 }
163}
164
165/*
166 * Set the value of a variable to NULL without freeing items.
167 */
168 void
169init_tv(typval_T *varp)
170{
171 if (varp != NULL)
172 CLEAR_POINTER(varp);
173}
174
Bram Moolenaar36967b32020-08-17 21:41:02 +0200175 static varnumber_T
176tv_get_bool_or_number_chk(typval_T *varp, int *denote, int want_bool)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200177{
178 varnumber_T n = 0L;
179
180 switch (varp->v_type)
181 {
182 case VAR_NUMBER:
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200183 if (in_vim9script() && want_bool && varp->vval.v_number != 0
Bram Moolenaard70840e2020-08-22 15:06:35 +0200184 && varp->vval.v_number != 1)
185 {
186 semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
187 break;
188 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200189 return varp->vval.v_number;
190 case VAR_FLOAT:
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000191 emsg(_(e_using_float_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200192 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200193 case VAR_FUNC:
194 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000195 emsg(_(e_using_funcref_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200196 break;
197 case VAR_STRING:
Bram Moolenaar56acb092020-08-16 14:48:19 +0200198 if (in_vim9script())
199 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100200 emsg_using_string_as(varp, !want_bool);
Bram Moolenaar56acb092020-08-16 14:48:19 +0200201 break;
202 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200203 if (varp->vval.v_string != NULL)
204 vim_str2nr(varp->vval.v_string, NULL, NULL,
205 STR2NR_ALL, &n, NULL, 0, FALSE);
206 return n;
207 case VAR_LIST:
Bram Moolenaar677658a2022-01-05 16:09:06 +0000208 emsg(_(e_using_list_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200209 break;
210 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000211 emsg(_(e_using_dictionary_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200212 break;
213 case VAR_BOOL:
214 case VAR_SPECIAL:
Bram Moolenaar36967b32020-08-17 21:41:02 +0200215 if (!want_bool && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200216 {
Bram Moolenaard92cc132020-11-18 17:17:15 +0100217 if (varp->v_type == VAR_BOOL)
218 emsg(_(e_using_bool_as_number));
219 else
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000220 emsg(_(e_using_special_as_number));
Bram Moolenaar56acb092020-08-16 14:48:19 +0200221 break;
222 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200223 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
224 case VAR_JOB:
225#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000226 emsg(_(e_using_job_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200227 break;
228#endif
229 case VAR_CHANNEL:
230#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000231 emsg(_(e_using_channel_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200232 break;
233#endif
234 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000235 emsg(_(e_using_blob_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200236 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200237 case VAR_VOID:
238 emsg(_(e_cannot_use_void_value));
239 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200240 case VAR_UNKNOWN:
241 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200242 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200243 internal_error_no_abort("tv_get_number(UNKNOWN)");
244 break;
245 }
246 if (denote == NULL) // useful for values that must be unsigned
247 n = -1;
248 else
249 *denote = TRUE;
250 return n;
251}
252
Bram Moolenaar36967b32020-08-17 21:41:02 +0200253/*
254 * Get the number value of a variable.
255 * If it is a String variable, uses vim_str2nr().
256 * For incompatible types, return 0.
257 * tv_get_number_chk() is similar to tv_get_number(), but informs the
258 * caller of incompatible types: it sets *denote to TRUE if "denote"
259 * is not NULL or returns -1 otherwise.
260 */
261 varnumber_T
262tv_get_number(typval_T *varp)
263{
264 int error = FALSE;
265
266 return tv_get_number_chk(varp, &error); // return 0L on error
267}
268
269 varnumber_T
270tv_get_number_chk(typval_T *varp, int *denote)
271{
272 return tv_get_bool_or_number_chk(varp, denote, FALSE);
273}
274
275/*
276 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200277 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200278 */
279 varnumber_T
280tv_get_bool(typval_T *varp)
281{
282 return tv_get_bool_or_number_chk(varp, NULL, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200283}
284
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200285/*
286 * Get the boolean value of "varp". This is like tv_get_number_chk(),
287 * but in Vim9 script accepts Number and Bool.
288 */
289 varnumber_T
290tv_get_bool_chk(typval_T *varp, int *denote)
291{
292 return tv_get_bool_or_number_chk(varp, denote, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200293}
294
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000295 static float_T
296tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200297{
298 switch (varp->v_type)
299 {
300 case VAR_NUMBER:
301 return (float_T)(varp->vval.v_number);
302 case VAR_FLOAT:
303 return varp->vval.v_float;
304 case VAR_FUNC:
305 case VAR_PARTIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000306 emsg(_(e_using_funcref_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200307 break;
308 case VAR_STRING:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000309 emsg(_(e_using_string_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200310 break;
311 case VAR_LIST:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000312 emsg(_(e_using_list_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200313 break;
314 case VAR_DICT:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000315 emsg(_(e_using_dictionary_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200316 break;
317 case VAR_BOOL:
Bram Moolenaarb2810f12022-01-08 21:38:52 +0000318 emsg(_(e_using_boolean_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200319 break;
320 case VAR_SPECIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000321 emsg(_(e_using_special_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200322 break;
323 case VAR_JOB:
324# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000325 emsg(_(e_using_job_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200326 break;
327# endif
328 case VAR_CHANNEL:
329# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000330 emsg(_(e_using_channel_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200331 break;
332# endif
333 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000334 emsg(_(e_using_blob_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200335 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200336 case VAR_VOID:
337 emsg(_(e_cannot_use_void_value));
338 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200339 case VAR_UNKNOWN:
340 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200341 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200342 internal_error_no_abort("tv_get_float(UNKNOWN)");
343 break;
344 }
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000345 if (error != NULL)
346 *error = TRUE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200347 return 0;
348}
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000349
350 float_T
351tv_get_float(typval_T *varp)
352{
353 return tv_get_float_chk(varp, NULL);
354}
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200355
356/*
Ernie Rael51d04d12022-05-04 15:40:22 +0100357 * Give an error and return FAIL unless "args[idx]" is unknown
358 */
359 int
360check_for_unknown_arg(typval_T *args, int idx)
361{
362 if (args[idx].v_type != VAR_UNKNOWN)
363 {
364 semsg(_(e_too_many_arguments), idx + 1);
365 return FAIL;
366 }
367 return OK;
368}
369
370/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200371 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100372 */
373 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100374check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100375{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100376 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100377 {
rbtnnddc80af2021-12-17 18:01:31 +0000378 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100379 return FAIL;
380 }
381 return OK;
382}
383
384/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100385 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100386 */
387 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100388check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100389{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100390 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100391 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100392 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100393 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200394 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100395 return FAIL;
396 }
397 return OK;
398}
399
400/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200401 * Check for an optional string argument at 'idx'
402 */
403 int
404check_for_opt_string_arg(typval_T *args, int idx)
405{
406 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100407 || check_for_string_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200408}
409
410/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200411 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200412 */
413 int
414check_for_number_arg(typval_T *args, int idx)
415{
416 if (args[idx].v_type != VAR_NUMBER)
417 {
rbtnnddc80af2021-12-17 18:01:31 +0000418 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200419 return FAIL;
420 }
421 return OK;
422}
423
424/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200425 * Check for an optional number argument at 'idx'
426 */
427 int
428check_for_opt_number_arg(typval_T *args, int idx)
429{
430 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100431 || check_for_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200432}
433
434/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200435 * Give an error and return FAIL unless "args[idx]" is a float or a number.
436 */
437 int
438check_for_float_or_nr_arg(typval_T *args, int idx)
439{
440 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
441 {
rbtnnddc80af2021-12-17 18:01:31 +0000442 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200443 return FAIL;
444 }
445 return OK;
446}
447
448/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200449 * Give an error and return FAIL unless "args[idx]" is a bool.
450 */
451 int
452check_for_bool_arg(typval_T *args, int idx)
453{
454 if (args[idx].v_type != VAR_BOOL
455 && !(args[idx].v_type == VAR_NUMBER
456 && (args[idx].vval.v_number == 0
457 || args[idx].vval.v_number == 1)))
458 {
rbtnnddc80af2021-12-17 18:01:31 +0000459 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200460 return FAIL;
461 }
462 return OK;
463}
464
465/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200466 * Check for an optional bool argument at 'idx'.
467 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200468 */
469 int
470check_for_opt_bool_arg(typval_T *args, int idx)
471{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200472 if (args[idx].v_type == VAR_UNKNOWN)
473 return OK;
474 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200475}
476
477/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200478 * Give an error and return FAIL unless "args[idx]" is a blob.
479 */
480 int
481check_for_blob_arg(typval_T *args, int idx)
482{
483 if (args[idx].v_type != VAR_BLOB)
484 {
rbtnnddc80af2021-12-17 18:01:31 +0000485 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200486 return FAIL;
487 }
488 return OK;
489}
490
491/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200492 * Give an error and return FAIL unless "args[idx]" is a list.
493 */
494 int
495check_for_list_arg(typval_T *args, int idx)
496{
497 if (args[idx].v_type != VAR_LIST)
498 {
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200499 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200500 return FAIL;
501 }
502 return OK;
503}
504
505/*
Bram Moolenaard83392a2022-09-01 12:22:46 +0100506 * Give an error and return FAIL unless "args[idx]" is a non-NULL list.
507 */
508 int
509check_for_nonnull_list_arg(typval_T *args, int idx)
510{
511 if (check_for_list_arg(args, idx) == FAIL)
512 return FAIL;
513
514 if (args[idx].vval.v_list == NULL)
515 {
516 semsg(_(e_non_null_list_required_for_argument_nr), idx + 1);
517 return FAIL;
518 }
519 return OK;
520}
521
522/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200523 * Check for an optional list argument at 'idx'
524 */
525 int
526check_for_opt_list_arg(typval_T *args, int idx)
527{
528 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100529 || check_for_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200530}
531
532/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200533 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200534 */
535 int
536check_for_dict_arg(typval_T *args, int idx)
537{
538 if (args[idx].v_type != VAR_DICT)
539 {
rbtnnddc80af2021-12-17 18:01:31 +0000540 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200541 return FAIL;
542 }
543 return OK;
544}
545
546/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100547 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
548 */
549 int
550check_for_nonnull_dict_arg(typval_T *args, int idx)
551{
552 if (check_for_dict_arg(args, idx) == FAIL)
553 return FAIL;
554
555 if (args[idx].vval.v_dict == NULL)
556 {
557 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
558 return FAIL;
559 }
560 return OK;
561}
562
563/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200564 * Check for an optional dict argument at 'idx'
565 */
566 int
567check_for_opt_dict_arg(typval_T *args, int idx)
568{
569 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100570 || check_for_dict_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200571}
572
Dominique Pelle748b3082022-01-08 12:41:16 +0000573#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200574/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200575 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
576 */
577 int
578check_for_chan_or_job_arg(typval_T *args, int idx)
579{
580 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
581 {
rbtnnddc80af2021-12-17 18:01:31 +0000582 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200583 return FAIL;
584 }
585 return OK;
586}
587
588/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200589 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
590 * job.
591 */
592 int
593check_for_opt_chan_or_job_arg(typval_T *args, int idx)
594{
595 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100596 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200597}
598
599/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200600 * Give an error and return FAIL unless "args[idx]" is a job.
601 */
602 int
603check_for_job_arg(typval_T *args, int idx)
604{
605 if (args[idx].v_type != VAR_JOB)
606 {
rbtnnddc80af2021-12-17 18:01:31 +0000607 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200608 return FAIL;
609 }
610 return OK;
611}
612
613/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200614 * Check for an optional job argument at 'idx'.
615 */
616 int
617check_for_opt_job_arg(typval_T *args, int idx)
618{
619 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100620 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200621}
Dominique Pelle748b3082022-01-08 12:41:16 +0000622#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200623
624/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200625 * Give an error and return FAIL unless "args[idx]" is a string or
626 * a number.
627 */
628 int
629check_for_string_or_number_arg(typval_T *args, int idx)
630{
631 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
632 {
rbtnnddc80af2021-12-17 18:01:31 +0000633 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200634 return FAIL;
635 }
636 return OK;
637}
638
639/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200640 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200641 */
642 int
643check_for_opt_string_or_number_arg(typval_T *args, int idx)
644{
645 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100646 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200647}
648
649/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200650 * Give an error and return FAIL unless "args[idx]" is a buffer number.
651 * Buffer number can be a number or a string.
652 */
653 int
654check_for_buffer_arg(typval_T *args, int idx)
655{
656 return check_for_string_or_number_arg(args, idx);
657}
658
659/*
660 * Check for an optional buffer argument at 'idx'
661 */
662 int
663check_for_opt_buffer_arg(typval_T *args, int idx)
664{
665 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100666 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200667}
668
669/*
670 * Give an error and return FAIL unless "args[idx]" is a line number.
671 * Line number can be a number or a string.
672 */
673 int
674check_for_lnum_arg(typval_T *args, int idx)
675{
676 return check_for_string_or_number_arg(args, idx);
677}
678
679/*
680 * Check for an optional line number argument at 'idx'
681 */
682 int
683check_for_opt_lnum_arg(typval_T *args, int idx)
684{
685 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100686 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200687}
688
Dominique Pelle748b3082022-01-08 12:41:16 +0000689#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200690/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200691 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200692 */
693 int
694check_for_string_or_blob_arg(typval_T *args, int idx)
695{
696 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
697 {
rbtnnddc80af2021-12-17 18:01:31 +0000698 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200699 return FAIL;
700 }
701 return OK;
702}
Dominique Pelle748b3082022-01-08 12:41:16 +0000703#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200704
705/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200706 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200707 */
708 int
709check_for_string_or_list_arg(typval_T *args, int idx)
710{
711 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
712 {
rbtnnddc80af2021-12-17 18:01:31 +0000713 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200714 return FAIL;
715 }
716 return OK;
717}
718
719/*
rbtnn0ccb5842021-12-18 18:33:46 +0000720 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
721 * blob.
722 */
723 int
724check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
725{
726 if (args[idx].v_type != VAR_STRING
727 && args[idx].v_type != VAR_LIST
728 && args[idx].v_type != VAR_BLOB)
729 {
730 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
731 return FAIL;
732 }
733 return OK;
734}
735
736/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200737 * Check for an optional string or list argument at 'idx'
738 */
739 int
740check_for_opt_string_or_list_arg(typval_T *args, int idx)
741{
742 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100743 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200744}
745
746/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200747 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
748 */
749 int
750check_for_string_or_dict_arg(typval_T *args, int idx)
751{
752 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
753 {
rbtnnddc80af2021-12-17 18:01:31 +0000754 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200755 return FAIL;
756 }
757 return OK;
758}
759
760/*
761 * Give an error and return FAIL unless "args[idx]" is a string or a number
762 * or a list.
763 */
764 int
765check_for_string_or_number_or_list_arg(typval_T *args, int idx)
766{
767 if (args[idx].v_type != VAR_STRING
768 && args[idx].v_type != VAR_NUMBER
769 && args[idx].v_type != VAR_LIST)
770 {
rbtnn0ccb5842021-12-18 18:33:46 +0000771 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200772 return FAIL;
773 }
774 return OK;
775}
776
777/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200778 * Give an error and return FAIL unless "args[idx]" is an optional string
779 * or number or a list
780 */
781 int
782check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
783{
784 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100785 || check_for_string_or_number_or_list_arg(args, idx)
786 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200787}
788
789/*
Bakudankun375141e2022-09-09 18:46:47 +0100790 * Give an error and return FAIL unless "args[idx]" is a string or a number
791 * or a list or a blob.
792 */
793 int
794check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
795{
796 if (args[idx].v_type != VAR_STRING
797 && args[idx].v_type != VAR_NUMBER
798 && args[idx].v_type != VAR_LIST
799 && args[idx].v_type != VAR_BLOB)
800 {
801 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
802 return FAIL;
803 }
804 return OK;
805}
806
807/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200808 * Give an error and return FAIL unless "args[idx]" is a string or a list
809 * or a dict.
810 */
811 int
812check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
813{
814 if (args[idx].v_type != VAR_STRING
815 && args[idx].v_type != VAR_LIST
816 && args[idx].v_type != VAR_DICT)
817 {
rbtnnddc80af2021-12-17 18:01:31 +0000818 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200819 return FAIL;
820 }
821 return OK;
822}
823
824/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000825 * Give an error and return FAIL unless "args[idx]" is a string
826 * or a function reference.
827 */
828 int
829check_for_string_or_func_arg(typval_T *args, int idx)
830{
831 if (args[idx].v_type != VAR_PARTIAL
832 && args[idx].v_type != VAR_FUNC
833 && args[idx].v_type != VAR_STRING)
834 {
835 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
836 return FAIL;
837 }
838 return OK;
839}
840
841/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200842 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
843 */
844 int
845check_for_list_or_blob_arg(typval_T *args, int idx)
846{
847 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
848 {
rbtnn0ccb5842021-12-18 18:33:46 +0000849 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200850 return FAIL;
851 }
852 return OK;
853}
854
855/*
856 * Give an error and return FAIL unless "args[idx]" is a list or dict
857 */
858 int
859check_for_list_or_dict_arg(typval_T *args, int idx)
860{
861 if (args[idx].v_type != VAR_LIST
862 && args[idx].v_type != VAR_DICT)
863 {
rbtnnddc80af2021-12-17 18:01:31 +0000864 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200865 return FAIL;
866 }
867 return OK;
868}
869
870/*
871 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
872 * blob.
873 */
874 int
875check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
876{
877 if (args[idx].v_type != VAR_LIST
878 && args[idx].v_type != VAR_DICT
879 && args[idx].v_type != VAR_BLOB)
880 {
rbtnnddc80af2021-12-17 18:01:31 +0000881 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200882 return FAIL;
883 }
884 return OK;
885}
886
887/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000888 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
889 * blob or a string.
890 */
891 int
892check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
893{
894 if (args[idx].v_type != VAR_LIST
895 && args[idx].v_type != VAR_DICT
896 && args[idx].v_type != VAR_BLOB
897 && args[idx].v_type != VAR_STRING)
898 {
rbtnnddc80af2021-12-17 18:01:31 +0000899 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000900 return FAIL;
901 }
902 return OK;
903}
904
905/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200906 * Give an error and return FAIL unless "args[idx]" is an optional buffer
907 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200908 */
909 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200910check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200911{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200912 if (args[idx].v_type != VAR_UNKNOWN
913 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200914 && args[idx].v_type != VAR_NUMBER
915 && args[idx].v_type != VAR_DICT)
916 {
rbtnnddc80af2021-12-17 18:01:31 +0000917 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200918 return FAIL;
919 }
920 return OK;
921}
922
923/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200924 * Get the string value of a variable.
925 * If it is a Number variable, the number is converted into a string.
926 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
927 * tv_get_string_buf() uses a given buffer.
928 * If the String variable has never been set, return an empty string.
929 * Never returns NULL;
930 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
931 * NULL on error.
932 */
933 char_u *
934tv_get_string(typval_T *varp)
935{
936 static char_u mybuf[NUMBUFLEN];
937
938 return tv_get_string_buf(varp, mybuf);
939}
940
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100941/*
942 * Like tv_get_string() but don't allow number to string conversion for Vim9.
943 */
944 char_u *
945tv_get_string_strict(typval_T *varp)
946{
947 static char_u mybuf[NUMBUFLEN];
948 char_u *res = tv_get_string_buf_chk_strict(
949 varp, mybuf, in_vim9script());
950
951 return res != NULL ? res : (char_u *)"";
952}
953
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200954 char_u *
955tv_get_string_buf(typval_T *varp, char_u *buf)
956{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200957 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200958
959 return res != NULL ? res : (char_u *)"";
960}
961
962/*
963 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
964 */
965 char_u *
966tv_get_string_chk(typval_T *varp)
967{
968 static char_u mybuf[NUMBUFLEN];
969
970 return tv_get_string_buf_chk(varp, mybuf);
971}
972
973 char_u *
974tv_get_string_buf_chk(typval_T *varp, char_u *buf)
975{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100976 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
977}
978
979 char_u *
980tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
981{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200982 switch (varp->v_type)
983 {
984 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100985 if (strict)
986 {
987 emsg(_(e_using_number_as_string));
988 break;
989 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200990 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
991 (varnumber_T)varp->vval.v_number);
992 return buf;
993 case VAR_FUNC:
994 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000995 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200996 break;
997 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000998 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200999 break;
1000 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001001 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001002 break;
1003 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001004 if (strict)
1005 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001006 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001007 break;
1008 }
1009 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1010 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001011 case VAR_STRING:
1012 if (varp->vval.v_string != NULL)
1013 return varp->vval.v_string;
1014 return (char_u *)"";
1015 case VAR_BOOL:
1016 case VAR_SPECIAL:
1017 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1018 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001019 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001020 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001021 break;
1022 case VAR_JOB:
1023#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001024 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001025 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001026 semsg(_(e_using_invalid_value_as_string_str), "job");
1027 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001028 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001029 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001030#endif
1031 break;
1032 case VAR_CHANNEL:
1033#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001034 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001035 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001036 semsg(_(e_using_invalid_value_as_string_str), "channel");
1037 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001038 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001039 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001040#endif
1041 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001042 case VAR_VOID:
1043 emsg(_(e_cannot_use_void_value));
1044 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001045 case VAR_UNKNOWN:
1046 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001047 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001048 semsg(_(e_using_invalid_value_as_string_str),
1049 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001050 break;
1051 }
1052 return NULL;
1053}
1054
1055/*
1056 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1057 * string() on Dict, List, etc.
1058 */
1059 char_u *
1060tv_stringify(typval_T *varp, char_u *buf)
1061{
1062 if (varp->v_type == VAR_LIST
1063 || varp->v_type == VAR_DICT
1064 || varp->v_type == VAR_BLOB
1065 || varp->v_type == VAR_FUNC
1066 || varp->v_type == VAR_PARTIAL
1067 || varp->v_type == VAR_FLOAT)
1068 {
1069 typval_T tmp;
1070
1071 f_string(varp, &tmp);
1072 tv_get_string_buf(&tmp, buf);
1073 clear_tv(varp);
1074 *varp = tmp;
1075 return tmp.vval.v_string;
1076 }
1077 return tv_get_string_buf(varp, buf);
1078}
1079
1080/*
1081 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1082 * Also give an error message, using "name" or _("name") when use_gettext is
1083 * TRUE.
1084 */
1085 int
1086tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1087{
1088 int lock = 0;
1089
1090 switch (tv->v_type)
1091 {
1092 case VAR_BLOB:
1093 if (tv->vval.v_blob != NULL)
1094 lock = tv->vval.v_blob->bv_lock;
1095 break;
1096 case VAR_LIST:
1097 if (tv->vval.v_list != NULL)
1098 lock = tv->vval.v_list->lv_lock;
1099 break;
1100 case VAR_DICT:
1101 if (tv->vval.v_dict != NULL)
1102 lock = tv->vval.v_dict->dv_lock;
1103 break;
1104 default:
1105 break;
1106 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001107 return value_check_lock(tv->v_lock, name, use_gettext)
1108 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001109}
1110
1111/*
1112 * Copy the values from typval_T "from" to typval_T "to".
1113 * When needed allocates string or increases reference count.
1114 * Does not make a copy of a list, blob or dict but copies the reference!
1115 * It is OK for "from" and "to" to point to the same item. This is used to
1116 * make a copy later.
1117 */
1118 void
1119copy_tv(typval_T *from, typval_T *to)
1120{
1121 to->v_type = from->v_type;
1122 to->v_lock = 0;
1123 switch (from->v_type)
1124 {
1125 case VAR_NUMBER:
1126 case VAR_BOOL:
1127 case VAR_SPECIAL:
1128 to->vval.v_number = from->vval.v_number;
1129 break;
1130 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001131 to->vval.v_float = from->vval.v_float;
1132 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001133 case VAR_JOB:
1134#ifdef FEAT_JOB_CHANNEL
1135 to->vval.v_job = from->vval.v_job;
1136 if (to->vval.v_job != NULL)
1137 ++to->vval.v_job->jv_refcount;
1138 break;
1139#endif
1140 case VAR_CHANNEL:
1141#ifdef FEAT_JOB_CHANNEL
1142 to->vval.v_channel = from->vval.v_channel;
1143 if (to->vval.v_channel != NULL)
1144 ++to->vval.v_channel->ch_refcount;
1145 break;
1146#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001147 case VAR_INSTR:
1148 to->vval.v_instr = from->vval.v_instr;
1149 break;
1150
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001151 case VAR_STRING:
1152 case VAR_FUNC:
1153 if (from->vval.v_string == NULL)
1154 to->vval.v_string = NULL;
1155 else
1156 {
1157 to->vval.v_string = vim_strsave(from->vval.v_string);
1158 if (from->v_type == VAR_FUNC)
1159 func_ref(to->vval.v_string);
1160 }
1161 break;
1162 case VAR_PARTIAL:
1163 if (from->vval.v_partial == NULL)
1164 to->vval.v_partial = NULL;
1165 else
1166 {
1167 to->vval.v_partial = from->vval.v_partial;
1168 ++to->vval.v_partial->pt_refcount;
1169 }
1170 break;
1171 case VAR_BLOB:
1172 if (from->vval.v_blob == NULL)
1173 to->vval.v_blob = NULL;
1174 else
1175 {
1176 to->vval.v_blob = from->vval.v_blob;
1177 ++to->vval.v_blob->bv_refcount;
1178 }
1179 break;
1180 case VAR_LIST:
1181 if (from->vval.v_list == NULL)
1182 to->vval.v_list = NULL;
1183 else
1184 {
1185 to->vval.v_list = from->vval.v_list;
1186 ++to->vval.v_list->lv_refcount;
1187 }
1188 break;
1189 case VAR_DICT:
1190 if (from->vval.v_dict == NULL)
1191 to->vval.v_dict = NULL;
1192 else
1193 {
1194 to->vval.v_dict = from->vval.v_dict;
1195 ++to->vval.v_dict->dv_refcount;
1196 }
1197 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001198 case VAR_VOID:
1199 emsg(_(e_cannot_use_void_value));
1200 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001201 case VAR_UNKNOWN:
1202 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001203 internal_error_no_abort("copy_tv(UNKNOWN)");
1204 break;
1205 }
1206}
1207
1208/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001209 * Compare "tv1" and "tv2".
1210 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001211 */
1212 int
1213typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001214 typval_T *tv1, // first operand
1215 typval_T *tv2, // second operand
1216 exprtype_T type, // operator
1217 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001218{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001219 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001220 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001221 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1222
Bram Moolenaar265f8112021-12-19 12:33:05 +00001223 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001224 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001225 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001226 // it means TRUE.
1227 n1 = (type == EXPR_ISNOT);
1228 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001229 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1230 || (tv2->v_type == VAR_SPECIAL
1231 && tv2->vval.v_number == VVAL_NULL))
1232 && tv1->v_type != tv2->v_type
1233 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1234 {
1235 n1 = typval_compare_null(tv1, tv2);
1236 if (n1 == MAYBE)
1237 {
1238 clear_tv(tv1);
1239 return FAIL;
1240 }
1241 if (type == EXPR_NEQUAL)
1242 n1 = !n1;
1243 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001244 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001245 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001246 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001247 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001248 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001249 return FAIL;
1250 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001251 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001252 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001253 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001254 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001255 if (typval_compare_list(tv1, tv2, type, ic, &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_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001263 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001264 if (typval_compare_dict(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_FUNC || tv2->v_type == VAR_FUNC
1272 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001273 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001274 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001275 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001276 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001277 return FAIL;
1278 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001279 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001280 }
1281
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001282 // If one of the two variables is a float, compare as a float.
1283 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001284 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001285 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1286 {
1287 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001288 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001289
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001290 f1 = tv_get_float_chk(tv1, &error);
1291 if (!error)
1292 f2 = tv_get_float_chk(tv2, &error);
1293 if (error)
1294 {
1295 clear_tv(tv1);
1296 return FAIL;
1297 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001298 n1 = FALSE;
1299 switch (type)
1300 {
1301 case EXPR_IS:
1302 case EXPR_EQUAL: n1 = (f1 == f2); break;
1303 case EXPR_ISNOT:
1304 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1305 case EXPR_GREATER: n1 = (f1 > f2); break;
1306 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1307 case EXPR_SMALLER: n1 = (f1 < f2); break;
1308 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1309 case EXPR_UNKNOWN:
1310 case EXPR_MATCH:
1311 default: break; // avoid gcc warning
1312 }
1313 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001314
1315 // If one of the two variables is a number, compare as a number.
1316 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001317 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001318 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1319 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001320 int error = FALSE;
1321
1322 n1 = tv_get_number_chk(tv1, &error);
1323 if (!error)
1324 n2 = tv_get_number_chk(tv2, &error);
1325 if (error)
1326 {
1327 clear_tv(tv1);
1328 return FAIL;
1329 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001330 switch (type)
1331 {
1332 case EXPR_IS:
1333 case EXPR_EQUAL: n1 = (n1 == n2); break;
1334 case EXPR_ISNOT:
1335 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1336 case EXPR_GREATER: n1 = (n1 > n2); break;
1337 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1338 case EXPR_SMALLER: n1 = (n1 < n2); break;
1339 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1340 case EXPR_UNKNOWN:
1341 case EXPR_MATCH:
1342 default: break; // avoid gcc warning
1343 }
1344 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001345 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1346 || tv2->v_type == VAR_BOOL
1347 || (tv1->v_type == VAR_SPECIAL
1348 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001349 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001350 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001351 {
1352 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001353 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1354 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001355 return FAIL;
1356 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001357 n1 = tv1->vval.v_number;
1358 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001359 switch (type)
1360 {
1361 case EXPR_IS:
1362 case EXPR_EQUAL: n1 = (n1 == n2); break;
1363 case EXPR_ISNOT:
1364 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1365 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001366 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001367 vartype_name(tv1->v_type));
1368 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001369 return FAIL;
1370 }
1371 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001372#ifdef FEAT_JOB_CHANNEL
1373 else if (tv1->v_type == tv2->v_type
1374 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1375 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1376 {
1377 if (tv1->v_type == VAR_CHANNEL)
1378 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1379 else
1380 n1 = tv1->vval.v_job == tv2->vval.v_job;
1381 if (type == EXPR_NEQUAL)
1382 n1 = !n1;
1383 }
1384#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001385 else
1386 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001387 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001388 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001389 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001390 return FAIL;
1391 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001392 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001393 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001394 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001395 if (in_vim9script())
1396 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001397 tv1->v_type = VAR_BOOL;
1398 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001399 }
1400 else
1401 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001402 tv1->v_type = VAR_NUMBER;
1403 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001404 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001405
1406 return OK;
1407}
1408
Bram Moolenaar34453202021-01-31 13:08:38 +01001409/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001410 * Compare "tv1" to "tv2" as lists acording to "type" and "ic".
1411 * Put the result, false or true, in "res".
1412 * Return FAIL and give an error message when the comparison can't be done.
1413 */
1414 int
1415typval_compare_list(
1416 typval_T *tv1,
1417 typval_T *tv2,
1418 exprtype_T type,
1419 int ic,
1420 int *res)
1421{
1422 int val = 0;
1423
1424 if (type == EXPR_IS || type == EXPR_ISNOT)
1425 {
1426 val = (tv1->v_type == tv2->v_type
1427 && tv1->vval.v_list == tv2->vval.v_list);
1428 if (type == EXPR_ISNOT)
1429 val = !val;
1430 }
1431 else if (tv1->v_type != tv2->v_type
1432 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1433 {
1434 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001435 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001436 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001437 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001438 return FAIL;
1439 }
1440 else
1441 {
1442 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1443 ic, FALSE);
1444 if (type == EXPR_NEQUAL)
1445 val = !val;
1446 }
1447 *res = val;
1448 return OK;
1449}
1450
1451/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001452 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001453 */
1454 int
1455typval_compare_null(typval_T *tv1, typval_T *tv2)
1456{
1457 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1458 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1459 {
1460 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1461
1462 switch (tv->v_type)
1463 {
1464 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001465#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001466 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001467#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001468 case VAR_DICT: return tv->vval.v_dict == NULL;
1469 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001470#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001471 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001472#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001473 case VAR_LIST: return tv->vval.v_list == NULL;
1474 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1475 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001476
1477 case VAR_NUMBER: if (!in_vim9script())
1478 return tv->vval.v_number == 0;
1479 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001480 case VAR_FLOAT: if (!in_vim9script())
1481 return tv->vval.v_float == 0.0;
1482 break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001483 default: break;
1484 }
1485 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001486 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001487 // we won't give an error
1488 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001489}
1490
1491/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001492 * Compare "tv1" to "tv2" as blobs acording to "type".
1493 * Put the result, false or true, in "res".
1494 * Return FAIL and give an error message when the comparison can't be done.
1495 */
1496 int
1497typval_compare_blob(
1498 typval_T *tv1,
1499 typval_T *tv2,
1500 exprtype_T type,
1501 int *res)
1502{
1503 int val = 0;
1504
1505 if (type == EXPR_IS || type == EXPR_ISNOT)
1506 {
1507 val = (tv1->v_type == tv2->v_type
1508 && tv1->vval.v_blob == tv2->vval.v_blob);
1509 if (type == EXPR_ISNOT)
1510 val = !val;
1511 }
1512 else if (tv1->v_type != tv2->v_type
1513 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1514 {
1515 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001516 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001517 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001518 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001519 return FAIL;
1520 }
1521 else
1522 {
1523 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1524 if (type == EXPR_NEQUAL)
1525 val = !val;
1526 }
1527 *res = val;
1528 return OK;
1529}
1530
1531/*
1532 * Compare "tv1" to "tv2" as dictionaries acording to "type" and "ic".
1533 * Put the result, false or true, in "res".
1534 * Return FAIL and give an error message when the comparison can't be done.
1535 */
1536 int
1537typval_compare_dict(
1538 typval_T *tv1,
1539 typval_T *tv2,
1540 exprtype_T type,
1541 int ic,
1542 int *res)
1543{
1544 int val;
1545
1546 if (type == EXPR_IS || type == EXPR_ISNOT)
1547 {
1548 val = (tv1->v_type == tv2->v_type
1549 && tv1->vval.v_dict == tv2->vval.v_dict);
1550 if (type == EXPR_ISNOT)
1551 val = !val;
1552 }
1553 else if (tv1->v_type != tv2->v_type
1554 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1555 {
1556 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001557 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001558 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001559 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001560 return FAIL;
1561 }
1562 else
1563 {
1564 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1565 if (type == EXPR_NEQUAL)
1566 val = !val;
1567 }
1568 *res = val;
1569 return OK;
1570}
1571
1572/*
1573 * Compare "tv1" to "tv2" as funcrefs acording to "type" and "ic".
1574 * Put the result, false or true, in "res".
1575 * Return FAIL and give an error message when the comparison can't be done.
1576 */
1577 int
1578typval_compare_func(
1579 typval_T *tv1,
1580 typval_T *tv2,
1581 exprtype_T type,
1582 int ic,
1583 int *res)
1584{
1585 int val = 0;
1586
1587 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1588 && type != EXPR_IS && type != EXPR_ISNOT)
1589 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001590 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001591 return FAIL;
1592 }
1593 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1594 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1595 // When both partials are NULL, then they are equal.
1596 // Otherwise they are not equal.
1597 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1598 else if (type == EXPR_IS || type == EXPR_ISNOT)
1599 {
1600 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1601 // strings are considered the same if their value is
1602 // the same
1603 val = tv_equal(tv1, tv2, ic, FALSE);
1604 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1605 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1606 else
1607 val = FALSE;
1608 }
1609 else
1610 val = tv_equal(tv1, tv2, ic, FALSE);
1611 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1612 val = !val;
1613 *res = val;
1614 return OK;
1615}
1616
1617/*
1618 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1619 * Put the result, false or true, in "res".
1620 * Return FAIL and give an error message when the comparison can't be done.
1621 */
1622 int
1623typval_compare_string(
1624 typval_T *tv1,
1625 typval_T *tv2,
1626 exprtype_T type,
1627 int ic,
1628 int *res)
1629{
1630 int i = 0;
1631 int val = FALSE;
1632 char_u *s1, *s2;
1633 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1634
1635 if (in_vim9script()
1636 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1637 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1638 {
1639 semsg(_(e_cannot_compare_str_with_str),
1640 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1641 return FAIL;
1642 }
1643 s1 = tv_get_string_buf(tv1, buf1);
1644 s2 = tv_get_string_buf(tv2, buf2);
1645 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1646 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1647 switch (type)
1648 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001649 case EXPR_IS: if (in_vim9script())
1650 {
1651 // Really check it is the same string, not just
1652 // the same value.
1653 val = tv1->vval.v_string == tv2->vval.v_string;
1654 break;
1655 }
1656 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001657 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001658 case EXPR_ISNOT: if (in_vim9script())
1659 {
1660 // Really check it is not the same string, not
1661 // just a different value.
1662 val = tv1->vval.v_string != tv2->vval.v_string;
1663 break;
1664 }
1665 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001666 case EXPR_NEQUAL: val = (i != 0); break;
1667 case EXPR_GREATER: val = (i > 0); break;
1668 case EXPR_GEQUAL: val = (i >= 0); break;
1669 case EXPR_SMALLER: val = (i < 0); break;
1670 case EXPR_SEQUAL: val = (i <= 0); break;
1671
1672 case EXPR_MATCH:
1673 case EXPR_NOMATCH:
1674 val = pattern_match(s2, s1, ic);
1675 if (type == EXPR_NOMATCH)
1676 val = !val;
1677 break;
1678
1679 default: break; // avoid gcc warning
1680 }
1681 *res = val;
1682 return OK;
1683}
1684/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001685 * Convert any type to a string, never give an error.
1686 * When "quotes" is TRUE add quotes to a string.
1687 * Returns an allocated string.
1688 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001689 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001690typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001691{
1692 char_u *tofree;
1693 char_u numbuf[NUMBUFLEN];
1694 char_u *ret = NULL;
1695
1696 if (arg == NULL)
1697 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001698 if (!quotes && arg->v_type == VAR_STRING)
1699 {
1700 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1701 : arg->vval.v_string);
1702 }
1703 else
1704 {
1705 ret = tv2string(arg, &tofree, numbuf, 0);
1706 // Make a copy if we have a value but it's not in allocated memory.
1707 if (ret != NULL && tofree == NULL)
1708 ret = vim_strsave(ret);
1709 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001710 return ret;
1711}
1712
1713/*
1714 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1715 * or it refers to a List or Dictionary that is locked.
1716 */
1717 int
1718tv_islocked(typval_T *tv)
1719{
1720 return (tv->v_lock & VAR_LOCKED)
1721 || (tv->v_type == VAR_LIST
1722 && tv->vval.v_list != NULL
1723 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1724 || (tv->v_type == VAR_DICT
1725 && tv->vval.v_dict != NULL
1726 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1727}
1728
1729 static int
1730func_equal(
1731 typval_T *tv1,
1732 typval_T *tv2,
1733 int ic) // ignore case
1734{
1735 char_u *s1, *s2;
1736 dict_T *d1, *d2;
1737 int a1, a2;
1738 int i;
1739
1740 // empty and NULL function name considered the same
1741 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1742 : partial_name(tv1->vval.v_partial);
1743 if (s1 != NULL && *s1 == NUL)
1744 s1 = NULL;
1745 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1746 : partial_name(tv2->vval.v_partial);
1747 if (s2 != NULL && *s2 == NUL)
1748 s2 = NULL;
1749 if (s1 == NULL || s2 == NULL)
1750 {
1751 if (s1 != s2)
1752 return FALSE;
1753 }
1754 else if (STRCMP(s1, s2) != 0)
1755 return FALSE;
1756
1757 // empty dict and NULL dict is different
1758 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1759 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1760 if (d1 == NULL || d2 == NULL)
1761 {
1762 if (d1 != d2)
1763 return FALSE;
1764 }
1765 else if (!dict_equal(d1, d2, ic, TRUE))
1766 return FALSE;
1767
1768 // empty list and no list considered the same
1769 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1770 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1771 if (a1 != a2)
1772 return FALSE;
1773 for (i = 0; i < a1; ++i)
1774 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1775 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1776 return FALSE;
1777
1778 return TRUE;
1779}
1780
1781/*
1782 * Return TRUE if "tv1" and "tv2" have the same value.
1783 * Compares the items just like "==" would compare them, but strings and
1784 * numbers are different. Floats and numbers are also different.
1785 */
1786 int
1787tv_equal(
1788 typval_T *tv1,
1789 typval_T *tv2,
1790 int ic, // ignore case
1791 int recursive) // TRUE when used recursively
1792{
1793 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1794 char_u *s1, *s2;
1795 static int recursive_cnt = 0; // catch recursive loops
1796 int r;
1797 static int tv_equal_recurse_limit;
1798
1799 // Catch lists and dicts that have an endless loop by limiting
1800 // recursiveness to a limit. We guess they are equal then.
1801 // A fixed limit has the problem of still taking an awful long time.
1802 // Reduce the limit every time running into it. That should work fine for
1803 // deeply linked structures that are not recursively linked and catch
1804 // recursiveness quickly.
1805 if (!recursive)
1806 tv_equal_recurse_limit = 1000;
1807 if (recursive_cnt >= tv_equal_recurse_limit)
1808 {
1809 --tv_equal_recurse_limit;
1810 return TRUE;
1811 }
1812
1813 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1814 // arguments.
1815 if ((tv1->v_type == VAR_FUNC
1816 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1817 && (tv2->v_type == VAR_FUNC
1818 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1819 {
1820 ++recursive_cnt;
1821 r = func_equal(tv1, tv2, ic);
1822 --recursive_cnt;
1823 return r;
1824 }
1825
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001826 if (tv1->v_type != tv2->v_type
1827 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1828 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001829 return FALSE;
1830
1831 switch (tv1->v_type)
1832 {
1833 case VAR_LIST:
1834 ++recursive_cnt;
1835 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1836 --recursive_cnt;
1837 return r;
1838
1839 case VAR_DICT:
1840 ++recursive_cnt;
1841 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1842 --recursive_cnt;
1843 return r;
1844
1845 case VAR_BLOB:
1846 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1847
1848 case VAR_NUMBER:
1849 case VAR_BOOL:
1850 case VAR_SPECIAL:
1851 return tv1->vval.v_number == tv2->vval.v_number;
1852
1853 case VAR_STRING:
1854 s1 = tv_get_string_buf(tv1, buf1);
1855 s2 = tv_get_string_buf(tv2, buf2);
1856 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1857
1858 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001859 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001860 case VAR_JOB:
1861#ifdef FEAT_JOB_CHANNEL
1862 return tv1->vval.v_job == tv2->vval.v_job;
1863#endif
1864 case VAR_CHANNEL:
1865#ifdef FEAT_JOB_CHANNEL
1866 return tv1->vval.v_channel == tv2->vval.v_channel;
1867#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001868 case VAR_INSTR:
1869 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001870
1871 case VAR_PARTIAL:
1872 return tv1->vval.v_partial == tv2->vval.v_partial;
1873
1874 case VAR_FUNC:
1875 return tv1->vval.v_string == tv2->vval.v_string;
1876
1877 case VAR_UNKNOWN:
1878 case VAR_ANY:
1879 case VAR_VOID:
1880 break;
1881 }
1882
1883 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1884 // does not equal anything, not even itself.
1885 return FALSE;
1886}
1887
1888/*
1889 * Get an option value.
1890 * "arg" points to the '&' or '+' before the option name.
1891 * "arg" is advanced to character after the option name.
1892 * Return OK or FAIL.
1893 */
1894 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001895eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001896 char_u **arg,
1897 typval_T *rettv, // when NULL, only check if option exists
1898 int evaluate)
1899{
1900 char_u *option_end;
1901 long numval;
1902 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001903 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001904 int c;
1905 int working = (**arg == '+'); // has("+option")
1906 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001907 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001908
1909 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001910 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001911 if (option_end == NULL)
1912 {
1913 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001914 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001915 return FAIL;
1916 }
1917
1918 if (!evaluate)
1919 {
1920 *arg = option_end;
1921 return OK;
1922 }
1923
1924 c = *option_end;
1925 *option_end = NUL;
1926 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001927 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001928
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001929 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001930 {
1931 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001932 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001933 ret = FAIL;
1934 }
1935 else if (rettv != NULL)
1936 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001937 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001938 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001939 {
1940 rettv->v_type = VAR_STRING;
1941 rettv->vval.v_string = NULL;
1942 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001943 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001944 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001945 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1946 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001947 rettv->vval.v_number = 0;
1948 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001949 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001950 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001951 if (in_vim9script() && opt_type == gov_bool)
1952 {
1953 rettv->v_type = VAR_BOOL;
1954 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1955 }
1956 else
1957 {
1958 rettv->v_type = VAR_NUMBER;
1959 rettv->vval.v_number = numval;
1960 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001961 }
1962 else // string option
1963 {
1964 rettv->v_type = VAR_STRING;
1965 rettv->vval.v_string = stringval;
1966 }
1967 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001968 else if (working && (opt_type == gov_hidden_bool
1969 || opt_type == gov_hidden_number
1970 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001971 ret = FAIL;
1972
1973 *option_end = c; // put back for error messages
1974 *arg = option_end;
1975
1976 return ret;
1977}
1978
1979/*
1980 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1981 * Return OK or FAIL.
1982 */
1983 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001984eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001985 char_u **arg,
1986 typval_T *rettv,
1987 int evaluate,
1988 int want_string UNUSED)
1989{
1990 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02001991 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001992 char_u *p;
1993 int get_float = FALSE;
1994
1995 // We accept a float when the format matches
1996 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1997 // strict to avoid backwards compatibility problems.
1998 // With script version 2 and later the leading digit can be
1999 // omitted.
2000 // Don't look for a float after the "." operator, so that
2001 // ":let vers = 1.2.3" doesn't fail.
2002 if (**arg == '.')
2003 p = *arg;
2004 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002005 {
2006 p = *arg + 1;
2007 if (skip_quotes)
2008 for (;;)
2009 {
2010 if (*p == '\'')
2011 ++p;
2012 if (!vim_isdigit(*p))
2013 break;
2014 p = skipdigits(p);
2015 }
2016 else
2017 p = skipdigits(p);
2018 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002019 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2020 {
2021 get_float = TRUE;
2022 p = skipdigits(p + 2);
2023 if (*p == 'e' || *p == 'E')
2024 {
2025 ++p;
2026 if (*p == '-' || *p == '+')
2027 ++p;
2028 if (!vim_isdigit(*p))
2029 get_float = FALSE;
2030 else
2031 p = skipdigits(p + 1);
2032 }
2033 if (ASCII_ISALPHA(*p) || *p == '.')
2034 get_float = FALSE;
2035 }
2036 if (get_float)
2037 {
2038 float_T f;
2039
Bram Moolenaar29500652021-08-08 15:43:34 +02002040 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002041 if (evaluate)
2042 {
2043 rettv->v_type = VAR_FLOAT;
2044 rettv->vval.v_float = f;
2045 }
2046 }
2047 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002048 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2049 {
2050 char_u *bp;
2051 blob_T *blob = NULL; // init for gcc
2052
2053 // Blob constant: 0z0123456789abcdef
2054 if (evaluate)
2055 blob = blob_alloc();
2056 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2057 {
2058 if (!vim_isxdigit(bp[1]))
2059 {
2060 if (blob != NULL)
2061 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002062 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002063 ga_clear(&blob->bv_ga);
2064 VIM_CLEAR(blob);
2065 }
2066 return FAIL;
2067 }
2068 if (blob != NULL)
2069 ga_append(&blob->bv_ga,
2070 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2071 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2072 ++bp;
2073 }
2074 if (blob != NULL)
2075 rettv_blob_set(rettv, blob);
2076 *arg = bp;
2077 }
2078 else
2079 {
2080 varnumber_T n;
2081
2082 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002083 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002084 ? STR2NR_NO_OCT + STR2NR_QUOTE
2085 : STR2NR_ALL, &n, NULL, 0, TRUE);
2086 if (len == 0)
2087 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002088 if (evaluate)
2089 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002090 return FAIL;
2091 }
2092 *arg += len;
2093 if (evaluate)
2094 {
2095 rettv->v_type = VAR_NUMBER;
2096 rettv->vval.v_number = n;
2097 }
2098 }
2099 return OK;
2100}
2101
2102/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002103 * Evaluate a string constant and put the result in "rettv".
2104 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2105 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2106 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002107 * Return OK or FAIL.
2108 */
2109 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002110eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002111{
2112 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002113 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002114 int extra = interpolate ? 1 : 0;
2115 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002116 int len;
2117
2118 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002119 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002120 {
2121 if (*p == '\\' && p[1] != NUL)
2122 {
2123 ++p;
2124 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002125 // to 9 characters (6 for the char and 3 for a modifier):
2126 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002127 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002128 {
2129 int modifiers = 0;
2130 int flags = FSK_KEYCODE | FSK_IN_STRING;
2131
zeertzjqdb088872022-05-02 22:53:45 +01002132 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002133
2134 // Skip to the '>' to avoid using '{' inside for string
2135 // interpolation.
2136 if (p[1] != '*')
2137 flags |= FSK_SIMPLIFY;
2138 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2139 --p; // leave "p" on the ">"
2140 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002141 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002142 else if (interpolate && (*p == '{' || *p == '}'))
2143 {
2144 if (*p == '{' && p[1] != '{') // start of expression
2145 break;
2146 ++p;
2147 if (p[-1] == '}' && *p != '}') // single '}' is an error
2148 {
2149 semsg(_(e_stray_closing_curly_str), *arg);
2150 return FAIL;
2151 }
2152 --extra; // "{{" becomes "{", "}}" becomes "}"
2153 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002154 }
2155
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002156 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002157 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002158 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002159 return FAIL;
2160 }
2161
2162 // If only parsing, set *arg and return here
2163 if (!evaluate)
2164 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002165 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002166 return OK;
2167 }
2168
2169 // Copy the string into allocated memory, handling backslashed
2170 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002171 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002172 len = (int)(p - *arg + extra);
2173 rettv->vval.v_string = alloc(len);
2174 if (rettv->vval.v_string == NULL)
2175 return FAIL;
2176 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002177
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002178 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002179 {
2180 if (*p == '\\')
2181 {
2182 switch (*++p)
2183 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002184 case 'b': *end++ = BS; ++p; break;
2185 case 'e': *end++ = ESC; ++p; break;
2186 case 'f': *end++ = FF; ++p; break;
2187 case 'n': *end++ = NL; ++p; break;
2188 case 'r': *end++ = CAR; ++p; break;
2189 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002190
2191 case 'X': // hex: "\x1", "\x12"
2192 case 'x':
2193 case 'u': // Unicode: "\u0023"
2194 case 'U':
2195 if (vim_isxdigit(p[1]))
2196 {
2197 int n, nr;
2198 int c = toupper(*p);
2199
2200 if (c == 'X')
2201 n = 2;
2202 else if (*p == 'u')
2203 n = 4;
2204 else
2205 n = 8;
2206 nr = 0;
2207 while (--n >= 0 && vim_isxdigit(p[1]))
2208 {
2209 ++p;
2210 nr = (nr << 4) + hex2nr(*p);
2211 }
2212 ++p;
2213 // For "\u" store the number according to
2214 // 'encoding'.
2215 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002216 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002217 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002218 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002219 }
2220 break;
2221
2222 // octal: "\1", "\12", "\123"
2223 case '0':
2224 case '1':
2225 case '2':
2226 case '3':
2227 case '4':
2228 case '5':
2229 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002230 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002231 if (*p >= '0' && *p <= '7')
2232 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002233 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002234 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002235 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002236 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002237 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002238 break;
2239
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002240 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002241 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002242 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002243 int flags = FSK_KEYCODE | FSK_IN_STRING;
2244
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002245 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002246 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002247 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002248 if (extra != 0)
2249 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002250 end += extra;
2251 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002252 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002253 break;
2254 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002255 }
2256 // FALLTHROUGH
2257
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002258 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002259 break;
2260 }
2261 }
2262 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002263 {
2264 if (interpolate && (*p == '{' || *p == '}'))
2265 {
2266 if (*p == '{' && p[1] != '{') // start of expression
2267 break;
2268 ++p; // reduce "{{" to "{" and "}}" to "}"
2269 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002270 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002271 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002272 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002273 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002274 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002275 ++p;
2276 *arg = p;
2277
2278 return OK;
2279}
2280
2281/*
2282 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002283 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2284 * Return OK when a "rettv" was set to the string.
2285 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002286 */
2287 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002288eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002289{
2290 char_u *p;
2291 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002292 int reduce = interpolate ? -1 : 0;
2293 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002294
2295 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002296 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002297 {
2298 if (*p == '\'')
2299 {
2300 if (p[1] != '\'')
2301 break;
2302 ++reduce;
2303 ++p;
2304 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002305 else if (interpolate)
2306 {
2307 if (*p == '{')
2308 {
2309 if (p[1] != '{')
2310 break;
2311 ++p;
2312 ++reduce;
2313 }
2314 else if (*p == '}')
2315 {
2316 ++p;
2317 if (*p != '}')
2318 {
2319 semsg(_(e_stray_closing_curly_str), *arg);
2320 return FAIL;
2321 }
2322 ++reduce;
2323 }
2324 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002325 }
2326
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002327 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002328 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002329 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002330 return FAIL;
2331 }
2332
2333 // If only parsing return after setting "*arg"
2334 if (!evaluate)
2335 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002336 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002337 return OK;
2338 }
2339
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002340 // Copy the string into allocated memory, handling '' to ' reduction and
2341 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002342 str = alloc((p - *arg) - reduce);
2343 if (str == NULL)
2344 return FAIL;
2345 rettv->v_type = VAR_STRING;
2346 rettv->vval.v_string = str;
2347
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002348 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002349 {
2350 if (*p == '\'')
2351 {
2352 if (p[1] != '\'')
2353 break;
2354 ++p;
2355 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002356 else if (interpolate && (*p == '{' || *p == '}'))
2357 {
2358 if (*p == '{' && p[1] != '{')
2359 break;
2360 ++p;
2361 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002362 MB_COPY_CHAR(p, str);
2363 }
2364 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002365 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002366
2367 return OK;
2368}
2369
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002370/*
2371 * Evaluate a single or double quoted string possibly containing expressions.
2372 * "arg" points to the '$'. The result is put in "rettv".
2373 * Returns OK or FAIL.
2374 */
LemonBoy2eaef102022-05-06 13:14:50 +01002375 int
2376eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2377{
2378 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002379 int ret = OK;
2380 int quote;
2381 garray_T ga;
2382 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002383
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002384 ga_init2(&ga, 1, 80);
2385
2386 // *arg is on the '$' character, move it to the first string character.
2387 ++*arg;
2388 quote = **arg;
2389 ++*arg;
2390
2391 for (;;)
2392 {
2393 // Get the string up to the matching quote or to a single '{'.
2394 // "arg" is advanced to either the quote or the '{'.
2395 if (quote == '"')
2396 ret = eval_string(arg, &tv, evaluate, TRUE);
2397 else
2398 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2399 if (ret == FAIL)
2400 break;
2401 if (evaluate)
2402 {
2403 ga_concat(&ga, tv.vval.v_string);
2404 clear_tv(&tv);
2405 }
2406
2407 if (**arg != '{')
2408 {
2409 // found terminating quote
2410 ++*arg;
2411 break;
2412 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002413 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002414 if (p == NULL)
2415 {
2416 ret = FAIL;
2417 break;
2418 }
2419 *arg = p;
2420 }
LemonBoy2eaef102022-05-06 13:14:50 +01002421
2422 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002423 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2424 {
2425 ga_clear(&ga);
2426 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002427 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002428 }
LemonBoy2eaef102022-05-06 13:14:50 +01002429
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002430 rettv->vval.v_string = ga.ga_data;
2431 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002432}
2433
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002434/*
2435 * Return a string with the string representation of a variable.
2436 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2437 * "numbuf" is used for a number.
2438 * Puts quotes around strings, so that they can be parsed back by eval().
2439 * May return NULL.
2440 */
2441 char_u *
2442tv2string(
2443 typval_T *tv,
2444 char_u **tofree,
2445 char_u *numbuf,
2446 int copyID)
2447{
2448 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2449}
2450
2451/*
2452 * Get the value of an environment variable.
2453 * "arg" is pointing to the '$'. It is advanced to after the name.
2454 * If the environment variable was not set, silently assume it is empty.
2455 * Return FAIL if the name is invalid.
2456 */
2457 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002458eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002459{
2460 char_u *string = NULL;
2461 int len;
2462 int cc;
2463 char_u *name;
2464 int mustfree = FALSE;
2465
2466 ++*arg;
2467 name = *arg;
2468 len = get_env_len(arg);
2469 if (evaluate)
2470 {
2471 if (len == 0)
2472 return FAIL; // invalid empty name
2473
2474 cc = name[len];
2475 name[len] = NUL;
2476 // first try vim_getenv(), fast for normal environment vars
2477 string = vim_getenv(name, &mustfree);
2478 if (string != NULL && *string != NUL)
2479 {
2480 if (!mustfree)
2481 string = vim_strsave(string);
2482 }
2483 else
2484 {
2485 if (mustfree)
2486 vim_free(string);
2487
2488 // next try expanding things like $VIM and ${HOME}
2489 string = expand_env_save(name - 1);
2490 if (string != NULL && *string == '$')
2491 VIM_CLEAR(string);
2492 }
2493 name[len] = cc;
2494
2495 rettv->v_type = VAR_STRING;
2496 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002497 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002498 }
2499
2500 return OK;
2501}
2502
2503/*
2504 * Get the lnum from the first argument.
2505 * Also accepts ".", "$", etc., but that only works for the current buffer.
2506 * Returns -1 on error.
2507 */
2508 linenr_T
2509tv_get_lnum(typval_T *argvars)
2510{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002511 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002512
Bram Moolenaar56acb092020-08-16 14:48:19 +02002513 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2514 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002515 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002516 {
2517 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002518 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002519
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002520 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002521 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002522 if (fp != NULL)
2523 lnum = fp->lnum;
2524 }
2525 return lnum;
2526}
2527
2528/*
2529 * Get the lnum from the first argument.
2530 * Also accepts "$", then "buf" is used.
2531 * Returns 0 on error.
2532 */
2533 linenr_T
2534tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2535{
2536 if (argvars[0].v_type == VAR_STRING
2537 && argvars[0].vval.v_string != NULL
2538 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002539 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002540 && buf != NULL)
2541 return buf->b_ml.ml_line_count;
2542 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2543}
2544
2545/*
2546 * Get buffer by number or pattern.
2547 */
2548 buf_T *
2549tv_get_buf(typval_T *tv, int curtab_only)
2550{
2551 char_u *name = tv->vval.v_string;
2552 buf_T *buf;
2553
2554 if (tv->v_type == VAR_NUMBER)
2555 return buflist_findnr((int)tv->vval.v_number);
2556 if (tv->v_type != VAR_STRING)
2557 return NULL;
2558 if (name == NULL || *name == NUL)
2559 return curbuf;
2560 if (name[0] == '$' && name[1] == NUL)
2561 return lastbuf;
2562
2563 buf = buflist_find_by_name(name, curtab_only);
2564
2565 // If not found, try expanding the name, like done for bufexists().
2566 if (buf == NULL)
2567 buf = find_buffer(tv);
2568
2569 return buf;
2570}
2571
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002572/*
2573 * Like tv_get_buf() but give an error message is the type is wrong.
2574 */
2575 buf_T *
2576tv_get_buf_from_arg(typval_T *tv)
2577{
2578 buf_T *buf;
2579
2580 ++emsg_off;
2581 buf = tv_get_buf(tv, FALSE);
2582 --emsg_off;
2583 if (buf == NULL
2584 && tv->v_type != VAR_NUMBER
2585 && tv->v_type != VAR_STRING)
2586 // issue errmsg for type error
2587 (void)tv_get_number(tv);
2588 return buf;
2589}
2590
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002591#endif // FEAT_EVAL