blob: e8aebee54654ada63cf6d4847a09e202a4e14456 [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 Lakshmanana9a7c0c2021-07-17 19:11:07 +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 {
984 semsg(_(e_object_required_for_argument_nr), idx + 1);
985 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 {
998 semsg(_(e_list_or_class_required_for_argument_nr), idx + 1);
999 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
Bram Moolenaar7a222242022-03-01 19:23:24 +00001581 case VAR_DICT: return tv->vval.v_dict == NULL;
1582 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001583#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001584 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001585#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001586 case VAR_LIST: return tv->vval.v_list == NULL;
1587 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1588 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001589
1590 case VAR_NUMBER: if (!in_vim9script())
1591 return tv->vval.v_number == 0;
1592 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001593 case VAR_FLOAT: if (!in_vim9script())
1594 return tv->vval.v_float == 0.0;
1595 break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001596 default: break;
1597 }
1598 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001599 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001600 // we won't give an error
1601 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001602}
1603
1604/*
dundargocc57b5bc2022-11-02 13:30:51 +00001605 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001606 * Put the result, false or true, in "res".
1607 * Return FAIL and give an error message when the comparison can't be done.
1608 */
1609 int
1610typval_compare_blob(
1611 typval_T *tv1,
1612 typval_T *tv2,
1613 exprtype_T type,
1614 int *res)
1615{
1616 int val = 0;
1617
1618 if (type == EXPR_IS || type == EXPR_ISNOT)
1619 {
1620 val = (tv1->v_type == tv2->v_type
1621 && tv1->vval.v_blob == tv2->vval.v_blob);
1622 if (type == EXPR_ISNOT)
1623 val = !val;
1624 }
1625 else if (tv1->v_type != tv2->v_type
1626 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1627 {
1628 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001629 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001630 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001631 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001632 return FAIL;
1633 }
1634 else
1635 {
1636 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1637 if (type == EXPR_NEQUAL)
1638 val = !val;
1639 }
1640 *res = val;
1641 return OK;
1642}
1643
1644/*
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001645 * Compare "tv1" to "tv2" as classes according to "type".
1646 * Put the result, false or true, in "res".
1647 * Return FAIL and give an error message when the comparison can't be done.
1648 */
1649 int
1650typval_compare_class(
1651 typval_T *tv1,
1652 typval_T *tv2,
1653 exprtype_T type UNUSED,
1654 int ic UNUSED,
1655 int *res)
1656{
1657 // TODO: use "type"
1658 *res = tv1->vval.v_class == tv2->vval.v_class;
1659 return OK;
1660}
1661
1662/*
1663 * Compare "tv1" to "tv2" as objects according to "type".
1664 * Put the result, false or true, in "res".
1665 * Return FAIL and give an error message when the comparison can't be done.
1666 */
1667 int
1668typval_compare_object(
1669 typval_T *tv1,
1670 typval_T *tv2,
1671 exprtype_T type,
1672 int ic,
1673 int *res)
1674{
1675 int res_match = type == EXPR_EQUAL || type == EXPR_IS ? TRUE : FALSE;
1676
1677 if (tv1->vval.v_object == NULL && tv2->vval.v_object == NULL)
1678 {
1679 *res = res_match;
1680 return OK;
1681 }
1682 if (tv1->vval.v_object == NULL || tv2->vval.v_object == NULL)
1683 {
1684 *res = !res_match;
1685 return OK;
1686 }
1687
1688 class_T *cl1 = tv1->vval.v_object->obj_class;
1689 class_T *cl2 = tv2->vval.v_object->obj_class;
1690 if (cl1 != cl2 || cl1 == NULL || cl2 == NULL)
1691 {
1692 *res = !res_match;
1693 return OK;
1694 }
1695
1696 object_T *obj1 = tv1->vval.v_object;
1697 object_T *obj2 = tv2->vval.v_object;
1698 if (type == EXPR_IS || type == EXPR_ISNOT)
1699 {
1700 *res = obj1 == obj2 ? res_match : !res_match;
1701 return OK;
1702 }
1703
1704 for (int i = 0; i < cl1->class_obj_member_count; ++i)
1705 if (!tv_equal((typval_T *)(obj1 + 1) + i,
1706 (typval_T *)(obj2 + 1) + i, ic, TRUE))
1707 {
1708 *res = !res_match;
1709 return OK;
1710 }
1711 *res = res_match;
1712 return OK;
1713}
1714
1715/*
dundargocc57b5bc2022-11-02 13:30:51 +00001716 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001717 * Put the result, false or true, in "res".
1718 * Return FAIL and give an error message when the comparison can't be done.
1719 */
1720 int
1721typval_compare_dict(
1722 typval_T *tv1,
1723 typval_T *tv2,
1724 exprtype_T type,
1725 int ic,
1726 int *res)
1727{
1728 int val;
1729
1730 if (type == EXPR_IS || type == EXPR_ISNOT)
1731 {
1732 val = (tv1->v_type == tv2->v_type
1733 && tv1->vval.v_dict == tv2->vval.v_dict);
1734 if (type == EXPR_ISNOT)
1735 val = !val;
1736 }
1737 else if (tv1->v_type != tv2->v_type
1738 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1739 {
1740 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001741 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001742 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001743 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001744 return FAIL;
1745 }
1746 else
1747 {
1748 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1749 if (type == EXPR_NEQUAL)
1750 val = !val;
1751 }
1752 *res = val;
1753 return OK;
1754}
1755
1756/*
dundargocc57b5bc2022-11-02 13:30:51 +00001757 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001758 * Put the result, false or true, in "res".
1759 * Return FAIL and give an error message when the comparison can't be done.
1760 */
1761 int
1762typval_compare_func(
1763 typval_T *tv1,
1764 typval_T *tv2,
1765 exprtype_T type,
1766 int ic,
1767 int *res)
1768{
1769 int val = 0;
1770
1771 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1772 && type != EXPR_IS && type != EXPR_ISNOT)
1773 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001774 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001775 return FAIL;
1776 }
1777 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1778 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1779 // When both partials are NULL, then they are equal.
1780 // Otherwise they are not equal.
1781 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1782 else if (type == EXPR_IS || type == EXPR_ISNOT)
1783 {
1784 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1785 // strings are considered the same if their value is
1786 // the same
1787 val = tv_equal(tv1, tv2, ic, FALSE);
1788 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1789 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1790 else
1791 val = FALSE;
1792 }
1793 else
1794 val = tv_equal(tv1, tv2, ic, FALSE);
1795 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1796 val = !val;
1797 *res = val;
1798 return OK;
1799}
1800
1801/*
1802 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1803 * Put the result, false or true, in "res".
1804 * Return FAIL and give an error message when the comparison can't be done.
1805 */
1806 int
1807typval_compare_string(
1808 typval_T *tv1,
1809 typval_T *tv2,
1810 exprtype_T type,
1811 int ic,
1812 int *res)
1813{
1814 int i = 0;
1815 int val = FALSE;
1816 char_u *s1, *s2;
1817 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1818
1819 if (in_vim9script()
1820 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1821 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1822 {
1823 semsg(_(e_cannot_compare_str_with_str),
1824 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1825 return FAIL;
1826 }
1827 s1 = tv_get_string_buf(tv1, buf1);
1828 s2 = tv_get_string_buf(tv2, buf2);
1829 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1830 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1831 switch (type)
1832 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001833 case EXPR_IS: if (in_vim9script())
1834 {
1835 // Really check it is the same string, not just
1836 // the same value.
1837 val = tv1->vval.v_string == tv2->vval.v_string;
1838 break;
1839 }
1840 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001841 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001842 case EXPR_ISNOT: if (in_vim9script())
1843 {
1844 // Really check it is not the same string, not
1845 // just a different value.
1846 val = tv1->vval.v_string != tv2->vval.v_string;
1847 break;
1848 }
1849 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001850 case EXPR_NEQUAL: val = (i != 0); break;
1851 case EXPR_GREATER: val = (i > 0); break;
1852 case EXPR_GEQUAL: val = (i >= 0); break;
1853 case EXPR_SMALLER: val = (i < 0); break;
1854 case EXPR_SEQUAL: val = (i <= 0); break;
1855
1856 case EXPR_MATCH:
1857 case EXPR_NOMATCH:
1858 val = pattern_match(s2, s1, ic);
1859 if (type == EXPR_NOMATCH)
1860 val = !val;
1861 break;
1862
1863 default: break; // avoid gcc warning
1864 }
1865 *res = val;
1866 return OK;
1867}
1868/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001869 * Convert any type to a string, never give an error.
1870 * When "quotes" is TRUE add quotes to a string.
1871 * Returns an allocated string.
1872 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001873 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001874typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001875{
1876 char_u *tofree;
1877 char_u numbuf[NUMBUFLEN];
1878 char_u *ret = NULL;
1879
1880 if (arg == NULL)
1881 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001882 if (!quotes && arg->v_type == VAR_STRING)
1883 {
1884 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1885 : arg->vval.v_string);
1886 }
1887 else
1888 {
1889 ret = tv2string(arg, &tofree, numbuf, 0);
1890 // Make a copy if we have a value but it's not in allocated memory.
1891 if (ret != NULL && tofree == NULL)
1892 ret = vim_strsave(ret);
1893 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001894 return ret;
1895}
1896
1897/*
1898 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1899 * or it refers to a List or Dictionary that is locked.
1900 */
1901 int
1902tv_islocked(typval_T *tv)
1903{
1904 return (tv->v_lock & VAR_LOCKED)
1905 || (tv->v_type == VAR_LIST
1906 && tv->vval.v_list != NULL
1907 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1908 || (tv->v_type == VAR_DICT
1909 && tv->vval.v_dict != NULL
1910 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1911}
1912
1913 static int
1914func_equal(
1915 typval_T *tv1,
1916 typval_T *tv2,
1917 int ic) // ignore case
1918{
1919 char_u *s1, *s2;
1920 dict_T *d1, *d2;
1921 int a1, a2;
1922 int i;
1923
1924 // empty and NULL function name considered the same
1925 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1926 : partial_name(tv1->vval.v_partial);
1927 if (s1 != NULL && *s1 == NUL)
1928 s1 = NULL;
1929 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1930 : partial_name(tv2->vval.v_partial);
1931 if (s2 != NULL && *s2 == NUL)
1932 s2 = NULL;
1933 if (s1 == NULL || s2 == NULL)
1934 {
1935 if (s1 != s2)
1936 return FALSE;
1937 }
1938 else if (STRCMP(s1, s2) != 0)
1939 return FALSE;
1940
1941 // empty dict and NULL dict is different
1942 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1943 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1944 if (d1 == NULL || d2 == NULL)
1945 {
1946 if (d1 != d2)
1947 return FALSE;
1948 }
1949 else if (!dict_equal(d1, d2, ic, TRUE))
1950 return FALSE;
1951
1952 // empty list and no list considered the same
1953 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1954 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1955 if (a1 != a2)
1956 return FALSE;
1957 for (i = 0; i < a1; ++i)
1958 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1959 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1960 return FALSE;
1961
1962 return TRUE;
1963}
1964
1965/*
1966 * Return TRUE if "tv1" and "tv2" have the same value.
1967 * Compares the items just like "==" would compare them, but strings and
1968 * numbers are different. Floats and numbers are also different.
1969 */
1970 int
1971tv_equal(
1972 typval_T *tv1,
1973 typval_T *tv2,
1974 int ic, // ignore case
1975 int recursive) // TRUE when used recursively
1976{
1977 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1978 char_u *s1, *s2;
1979 static int recursive_cnt = 0; // catch recursive loops
1980 int r;
1981 static int tv_equal_recurse_limit;
1982
1983 // Catch lists and dicts that have an endless loop by limiting
1984 // recursiveness to a limit. We guess they are equal then.
1985 // A fixed limit has the problem of still taking an awful long time.
1986 // Reduce the limit every time running into it. That should work fine for
1987 // deeply linked structures that are not recursively linked and catch
1988 // recursiveness quickly.
1989 if (!recursive)
1990 tv_equal_recurse_limit = 1000;
1991 if (recursive_cnt >= tv_equal_recurse_limit)
1992 {
1993 --tv_equal_recurse_limit;
1994 return TRUE;
1995 }
1996
1997 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1998 // arguments.
1999 if ((tv1->v_type == VAR_FUNC
2000 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
2001 && (tv2->v_type == VAR_FUNC
2002 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
2003 {
2004 ++recursive_cnt;
2005 r = func_equal(tv1, tv2, ic);
2006 --recursive_cnt;
2007 return r;
2008 }
2009
Bram Moolenaar418a29f2021-02-10 22:23:41 +01002010 if (tv1->v_type != tv2->v_type
2011 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
2012 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002013 return FALSE;
2014
2015 switch (tv1->v_type)
2016 {
2017 case VAR_LIST:
2018 ++recursive_cnt;
2019 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
2020 --recursive_cnt;
2021 return r;
2022
2023 case VAR_DICT:
2024 ++recursive_cnt;
2025 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
2026 --recursive_cnt;
2027 return r;
2028
2029 case VAR_BLOB:
2030 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
2031
2032 case VAR_NUMBER:
2033 case VAR_BOOL:
2034 case VAR_SPECIAL:
2035 return tv1->vval.v_number == tv2->vval.v_number;
2036
2037 case VAR_STRING:
2038 s1 = tv_get_string_buf(tv1, buf1);
2039 s2 = tv_get_string_buf(tv2, buf2);
2040 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
2041
2042 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002043 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002044 case VAR_JOB:
2045#ifdef FEAT_JOB_CHANNEL
2046 return tv1->vval.v_job == tv2->vval.v_job;
2047#endif
2048 case VAR_CHANNEL:
2049#ifdef FEAT_JOB_CHANNEL
2050 return tv1->vval.v_channel == tv2->vval.v_channel;
2051#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002052 case VAR_INSTR:
2053 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002054
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002055 case VAR_CLASS:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002056 // A class only exists once, equality is identity.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002057 return tv1->vval.v_class == tv2->vval.v_class;
2058
2059 case VAR_OBJECT:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002060 (void)typval_compare_object(tv1, tv2, EXPR_EQUAL, ic, &r);
2061 return r;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002062
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002063 case VAR_PARTIAL:
2064 return tv1->vval.v_partial == tv2->vval.v_partial;
2065
2066 case VAR_FUNC:
2067 return tv1->vval.v_string == tv2->vval.v_string;
2068
2069 case VAR_UNKNOWN:
2070 case VAR_ANY:
2071 case VAR_VOID:
2072 break;
2073 }
2074
2075 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
2076 // does not equal anything, not even itself.
2077 return FALSE;
2078}
2079
2080/*
2081 * Get an option value.
2082 * "arg" points to the '&' or '+' before the option name.
2083 * "arg" is advanced to character after the option name.
2084 * Return OK or FAIL.
2085 */
2086 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002087eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002088 char_u **arg,
2089 typval_T *rettv, // when NULL, only check if option exists
2090 int evaluate)
2091{
2092 char_u *option_end;
2093 long numval;
2094 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002095 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002096 int c;
2097 int working = (**arg == '+'); // has("+option")
2098 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002099 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002100
2101 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002102 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002103 if (option_end == NULL)
2104 {
2105 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002106 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002107 return FAIL;
2108 }
2109
2110 if (!evaluate)
2111 {
2112 *arg = option_end;
2113 return OK;
2114 }
2115
2116 c = *option_end;
2117 *option_end = NUL;
2118 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002119 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002120
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002121 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002122 {
2123 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002124 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002125 ret = FAIL;
2126 }
2127 else if (rettv != NULL)
2128 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01002129 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002130 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002131 {
2132 rettv->v_type = VAR_STRING;
2133 rettv->vval.v_string = NULL;
2134 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002135 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002136 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002137 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2138 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002139 rettv->vval.v_number = 0;
2140 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002141 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002142 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002143 if (in_vim9script() && opt_type == gov_bool)
2144 {
2145 rettv->v_type = VAR_BOOL;
2146 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2147 }
2148 else
2149 {
2150 rettv->v_type = VAR_NUMBER;
2151 rettv->vval.v_number = numval;
2152 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002153 }
2154 else // string option
2155 {
2156 rettv->v_type = VAR_STRING;
2157 rettv->vval.v_string = stringval;
2158 }
2159 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002160 else if (working && (opt_type == gov_hidden_bool
2161 || opt_type == gov_hidden_number
2162 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002163 ret = FAIL;
2164
2165 *option_end = c; // put back for error messages
2166 *arg = option_end;
2167
2168 return ret;
2169}
2170
2171/*
2172 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2173 * Return OK or FAIL.
2174 */
2175 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002176eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002177 char_u **arg,
2178 typval_T *rettv,
2179 int evaluate,
2180 int want_string UNUSED)
2181{
2182 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002183 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002184 char_u *p;
2185 int get_float = FALSE;
2186
2187 // We accept a float when the format matches
2188 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2189 // strict to avoid backwards compatibility problems.
2190 // With script version 2 and later the leading digit can be
2191 // omitted.
2192 // Don't look for a float after the "." operator, so that
2193 // ":let vers = 1.2.3" doesn't fail.
2194 if (**arg == '.')
2195 p = *arg;
2196 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002197 {
2198 p = *arg + 1;
2199 if (skip_quotes)
2200 for (;;)
2201 {
2202 if (*p == '\'')
2203 ++p;
2204 if (!vim_isdigit(*p))
2205 break;
2206 p = skipdigits(p);
2207 }
2208 else
2209 p = skipdigits(p);
2210 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002211 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2212 {
2213 get_float = TRUE;
2214 p = skipdigits(p + 2);
2215 if (*p == 'e' || *p == 'E')
2216 {
2217 ++p;
2218 if (*p == '-' || *p == '+')
2219 ++p;
2220 if (!vim_isdigit(*p))
2221 get_float = FALSE;
2222 else
2223 p = skipdigits(p + 1);
2224 }
2225 if (ASCII_ISALPHA(*p) || *p == '.')
2226 get_float = FALSE;
2227 }
2228 if (get_float)
2229 {
2230 float_T f;
2231
Bram Moolenaar29500652021-08-08 15:43:34 +02002232 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002233 if (evaluate)
2234 {
2235 rettv->v_type = VAR_FLOAT;
2236 rettv->vval.v_float = f;
2237 }
2238 }
2239 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002240 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2241 {
2242 char_u *bp;
2243 blob_T *blob = NULL; // init for gcc
2244
2245 // Blob constant: 0z0123456789abcdef
2246 if (evaluate)
2247 blob = blob_alloc();
2248 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2249 {
2250 if (!vim_isxdigit(bp[1]))
2251 {
2252 if (blob != NULL)
2253 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002254 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002255 ga_clear(&blob->bv_ga);
2256 VIM_CLEAR(blob);
2257 }
2258 return FAIL;
2259 }
2260 if (blob != NULL)
2261 ga_append(&blob->bv_ga,
2262 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2263 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2264 ++bp;
2265 }
2266 if (blob != NULL)
2267 rettv_blob_set(rettv, blob);
2268 *arg = bp;
2269 }
2270 else
2271 {
2272 varnumber_T n;
2273
2274 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002275 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002276 ? STR2NR_NO_OCT + STR2NR_QUOTE
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002277 : STR2NR_ALL, &n, NULL, 0, TRUE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002278 if (len == 0)
2279 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002280 if (evaluate)
2281 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002282 return FAIL;
2283 }
2284 *arg += len;
2285 if (evaluate)
2286 {
2287 rettv->v_type = VAR_NUMBER;
2288 rettv->vval.v_number = n;
2289 }
2290 }
2291 return OK;
2292}
2293
2294/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002295 * Evaluate a string constant and put the result in "rettv".
2296 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2297 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2298 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002299 * Return OK or FAIL.
2300 */
2301 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002302eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002303{
2304 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002305 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002306 int extra = interpolate ? 1 : 0;
2307 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002308 int len;
2309
2310 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002311 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002312 {
2313 if (*p == '\\' && p[1] != NUL)
2314 {
2315 ++p;
2316 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002317 // to 9 characters (6 for the char and 3 for a modifier):
2318 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002319 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002320 {
2321 int modifiers = 0;
2322 int flags = FSK_KEYCODE | FSK_IN_STRING;
2323
zeertzjqdb088872022-05-02 22:53:45 +01002324 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002325
2326 // Skip to the '>' to avoid using '{' inside for string
2327 // interpolation.
2328 if (p[1] != '*')
2329 flags |= FSK_SIMPLIFY;
2330 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2331 --p; // leave "p" on the ">"
2332 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002333 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002334 else if (interpolate && (*p == '{' || *p == '}'))
2335 {
2336 if (*p == '{' && p[1] != '{') // start of expression
2337 break;
2338 ++p;
2339 if (p[-1] == '}' && *p != '}') // single '}' is an error
2340 {
2341 semsg(_(e_stray_closing_curly_str), *arg);
2342 return FAIL;
2343 }
2344 --extra; // "{{" becomes "{", "}}" becomes "}"
2345 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002346 }
2347
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002348 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002349 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002350 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002351 return FAIL;
2352 }
2353
2354 // If only parsing, set *arg and return here
2355 if (!evaluate)
2356 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002357 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002358 return OK;
2359 }
2360
2361 // Copy the string into allocated memory, handling backslashed
2362 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002363 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002364 len = (int)(p - *arg + extra);
2365 rettv->vval.v_string = alloc(len);
2366 if (rettv->vval.v_string == NULL)
2367 return FAIL;
2368 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002369
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002370 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002371 {
2372 if (*p == '\\')
2373 {
2374 switch (*++p)
2375 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002376 case 'b': *end++ = BS; ++p; break;
2377 case 'e': *end++ = ESC; ++p; break;
2378 case 'f': *end++ = FF; ++p; break;
2379 case 'n': *end++ = NL; ++p; break;
2380 case 'r': *end++ = CAR; ++p; break;
2381 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002382
2383 case 'X': // hex: "\x1", "\x12"
2384 case 'x':
2385 case 'u': // Unicode: "\u0023"
2386 case 'U':
2387 if (vim_isxdigit(p[1]))
2388 {
2389 int n, nr;
2390 int c = toupper(*p);
2391
2392 if (c == 'X')
2393 n = 2;
2394 else if (*p == 'u')
2395 n = 4;
2396 else
2397 n = 8;
2398 nr = 0;
2399 while (--n >= 0 && vim_isxdigit(p[1]))
2400 {
2401 ++p;
2402 nr = (nr << 4) + hex2nr(*p);
2403 }
2404 ++p;
2405 // For "\u" store the number according to
2406 // 'encoding'.
2407 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002408 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002409 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002410 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002411 }
2412 break;
2413
2414 // octal: "\1", "\12", "\123"
2415 case '0':
2416 case '1':
2417 case '2':
2418 case '3':
2419 case '4':
2420 case '5':
2421 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002422 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002423 if (*p >= '0' && *p <= '7')
2424 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002425 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002426 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002427 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002428 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002429 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002430 break;
2431
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002432 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002433 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002434 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002435 int flags = FSK_KEYCODE | FSK_IN_STRING;
2436
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002437 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002438 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002439 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002440 if (extra != 0)
2441 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002442 end += extra;
2443 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002444 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002445 break;
2446 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002447 }
2448 // FALLTHROUGH
2449
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002450 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002451 break;
2452 }
2453 }
2454 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002455 {
2456 if (interpolate && (*p == '{' || *p == '}'))
2457 {
2458 if (*p == '{' && p[1] != '{') // start of expression
2459 break;
2460 ++p; // reduce "{{" to "{" and "}}" to "}"
2461 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002462 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002463 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002464 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002465 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002466 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002467 ++p;
2468 *arg = p;
2469
2470 return OK;
2471}
2472
2473/*
2474 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002475 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2476 * Return OK when a "rettv" was set to the string.
2477 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002478 */
2479 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002480eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002481{
2482 char_u *p;
2483 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002484 int reduce = interpolate ? -1 : 0;
2485 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002486
2487 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002488 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002489 {
2490 if (*p == '\'')
2491 {
2492 if (p[1] != '\'')
2493 break;
2494 ++reduce;
2495 ++p;
2496 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002497 else if (interpolate)
2498 {
2499 if (*p == '{')
2500 {
2501 if (p[1] != '{')
2502 break;
2503 ++p;
2504 ++reduce;
2505 }
2506 else if (*p == '}')
2507 {
2508 ++p;
2509 if (*p != '}')
2510 {
2511 semsg(_(e_stray_closing_curly_str), *arg);
2512 return FAIL;
2513 }
2514 ++reduce;
2515 }
2516 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002517 }
2518
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002519 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002520 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002521 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002522 return FAIL;
2523 }
2524
2525 // If only parsing return after setting "*arg"
2526 if (!evaluate)
2527 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002528 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002529 return OK;
2530 }
2531
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002532 // Copy the string into allocated memory, handling '' to ' reduction and
2533 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002534 str = alloc((p - *arg) - reduce);
2535 if (str == NULL)
2536 return FAIL;
2537 rettv->v_type = VAR_STRING;
2538 rettv->vval.v_string = str;
2539
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002540 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002541 {
2542 if (*p == '\'')
2543 {
2544 if (p[1] != '\'')
2545 break;
2546 ++p;
2547 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002548 else if (interpolate && (*p == '{' || *p == '}'))
2549 {
2550 if (*p == '{' && p[1] != '{')
2551 break;
2552 ++p;
2553 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002554 MB_COPY_CHAR(p, str);
2555 }
2556 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002557 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002558
2559 return OK;
2560}
2561
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002562/*
2563 * Evaluate a single or double quoted string possibly containing expressions.
2564 * "arg" points to the '$'. The result is put in "rettv".
2565 * Returns OK or FAIL.
2566 */
LemonBoy2eaef102022-05-06 13:14:50 +01002567 int
2568eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2569{
2570 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002571 int ret = OK;
2572 int quote;
2573 garray_T ga;
2574 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002575
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002576 ga_init2(&ga, 1, 80);
2577
2578 // *arg is on the '$' character, move it to the first string character.
2579 ++*arg;
2580 quote = **arg;
2581 ++*arg;
2582
2583 for (;;)
2584 {
2585 // Get the string up to the matching quote or to a single '{'.
2586 // "arg" is advanced to either the quote or the '{'.
2587 if (quote == '"')
2588 ret = eval_string(arg, &tv, evaluate, TRUE);
2589 else
2590 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2591 if (ret == FAIL)
2592 break;
2593 if (evaluate)
2594 {
2595 ga_concat(&ga, tv.vval.v_string);
2596 clear_tv(&tv);
2597 }
2598
2599 if (**arg != '{')
2600 {
2601 // found terminating quote
2602 ++*arg;
2603 break;
2604 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002605 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002606 if (p == NULL)
2607 {
2608 ret = FAIL;
2609 break;
2610 }
2611 *arg = p;
2612 }
LemonBoy2eaef102022-05-06 13:14:50 +01002613
2614 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002615 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2616 {
2617 ga_clear(&ga);
2618 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002619 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002620 }
LemonBoy2eaef102022-05-06 13:14:50 +01002621
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002622 rettv->vval.v_string = ga.ga_data;
2623 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002624}
2625
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002626/*
2627 * Return a string with the string representation of a variable.
2628 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2629 * "numbuf" is used for a number.
2630 * Puts quotes around strings, so that they can be parsed back by eval().
2631 * May return NULL.
2632 */
2633 char_u *
2634tv2string(
2635 typval_T *tv,
2636 char_u **tofree,
2637 char_u *numbuf,
2638 int copyID)
2639{
2640 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2641}
2642
2643/*
2644 * Get the value of an environment variable.
2645 * "arg" is pointing to the '$'. It is advanced to after the name.
2646 * If the environment variable was not set, silently assume it is empty.
2647 * Return FAIL if the name is invalid.
2648 */
2649 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002650eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002651{
2652 char_u *string = NULL;
2653 int len;
2654 int cc;
2655 char_u *name;
2656 int mustfree = FALSE;
2657
2658 ++*arg;
2659 name = *arg;
2660 len = get_env_len(arg);
2661 if (evaluate)
2662 {
2663 if (len == 0)
2664 return FAIL; // invalid empty name
2665
2666 cc = name[len];
2667 name[len] = NUL;
2668 // first try vim_getenv(), fast for normal environment vars
2669 string = vim_getenv(name, &mustfree);
2670 if (string != NULL && *string != NUL)
2671 {
2672 if (!mustfree)
2673 string = vim_strsave(string);
2674 }
2675 else
2676 {
2677 if (mustfree)
2678 vim_free(string);
2679
2680 // next try expanding things like $VIM and ${HOME}
2681 string = expand_env_save(name - 1);
2682 if (string != NULL && *string == '$')
2683 VIM_CLEAR(string);
2684 }
2685 name[len] = cc;
2686
2687 rettv->v_type = VAR_STRING;
2688 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002689 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002690 }
2691
2692 return OK;
2693}
2694
2695/*
2696 * Get the lnum from the first argument.
2697 * Also accepts ".", "$", etc., but that only works for the current buffer.
2698 * Returns -1 on error.
2699 */
2700 linenr_T
2701tv_get_lnum(typval_T *argvars)
2702{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002703 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002704 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002705
Bram Moolenaar56acb092020-08-16 14:48:19 +02002706 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2707 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002708 if (lnum <= 0 && did_emsg_before == did_emsg
2709 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002710 {
2711 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002712 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002713
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002714 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002715 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002716 if (fp != NULL)
2717 lnum = fp->lnum;
2718 }
2719 return lnum;
2720}
2721
2722/*
2723 * Get the lnum from the first argument.
2724 * Also accepts "$", then "buf" is used.
2725 * Returns 0 on error.
2726 */
2727 linenr_T
2728tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2729{
2730 if (argvars[0].v_type == VAR_STRING
2731 && argvars[0].vval.v_string != NULL
2732 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002733 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002734 && buf != NULL)
2735 return buf->b_ml.ml_line_count;
2736 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2737}
2738
2739/*
2740 * Get buffer by number or pattern.
2741 */
2742 buf_T *
2743tv_get_buf(typval_T *tv, int curtab_only)
2744{
2745 char_u *name = tv->vval.v_string;
2746 buf_T *buf;
2747
2748 if (tv->v_type == VAR_NUMBER)
2749 return buflist_findnr((int)tv->vval.v_number);
2750 if (tv->v_type != VAR_STRING)
2751 return NULL;
2752 if (name == NULL || *name == NUL)
2753 return curbuf;
2754 if (name[0] == '$' && name[1] == NUL)
2755 return lastbuf;
2756
2757 buf = buflist_find_by_name(name, curtab_only);
2758
2759 // If not found, try expanding the name, like done for bufexists().
2760 if (buf == NULL)
2761 buf = find_buffer(tv);
2762
2763 return buf;
2764}
2765
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002766/*
2767 * Like tv_get_buf() but give an error message is the type is wrong.
2768 */
2769 buf_T *
2770tv_get_buf_from_arg(typval_T *tv)
2771{
2772 buf_T *buf;
2773
2774 ++emsg_off;
2775 buf = tv_get_buf(tv, FALSE);
2776 --emsg_off;
2777 if (buf == NULL
2778 && tv->v_type != VAR_NUMBER
2779 && tv->v_type != VAR_STRING)
2780 // issue errmsg for type error
2781 (void)tv_get_number(tv);
2782 return buf;
2783}
2784
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002785#endif // FEAT_EVAL