blob: f9f6bd691622320173e2a2627698b76000817cfa [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{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +000055 if (varp == NULL)
56 return;
Bram Moolenaar00b28d62022-12-08 15:32:33 +000057
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +000058 switch (varp->v_type)
59 {
60 case VAR_FUNC:
61 func_unref(varp->vval.v_string);
62 // FALLTHROUGH
63 case VAR_STRING:
64 vim_free(varp->vval.v_string);
65 break;
66 case VAR_PARTIAL:
67 partial_unref(varp->vval.v_partial);
68 break;
69 case VAR_BLOB:
70 blob_unref(varp->vval.v_blob);
71 break;
72 case VAR_LIST:
73 list_unref(varp->vval.v_list);
74 break;
75 case VAR_DICT:
76 dict_unref(varp->vval.v_dict);
77 break;
78 case VAR_JOB:
79#ifdef FEAT_JOB_CHANNEL
80 job_unref(varp->vval.v_job);
81 break;
82#endif
83 case VAR_CHANNEL:
84#ifdef FEAT_JOB_CHANNEL
85 channel_unref(varp->vval.v_channel);
86 break;
87#endif
88 case VAR_CLASS:
89 class_unref(varp->vval.v_class);
90 break;
91 case VAR_OBJECT:
92 object_unref(varp->vval.v_object);
93 break;
94
95 case VAR_NUMBER:
96 case VAR_FLOAT:
97 case VAR_ANY:
98 case VAR_UNKNOWN:
99 case VAR_VOID:
100 case VAR_BOOL:
101 case VAR_SPECIAL:
102 case VAR_INSTR:
103 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200104 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000105 vim_free(varp);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200106}
107
108/*
109 * Free the memory for a variable value and set the value to NULL or 0.
110 */
111 void
112clear_tv(typval_T *varp)
113{
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000114 if (varp == NULL)
115 return;
116
117 switch (varp->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200118 {
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000119 case VAR_FUNC:
120 func_unref(varp->vval.v_string);
121 // FALLTHROUGH
122 case VAR_STRING:
123 VIM_CLEAR(varp->vval.v_string);
124 break;
125 case VAR_PARTIAL:
126 partial_unref(varp->vval.v_partial);
127 varp->vval.v_partial = NULL;
128 break;
129 case VAR_BLOB:
130 blob_unref(varp->vval.v_blob);
131 varp->vval.v_blob = NULL;
132 break;
133 case VAR_LIST:
134 list_unref(varp->vval.v_list);
135 varp->vval.v_list = NULL;
136 break;
137 case VAR_DICT:
138 dict_unref(varp->vval.v_dict);
139 varp->vval.v_dict = NULL;
140 break;
141 case VAR_NUMBER:
142 case VAR_BOOL:
143 case VAR_SPECIAL:
144 varp->vval.v_number = 0;
145 break;
146 case VAR_FLOAT:
147 varp->vval.v_float = 0.0;
148 break;
149 case VAR_JOB:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200150#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000151 job_unref(varp->vval.v_job);
152 varp->vval.v_job = NULL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200153#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000154 break;
155 case VAR_CHANNEL:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200156#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000157 channel_unref(varp->vval.v_channel);
158 varp->vval.v_channel = NULL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200159#endif
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000160 break;
161 case VAR_INSTR:
162 VIM_CLEAR(varp->vval.v_instr);
163 break;
164 case VAR_CLASS:
165 class_unref(varp->vval.v_class);
166 varp->vval.v_class = NULL;
167 break;
168 case VAR_OBJECT:
169 object_unref(varp->vval.v_object);
170 varp->vval.v_object = NULL;
171 break;
172 case VAR_UNKNOWN:
173 case VAR_ANY:
174 case VAR_VOID:
175 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200176 }
Yegappan Lakshmanan032713f2023-01-25 21:05:38 +0000177 varp->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200178}
179
180/*
181 * Set the value of a variable to NULL without freeing items.
182 */
183 void
184init_tv(typval_T *varp)
185{
186 if (varp != NULL)
187 CLEAR_POINTER(varp);
188}
189
Bram Moolenaar36967b32020-08-17 21:41:02 +0200190 static varnumber_T
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000191tv_get_bool_or_number_chk(
192 typval_T *varp,
193 int *denote,
194 int want_bool,
195 int vim9_string_error) // in Vim9 using a string is an error
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200196{
197 varnumber_T n = 0L;
198
199 switch (varp->v_type)
200 {
201 case VAR_NUMBER:
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200202 if (in_vim9script() && want_bool && varp->vval.v_number != 0
Bram Moolenaard70840e2020-08-22 15:06:35 +0200203 && varp->vval.v_number != 1)
204 {
205 semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
206 break;
207 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200208 return varp->vval.v_number;
209 case VAR_FLOAT:
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000210 emsg(_(e_using_float_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200211 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200212 case VAR_FUNC:
213 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000214 emsg(_(e_using_funcref_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200215 break;
216 case VAR_STRING:
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000217 if (vim9_string_error && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200218 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100219 emsg_using_string_as(varp, !want_bool);
Bram Moolenaar56acb092020-08-16 14:48:19 +0200220 break;
221 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200222 if (varp->vval.v_string != NULL)
223 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar5fb78c32023-03-04 20:47:39 +0000224 STR2NR_ALL, &n, NULL, 0, FALSE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200225 return n;
226 case VAR_LIST:
Bram Moolenaar677658a2022-01-05 16:09:06 +0000227 emsg(_(e_using_list_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200228 break;
229 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000230 emsg(_(e_using_dictionary_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200231 break;
232 case VAR_BOOL:
233 case VAR_SPECIAL:
Bram Moolenaar36967b32020-08-17 21:41:02 +0200234 if (!want_bool && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200235 {
Bram Moolenaard92cc132020-11-18 17:17:15 +0100236 if (varp->v_type == VAR_BOOL)
237 emsg(_(e_using_bool_as_number));
238 else
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000239 emsg(_(e_using_special_as_number));
Bram Moolenaar56acb092020-08-16 14:48:19 +0200240 break;
241 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200242 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
243 case VAR_JOB:
244#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000245 emsg(_(e_using_job_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200246 break;
247#endif
248 case VAR_CHANNEL:
249#ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000250 emsg(_(e_using_channel_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200251 break;
252#endif
253 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000254 emsg(_(e_using_blob_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200255 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000256 case VAR_CLASS:
257 emsg(_(e_using_class_as_number));
258 break;
259 case VAR_OBJECT:
260 emsg(_(e_using_object_as_number));
261 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200262 case VAR_VOID:
263 emsg(_(e_cannot_use_void_value));
264 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200265 case VAR_UNKNOWN:
266 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200267 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200268 internal_error_no_abort("tv_get_number(UNKNOWN)");
269 break;
270 }
271 if (denote == NULL) // useful for values that must be unsigned
272 n = -1;
273 else
274 *denote = TRUE;
275 return n;
276}
277
Bram Moolenaar36967b32020-08-17 21:41:02 +0200278/*
279 * Get the number value of a variable.
280 * If it is a String variable, uses vim_str2nr().
281 * For incompatible types, return 0.
282 * tv_get_number_chk() is similar to tv_get_number(), but informs the
283 * caller of incompatible types: it sets *denote to TRUE if "denote"
284 * is not NULL or returns -1 otherwise.
285 */
286 varnumber_T
287tv_get_number(typval_T *varp)
288{
289 int error = FALSE;
290
291 return tv_get_number_chk(varp, &error); // return 0L on error
292}
293
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000294/*
295 * Like tv_get_numbe() but in Vim9 script do convert a number in a string to a
296 * number without giving an error.
297 */
298 varnumber_T
299tv_to_number(typval_T *varp)
300{
301 int error = FALSE;
302
303 return tv_get_bool_or_number_chk(varp, &error, FALSE, FALSE);
304}
305
Bram Moolenaar36967b32020-08-17 21:41:02 +0200306 varnumber_T
307tv_get_number_chk(typval_T *varp, int *denote)
308{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000309 return tv_get_bool_or_number_chk(varp, denote, FALSE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200310}
311
312/*
313 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200314 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200315 */
316 varnumber_T
317tv_get_bool(typval_T *varp)
318{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000319 return tv_get_bool_or_number_chk(varp, NULL, TRUE, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200320}
321
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200322/*
323 * Get the boolean value of "varp". This is like tv_get_number_chk(),
324 * but in Vim9 script accepts Number and Bool.
325 */
326 varnumber_T
327tv_get_bool_chk(typval_T *varp, int *denote)
328{
Bram Moolenaarbe19d782023-03-09 22:06:49 +0000329 return tv_get_bool_or_number_chk(varp, denote, TRUE, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200330}
331
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000332 static float_T
333tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200334{
335 switch (varp->v_type)
336 {
337 case VAR_NUMBER:
338 return (float_T)(varp->vval.v_number);
339 case VAR_FLOAT:
340 return varp->vval.v_float;
341 case VAR_FUNC:
342 case VAR_PARTIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000343 emsg(_(e_using_funcref_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200344 break;
345 case VAR_STRING:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000346 emsg(_(e_using_string_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200347 break;
348 case VAR_LIST:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000349 emsg(_(e_using_list_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200350 break;
351 case VAR_DICT:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000352 emsg(_(e_using_dictionary_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200353 break;
354 case VAR_BOOL:
Bram Moolenaarb2810f12022-01-08 21:38:52 +0000355 emsg(_(e_using_boolean_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200356 break;
357 case VAR_SPECIAL:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000358 emsg(_(e_using_special_value_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200359 break;
360 case VAR_JOB:
361# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000362 emsg(_(e_using_job_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200363 break;
364# endif
365 case VAR_CHANNEL:
366# ifdef FEAT_JOB_CHANNEL
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000367 emsg(_(e_using_channel_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200368 break;
369# endif
370 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000371 emsg(_(e_using_blob_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200372 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000373 case VAR_CLASS:
374 emsg(_(e_using_class_as_float));
375 break;
376 case VAR_OBJECT:
377 emsg(_(e_using_object_as_float));
378 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200379 case VAR_VOID:
380 emsg(_(e_cannot_use_void_value));
381 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200382 case VAR_UNKNOWN:
383 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200384 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200385 internal_error_no_abort("tv_get_float(UNKNOWN)");
386 break;
387 }
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000388 if (error != NULL)
389 *error = TRUE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200390 return 0;
391}
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000392
393 float_T
394tv_get_float(typval_T *varp)
395{
396 return tv_get_float_chk(varp, NULL);
397}
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200398
399/*
Ernie Rael51d04d12022-05-04 15:40:22 +0100400 * Give an error and return FAIL unless "args[idx]" is unknown
401 */
402 int
403check_for_unknown_arg(typval_T *args, int idx)
404{
405 if (args[idx].v_type != VAR_UNKNOWN)
406 {
407 semsg(_(e_too_many_arguments), idx + 1);
408 return FAIL;
409 }
410 return OK;
411}
412
413/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200414 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100415 */
416 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100417check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100418{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100419 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100420 {
rbtnnddc80af2021-12-17 18:01:31 +0000421 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100422 return FAIL;
423 }
424 return OK;
425}
426
427/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100428 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100429 */
430 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100431check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100432{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100433 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100434 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100435 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100436 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200437 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100438 return FAIL;
439 }
440 return OK;
441}
442
443/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200444 * Check for an optional string argument at 'idx'
445 */
446 int
447check_for_opt_string_arg(typval_T *args, int idx)
448{
449 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100450 || check_for_string_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200451}
452
453/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200454 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200455 */
456 int
457check_for_number_arg(typval_T *args, int idx)
458{
459 if (args[idx].v_type != VAR_NUMBER)
460 {
rbtnnddc80af2021-12-17 18:01:31 +0000461 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200462 return FAIL;
463 }
464 return OK;
465}
466
467/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200468 * Check for an optional number argument at 'idx'
469 */
470 int
471check_for_opt_number_arg(typval_T *args, int idx)
472{
473 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100474 || check_for_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200475}
476
477/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200478 * Give an error and return FAIL unless "args[idx]" is a float or a number.
479 */
480 int
481check_for_float_or_nr_arg(typval_T *args, int idx)
482{
483 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
484 {
rbtnnddc80af2021-12-17 18:01:31 +0000485 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +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 bool.
493 */
494 int
495check_for_bool_arg(typval_T *args, int idx)
496{
497 if (args[idx].v_type != VAR_BOOL
498 && !(args[idx].v_type == VAR_NUMBER
499 && (args[idx].vval.v_number == 0
500 || args[idx].vval.v_number == 1)))
501 {
rbtnnddc80af2021-12-17 18:01:31 +0000502 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200503 return FAIL;
504 }
505 return OK;
506}
507
508/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200509 * Check for an optional bool argument at 'idx'.
510 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200511 */
512 int
513check_for_opt_bool_arg(typval_T *args, int idx)
514{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200515 if (args[idx].v_type == VAR_UNKNOWN)
516 return OK;
517 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200518}
519
520/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200521 * Give an error and return FAIL unless "args[idx]" is a blob.
522 */
523 int
524check_for_blob_arg(typval_T *args, int idx)
525{
526 if (args[idx].v_type != VAR_BLOB)
527 {
rbtnnddc80af2021-12-17 18:01:31 +0000528 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200529 return FAIL;
530 }
531 return OK;
532}
533
534/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200535 * Give an error and return FAIL unless "args[idx]" is a list.
536 */
537 int
538check_for_list_arg(typval_T *args, int idx)
539{
540 if (args[idx].v_type != VAR_LIST)
541 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +0200542 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200543 return FAIL;
544 }
545 return OK;
546}
547
548/*
Bram Moolenaard83392a2022-09-01 12:22:46 +0100549 * Give an error and return FAIL unless "args[idx]" is a non-NULL list.
550 */
551 int
552check_for_nonnull_list_arg(typval_T *args, int idx)
553{
554 if (check_for_list_arg(args, idx) == FAIL)
555 return FAIL;
556
557 if (args[idx].vval.v_list == NULL)
558 {
559 semsg(_(e_non_null_list_required_for_argument_nr), idx + 1);
560 return FAIL;
561 }
562 return OK;
563}
564
565/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200566 * Check for an optional list argument at 'idx'
567 */
568 int
569check_for_opt_list_arg(typval_T *args, int idx)
570{
571 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100572 || check_for_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200573}
574
575/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200576 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200577 */
578 int
579check_for_dict_arg(typval_T *args, int idx)
580{
581 if (args[idx].v_type != VAR_DICT)
582 {
rbtnnddc80af2021-12-17 18:01:31 +0000583 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200584 return FAIL;
585 }
586 return OK;
587}
588
589/*
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100590 * Give an error and return FAIL unless "args[idx]" is a non-NULL dict.
591 */
592 int
593check_for_nonnull_dict_arg(typval_T *args, int idx)
594{
595 if (check_for_dict_arg(args, idx) == FAIL)
596 return FAIL;
597
598 if (args[idx].vval.v_dict == NULL)
599 {
600 semsg(_(e_non_null_dict_required_for_argument_nr), idx + 1);
601 return FAIL;
602 }
603 return OK;
604}
605
606/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200607 * Check for an optional dict argument at 'idx'
608 */
609 int
610check_for_opt_dict_arg(typval_T *args, int idx)
611{
612 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100613 || check_for_dict_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200614}
615
Dominique Pelle748b3082022-01-08 12:41:16 +0000616#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200617/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200618 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
619 */
620 int
621check_for_chan_or_job_arg(typval_T *args, int idx)
622{
623 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
624 {
rbtnnddc80af2021-12-17 18:01:31 +0000625 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200626 return FAIL;
627 }
628 return OK;
629}
630
631/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200632 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
633 * job.
634 */
635 int
636check_for_opt_chan_or_job_arg(typval_T *args, int idx)
637{
638 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100639 || check_for_chan_or_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200640}
641
642/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200643 * Give an error and return FAIL unless "args[idx]" is a job.
644 */
645 int
646check_for_job_arg(typval_T *args, int idx)
647{
648 if (args[idx].v_type != VAR_JOB)
649 {
rbtnnddc80af2021-12-17 18:01:31 +0000650 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200651 return FAIL;
652 }
653 return OK;
654}
655
656/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200657 * Check for an optional job argument at 'idx'.
658 */
659 int
660check_for_opt_job_arg(typval_T *args, int idx)
661{
662 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100663 || check_for_job_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200664}
Bram Moolenaar3b8c7082022-11-30 20:20:56 +0000665#else
666/*
667 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
668 * job. Used without the +channel feature, thus only VAR_UNKNOWN is accepted.
669 */
670 int
671check_for_opt_chan_or_job_arg(typval_T *args, int idx)
672{
673 return args[idx].v_type == VAR_UNKNOWN ? OK : FAIL;
674}
Dominique Pelle748b3082022-01-08 12:41:16 +0000675#endif
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200676
677/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200678 * Give an error and return FAIL unless "args[idx]" is a string or
679 * a number.
680 */
681 int
682check_for_string_or_number_arg(typval_T *args, int idx)
683{
684 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
685 {
rbtnnddc80af2021-12-17 18:01:31 +0000686 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200687 return FAIL;
688 }
689 return OK;
690}
691
692/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200693 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200694 */
695 int
696check_for_opt_string_or_number_arg(typval_T *args, int idx)
697{
698 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100699 || check_for_string_or_number_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200700}
701
702/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200703 * Give an error and return FAIL unless "args[idx]" is a buffer number.
704 * Buffer number can be a number or a string.
705 */
706 int
707check_for_buffer_arg(typval_T *args, int idx)
708{
709 return check_for_string_or_number_arg(args, idx);
710}
711
712/*
713 * Check for an optional buffer argument at 'idx'
714 */
715 int
716check_for_opt_buffer_arg(typval_T *args, int idx)
717{
718 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100719 || check_for_buffer_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200720}
721
722/*
723 * Give an error and return FAIL unless "args[idx]" is a line number.
724 * Line number can be a number or a string.
725 */
726 int
727check_for_lnum_arg(typval_T *args, int idx)
728{
729 return check_for_string_or_number_arg(args, idx);
730}
731
732/*
733 * Check for an optional line number argument at 'idx'
734 */
735 int
736check_for_opt_lnum_arg(typval_T *args, int idx)
737{
738 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100739 || check_for_lnum_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200740}
741
Dominique Pelle748b3082022-01-08 12:41:16 +0000742#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200743/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200744 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200745 */
746 int
747check_for_string_or_blob_arg(typval_T *args, int idx)
748{
749 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
750 {
rbtnnddc80af2021-12-17 18:01:31 +0000751 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200752 return FAIL;
753 }
754 return OK;
755}
Dominique Pelle748b3082022-01-08 12:41:16 +0000756#endif
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200757
758/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200759 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200760 */
761 int
762check_for_string_or_list_arg(typval_T *args, int idx)
763{
764 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
765 {
rbtnnddc80af2021-12-17 18:01:31 +0000766 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200767 return FAIL;
768 }
769 return OK;
770}
771
772/*
rbtnn0ccb5842021-12-18 18:33:46 +0000773 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
774 * blob.
775 */
776 int
777check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
778{
779 if (args[idx].v_type != VAR_STRING
780 && args[idx].v_type != VAR_LIST
781 && args[idx].v_type != VAR_BLOB)
782 {
783 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
784 return FAIL;
785 }
786 return OK;
787}
788
789/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200790 * Check for an optional string or list argument at 'idx'
791 */
792 int
793check_for_opt_string_or_list_arg(typval_T *args, int idx)
794{
795 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100796 || check_for_string_or_list_arg(args, idx) != FAIL) ? OK : FAIL;
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200797}
798
799/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200800 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
801 */
802 int
803check_for_string_or_dict_arg(typval_T *args, int idx)
804{
805 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
806 {
rbtnnddc80af2021-12-17 18:01:31 +0000807 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200808 return FAIL;
809 }
810 return OK;
811}
812
813/*
814 * Give an error and return FAIL unless "args[idx]" is a string or a number
815 * or a list.
816 */
817 int
818check_for_string_or_number_or_list_arg(typval_T *args, int idx)
819{
820 if (args[idx].v_type != VAR_STRING
821 && args[idx].v_type != VAR_NUMBER
822 && args[idx].v_type != VAR_LIST)
823 {
rbtnn0ccb5842021-12-18 18:33:46 +0000824 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200825 return FAIL;
826 }
827 return OK;
828}
829
830/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200831 * Give an error and return FAIL unless "args[idx]" is an optional string
832 * or number or a list
833 */
834 int
835check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
836{
837 return (args[idx].v_type == VAR_UNKNOWN
zeertzjqcd2d5c12022-09-12 14:09:30 +0100838 || check_for_string_or_number_or_list_arg(args, idx)
839 != FAIL) ? OK : FAIL;
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200840}
841
842/*
Bakudankun375141e2022-09-09 18:46:47 +0100843 * Give an error and return FAIL unless "args[idx]" is a string or a number
844 * or a list or a blob.
845 */
846 int
847check_for_string_or_number_or_list_or_blob_arg(typval_T *args, int idx)
848{
849 if (args[idx].v_type != VAR_STRING
850 && args[idx].v_type != VAR_NUMBER
851 && args[idx].v_type != VAR_LIST
852 && args[idx].v_type != VAR_BLOB)
853 {
854 semsg(_(e_string_number_list_or_blob_required_for_argument_nr), idx + 1);
855 return FAIL;
856 }
857 return OK;
858}
859
860/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200861 * Give an error and return FAIL unless "args[idx]" is a string or a list
862 * or a dict.
863 */
864 int
865check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
866{
867 if (args[idx].v_type != VAR_STRING
868 && args[idx].v_type != VAR_LIST
869 && args[idx].v_type != VAR_DICT)
870 {
rbtnnddc80af2021-12-17 18:01:31 +0000871 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200872 return FAIL;
873 }
874 return OK;
875}
876
877/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000878 * Give an error and return FAIL unless "args[idx]" is a string
879 * or a function reference.
880 */
881 int
882check_for_string_or_func_arg(typval_T *args, int idx)
883{
884 if (args[idx].v_type != VAR_PARTIAL
885 && args[idx].v_type != VAR_FUNC
886 && args[idx].v_type != VAR_STRING)
887 {
888 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
889 return FAIL;
890 }
891 return OK;
892}
893
894/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200895 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
896 */
897 int
898check_for_list_or_blob_arg(typval_T *args, int idx)
899{
900 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
901 {
rbtnn0ccb5842021-12-18 18:33:46 +0000902 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200903 return FAIL;
904 }
905 return OK;
906}
907
908/*
909 * Give an error and return FAIL unless "args[idx]" is a list or dict
910 */
911 int
912check_for_list_or_dict_arg(typval_T *args, int idx)
913{
914 if (args[idx].v_type != VAR_LIST
915 && args[idx].v_type != VAR_DICT)
916 {
rbtnnddc80af2021-12-17 18:01:31 +0000917 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200918 return FAIL;
919 }
920 return OK;
921}
922
923/*
924 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
925 * blob.
926 */
927 int
928check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
929{
930 if (args[idx].v_type != VAR_LIST
931 && args[idx].v_type != VAR_DICT
932 && args[idx].v_type != VAR_BLOB)
933 {
rbtnnddc80af2021-12-17 18:01:31 +0000934 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200935 return FAIL;
936 }
937 return OK;
938}
939
940/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000941 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
942 * blob or a string.
943 */
944 int
945check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
946{
947 if (args[idx].v_type != VAR_LIST
948 && args[idx].v_type != VAR_DICT
949 && args[idx].v_type != VAR_BLOB
950 && args[idx].v_type != VAR_STRING)
951 {
rbtnnddc80af2021-12-17 18:01:31 +0000952 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000953 return FAIL;
954 }
955 return OK;
956}
957
958/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200959 * Give an error and return FAIL unless "args[idx]" is an optional buffer
960 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200961 */
962 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200963check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200964{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200965 if (args[idx].v_type != VAR_UNKNOWN
966 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200967 && args[idx].v_type != VAR_NUMBER
968 && args[idx].v_type != VAR_DICT)
969 {
rbtnnddc80af2021-12-17 18:01:31 +0000970 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200971 return FAIL;
972 }
973 return OK;
974}
975
976/*
LemonBoyafe04662023-08-23 21:08:11 +0200977 * Give an error and return FAIL unless "args[idx]" is an object.
978 */
979 int
980check_for_object_arg(typval_T *args, int idx)
981{
982 if (args[idx].v_type != VAR_OBJECT)
983 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +0200984 semsg(_(e_object_required_for_argument_nr), idx + 1);
LemonBoyafe04662023-08-23 21:08:11 +0200985 return FAIL;
986 }
987 return OK;
988}
989
990/*
991 * Give an error and return FAIL unless "args[idx]" is a class or a list.
992 */
993 int
994check_for_class_or_list_arg(typval_T *args, int idx)
995{
996 if (args[idx].v_type != VAR_CLASS && args[idx].v_type != VAR_LIST)
997 {
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +0200998 semsg(_(e_list_or_class_required_for_argument_nr), idx + 1);
LemonBoyafe04662023-08-23 21:08:11 +0200999 return FAIL;
1000 }
1001 return OK;
1002}
1003
1004/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001005 * Get the string value of a variable.
1006 * If it is a Number variable, the number is converted into a string.
1007 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1008 * tv_get_string_buf() uses a given buffer.
1009 * If the String variable has never been set, return an empty string.
1010 * Never returns NULL;
1011 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
1012 * NULL on error.
1013 */
1014 char_u *
1015tv_get_string(typval_T *varp)
1016{
1017 static char_u mybuf[NUMBUFLEN];
1018
1019 return tv_get_string_buf(varp, mybuf);
1020}
1021
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001022/*
1023 * Like tv_get_string() but don't allow number to string conversion for Vim9.
1024 */
1025 char_u *
1026tv_get_string_strict(typval_T *varp)
1027{
1028 static char_u mybuf[NUMBUFLEN];
1029 char_u *res = tv_get_string_buf_chk_strict(
1030 varp, mybuf, in_vim9script());
1031
1032 return res != NULL ? res : (char_u *)"";
1033}
1034
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001035 char_u *
1036tv_get_string_buf(typval_T *varp, char_u *buf)
1037{
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001038 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001039
1040 return res != NULL ? res : (char_u *)"";
1041}
1042
1043/*
1044 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1045 */
1046 char_u *
1047tv_get_string_chk(typval_T *varp)
1048{
1049 static char_u mybuf[NUMBUFLEN];
1050
1051 return tv_get_string_buf_chk(varp, mybuf);
1052}
1053
1054 char_u *
1055tv_get_string_buf_chk(typval_T *varp, char_u *buf)
1056{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001057 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
1058}
1059
1060 char_u *
1061tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
1062{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001063 switch (varp->v_type)
1064 {
1065 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001066 if (strict)
1067 {
1068 emsg(_(e_using_number_as_string));
1069 break;
1070 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001071 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1072 (varnumber_T)varp->vval.v_number);
1073 return buf;
1074 case VAR_FUNC:
1075 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001076 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001077 break;
1078 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001079 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001080 break;
1081 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001082 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001083 break;
1084 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001085 if (strict)
1086 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001087 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001088 break;
1089 }
1090 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1091 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001092 case VAR_STRING:
1093 if (varp->vval.v_string != NULL)
1094 return varp->vval.v_string;
1095 return (char_u *)"";
1096 case VAR_BOOL:
1097 case VAR_SPECIAL:
1098 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1099 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001100 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001101 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001102 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001103 case VAR_CLASS:
1104 emsg(_(e_using_class_as_string));
1105 break;
1106 case VAR_OBJECT:
1107 emsg(_(e_using_object_as_string));
1108 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001109 case VAR_JOB:
1110#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001111 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001112 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001113 semsg(_(e_using_invalid_value_as_string_str), "job");
1114 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001115 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001116 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001117#endif
1118 break;
1119 case VAR_CHANNEL:
1120#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001121 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001122 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001123 semsg(_(e_using_invalid_value_as_string_str), "channel");
1124 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001125 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001126 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001127#endif
1128 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001129 case VAR_VOID:
1130 emsg(_(e_cannot_use_void_value));
1131 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001132 case VAR_UNKNOWN:
1133 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001134 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001135 semsg(_(e_using_invalid_value_as_string_str),
1136 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001137 break;
1138 }
1139 return NULL;
1140}
1141
1142/*
1143 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1144 * string() on Dict, List, etc.
1145 */
1146 char_u *
1147tv_stringify(typval_T *varp, char_u *buf)
1148{
1149 if (varp->v_type == VAR_LIST
1150 || varp->v_type == VAR_DICT
1151 || varp->v_type == VAR_BLOB
1152 || varp->v_type == VAR_FUNC
1153 || varp->v_type == VAR_PARTIAL
1154 || varp->v_type == VAR_FLOAT)
1155 {
1156 typval_T tmp;
1157
1158 f_string(varp, &tmp);
1159 tv_get_string_buf(&tmp, buf);
1160 clear_tv(varp);
1161 *varp = tmp;
1162 return tmp.vval.v_string;
1163 }
1164 return tv_get_string_buf(varp, buf);
1165}
1166
1167/*
1168 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1169 * Also give an error message, using "name" or _("name") when use_gettext is
1170 * TRUE.
1171 */
1172 int
1173tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1174{
1175 int lock = 0;
1176
1177 switch (tv->v_type)
1178 {
1179 case VAR_BLOB:
1180 if (tv->vval.v_blob != NULL)
1181 lock = tv->vval.v_blob->bv_lock;
1182 break;
1183 case VAR_LIST:
1184 if (tv->vval.v_list != NULL)
1185 lock = tv->vval.v_list->lv_lock;
1186 break;
1187 case VAR_DICT:
1188 if (tv->vval.v_dict != NULL)
1189 lock = tv->vval.v_dict->dv_lock;
1190 break;
1191 default:
1192 break;
1193 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001194 return value_check_lock(tv->v_lock, name, use_gettext)
1195 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001196}
1197
1198/*
1199 * Copy the values from typval_T "from" to typval_T "to".
1200 * When needed allocates string or increases reference count.
1201 * Does not make a copy of a list, blob or dict but copies the reference!
1202 * It is OK for "from" and "to" to point to the same item. This is used to
1203 * make a copy later.
1204 */
1205 void
1206copy_tv(typval_T *from, typval_T *to)
1207{
1208 to->v_type = from->v_type;
1209 to->v_lock = 0;
1210 switch (from->v_type)
1211 {
1212 case VAR_NUMBER:
1213 case VAR_BOOL:
1214 case VAR_SPECIAL:
1215 to->vval.v_number = from->vval.v_number;
1216 break;
1217 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001218 to->vval.v_float = from->vval.v_float;
1219 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001220 case VAR_JOB:
1221#ifdef FEAT_JOB_CHANNEL
1222 to->vval.v_job = from->vval.v_job;
1223 if (to->vval.v_job != NULL)
1224 ++to->vval.v_job->jv_refcount;
1225 break;
1226#endif
1227 case VAR_CHANNEL:
1228#ifdef FEAT_JOB_CHANNEL
1229 to->vval.v_channel = from->vval.v_channel;
1230 if (to->vval.v_channel != NULL)
1231 ++to->vval.v_channel->ch_refcount;
1232 break;
1233#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001234 case VAR_INSTR:
1235 to->vval.v_instr = from->vval.v_instr;
1236 break;
1237
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001238 case VAR_CLASS:
1239 copy_class(from, to);
1240 break;
1241
1242 case VAR_OBJECT:
1243 copy_object(from, to);
1244 break;
1245
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001246 case VAR_STRING:
1247 case VAR_FUNC:
1248 if (from->vval.v_string == NULL)
1249 to->vval.v_string = NULL;
1250 else
1251 {
1252 to->vval.v_string = vim_strsave(from->vval.v_string);
1253 if (from->v_type == VAR_FUNC)
1254 func_ref(to->vval.v_string);
1255 }
1256 break;
1257 case VAR_PARTIAL:
1258 if (from->vval.v_partial == NULL)
1259 to->vval.v_partial = NULL;
1260 else
1261 {
1262 to->vval.v_partial = from->vval.v_partial;
1263 ++to->vval.v_partial->pt_refcount;
1264 }
1265 break;
1266 case VAR_BLOB:
1267 if (from->vval.v_blob == NULL)
1268 to->vval.v_blob = NULL;
1269 else
1270 {
1271 to->vval.v_blob = from->vval.v_blob;
1272 ++to->vval.v_blob->bv_refcount;
1273 }
1274 break;
1275 case VAR_LIST:
1276 if (from->vval.v_list == NULL)
1277 to->vval.v_list = NULL;
1278 else
1279 {
1280 to->vval.v_list = from->vval.v_list;
1281 ++to->vval.v_list->lv_refcount;
1282 }
1283 break;
1284 case VAR_DICT:
1285 if (from->vval.v_dict == NULL)
1286 to->vval.v_dict = NULL;
1287 else
1288 {
1289 to->vval.v_dict = from->vval.v_dict;
1290 ++to->vval.v_dict->dv_refcount;
1291 }
1292 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001293 case VAR_VOID:
1294 emsg(_(e_cannot_use_void_value));
1295 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001296 case VAR_UNKNOWN:
1297 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001298 internal_error_no_abort("copy_tv(UNKNOWN)");
1299 break;
1300 }
1301}
1302
1303/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001304 * Compare "tv1" and "tv2".
1305 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001306 */
1307 int
1308typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001309 typval_T *tv1, // first operand
1310 typval_T *tv2, // second operand
1311 exprtype_T type, // operator
1312 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001313{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001314 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001315 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001316 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1317
Bram Moolenaar265f8112021-12-19 12:33:05 +00001318 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001319 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001320 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001321 // it means TRUE.
1322 n1 = (type == EXPR_ISNOT);
1323 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001324 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1325 || (tv2->v_type == VAR_SPECIAL
1326 && tv2->vval.v_number == VVAL_NULL))
1327 && tv1->v_type != tv2->v_type
1328 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1329 {
1330 n1 = typval_compare_null(tv1, tv2);
1331 if (n1 == MAYBE)
1332 {
1333 clear_tv(tv1);
1334 return FAIL;
1335 }
1336 if (type == EXPR_NEQUAL)
1337 n1 = !n1;
1338 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001339 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001340 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001341 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001342 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001343 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001344 return FAIL;
1345 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001346 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001347 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001348 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001349 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001350 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001351 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001352 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001353 return FAIL;
1354 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001355 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001356 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001357 else if (tv1->v_type == VAR_CLASS || tv2->v_type == VAR_CLASS)
1358 {
1359 if (typval_compare_class(tv1, tv2, type, ic, &res) == FAIL)
1360 {
1361 clear_tv(tv1);
1362 return FAIL;
1363 }
1364 n1 = res;
1365 }
1366 else if (tv1->v_type == VAR_OBJECT || tv2->v_type == VAR_OBJECT)
1367 {
1368 if (typval_compare_object(tv1, tv2, type, ic, &res) == FAIL)
1369 {
1370 clear_tv(tv1);
1371 return FAIL;
1372 }
1373 n1 = res;
1374 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001375 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001376 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001377 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001378 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001379 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001380 return FAIL;
1381 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001382 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001383 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001384 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1385 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001386 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001387 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001388 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001389 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001390 return FAIL;
1391 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001392 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001393 }
1394
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001395 // If one of the two variables is a float, compare as a float.
1396 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001397 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001398 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1399 {
1400 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001401 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001402
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001403 f1 = tv_get_float_chk(tv1, &error);
1404 if (!error)
1405 f2 = tv_get_float_chk(tv2, &error);
1406 if (error)
1407 {
1408 clear_tv(tv1);
1409 return FAIL;
1410 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001411 n1 = FALSE;
1412 switch (type)
1413 {
1414 case EXPR_IS:
1415 case EXPR_EQUAL: n1 = (f1 == f2); break;
1416 case EXPR_ISNOT:
1417 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1418 case EXPR_GREATER: n1 = (f1 > f2); break;
1419 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1420 case EXPR_SMALLER: n1 = (f1 < f2); break;
1421 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1422 case EXPR_UNKNOWN:
1423 case EXPR_MATCH:
1424 default: break; // avoid gcc warning
1425 }
1426 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001427
1428 // If one of the two variables is a number, compare as a number.
1429 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001430 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001431 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1432 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001433 int error = FALSE;
1434
1435 n1 = tv_get_number_chk(tv1, &error);
1436 if (!error)
1437 n2 = tv_get_number_chk(tv2, &error);
1438 if (error)
1439 {
1440 clear_tv(tv1);
1441 return FAIL;
1442 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001443 switch (type)
1444 {
1445 case EXPR_IS:
1446 case EXPR_EQUAL: n1 = (n1 == n2); break;
1447 case EXPR_ISNOT:
1448 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1449 case EXPR_GREATER: n1 = (n1 > n2); break;
1450 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1451 case EXPR_SMALLER: n1 = (n1 < n2); break;
1452 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1453 case EXPR_UNKNOWN:
1454 case EXPR_MATCH:
1455 default: break; // avoid gcc warning
1456 }
1457 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001458 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1459 || tv2->v_type == VAR_BOOL
1460 || (tv1->v_type == VAR_SPECIAL
1461 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001462 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001463 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001464 {
1465 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001466 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1467 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001468 return FAIL;
1469 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001470 n1 = tv1->vval.v_number;
1471 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001472 switch (type)
1473 {
1474 case EXPR_IS:
1475 case EXPR_EQUAL: n1 = (n1 == n2); break;
1476 case EXPR_ISNOT:
1477 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1478 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001479 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001480 vartype_name(tv1->v_type));
1481 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001482 return FAIL;
1483 }
1484 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001485#ifdef FEAT_JOB_CHANNEL
1486 else if (tv1->v_type == tv2->v_type
1487 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1488 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1489 {
1490 if (tv1->v_type == VAR_CHANNEL)
1491 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1492 else
1493 n1 = tv1->vval.v_job == tv2->vval.v_job;
1494 if (type == EXPR_NEQUAL)
1495 n1 = !n1;
1496 }
1497#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001498 else
1499 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001500 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001501 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001502 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001503 return FAIL;
1504 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001505 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001506 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001507 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001508 if (in_vim9script())
1509 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001510 tv1->v_type = VAR_BOOL;
1511 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001512 }
1513 else
1514 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001515 tv1->v_type = VAR_NUMBER;
1516 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001517 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001518
1519 return OK;
1520}
1521
Bram Moolenaar34453202021-01-31 13:08:38 +01001522/*
dundargocc57b5bc2022-11-02 13:30:51 +00001523 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001524 * Put the result, false or true, in "res".
1525 * Return FAIL and give an error message when the comparison can't be done.
1526 */
1527 int
1528typval_compare_list(
1529 typval_T *tv1,
1530 typval_T *tv2,
1531 exprtype_T type,
1532 int ic,
1533 int *res)
1534{
1535 int val = 0;
1536
1537 if (type == EXPR_IS || type == EXPR_ISNOT)
1538 {
1539 val = (tv1->v_type == tv2->v_type
1540 && tv1->vval.v_list == tv2->vval.v_list);
1541 if (type == EXPR_ISNOT)
1542 val = !val;
1543 }
1544 else if (tv1->v_type != tv2->v_type
1545 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1546 {
1547 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001548 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001549 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001550 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001551 return FAIL;
1552 }
1553 else
1554 {
1555 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1556 ic, FALSE);
1557 if (type == EXPR_NEQUAL)
1558 val = !val;
1559 }
1560 *res = val;
1561 return OK;
1562}
1563
1564/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001565 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001566 */
1567 int
1568typval_compare_null(typval_T *tv1, typval_T *tv2)
1569{
1570 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1571 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1572 {
1573 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1574
1575 switch (tv->v_type)
1576 {
1577 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001578#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001579 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001580#endif
Ernie Rael5c018be2023-08-27 18:40:26 +02001581 // TODO: null_class handling
1582 // case VAR_CLASS: return tv->vval.v_class == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001583 case VAR_DICT: return tv->vval.v_dict == NULL;
1584 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001585#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001586 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001587#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001588 case VAR_LIST: return tv->vval.v_list == NULL;
Ernie Rael5c018be2023-08-27 18:40:26 +02001589 case VAR_OBJECT: return tv->vval.v_object == NULL;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001590 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1591 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001592
1593 case VAR_NUMBER: if (!in_vim9script())
1594 return tv->vval.v_number == 0;
1595 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001596 case VAR_FLOAT: if (!in_vim9script())
1597 return tv->vval.v_float == 0.0;
1598 break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001599 default: break;
1600 }
1601 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001602 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001603 // we won't give an error
1604 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001605}
1606
1607/*
dundargocc57b5bc2022-11-02 13:30:51 +00001608 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001609 * Put the result, false or true, in "res".
1610 * Return FAIL and give an error message when the comparison can't be done.
1611 */
1612 int
1613typval_compare_blob(
1614 typval_T *tv1,
1615 typval_T *tv2,
1616 exprtype_T type,
1617 int *res)
1618{
1619 int val = 0;
1620
1621 if (type == EXPR_IS || type == EXPR_ISNOT)
1622 {
1623 val = (tv1->v_type == tv2->v_type
1624 && tv1->vval.v_blob == tv2->vval.v_blob);
1625 if (type == EXPR_ISNOT)
1626 val = !val;
1627 }
1628 else if (tv1->v_type != tv2->v_type
1629 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1630 {
1631 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001632 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001633 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001634 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001635 return FAIL;
1636 }
1637 else
1638 {
1639 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1640 if (type == EXPR_NEQUAL)
1641 val = !val;
1642 }
1643 *res = val;
1644 return OK;
1645}
1646
1647/*
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001648 * Compare "tv1" to "tv2" as classes according to "type".
1649 * Put the result, false or true, in "res".
1650 * Return FAIL and give an error message when the comparison can't be done.
1651 */
1652 int
1653typval_compare_class(
1654 typval_T *tv1,
1655 typval_T *tv2,
1656 exprtype_T type UNUSED,
1657 int ic UNUSED,
1658 int *res)
1659{
1660 // TODO: use "type"
1661 *res = tv1->vval.v_class == tv2->vval.v_class;
1662 return OK;
1663}
1664
1665/*
1666 * Compare "tv1" to "tv2" as objects according to "type".
1667 * Put the result, false or true, in "res".
1668 * Return FAIL and give an error message when the comparison can't be done.
1669 */
1670 int
1671typval_compare_object(
1672 typval_T *tv1,
1673 typval_T *tv2,
1674 exprtype_T type,
1675 int ic,
1676 int *res)
1677{
1678 int res_match = type == EXPR_EQUAL || type == EXPR_IS ? TRUE : FALSE;
1679
1680 if (tv1->vval.v_object == NULL && tv2->vval.v_object == NULL)
1681 {
1682 *res = res_match;
1683 return OK;
1684 }
1685 if (tv1->vval.v_object == NULL || tv2->vval.v_object == NULL)
1686 {
1687 *res = !res_match;
1688 return OK;
1689 }
1690
1691 class_T *cl1 = tv1->vval.v_object->obj_class;
1692 class_T *cl2 = tv2->vval.v_object->obj_class;
1693 if (cl1 != cl2 || cl1 == NULL || cl2 == NULL)
1694 {
1695 *res = !res_match;
1696 return OK;
1697 }
1698
1699 object_T *obj1 = tv1->vval.v_object;
1700 object_T *obj2 = tv2->vval.v_object;
1701 if (type == EXPR_IS || type == EXPR_ISNOT)
1702 {
1703 *res = obj1 == obj2 ? res_match : !res_match;
1704 return OK;
1705 }
1706
1707 for (int i = 0; i < cl1->class_obj_member_count; ++i)
1708 if (!tv_equal((typval_T *)(obj1 + 1) + i,
1709 (typval_T *)(obj2 + 1) + i, ic, TRUE))
1710 {
1711 *res = !res_match;
1712 return OK;
1713 }
1714 *res = res_match;
1715 return OK;
1716}
1717
1718/*
dundargocc57b5bc2022-11-02 13:30:51 +00001719 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001720 * Put the result, false or true, in "res".
1721 * Return FAIL and give an error message when the comparison can't be done.
1722 */
1723 int
1724typval_compare_dict(
1725 typval_T *tv1,
1726 typval_T *tv2,
1727 exprtype_T type,
1728 int ic,
1729 int *res)
1730{
1731 int val;
1732
1733 if (type == EXPR_IS || type == EXPR_ISNOT)
1734 {
1735 val = (tv1->v_type == tv2->v_type
1736 && tv1->vval.v_dict == tv2->vval.v_dict);
1737 if (type == EXPR_ISNOT)
1738 val = !val;
1739 }
1740 else if (tv1->v_type != tv2->v_type
1741 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1742 {
1743 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001744 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001745 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001746 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001747 return FAIL;
1748 }
1749 else
1750 {
1751 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1752 if (type == EXPR_NEQUAL)
1753 val = !val;
1754 }
1755 *res = val;
1756 return OK;
1757}
1758
1759/*
dundargocc57b5bc2022-11-02 13:30:51 +00001760 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001761 * Put the result, false or true, in "res".
1762 * Return FAIL and give an error message when the comparison can't be done.
1763 */
1764 int
1765typval_compare_func(
1766 typval_T *tv1,
1767 typval_T *tv2,
1768 exprtype_T type,
1769 int ic,
1770 int *res)
1771{
1772 int val = 0;
1773
1774 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1775 && type != EXPR_IS && type != EXPR_ISNOT)
1776 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001777 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001778 return FAIL;
1779 }
1780 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1781 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1782 // When both partials are NULL, then they are equal.
1783 // Otherwise they are not equal.
1784 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1785 else if (type == EXPR_IS || type == EXPR_ISNOT)
1786 {
1787 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1788 // strings are considered the same if their value is
1789 // the same
1790 val = tv_equal(tv1, tv2, ic, FALSE);
1791 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1792 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1793 else
1794 val = FALSE;
1795 }
1796 else
1797 val = tv_equal(tv1, tv2, ic, FALSE);
1798 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1799 val = !val;
1800 *res = val;
1801 return OK;
1802}
1803
1804/*
1805 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1806 * Put the result, false or true, in "res".
1807 * Return FAIL and give an error message when the comparison can't be done.
1808 */
1809 int
1810typval_compare_string(
1811 typval_T *tv1,
1812 typval_T *tv2,
1813 exprtype_T type,
1814 int ic,
1815 int *res)
1816{
1817 int i = 0;
1818 int val = FALSE;
1819 char_u *s1, *s2;
1820 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1821
1822 if (in_vim9script()
1823 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1824 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1825 {
1826 semsg(_(e_cannot_compare_str_with_str),
1827 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1828 return FAIL;
1829 }
1830 s1 = tv_get_string_buf(tv1, buf1);
1831 s2 = tv_get_string_buf(tv2, buf2);
1832 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1833 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1834 switch (type)
1835 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001836 case EXPR_IS: if (in_vim9script())
1837 {
1838 // Really check it is the same string, not just
1839 // the same value.
1840 val = tv1->vval.v_string == tv2->vval.v_string;
1841 break;
1842 }
1843 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001844 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001845 case EXPR_ISNOT: if (in_vim9script())
1846 {
1847 // Really check it is not the same string, not
1848 // just a different value.
1849 val = tv1->vval.v_string != tv2->vval.v_string;
1850 break;
1851 }
1852 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001853 case EXPR_NEQUAL: val = (i != 0); break;
1854 case EXPR_GREATER: val = (i > 0); break;
1855 case EXPR_GEQUAL: val = (i >= 0); break;
1856 case EXPR_SMALLER: val = (i < 0); break;
1857 case EXPR_SEQUAL: val = (i <= 0); break;
1858
1859 case EXPR_MATCH:
1860 case EXPR_NOMATCH:
1861 val = pattern_match(s2, s1, ic);
1862 if (type == EXPR_NOMATCH)
1863 val = !val;
1864 break;
1865
1866 default: break; // avoid gcc warning
1867 }
1868 *res = val;
1869 return OK;
1870}
1871/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001872 * Convert any type to a string, never give an error.
1873 * When "quotes" is TRUE add quotes to a string.
1874 * Returns an allocated string.
1875 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001876 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001877typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001878{
1879 char_u *tofree;
1880 char_u numbuf[NUMBUFLEN];
1881 char_u *ret = NULL;
1882
1883 if (arg == NULL)
1884 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001885 if (!quotes && arg->v_type == VAR_STRING)
1886 {
1887 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1888 : arg->vval.v_string);
1889 }
1890 else
1891 {
1892 ret = tv2string(arg, &tofree, numbuf, 0);
1893 // Make a copy if we have a value but it's not in allocated memory.
1894 if (ret != NULL && tofree == NULL)
1895 ret = vim_strsave(ret);
1896 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001897 return ret;
1898}
1899
1900/*
1901 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1902 * or it refers to a List or Dictionary that is locked.
1903 */
1904 int
1905tv_islocked(typval_T *tv)
1906{
1907 return (tv->v_lock & VAR_LOCKED)
1908 || (tv->v_type == VAR_LIST
1909 && tv->vval.v_list != NULL
1910 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1911 || (tv->v_type == VAR_DICT
1912 && tv->vval.v_dict != NULL
1913 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1914}
1915
1916 static int
1917func_equal(
1918 typval_T *tv1,
1919 typval_T *tv2,
1920 int ic) // ignore case
1921{
1922 char_u *s1, *s2;
1923 dict_T *d1, *d2;
1924 int a1, a2;
1925 int i;
1926
1927 // empty and NULL function name considered the same
1928 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1929 : partial_name(tv1->vval.v_partial);
1930 if (s1 != NULL && *s1 == NUL)
1931 s1 = NULL;
1932 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1933 : partial_name(tv2->vval.v_partial);
1934 if (s2 != NULL && *s2 == NUL)
1935 s2 = NULL;
1936 if (s1 == NULL || s2 == NULL)
1937 {
1938 if (s1 != s2)
1939 return FALSE;
1940 }
1941 else if (STRCMP(s1, s2) != 0)
1942 return FALSE;
1943
1944 // empty dict and NULL dict is different
1945 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1946 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1947 if (d1 == NULL || d2 == NULL)
1948 {
1949 if (d1 != d2)
1950 return FALSE;
1951 }
1952 else if (!dict_equal(d1, d2, ic, TRUE))
1953 return FALSE;
1954
1955 // empty list and no list considered the same
1956 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1957 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1958 if (a1 != a2)
1959 return FALSE;
1960 for (i = 0; i < a1; ++i)
1961 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1962 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1963 return FALSE;
1964
1965 return TRUE;
1966}
1967
1968/*
1969 * Return TRUE if "tv1" and "tv2" have the same value.
1970 * Compares the items just like "==" would compare them, but strings and
1971 * numbers are different. Floats and numbers are also different.
1972 */
1973 int
1974tv_equal(
1975 typval_T *tv1,
1976 typval_T *tv2,
1977 int ic, // ignore case
1978 int recursive) // TRUE when used recursively
1979{
1980 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1981 char_u *s1, *s2;
1982 static int recursive_cnt = 0; // catch recursive loops
1983 int r;
1984 static int tv_equal_recurse_limit;
1985
1986 // Catch lists and dicts that have an endless loop by limiting
1987 // recursiveness to a limit. We guess they are equal then.
1988 // A fixed limit has the problem of still taking an awful long time.
1989 // Reduce the limit every time running into it. That should work fine for
1990 // deeply linked structures that are not recursively linked and catch
1991 // recursiveness quickly.
1992 if (!recursive)
1993 tv_equal_recurse_limit = 1000;
1994 if (recursive_cnt >= tv_equal_recurse_limit)
1995 {
1996 --tv_equal_recurse_limit;
1997 return TRUE;
1998 }
1999
2000 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
2001 // arguments.
2002 if ((tv1->v_type == VAR_FUNC
2003 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
2004 && (tv2->v_type == VAR_FUNC
2005 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
2006 {
2007 ++recursive_cnt;
2008 r = func_equal(tv1, tv2, ic);
2009 --recursive_cnt;
2010 return r;
2011 }
2012
Bram Moolenaar418a29f2021-02-10 22:23:41 +01002013 if (tv1->v_type != tv2->v_type
2014 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
2015 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002016 return FALSE;
2017
2018 switch (tv1->v_type)
2019 {
2020 case VAR_LIST:
2021 ++recursive_cnt;
2022 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
2023 --recursive_cnt;
2024 return r;
2025
2026 case VAR_DICT:
2027 ++recursive_cnt;
2028 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
2029 --recursive_cnt;
2030 return r;
2031
2032 case VAR_BLOB:
2033 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
2034
2035 case VAR_NUMBER:
2036 case VAR_BOOL:
2037 case VAR_SPECIAL:
2038 return tv1->vval.v_number == tv2->vval.v_number;
2039
2040 case VAR_STRING:
2041 s1 = tv_get_string_buf(tv1, buf1);
2042 s2 = tv_get_string_buf(tv2, buf2);
2043 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
2044
2045 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002046 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002047 case VAR_JOB:
2048#ifdef FEAT_JOB_CHANNEL
2049 return tv1->vval.v_job == tv2->vval.v_job;
2050#endif
2051 case VAR_CHANNEL:
2052#ifdef FEAT_JOB_CHANNEL
2053 return tv1->vval.v_channel == tv2->vval.v_channel;
2054#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002055 case VAR_INSTR:
2056 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002057
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002058 case VAR_CLASS:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002059 // A class only exists once, equality is identity.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002060 return tv1->vval.v_class == tv2->vval.v_class;
2061
2062 case VAR_OBJECT:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002063 (void)typval_compare_object(tv1, tv2, EXPR_EQUAL, ic, &r);
2064 return r;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002065
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002066 case VAR_PARTIAL:
2067 return tv1->vval.v_partial == tv2->vval.v_partial;
2068
2069 case VAR_FUNC:
2070 return tv1->vval.v_string == tv2->vval.v_string;
2071
2072 case VAR_UNKNOWN:
2073 case VAR_ANY:
2074 case VAR_VOID:
2075 break;
2076 }
2077
2078 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
2079 // does not equal anything, not even itself.
2080 return FALSE;
2081}
2082
2083/*
2084 * Get an option value.
2085 * "arg" points to the '&' or '+' before the option name.
2086 * "arg" is advanced to character after the option name.
2087 * Return OK or FAIL.
2088 */
2089 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002090eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002091 char_u **arg,
2092 typval_T *rettv, // when NULL, only check if option exists
2093 int evaluate)
2094{
2095 char_u *option_end;
2096 long numval;
2097 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002098 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002099 int c;
2100 int working = (**arg == '+'); // has("+option")
2101 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002102 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002103
2104 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002105 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002106 if (option_end == NULL)
2107 {
2108 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002109 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002110 return FAIL;
2111 }
2112
2113 if (!evaluate)
2114 {
2115 *arg = option_end;
2116 return OK;
2117 }
2118
2119 c = *option_end;
2120 *option_end = NUL;
2121 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002122 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002123
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002124 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002125 {
2126 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002127 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002128 ret = FAIL;
2129 }
2130 else if (rettv != NULL)
2131 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01002132 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002133 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002134 {
2135 rettv->v_type = VAR_STRING;
2136 rettv->vval.v_string = NULL;
2137 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002138 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002139 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002140 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2141 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002142 rettv->vval.v_number = 0;
2143 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002144 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002145 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002146 if (in_vim9script() && opt_type == gov_bool)
2147 {
2148 rettv->v_type = VAR_BOOL;
2149 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2150 }
2151 else
2152 {
2153 rettv->v_type = VAR_NUMBER;
2154 rettv->vval.v_number = numval;
2155 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002156 }
2157 else // string option
2158 {
2159 rettv->v_type = VAR_STRING;
2160 rettv->vval.v_string = stringval;
2161 }
2162 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002163 else if (working && (opt_type == gov_hidden_bool
2164 || opt_type == gov_hidden_number
2165 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002166 ret = FAIL;
2167
2168 *option_end = c; // put back for error messages
2169 *arg = option_end;
2170
2171 return ret;
2172}
2173
2174/*
2175 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2176 * Return OK or FAIL.
2177 */
2178 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002179eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002180 char_u **arg,
2181 typval_T *rettv,
2182 int evaluate,
2183 int want_string UNUSED)
2184{
2185 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002186 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002187 char_u *p;
2188 int get_float = FALSE;
2189
2190 // We accept a float when the format matches
2191 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2192 // strict to avoid backwards compatibility problems.
2193 // With script version 2 and later the leading digit can be
2194 // omitted.
2195 // Don't look for a float after the "." operator, so that
2196 // ":let vers = 1.2.3" doesn't fail.
2197 if (**arg == '.')
2198 p = *arg;
2199 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002200 {
2201 p = *arg + 1;
2202 if (skip_quotes)
2203 for (;;)
2204 {
2205 if (*p == '\'')
2206 ++p;
2207 if (!vim_isdigit(*p))
2208 break;
2209 p = skipdigits(p);
2210 }
2211 else
2212 p = skipdigits(p);
2213 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002214 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2215 {
2216 get_float = TRUE;
2217 p = skipdigits(p + 2);
2218 if (*p == 'e' || *p == 'E')
2219 {
2220 ++p;
2221 if (*p == '-' || *p == '+')
2222 ++p;
2223 if (!vim_isdigit(*p))
2224 get_float = FALSE;
2225 else
2226 p = skipdigits(p + 1);
2227 }
2228 if (ASCII_ISALPHA(*p) || *p == '.')
2229 get_float = FALSE;
2230 }
2231 if (get_float)
2232 {
2233 float_T f;
2234
Bram Moolenaar29500652021-08-08 15:43:34 +02002235 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002236 if (evaluate)
2237 {
2238 rettv->v_type = VAR_FLOAT;
2239 rettv->vval.v_float = f;
2240 }
2241 }
2242 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002243 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2244 {
2245 char_u *bp;
2246 blob_T *blob = NULL; // init for gcc
2247
2248 // Blob constant: 0z0123456789abcdef
2249 if (evaluate)
2250 blob = blob_alloc();
2251 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2252 {
2253 if (!vim_isxdigit(bp[1]))
2254 {
2255 if (blob != NULL)
2256 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002257 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002258 ga_clear(&blob->bv_ga);
2259 VIM_CLEAR(blob);
2260 }
2261 return FAIL;
2262 }
2263 if (blob != NULL)
2264 ga_append(&blob->bv_ga,
2265 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2266 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2267 ++bp;
2268 }
2269 if (blob != NULL)
2270 rettv_blob_set(rettv, blob);
2271 *arg = bp;
2272 }
2273 else
2274 {
2275 varnumber_T n;
2276
2277 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002278 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002279 ? STR2NR_NO_OCT + STR2NR_QUOTE
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002280 : STR2NR_ALL, &n, NULL, 0, TRUE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002281 if (len == 0)
2282 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002283 if (evaluate)
2284 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002285 return FAIL;
2286 }
2287 *arg += len;
2288 if (evaluate)
2289 {
2290 rettv->v_type = VAR_NUMBER;
2291 rettv->vval.v_number = n;
2292 }
2293 }
2294 return OK;
2295}
2296
2297/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002298 * Evaluate a string constant and put the result in "rettv".
2299 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2300 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2301 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002302 * Return OK or FAIL.
2303 */
2304 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002305eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002306{
2307 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002308 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002309 int extra = interpolate ? 1 : 0;
2310 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002311 int len;
2312
2313 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002314 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002315 {
2316 if (*p == '\\' && p[1] != NUL)
2317 {
2318 ++p;
2319 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002320 // to 9 characters (6 for the char and 3 for a modifier):
2321 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002322 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002323 {
2324 int modifiers = 0;
2325 int flags = FSK_KEYCODE | FSK_IN_STRING;
2326
zeertzjqdb088872022-05-02 22:53:45 +01002327 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002328
2329 // Skip to the '>' to avoid using '{' inside for string
2330 // interpolation.
2331 if (p[1] != '*')
2332 flags |= FSK_SIMPLIFY;
2333 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2334 --p; // leave "p" on the ">"
2335 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002336 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002337 else if (interpolate && (*p == '{' || *p == '}'))
2338 {
2339 if (*p == '{' && p[1] != '{') // start of expression
2340 break;
2341 ++p;
2342 if (p[-1] == '}' && *p != '}') // single '}' is an error
2343 {
2344 semsg(_(e_stray_closing_curly_str), *arg);
2345 return FAIL;
2346 }
2347 --extra; // "{{" becomes "{", "}}" becomes "}"
2348 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002349 }
2350
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002351 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002352 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002353 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002354 return FAIL;
2355 }
2356
2357 // If only parsing, set *arg and return here
2358 if (!evaluate)
2359 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002360 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002361 return OK;
2362 }
2363
2364 // Copy the string into allocated memory, handling backslashed
2365 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002366 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002367 len = (int)(p - *arg + extra);
2368 rettv->vval.v_string = alloc(len);
2369 if (rettv->vval.v_string == NULL)
2370 return FAIL;
2371 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002372
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002373 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002374 {
2375 if (*p == '\\')
2376 {
2377 switch (*++p)
2378 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002379 case 'b': *end++ = BS; ++p; break;
2380 case 'e': *end++ = ESC; ++p; break;
2381 case 'f': *end++ = FF; ++p; break;
2382 case 'n': *end++ = NL; ++p; break;
2383 case 'r': *end++ = CAR; ++p; break;
2384 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002385
2386 case 'X': // hex: "\x1", "\x12"
2387 case 'x':
2388 case 'u': // Unicode: "\u0023"
2389 case 'U':
2390 if (vim_isxdigit(p[1]))
2391 {
2392 int n, nr;
2393 int c = toupper(*p);
2394
2395 if (c == 'X')
2396 n = 2;
2397 else if (*p == 'u')
2398 n = 4;
2399 else
2400 n = 8;
2401 nr = 0;
2402 while (--n >= 0 && vim_isxdigit(p[1]))
2403 {
2404 ++p;
2405 nr = (nr << 4) + hex2nr(*p);
2406 }
2407 ++p;
2408 // For "\u" store the number according to
2409 // 'encoding'.
2410 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002411 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002412 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002413 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002414 }
2415 break;
2416
2417 // octal: "\1", "\12", "\123"
2418 case '0':
2419 case '1':
2420 case '2':
2421 case '3':
2422 case '4':
2423 case '5':
2424 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002425 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002426 if (*p >= '0' && *p <= '7')
2427 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002428 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002429 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002430 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002431 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002432 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002433 break;
2434
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002435 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002436 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002437 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002438 int flags = FSK_KEYCODE | FSK_IN_STRING;
2439
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002440 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002441 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002442 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002443 if (extra != 0)
2444 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002445 end += extra;
2446 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002447 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002448 break;
2449 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002450 }
2451 // FALLTHROUGH
2452
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002453 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002454 break;
2455 }
2456 }
2457 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002458 {
2459 if (interpolate && (*p == '{' || *p == '}'))
2460 {
2461 if (*p == '{' && p[1] != '{') // start of expression
2462 break;
2463 ++p; // reduce "{{" to "{" and "}}" to "}"
2464 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002465 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002466 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002467 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002468 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002469 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002470 ++p;
2471 *arg = p;
2472
2473 return OK;
2474}
2475
2476/*
2477 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002478 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2479 * Return OK when a "rettv" was set to the string.
2480 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002481 */
2482 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002483eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002484{
2485 char_u *p;
2486 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002487 int reduce = interpolate ? -1 : 0;
2488 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002489
2490 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002491 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002492 {
2493 if (*p == '\'')
2494 {
2495 if (p[1] != '\'')
2496 break;
2497 ++reduce;
2498 ++p;
2499 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002500 else if (interpolate)
2501 {
2502 if (*p == '{')
2503 {
2504 if (p[1] != '{')
2505 break;
2506 ++p;
2507 ++reduce;
2508 }
2509 else if (*p == '}')
2510 {
2511 ++p;
2512 if (*p != '}')
2513 {
2514 semsg(_(e_stray_closing_curly_str), *arg);
2515 return FAIL;
2516 }
2517 ++reduce;
2518 }
2519 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002520 }
2521
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002522 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002523 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002524 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002525 return FAIL;
2526 }
2527
2528 // If only parsing return after setting "*arg"
2529 if (!evaluate)
2530 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002531 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002532 return OK;
2533 }
2534
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002535 // Copy the string into allocated memory, handling '' to ' reduction and
2536 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002537 str = alloc((p - *arg) - reduce);
2538 if (str == NULL)
2539 return FAIL;
2540 rettv->v_type = VAR_STRING;
2541 rettv->vval.v_string = str;
2542
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002543 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002544 {
2545 if (*p == '\'')
2546 {
2547 if (p[1] != '\'')
2548 break;
2549 ++p;
2550 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002551 else if (interpolate && (*p == '{' || *p == '}'))
2552 {
2553 if (*p == '{' && p[1] != '{')
2554 break;
2555 ++p;
2556 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002557 MB_COPY_CHAR(p, str);
2558 }
2559 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002560 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002561
2562 return OK;
2563}
2564
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002565/*
2566 * Evaluate a single or double quoted string possibly containing expressions.
2567 * "arg" points to the '$'. The result is put in "rettv".
2568 * Returns OK or FAIL.
2569 */
LemonBoy2eaef102022-05-06 13:14:50 +01002570 int
2571eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2572{
2573 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002574 int ret = OK;
2575 int quote;
2576 garray_T ga;
2577 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002578
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002579 ga_init2(&ga, 1, 80);
2580
2581 // *arg is on the '$' character, move it to the first string character.
2582 ++*arg;
2583 quote = **arg;
2584 ++*arg;
2585
2586 for (;;)
2587 {
2588 // Get the string up to the matching quote or to a single '{'.
2589 // "arg" is advanced to either the quote or the '{'.
2590 if (quote == '"')
2591 ret = eval_string(arg, &tv, evaluate, TRUE);
2592 else
2593 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2594 if (ret == FAIL)
2595 break;
2596 if (evaluate)
2597 {
2598 ga_concat(&ga, tv.vval.v_string);
2599 clear_tv(&tv);
2600 }
2601
2602 if (**arg != '{')
2603 {
2604 // found terminating quote
2605 ++*arg;
2606 break;
2607 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002608 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002609 if (p == NULL)
2610 {
2611 ret = FAIL;
2612 break;
2613 }
2614 *arg = p;
2615 }
LemonBoy2eaef102022-05-06 13:14:50 +01002616
2617 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002618 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2619 {
2620 ga_clear(&ga);
2621 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002622 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002623 }
LemonBoy2eaef102022-05-06 13:14:50 +01002624
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002625 rettv->vval.v_string = ga.ga_data;
2626 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002627}
2628
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002629/*
2630 * Return a string with the string representation of a variable.
2631 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2632 * "numbuf" is used for a number.
2633 * Puts quotes around strings, so that they can be parsed back by eval().
2634 * May return NULL.
2635 */
2636 char_u *
2637tv2string(
2638 typval_T *tv,
2639 char_u **tofree,
2640 char_u *numbuf,
2641 int copyID)
2642{
2643 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2644}
2645
2646/*
2647 * Get the value of an environment variable.
2648 * "arg" is pointing to the '$'. It is advanced to after the name.
2649 * If the environment variable was not set, silently assume it is empty.
2650 * Return FAIL if the name is invalid.
2651 */
2652 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002653eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002654{
2655 char_u *string = NULL;
2656 int len;
2657 int cc;
2658 char_u *name;
2659 int mustfree = FALSE;
2660
2661 ++*arg;
2662 name = *arg;
2663 len = get_env_len(arg);
2664 if (evaluate)
2665 {
2666 if (len == 0)
2667 return FAIL; // invalid empty name
2668
2669 cc = name[len];
2670 name[len] = NUL;
2671 // first try vim_getenv(), fast for normal environment vars
2672 string = vim_getenv(name, &mustfree);
2673 if (string != NULL && *string != NUL)
2674 {
2675 if (!mustfree)
2676 string = vim_strsave(string);
2677 }
2678 else
2679 {
2680 if (mustfree)
2681 vim_free(string);
2682
2683 // next try expanding things like $VIM and ${HOME}
2684 string = expand_env_save(name - 1);
2685 if (string != NULL && *string == '$')
2686 VIM_CLEAR(string);
2687 }
2688 name[len] = cc;
2689
2690 rettv->v_type = VAR_STRING;
2691 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002692 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002693 }
2694
2695 return OK;
2696}
2697
2698/*
2699 * Get the lnum from the first argument.
2700 * Also accepts ".", "$", etc., but that only works for the current buffer.
2701 * Returns -1 on error.
2702 */
2703 linenr_T
2704tv_get_lnum(typval_T *argvars)
2705{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002706 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002707 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002708
Bram Moolenaar56acb092020-08-16 14:48:19 +02002709 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2710 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002711 if (lnum <= 0 && did_emsg_before == did_emsg
2712 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002713 {
2714 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002715 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002716
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002717 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002718 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002719 if (fp != NULL)
2720 lnum = fp->lnum;
2721 }
2722 return lnum;
2723}
2724
2725/*
2726 * Get the lnum from the first argument.
2727 * Also accepts "$", then "buf" is used.
2728 * Returns 0 on error.
2729 */
2730 linenr_T
2731tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2732{
2733 if (argvars[0].v_type == VAR_STRING
2734 && argvars[0].vval.v_string != NULL
2735 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002736 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002737 && buf != NULL)
2738 return buf->b_ml.ml_line_count;
2739 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2740}
2741
2742/*
2743 * Get buffer by number or pattern.
2744 */
2745 buf_T *
2746tv_get_buf(typval_T *tv, int curtab_only)
2747{
2748 char_u *name = tv->vval.v_string;
2749 buf_T *buf;
2750
2751 if (tv->v_type == VAR_NUMBER)
2752 return buflist_findnr((int)tv->vval.v_number);
2753 if (tv->v_type != VAR_STRING)
2754 return NULL;
2755 if (name == NULL || *name == NUL)
2756 return curbuf;
2757 if (name[0] == '$' && name[1] == NUL)
2758 return lastbuf;
2759
2760 buf = buflist_find_by_name(name, curtab_only);
2761
2762 // If not found, try expanding the name, like done for bufexists().
2763 if (buf == NULL)
2764 buf = find_buffer(tv);
2765
2766 return buf;
2767}
2768
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002769/*
2770 * Like tv_get_buf() but give an error message is the type is wrong.
2771 */
2772 buf_T *
2773tv_get_buf_from_arg(typval_T *tv)
2774{
2775 buf_T *buf;
2776
2777 ++emsg_off;
2778 buf = tv_get_buf(tv, FALSE);
2779 --emsg_off;
2780 if (buf == NULL
2781 && tv->v_type != VAR_NUMBER
2782 && tv->v_type != VAR_STRING)
2783 // issue errmsg for type error
2784 (void)tv_get_number(tv);
2785 return buf;
2786}
2787
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002788#endif // FEAT_EVAL