blob: a760f356bf4e745ac1e0e9646ee73ebfef400eb5 [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/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200977 * Get the string value of a variable.
978 * If it is a Number variable, the number is converted into a string.
979 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
980 * tv_get_string_buf() uses a given buffer.
981 * If the String variable has never been set, return an empty string.
982 * Never returns NULL;
983 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
984 * NULL on error.
985 */
986 char_u *
987tv_get_string(typval_T *varp)
988{
989 static char_u mybuf[NUMBUFLEN];
990
991 return tv_get_string_buf(varp, mybuf);
992}
993
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100994/*
995 * Like tv_get_string() but don't allow number to string conversion for Vim9.
996 */
997 char_u *
998tv_get_string_strict(typval_T *varp)
999{
1000 static char_u mybuf[NUMBUFLEN];
1001 char_u *res = tv_get_string_buf_chk_strict(
1002 varp, mybuf, in_vim9script());
1003
1004 return res != NULL ? res : (char_u *)"";
1005}
1006
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001007 char_u *
1008tv_get_string_buf(typval_T *varp, char_u *buf)
1009{
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001010 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001011
1012 return res != NULL ? res : (char_u *)"";
1013}
1014
1015/*
1016 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
1017 */
1018 char_u *
1019tv_get_string_chk(typval_T *varp)
1020{
1021 static char_u mybuf[NUMBUFLEN];
1022
1023 return tv_get_string_buf_chk(varp, mybuf);
1024}
1025
1026 char_u *
1027tv_get_string_buf_chk(typval_T *varp, char_u *buf)
1028{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001029 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
1030}
1031
1032 char_u *
1033tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
1034{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001035 switch (varp->v_type)
1036 {
1037 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +01001038 if (strict)
1039 {
1040 emsg(_(e_using_number_as_string));
1041 break;
1042 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001043 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
1044 (varnumber_T)varp->vval.v_number);
1045 return buf;
1046 case VAR_FUNC:
1047 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001048 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001049 break;
1050 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001051 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001052 break;
1053 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001054 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001055 break;
1056 case VAR_FLOAT:
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001057 if (strict)
1058 {
Bram Moolenaar0699b042022-01-01 16:01:23 +00001059 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +02001060 break;
1061 }
1062 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
1063 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001064 case VAR_STRING:
1065 if (varp->vval.v_string != NULL)
1066 return varp->vval.v_string;
1067 return (char_u *)"";
1068 case VAR_BOOL:
1069 case VAR_SPECIAL:
1070 STRCPY(buf, get_var_special_name(varp->vval.v_number));
1071 return buf;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01001072 case VAR_BLOB:
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001073 emsg(_(e_using_blob_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001074 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001075 case VAR_CLASS:
1076 emsg(_(e_using_class_as_string));
1077 break;
1078 case VAR_OBJECT:
1079 emsg(_(e_using_object_as_string));
1080 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001081 case VAR_JOB:
1082#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001083 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001084 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001085 semsg(_(e_using_invalid_value_as_string_str), "job");
1086 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001087 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001088 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001089#endif
1090 break;
1091 case VAR_CHANNEL:
1092#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001093 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001094 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001095 semsg(_(e_using_invalid_value_as_string_str), "channel");
1096 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001097 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +02001098 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001099#endif
1100 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02001101 case VAR_VOID:
1102 emsg(_(e_cannot_use_void_value));
1103 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001104 case VAR_UNKNOWN:
1105 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001106 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +02001107 semsg(_(e_using_invalid_value_as_string_str),
1108 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001109 break;
1110 }
1111 return NULL;
1112}
1113
1114/*
1115 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
1116 * string() on Dict, List, etc.
1117 */
1118 char_u *
1119tv_stringify(typval_T *varp, char_u *buf)
1120{
1121 if (varp->v_type == VAR_LIST
1122 || varp->v_type == VAR_DICT
1123 || varp->v_type == VAR_BLOB
1124 || varp->v_type == VAR_FUNC
1125 || varp->v_type == VAR_PARTIAL
1126 || varp->v_type == VAR_FLOAT)
1127 {
1128 typval_T tmp;
1129
1130 f_string(varp, &tmp);
1131 tv_get_string_buf(&tmp, buf);
1132 clear_tv(varp);
1133 *varp = tmp;
1134 return tmp.vval.v_string;
1135 }
1136 return tv_get_string_buf(varp, buf);
1137}
1138
1139/*
1140 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1141 * Also give an error message, using "name" or _("name") when use_gettext is
1142 * TRUE.
1143 */
1144 int
1145tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1146{
1147 int lock = 0;
1148
1149 switch (tv->v_type)
1150 {
1151 case VAR_BLOB:
1152 if (tv->vval.v_blob != NULL)
1153 lock = tv->vval.v_blob->bv_lock;
1154 break;
1155 case VAR_LIST:
1156 if (tv->vval.v_list != NULL)
1157 lock = tv->vval.v_list->lv_lock;
1158 break;
1159 case VAR_DICT:
1160 if (tv->vval.v_dict != NULL)
1161 lock = tv->vval.v_dict->dv_lock;
1162 break;
1163 default:
1164 break;
1165 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001166 return value_check_lock(tv->v_lock, name, use_gettext)
1167 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001168}
1169
1170/*
1171 * Copy the values from typval_T "from" to typval_T "to".
1172 * When needed allocates string or increases reference count.
1173 * Does not make a copy of a list, blob or dict but copies the reference!
1174 * It is OK for "from" and "to" to point to the same item. This is used to
1175 * make a copy later.
1176 */
1177 void
1178copy_tv(typval_T *from, typval_T *to)
1179{
1180 to->v_type = from->v_type;
1181 to->v_lock = 0;
1182 switch (from->v_type)
1183 {
1184 case VAR_NUMBER:
1185 case VAR_BOOL:
1186 case VAR_SPECIAL:
1187 to->vval.v_number = from->vval.v_number;
1188 break;
1189 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001190 to->vval.v_float = from->vval.v_float;
1191 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001192 case VAR_JOB:
1193#ifdef FEAT_JOB_CHANNEL
1194 to->vval.v_job = from->vval.v_job;
1195 if (to->vval.v_job != NULL)
1196 ++to->vval.v_job->jv_refcount;
1197 break;
1198#endif
1199 case VAR_CHANNEL:
1200#ifdef FEAT_JOB_CHANNEL
1201 to->vval.v_channel = from->vval.v_channel;
1202 if (to->vval.v_channel != NULL)
1203 ++to->vval.v_channel->ch_refcount;
1204 break;
1205#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001206 case VAR_INSTR:
1207 to->vval.v_instr = from->vval.v_instr;
1208 break;
1209
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001210 case VAR_CLASS:
1211 copy_class(from, to);
1212 break;
1213
1214 case VAR_OBJECT:
1215 copy_object(from, to);
1216 break;
1217
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001218 case VAR_STRING:
1219 case VAR_FUNC:
1220 if (from->vval.v_string == NULL)
1221 to->vval.v_string = NULL;
1222 else
1223 {
1224 to->vval.v_string = vim_strsave(from->vval.v_string);
1225 if (from->v_type == VAR_FUNC)
1226 func_ref(to->vval.v_string);
1227 }
1228 break;
1229 case VAR_PARTIAL:
1230 if (from->vval.v_partial == NULL)
1231 to->vval.v_partial = NULL;
1232 else
1233 {
1234 to->vval.v_partial = from->vval.v_partial;
1235 ++to->vval.v_partial->pt_refcount;
1236 }
1237 break;
1238 case VAR_BLOB:
1239 if (from->vval.v_blob == NULL)
1240 to->vval.v_blob = NULL;
1241 else
1242 {
1243 to->vval.v_blob = from->vval.v_blob;
1244 ++to->vval.v_blob->bv_refcount;
1245 }
1246 break;
1247 case VAR_LIST:
1248 if (from->vval.v_list == NULL)
1249 to->vval.v_list = NULL;
1250 else
1251 {
1252 to->vval.v_list = from->vval.v_list;
1253 ++to->vval.v_list->lv_refcount;
1254 }
1255 break;
1256 case VAR_DICT:
1257 if (from->vval.v_dict == NULL)
1258 to->vval.v_dict = NULL;
1259 else
1260 {
1261 to->vval.v_dict = from->vval.v_dict;
1262 ++to->vval.v_dict->dv_refcount;
1263 }
1264 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001265 case VAR_VOID:
1266 emsg(_(e_cannot_use_void_value));
1267 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001268 case VAR_UNKNOWN:
1269 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001270 internal_error_no_abort("copy_tv(UNKNOWN)");
1271 break;
1272 }
1273}
1274
1275/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001276 * Compare "tv1" and "tv2".
1277 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001278 */
1279 int
1280typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001281 typval_T *tv1, // first operand
1282 typval_T *tv2, // second operand
1283 exprtype_T type, // operator
1284 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001285{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001286 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001287 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001288 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1289
Bram Moolenaar265f8112021-12-19 12:33:05 +00001290 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001291 {
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001292 // For "is" a different type always means FALSE, for "isnot"
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001293 // it means TRUE.
1294 n1 = (type == EXPR_ISNOT);
1295 }
Bram Moolenaar7a222242022-03-01 19:23:24 +00001296 else if (((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1297 || (tv2->v_type == VAR_SPECIAL
1298 && tv2->vval.v_number == VVAL_NULL))
1299 && tv1->v_type != tv2->v_type
1300 && (type == EXPR_EQUAL || type == EXPR_NEQUAL))
1301 {
1302 n1 = typval_compare_null(tv1, tv2);
1303 if (n1 == MAYBE)
1304 {
1305 clear_tv(tv1);
1306 return FAIL;
1307 }
1308 if (type == EXPR_NEQUAL)
1309 n1 = !n1;
1310 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001311 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001312 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001313 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001314 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001315 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001316 return FAIL;
1317 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001318 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001319 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001320 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001321 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001322 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001323 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001324 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001325 return FAIL;
1326 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001327 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001328 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001329 else if (tv1->v_type == VAR_CLASS || tv2->v_type == VAR_CLASS)
1330 {
1331 if (typval_compare_class(tv1, tv2, type, ic, &res) == FAIL)
1332 {
1333 clear_tv(tv1);
1334 return FAIL;
1335 }
1336 n1 = res;
1337 }
1338 else if (tv1->v_type == VAR_OBJECT || tv2->v_type == VAR_OBJECT)
1339 {
1340 if (typval_compare_object(tv1, tv2, type, ic, &res) == FAIL)
1341 {
1342 clear_tv(tv1);
1343 return FAIL;
1344 }
1345 n1 = res;
1346 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001347 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001348 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001349 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001350 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001351 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001352 return FAIL;
1353 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001354 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001355 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001356 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1357 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001358 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001359 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001360 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001361 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001362 return FAIL;
1363 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001364 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001365 }
1366
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001367 // If one of the two variables is a float, compare as a float.
1368 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001369 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001370 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1371 {
1372 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001373 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001374
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001375 f1 = tv_get_float_chk(tv1, &error);
1376 if (!error)
1377 f2 = tv_get_float_chk(tv2, &error);
1378 if (error)
1379 {
1380 clear_tv(tv1);
1381 return FAIL;
1382 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001383 n1 = FALSE;
1384 switch (type)
1385 {
1386 case EXPR_IS:
1387 case EXPR_EQUAL: n1 = (f1 == f2); break;
1388 case EXPR_ISNOT:
1389 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1390 case EXPR_GREATER: n1 = (f1 > f2); break;
1391 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1392 case EXPR_SMALLER: n1 = (f1 < f2); break;
1393 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1394 case EXPR_UNKNOWN:
1395 case EXPR_MATCH:
1396 default: break; // avoid gcc warning
1397 }
1398 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001399
1400 // If one of the two variables is a number, compare as a number.
1401 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001402 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001403 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1404 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001405 int error = FALSE;
1406
1407 n1 = tv_get_number_chk(tv1, &error);
1408 if (!error)
1409 n2 = tv_get_number_chk(tv2, &error);
1410 if (error)
1411 {
1412 clear_tv(tv1);
1413 return FAIL;
1414 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001415 switch (type)
1416 {
1417 case EXPR_IS:
1418 case EXPR_EQUAL: n1 = (n1 == n2); break;
1419 case EXPR_ISNOT:
1420 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1421 case EXPR_GREATER: n1 = (n1 > n2); break;
1422 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1423 case EXPR_SMALLER: n1 = (n1 < n2); break;
1424 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1425 case EXPR_UNKNOWN:
1426 case EXPR_MATCH:
1427 default: break; // avoid gcc warning
1428 }
1429 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001430 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1431 || tv2->v_type == VAR_BOOL
1432 || (tv1->v_type == VAR_SPECIAL
1433 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001434 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001435 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001436 {
1437 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001438 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1439 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001440 return FAIL;
1441 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001442 n1 = tv1->vval.v_number;
1443 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001444 switch (type)
1445 {
1446 case EXPR_IS:
1447 case EXPR_EQUAL: n1 = (n1 == n2); break;
1448 case EXPR_ISNOT:
1449 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1450 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001451 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001452 vartype_name(tv1->v_type));
1453 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001454 return FAIL;
1455 }
1456 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001457#ifdef FEAT_JOB_CHANNEL
1458 else if (tv1->v_type == tv2->v_type
1459 && (tv1->v_type == VAR_CHANNEL || tv1->v_type == VAR_JOB)
1460 && (type == EXPR_NEQUAL || type == EXPR_EQUAL))
1461 {
1462 if (tv1->v_type == VAR_CHANNEL)
1463 n1 = tv1->vval.v_channel == tv2->vval.v_channel;
1464 else
1465 n1 = tv1->vval.v_job == tv2->vval.v_job;
1466 if (type == EXPR_NEQUAL)
1467 n1 = !n1;
1468 }
1469#endif
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001470 else
1471 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001472 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001473 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001474 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001475 return FAIL;
1476 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001477 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001478 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001479 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001480 if (in_vim9script())
1481 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001482 tv1->v_type = VAR_BOOL;
1483 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001484 }
1485 else
1486 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001487 tv1->v_type = VAR_NUMBER;
1488 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001489 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001490
1491 return OK;
1492}
1493
Bram Moolenaar34453202021-01-31 13:08:38 +01001494/*
dundargocc57b5bc2022-11-02 13:30:51 +00001495 * Compare "tv1" to "tv2" as lists according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001496 * Put the result, false or true, in "res".
1497 * Return FAIL and give an error message when the comparison can't be done.
1498 */
1499 int
1500typval_compare_list(
1501 typval_T *tv1,
1502 typval_T *tv2,
1503 exprtype_T type,
1504 int ic,
1505 int *res)
1506{
1507 int val = 0;
1508
1509 if (type == EXPR_IS || type == EXPR_ISNOT)
1510 {
1511 val = (tv1->v_type == tv2->v_type
1512 && tv1->vval.v_list == tv2->vval.v_list);
1513 if (type == EXPR_ISNOT)
1514 val = !val;
1515 }
1516 else if (tv1->v_type != tv2->v_type
1517 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1518 {
1519 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001520 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001521 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001522 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001523 return FAIL;
1524 }
1525 else
1526 {
1527 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1528 ic, FALSE);
1529 if (type == EXPR_NEQUAL)
1530 val = !val;
1531 }
1532 *res = val;
1533 return OK;
1534}
1535
1536/*
Bram Moolenaarf3507a52022-03-09 11:56:21 +00001537 * Compare v:null with another type. Return TRUE if the value is NULL.
Bram Moolenaar7a222242022-03-01 19:23:24 +00001538 */
1539 int
1540typval_compare_null(typval_T *tv1, typval_T *tv2)
1541{
1542 if ((tv1->v_type == VAR_SPECIAL && tv1->vval.v_number == VVAL_NULL)
1543 || (tv2->v_type == VAR_SPECIAL && tv2->vval.v_number == VVAL_NULL))
1544 {
1545 typval_T *tv = tv1->v_type == VAR_SPECIAL ? tv2 : tv1;
1546
1547 switch (tv->v_type)
1548 {
1549 case VAR_BLOB: return tv->vval.v_blob == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001550#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001551 case VAR_CHANNEL: return tv->vval.v_channel == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001552#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001553 case VAR_DICT: return tv->vval.v_dict == NULL;
1554 case VAR_FUNC: return tv->vval.v_string == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001555#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar7a222242022-03-01 19:23:24 +00001556 case VAR_JOB: return tv->vval.v_job == NULL;
Bram Moolenaarf6b0c792022-03-01 19:52:48 +00001557#endif
Bram Moolenaar7a222242022-03-01 19:23:24 +00001558 case VAR_LIST: return tv->vval.v_list == NULL;
1559 case VAR_PARTIAL: return tv->vval.v_partial == NULL;
1560 case VAR_STRING: return tv->vval.v_string == NULL;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001561
1562 case VAR_NUMBER: if (!in_vim9script())
1563 return tv->vval.v_number == 0;
1564 break;
Bram Moolenaarc6e9d702022-03-02 13:13:30 +00001565 case VAR_FLOAT: if (!in_vim9script())
1566 return tv->vval.v_float == 0.0;
1567 break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001568 default: break;
1569 }
1570 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001571 // although comparing null with number, float or bool is not very useful
Bram Moolenaar05667812022-03-15 20:21:33 +00001572 // we won't give an error
1573 return FALSE;
Bram Moolenaar7a222242022-03-01 19:23:24 +00001574}
1575
1576/*
dundargocc57b5bc2022-11-02 13:30:51 +00001577 * Compare "tv1" to "tv2" as blobs according to "type".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001578 * Put the result, false or true, in "res".
1579 * Return FAIL and give an error message when the comparison can't be done.
1580 */
1581 int
1582typval_compare_blob(
1583 typval_T *tv1,
1584 typval_T *tv2,
1585 exprtype_T type,
1586 int *res)
1587{
1588 int val = 0;
1589
1590 if (type == EXPR_IS || type == EXPR_ISNOT)
1591 {
1592 val = (tv1->v_type == tv2->v_type
1593 && tv1->vval.v_blob == tv2->vval.v_blob);
1594 if (type == EXPR_ISNOT)
1595 val = !val;
1596 }
1597 else if (tv1->v_type != tv2->v_type
1598 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1599 {
1600 if (tv1->v_type != tv2->v_type)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001601 emsg(_(e_can_only_compare_blob_with_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001602 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001603 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001604 return FAIL;
1605 }
1606 else
1607 {
1608 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1609 if (type == EXPR_NEQUAL)
1610 val = !val;
1611 }
1612 *res = val;
1613 return OK;
1614}
1615
1616/*
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00001617 * Compare "tv1" to "tv2" as classes according to "type".
1618 * Put the result, false or true, in "res".
1619 * Return FAIL and give an error message when the comparison can't be done.
1620 */
1621 int
1622typval_compare_class(
1623 typval_T *tv1,
1624 typval_T *tv2,
1625 exprtype_T type UNUSED,
1626 int ic UNUSED,
1627 int *res)
1628{
1629 // TODO: use "type"
1630 *res = tv1->vval.v_class == tv2->vval.v_class;
1631 return OK;
1632}
1633
1634/*
1635 * Compare "tv1" to "tv2" as objects according to "type".
1636 * Put the result, false or true, in "res".
1637 * Return FAIL and give an error message when the comparison can't be done.
1638 */
1639 int
1640typval_compare_object(
1641 typval_T *tv1,
1642 typval_T *tv2,
1643 exprtype_T type,
1644 int ic,
1645 int *res)
1646{
1647 int res_match = type == EXPR_EQUAL || type == EXPR_IS ? TRUE : FALSE;
1648
1649 if (tv1->vval.v_object == NULL && tv2->vval.v_object == NULL)
1650 {
1651 *res = res_match;
1652 return OK;
1653 }
1654 if (tv1->vval.v_object == NULL || tv2->vval.v_object == NULL)
1655 {
1656 *res = !res_match;
1657 return OK;
1658 }
1659
1660 class_T *cl1 = tv1->vval.v_object->obj_class;
1661 class_T *cl2 = tv2->vval.v_object->obj_class;
1662 if (cl1 != cl2 || cl1 == NULL || cl2 == NULL)
1663 {
1664 *res = !res_match;
1665 return OK;
1666 }
1667
1668 object_T *obj1 = tv1->vval.v_object;
1669 object_T *obj2 = tv2->vval.v_object;
1670 if (type == EXPR_IS || type == EXPR_ISNOT)
1671 {
1672 *res = obj1 == obj2 ? res_match : !res_match;
1673 return OK;
1674 }
1675
1676 for (int i = 0; i < cl1->class_obj_member_count; ++i)
1677 if (!tv_equal((typval_T *)(obj1 + 1) + i,
1678 (typval_T *)(obj2 + 1) + i, ic, TRUE))
1679 {
1680 *res = !res_match;
1681 return OK;
1682 }
1683 *res = res_match;
1684 return OK;
1685}
1686
1687/*
dundargocc57b5bc2022-11-02 13:30:51 +00001688 * Compare "tv1" to "tv2" as dictionaries according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001689 * Put the result, false or true, in "res".
1690 * Return FAIL and give an error message when the comparison can't be done.
1691 */
1692 int
1693typval_compare_dict(
1694 typval_T *tv1,
1695 typval_T *tv2,
1696 exprtype_T type,
1697 int ic,
1698 int *res)
1699{
1700 int val;
1701
1702 if (type == EXPR_IS || type == EXPR_ISNOT)
1703 {
1704 val = (tv1->v_type == tv2->v_type
1705 && tv1->vval.v_dict == tv2->vval.v_dict);
1706 if (type == EXPR_ISNOT)
1707 val = !val;
1708 }
1709 else if (tv1->v_type != tv2->v_type
1710 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1711 {
1712 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001713 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001714 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001715 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001716 return FAIL;
1717 }
1718 else
1719 {
1720 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1721 if (type == EXPR_NEQUAL)
1722 val = !val;
1723 }
1724 *res = val;
1725 return OK;
1726}
1727
1728/*
dundargocc57b5bc2022-11-02 13:30:51 +00001729 * Compare "tv1" to "tv2" as funcrefs according to "type" and "ic".
Bram Moolenaar265f8112021-12-19 12:33:05 +00001730 * Put the result, false or true, in "res".
1731 * Return FAIL and give an error message when the comparison can't be done.
1732 */
1733 int
1734typval_compare_func(
1735 typval_T *tv1,
1736 typval_T *tv2,
1737 exprtype_T type,
1738 int ic,
1739 int *res)
1740{
1741 int val = 0;
1742
1743 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1744 && type != EXPR_IS && type != EXPR_ISNOT)
1745 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001746 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001747 return FAIL;
1748 }
1749 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1750 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1751 // When both partials are NULL, then they are equal.
1752 // Otherwise they are not equal.
1753 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1754 else if (type == EXPR_IS || type == EXPR_ISNOT)
1755 {
1756 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1757 // strings are considered the same if their value is
1758 // the same
1759 val = tv_equal(tv1, tv2, ic, FALSE);
1760 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1761 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1762 else
1763 val = FALSE;
1764 }
1765 else
1766 val = tv_equal(tv1, tv2, ic, FALSE);
1767 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1768 val = !val;
1769 *res = val;
1770 return OK;
1771}
1772
1773/*
1774 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1775 * Put the result, false or true, in "res".
1776 * Return FAIL and give an error message when the comparison can't be done.
1777 */
1778 int
1779typval_compare_string(
1780 typval_T *tv1,
1781 typval_T *tv2,
1782 exprtype_T type,
1783 int ic,
1784 int *res)
1785{
1786 int i = 0;
1787 int val = FALSE;
1788 char_u *s1, *s2;
1789 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1790
1791 if (in_vim9script()
1792 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1793 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1794 {
1795 semsg(_(e_cannot_compare_str_with_str),
1796 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1797 return FAIL;
1798 }
1799 s1 = tv_get_string_buf(tv1, buf1);
1800 s2 = tv_get_string_buf(tv2, buf2);
1801 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1802 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1803 switch (type)
1804 {
Bram Moolenaarf8691002022-03-10 12:20:53 +00001805 case EXPR_IS: if (in_vim9script())
1806 {
1807 // Really check it is the same string, not just
1808 // the same value.
1809 val = tv1->vval.v_string == tv2->vval.v_string;
1810 break;
1811 }
1812 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001813 case EXPR_EQUAL: val = (i == 0); break;
Bram Moolenaarf8691002022-03-10 12:20:53 +00001814 case EXPR_ISNOT: if (in_vim9script())
1815 {
1816 // Really check it is not the same string, not
1817 // just a different value.
1818 val = tv1->vval.v_string != tv2->vval.v_string;
1819 break;
1820 }
1821 // FALLTHROUGH
Bram Moolenaar265f8112021-12-19 12:33:05 +00001822 case EXPR_NEQUAL: val = (i != 0); break;
1823 case EXPR_GREATER: val = (i > 0); break;
1824 case EXPR_GEQUAL: val = (i >= 0); break;
1825 case EXPR_SMALLER: val = (i < 0); break;
1826 case EXPR_SEQUAL: val = (i <= 0); break;
1827
1828 case EXPR_MATCH:
1829 case EXPR_NOMATCH:
1830 val = pattern_match(s2, s1, ic);
1831 if (type == EXPR_NOMATCH)
1832 val = !val;
1833 break;
1834
1835 default: break; // avoid gcc warning
1836 }
1837 *res = val;
1838 return OK;
1839}
1840/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001841 * Convert any type to a string, never give an error.
1842 * When "quotes" is TRUE add quotes to a string.
1843 * Returns an allocated string.
1844 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001845 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001846typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001847{
1848 char_u *tofree;
1849 char_u numbuf[NUMBUFLEN];
1850 char_u *ret = NULL;
1851
1852 if (arg == NULL)
1853 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001854 if (!quotes && arg->v_type == VAR_STRING)
1855 {
1856 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1857 : arg->vval.v_string);
1858 }
1859 else
1860 {
1861 ret = tv2string(arg, &tofree, numbuf, 0);
1862 // Make a copy if we have a value but it's not in allocated memory.
1863 if (ret != NULL && tofree == NULL)
1864 ret = vim_strsave(ret);
1865 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001866 return ret;
1867}
1868
1869/*
1870 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1871 * or it refers to a List or Dictionary that is locked.
1872 */
1873 int
1874tv_islocked(typval_T *tv)
1875{
1876 return (tv->v_lock & VAR_LOCKED)
1877 || (tv->v_type == VAR_LIST
1878 && tv->vval.v_list != NULL
1879 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1880 || (tv->v_type == VAR_DICT
1881 && tv->vval.v_dict != NULL
1882 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1883}
1884
1885 static int
1886func_equal(
1887 typval_T *tv1,
1888 typval_T *tv2,
1889 int ic) // ignore case
1890{
1891 char_u *s1, *s2;
1892 dict_T *d1, *d2;
1893 int a1, a2;
1894 int i;
1895
1896 // empty and NULL function name considered the same
1897 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1898 : partial_name(tv1->vval.v_partial);
1899 if (s1 != NULL && *s1 == NUL)
1900 s1 = NULL;
1901 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1902 : partial_name(tv2->vval.v_partial);
1903 if (s2 != NULL && *s2 == NUL)
1904 s2 = NULL;
1905 if (s1 == NULL || s2 == NULL)
1906 {
1907 if (s1 != s2)
1908 return FALSE;
1909 }
1910 else if (STRCMP(s1, s2) != 0)
1911 return FALSE;
1912
1913 // empty dict and NULL dict is different
1914 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1915 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1916 if (d1 == NULL || d2 == NULL)
1917 {
1918 if (d1 != d2)
1919 return FALSE;
1920 }
1921 else if (!dict_equal(d1, d2, ic, TRUE))
1922 return FALSE;
1923
1924 // empty list and no list considered the same
1925 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1926 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1927 if (a1 != a2)
1928 return FALSE;
1929 for (i = 0; i < a1; ++i)
1930 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1931 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1932 return FALSE;
1933
1934 return TRUE;
1935}
1936
1937/*
1938 * Return TRUE if "tv1" and "tv2" have the same value.
1939 * Compares the items just like "==" would compare them, but strings and
1940 * numbers are different. Floats and numbers are also different.
1941 */
1942 int
1943tv_equal(
1944 typval_T *tv1,
1945 typval_T *tv2,
1946 int ic, // ignore case
1947 int recursive) // TRUE when used recursively
1948{
1949 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1950 char_u *s1, *s2;
1951 static int recursive_cnt = 0; // catch recursive loops
1952 int r;
1953 static int tv_equal_recurse_limit;
1954
1955 // Catch lists and dicts that have an endless loop by limiting
1956 // recursiveness to a limit. We guess they are equal then.
1957 // A fixed limit has the problem of still taking an awful long time.
1958 // Reduce the limit every time running into it. That should work fine for
1959 // deeply linked structures that are not recursively linked and catch
1960 // recursiveness quickly.
1961 if (!recursive)
1962 tv_equal_recurse_limit = 1000;
1963 if (recursive_cnt >= tv_equal_recurse_limit)
1964 {
1965 --tv_equal_recurse_limit;
1966 return TRUE;
1967 }
1968
1969 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1970 // arguments.
1971 if ((tv1->v_type == VAR_FUNC
1972 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1973 && (tv2->v_type == VAR_FUNC
1974 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1975 {
1976 ++recursive_cnt;
1977 r = func_equal(tv1, tv2, ic);
1978 --recursive_cnt;
1979 return r;
1980 }
1981
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001982 if (tv1->v_type != tv2->v_type
1983 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1984 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001985 return FALSE;
1986
1987 switch (tv1->v_type)
1988 {
1989 case VAR_LIST:
1990 ++recursive_cnt;
1991 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1992 --recursive_cnt;
1993 return r;
1994
1995 case VAR_DICT:
1996 ++recursive_cnt;
1997 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1998 --recursive_cnt;
1999 return r;
2000
2001 case VAR_BLOB:
2002 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
2003
2004 case VAR_NUMBER:
2005 case VAR_BOOL:
2006 case VAR_SPECIAL:
2007 return tv1->vval.v_number == tv2->vval.v_number;
2008
2009 case VAR_STRING:
2010 s1 = tv_get_string_buf(tv1, buf1);
2011 s2 = tv_get_string_buf(tv2, buf2);
2012 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
2013
2014 case VAR_FLOAT:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002015 return tv1->vval.v_float == tv2->vval.v_float;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002016 case VAR_JOB:
2017#ifdef FEAT_JOB_CHANNEL
2018 return tv1->vval.v_job == tv2->vval.v_job;
2019#endif
2020 case VAR_CHANNEL:
2021#ifdef FEAT_JOB_CHANNEL
2022 return tv1->vval.v_channel == tv2->vval.v_channel;
2023#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002024 case VAR_INSTR:
2025 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002026
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002027 case VAR_CLASS:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002028 // A class only exists once, equality is identity.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002029 return tv1->vval.v_class == tv2->vval.v_class;
2030
2031 case VAR_OBJECT:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002032 (void)typval_compare_object(tv1, tv2, EXPR_EQUAL, ic, &r);
2033 return r;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002034
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002035 case VAR_PARTIAL:
2036 return tv1->vval.v_partial == tv2->vval.v_partial;
2037
2038 case VAR_FUNC:
2039 return tv1->vval.v_string == tv2->vval.v_string;
2040
2041 case VAR_UNKNOWN:
2042 case VAR_ANY:
2043 case VAR_VOID:
2044 break;
2045 }
2046
2047 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
2048 // does not equal anything, not even itself.
2049 return FALSE;
2050}
2051
2052/*
2053 * Get an option value.
2054 * "arg" points to the '&' or '+' before the option name.
2055 * "arg" is advanced to character after the option name.
2056 * Return OK or FAIL.
2057 */
2058 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002059eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002060 char_u **arg,
2061 typval_T *rettv, // when NULL, only check if option exists
2062 int evaluate)
2063{
2064 char_u *option_end;
2065 long numval;
2066 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002067 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002068 int c;
2069 int working = (**arg == '+'); // has("+option")
2070 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002071 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002072
2073 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002074 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002075 if (option_end == NULL)
2076 {
2077 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002078 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002079 return FAIL;
2080 }
2081
2082 if (!evaluate)
2083 {
2084 *arg = option_end;
2085 return OK;
2086 }
2087
2088 c = *option_end;
2089 *option_end = NUL;
2090 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00002091 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002092
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002093 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002094 {
2095 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002096 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002097 ret = FAIL;
2098 }
2099 else if (rettv != NULL)
2100 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01002101 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002102 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002103 {
2104 rettv->v_type = VAR_STRING;
2105 rettv->vval.v_string = NULL;
2106 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002107 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002108 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002109 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
2110 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002111 rettv->vval.v_number = 0;
2112 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002113 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002114 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002115 if (in_vim9script() && opt_type == gov_bool)
2116 {
2117 rettv->v_type = VAR_BOOL;
2118 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
2119 }
2120 else
2121 {
2122 rettv->v_type = VAR_NUMBER;
2123 rettv->vval.v_number = numval;
2124 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002125 }
2126 else // string option
2127 {
2128 rettv->v_type = VAR_STRING;
2129 rettv->vval.v_string = stringval;
2130 }
2131 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01002132 else if (working && (opt_type == gov_hidden_bool
2133 || opt_type == gov_hidden_number
2134 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002135 ret = FAIL;
2136
2137 *option_end = c; // put back for error messages
2138 *arg = option_end;
2139
2140 return ret;
2141}
2142
2143/*
2144 * Allocate a variable for a number constant. Also deals with "0z" for blob.
2145 * Return OK or FAIL.
2146 */
2147 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002148eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002149 char_u **arg,
2150 typval_T *rettv,
2151 int evaluate,
2152 int want_string UNUSED)
2153{
2154 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02002155 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002156 char_u *p;
2157 int get_float = FALSE;
2158
2159 // We accept a float when the format matches
2160 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
2161 // strict to avoid backwards compatibility problems.
2162 // With script version 2 and later the leading digit can be
2163 // omitted.
2164 // Don't look for a float after the "." operator, so that
2165 // ":let vers = 1.2.3" doesn't fail.
2166 if (**arg == '.')
2167 p = *arg;
2168 else
Bram Moolenaar29500652021-08-08 15:43:34 +02002169 {
2170 p = *arg + 1;
2171 if (skip_quotes)
2172 for (;;)
2173 {
2174 if (*p == '\'')
2175 ++p;
2176 if (!vim_isdigit(*p))
2177 break;
2178 p = skipdigits(p);
2179 }
2180 else
2181 p = skipdigits(p);
2182 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002183 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
2184 {
2185 get_float = TRUE;
2186 p = skipdigits(p + 2);
2187 if (*p == 'e' || *p == 'E')
2188 {
2189 ++p;
2190 if (*p == '-' || *p == '+')
2191 ++p;
2192 if (!vim_isdigit(*p))
2193 get_float = FALSE;
2194 else
2195 p = skipdigits(p + 1);
2196 }
2197 if (ASCII_ISALPHA(*p) || *p == '.')
2198 get_float = FALSE;
2199 }
2200 if (get_float)
2201 {
2202 float_T f;
2203
Bram Moolenaar29500652021-08-08 15:43:34 +02002204 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002205 if (evaluate)
2206 {
2207 rettv->v_type = VAR_FLOAT;
2208 rettv->vval.v_float = f;
2209 }
2210 }
2211 else
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002212 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
2213 {
2214 char_u *bp;
2215 blob_T *blob = NULL; // init for gcc
2216
2217 // Blob constant: 0z0123456789abcdef
2218 if (evaluate)
2219 blob = blob_alloc();
2220 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2221 {
2222 if (!vim_isxdigit(bp[1]))
2223 {
2224 if (blob != NULL)
2225 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002226 emsg(_(e_blob_literal_should_have_an_even_number_of_hex_characters));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002227 ga_clear(&blob->bv_ga);
2228 VIM_CLEAR(blob);
2229 }
2230 return FAIL;
2231 }
2232 if (blob != NULL)
2233 ga_append(&blob->bv_ga,
2234 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
2235 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2236 ++bp;
2237 }
2238 if (blob != NULL)
2239 rettv_blob_set(rettv, blob);
2240 *arg = bp;
2241 }
2242 else
2243 {
2244 varnumber_T n;
2245
2246 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02002247 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002248 ? STR2NR_NO_OCT + STR2NR_QUOTE
Bram Moolenaar5fb78c32023-03-04 20:47:39 +00002249 : STR2NR_ALL, &n, NULL, 0, TRUE, NULL);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002250 if (len == 0)
2251 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00002252 if (evaluate)
2253 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002254 return FAIL;
2255 }
2256 *arg += len;
2257 if (evaluate)
2258 {
2259 rettv->v_type = VAR_NUMBER;
2260 rettv->vval.v_number = n;
2261 }
2262 }
2263 return OK;
2264}
2265
2266/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002267 * Evaluate a string constant and put the result in "rettv".
2268 * "*arg" points to the double quote or to after it when "interpolate" is TRUE.
2269 * When "interpolate" is TRUE reduce "{{" to "{", reduce "}}" to "}" and stop
2270 * at a single "{".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002271 * Return OK or FAIL.
2272 */
2273 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002274eval_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002275{
2276 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002277 char_u *end;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002278 int extra = interpolate ? 1 : 0;
2279 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002280 int len;
2281
2282 // Find the end of the string, skipping backslashed characters.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002283 for (p = *arg + off; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002284 {
2285 if (*p == '\\' && p[1] != NUL)
2286 {
2287 ++p;
2288 // A "\<x>" form occupies at least 4 characters, and produces up
zeertzjqdb088872022-05-02 22:53:45 +01002289 // to 9 characters (6 for the char and 3 for a modifier):
2290 // reserve space for 5 extra.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002291 if (*p == '<')
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002292 {
2293 int modifiers = 0;
2294 int flags = FSK_KEYCODE | FSK_IN_STRING;
2295
zeertzjqdb088872022-05-02 22:53:45 +01002296 extra += 5;
Bram Moolenaar1e56bda2022-07-29 15:28:27 +01002297
2298 // Skip to the '>' to avoid using '{' inside for string
2299 // interpolation.
2300 if (p[1] != '*')
2301 flags |= FSK_SIMPLIFY;
2302 if (find_special_key(&p, &modifiers, flags, NULL) != 0)
2303 --p; // leave "p" on the ">"
2304 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002305 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002306 else if (interpolate && (*p == '{' || *p == '}'))
2307 {
2308 if (*p == '{' && p[1] != '{') // start of expression
2309 break;
2310 ++p;
2311 if (p[-1] == '}' && *p != '}') // single '}' is an error
2312 {
2313 semsg(_(e_stray_closing_curly_str), *arg);
2314 return FAIL;
2315 }
2316 --extra; // "{{" becomes "{", "}}" becomes "}"
2317 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002318 }
2319
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002320 if (*p != '"' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002321 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002322 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002323 return FAIL;
2324 }
2325
2326 // If only parsing, set *arg and return here
2327 if (!evaluate)
2328 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002329 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002330 return OK;
2331 }
2332
2333 // Copy the string into allocated memory, handling backslashed
2334 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002335 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002336 len = (int)(p - *arg + extra);
2337 rettv->vval.v_string = alloc(len);
2338 if (rettv->vval.v_string == NULL)
2339 return FAIL;
2340 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002341
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002342 for (p = *arg + off; *p != NUL && *p != '"'; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002343 {
2344 if (*p == '\\')
2345 {
2346 switch (*++p)
2347 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002348 case 'b': *end++ = BS; ++p; break;
2349 case 'e': *end++ = ESC; ++p; break;
2350 case 'f': *end++ = FF; ++p; break;
2351 case 'n': *end++ = NL; ++p; break;
2352 case 'r': *end++ = CAR; ++p; break;
2353 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002354
2355 case 'X': // hex: "\x1", "\x12"
2356 case 'x':
2357 case 'u': // Unicode: "\u0023"
2358 case 'U':
2359 if (vim_isxdigit(p[1]))
2360 {
2361 int n, nr;
2362 int c = toupper(*p);
2363
2364 if (c == 'X')
2365 n = 2;
2366 else if (*p == 'u')
2367 n = 4;
2368 else
2369 n = 8;
2370 nr = 0;
2371 while (--n >= 0 && vim_isxdigit(p[1]))
2372 {
2373 ++p;
2374 nr = (nr << 4) + hex2nr(*p);
2375 }
2376 ++p;
2377 // For "\u" store the number according to
2378 // 'encoding'.
2379 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002380 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002381 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002382 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002383 }
2384 break;
2385
2386 // octal: "\1", "\12", "\123"
2387 case '0':
2388 case '1':
2389 case '2':
2390 case '3':
2391 case '4':
2392 case '5':
2393 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002394 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002395 if (*p >= '0' && *p <= '7')
2396 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002397 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002398 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002399 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002400 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002401 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002402 break;
2403
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002404 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002405 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002406 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002407 int flags = FSK_KEYCODE | FSK_IN_STRING;
2408
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002409 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002410 flags |= FSK_SIMPLIFY;
zeertzjqdb088872022-05-02 22:53:45 +01002411 extra = trans_special(&p, end, flags, FALSE, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002412 if (extra != 0)
2413 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002414 end += extra;
2415 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002416 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002417 break;
2418 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002419 }
2420 // FALLTHROUGH
2421
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002422 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002423 break;
2424 }
2425 }
2426 else
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002427 {
2428 if (interpolate && (*p == '{' || *p == '}'))
2429 {
2430 if (*p == '{' && p[1] != '{') // start of expression
2431 break;
2432 ++p; // reduce "{{" to "{" and "}}" to "}"
2433 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002434 MB_COPY_CHAR(p, end);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002435 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002436 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002437 *end = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002438 if (*p == '"' && !interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002439 ++p;
2440 *arg = p;
2441
2442 return OK;
2443}
2444
2445/*
2446 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002447 * When "interpolate" is TRUE reduce "{{" to "{" and stop at a single "{".
2448 * Return OK when a "rettv" was set to the string.
2449 * Return FAIL on error, "rettv" is not set.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002450 */
2451 int
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002452eval_lit_string(char_u **arg, typval_T *rettv, int evaluate, int interpolate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002453{
2454 char_u *p;
2455 char_u *str;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002456 int reduce = interpolate ? -1 : 0;
2457 int off = interpolate ? 0 : 1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002458
2459 // Find the end of the string, skipping ''.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002460 for (p = *arg + off; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002461 {
2462 if (*p == '\'')
2463 {
2464 if (p[1] != '\'')
2465 break;
2466 ++reduce;
2467 ++p;
2468 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002469 else if (interpolate)
2470 {
2471 if (*p == '{')
2472 {
2473 if (p[1] != '{')
2474 break;
2475 ++p;
2476 ++reduce;
2477 }
2478 else if (*p == '}')
2479 {
2480 ++p;
2481 if (*p != '}')
2482 {
2483 semsg(_(e_stray_closing_curly_str), *arg);
2484 return FAIL;
2485 }
2486 ++reduce;
2487 }
2488 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002489 }
2490
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002491 if (*p != '\'' && !(interpolate && *p == '{'))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002492 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002493 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002494 return FAIL;
2495 }
2496
2497 // If only parsing return after setting "*arg"
2498 if (!evaluate)
2499 {
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002500 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002501 return OK;
2502 }
2503
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002504 // Copy the string into allocated memory, handling '' to ' reduction and
2505 // any expressions.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002506 str = alloc((p - *arg) - reduce);
2507 if (str == NULL)
2508 return FAIL;
2509 rettv->v_type = VAR_STRING;
2510 rettv->vval.v_string = str;
2511
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002512 for (p = *arg + off; *p != NUL; )
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002513 {
2514 if (*p == '\'')
2515 {
2516 if (p[1] != '\'')
2517 break;
2518 ++p;
2519 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002520 else if (interpolate && (*p == '{' || *p == '}'))
2521 {
2522 if (*p == '{' && p[1] != '{')
2523 break;
2524 ++p;
2525 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002526 MB_COPY_CHAR(p, str);
2527 }
2528 *str = NUL;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002529 *arg = p + off;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002530
2531 return OK;
2532}
2533
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002534/*
2535 * Evaluate a single or double quoted string possibly containing expressions.
2536 * "arg" points to the '$'. The result is put in "rettv".
2537 * Returns OK or FAIL.
2538 */
LemonBoy2eaef102022-05-06 13:14:50 +01002539 int
2540eval_interp_string(char_u **arg, typval_T *rettv, int evaluate)
2541{
2542 typval_T tv;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002543 int ret = OK;
2544 int quote;
2545 garray_T ga;
2546 char_u *p;
LemonBoy2eaef102022-05-06 13:14:50 +01002547
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002548 ga_init2(&ga, 1, 80);
2549
2550 // *arg is on the '$' character, move it to the first string character.
2551 ++*arg;
2552 quote = **arg;
2553 ++*arg;
2554
2555 for (;;)
2556 {
2557 // Get the string up to the matching quote or to a single '{'.
2558 // "arg" is advanced to either the quote or the '{'.
2559 if (quote == '"')
2560 ret = eval_string(arg, &tv, evaluate, TRUE);
2561 else
2562 ret = eval_lit_string(arg, &tv, evaluate, TRUE);
2563 if (ret == FAIL)
2564 break;
2565 if (evaluate)
2566 {
2567 ga_concat(&ga, tv.vval.v_string);
2568 clear_tv(&tv);
2569 }
2570
2571 if (**arg != '{')
2572 {
2573 // found terminating quote
2574 ++*arg;
2575 break;
2576 }
Bram Moolenaar70c41242022-05-10 18:11:43 +01002577 p = eval_one_expr_in_str(*arg, &ga, evaluate);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002578 if (p == NULL)
2579 {
2580 ret = FAIL;
2581 break;
2582 }
2583 *arg = p;
2584 }
LemonBoy2eaef102022-05-06 13:14:50 +01002585
2586 rettv->v_type = VAR_STRING;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002587 if (ret == FAIL || !evaluate || ga_append(&ga, NUL) == FAIL)
2588 {
2589 ga_clear(&ga);
2590 rettv->vval.v_string = NULL;
LemonBoy2eaef102022-05-06 13:14:50 +01002591 return ret;
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002592 }
LemonBoy2eaef102022-05-06 13:14:50 +01002593
Bram Moolenaar0abc2872022-05-10 13:24:30 +01002594 rettv->vval.v_string = ga.ga_data;
2595 return OK;
LemonBoy2eaef102022-05-06 13:14:50 +01002596}
2597
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002598/*
2599 * Return a string with the string representation of a variable.
2600 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2601 * "numbuf" is used for a number.
2602 * Puts quotes around strings, so that they can be parsed back by eval().
2603 * May return NULL.
2604 */
2605 char_u *
2606tv2string(
2607 typval_T *tv,
2608 char_u **tofree,
2609 char_u *numbuf,
2610 int copyID)
2611{
2612 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2613}
2614
2615/*
2616 * Get the value of an environment variable.
2617 * "arg" is pointing to the '$'. It is advanced to after the name.
2618 * If the environment variable was not set, silently assume it is empty.
2619 * Return FAIL if the name is invalid.
2620 */
2621 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002622eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002623{
2624 char_u *string = NULL;
2625 int len;
2626 int cc;
2627 char_u *name;
2628 int mustfree = FALSE;
2629
2630 ++*arg;
2631 name = *arg;
2632 len = get_env_len(arg);
2633 if (evaluate)
2634 {
2635 if (len == 0)
2636 return FAIL; // invalid empty name
2637
2638 cc = name[len];
2639 name[len] = NUL;
2640 // first try vim_getenv(), fast for normal environment vars
2641 string = vim_getenv(name, &mustfree);
2642 if (string != NULL && *string != NUL)
2643 {
2644 if (!mustfree)
2645 string = vim_strsave(string);
2646 }
2647 else
2648 {
2649 if (mustfree)
2650 vim_free(string);
2651
2652 // next try expanding things like $VIM and ${HOME}
2653 string = expand_env_save(name - 1);
2654 if (string != NULL && *string == '$')
2655 VIM_CLEAR(string);
2656 }
2657 name[len] = cc;
2658
2659 rettv->v_type = VAR_STRING;
2660 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002661 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002662 }
2663
2664 return OK;
2665}
2666
2667/*
2668 * Get the lnum from the first argument.
2669 * Also accepts ".", "$", etc., but that only works for the current buffer.
2670 * Returns -1 on error.
2671 */
2672 linenr_T
2673tv_get_lnum(typval_T *argvars)
2674{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002675 linenr_T lnum = -1;
Bram Moolenaar801cd352022-10-10 16:08:16 +01002676 int did_emsg_before = did_emsg;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002677
Bram Moolenaar56acb092020-08-16 14:48:19 +02002678 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2679 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar801cd352022-10-10 16:08:16 +01002680 if (lnum <= 0 && did_emsg_before == did_emsg
2681 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002682 {
2683 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002684 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002685
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002686 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002687 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002688 if (fp != NULL)
2689 lnum = fp->lnum;
2690 }
2691 return lnum;
2692}
2693
2694/*
2695 * Get the lnum from the first argument.
2696 * Also accepts "$", then "buf" is used.
2697 * Returns 0 on error.
2698 */
2699 linenr_T
2700tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2701{
2702 if (argvars[0].v_type == VAR_STRING
2703 && argvars[0].vval.v_string != NULL
2704 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002705 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002706 && buf != NULL)
2707 return buf->b_ml.ml_line_count;
2708 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2709}
2710
2711/*
2712 * Get buffer by number or pattern.
2713 */
2714 buf_T *
2715tv_get_buf(typval_T *tv, int curtab_only)
2716{
2717 char_u *name = tv->vval.v_string;
2718 buf_T *buf;
2719
2720 if (tv->v_type == VAR_NUMBER)
2721 return buflist_findnr((int)tv->vval.v_number);
2722 if (tv->v_type != VAR_STRING)
2723 return NULL;
2724 if (name == NULL || *name == NUL)
2725 return curbuf;
2726 if (name[0] == '$' && name[1] == NUL)
2727 return lastbuf;
2728
2729 buf = buflist_find_by_name(name, curtab_only);
2730
2731 // If not found, try expanding the name, like done for bufexists().
2732 if (buf == NULL)
2733 buf = find_buffer(tv);
2734
2735 return buf;
2736}
2737
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002738/*
2739 * Like tv_get_buf() but give an error message is the type is wrong.
2740 */
2741 buf_T *
2742tv_get_buf_from_arg(typval_T *tv)
2743{
2744 buf_T *buf;
2745
2746 ++emsg_off;
2747 buf = tv_get_buf(tv, FALSE);
2748 --emsg_off;
2749 if (buf == NULL
2750 && tv->v_type != VAR_NUMBER
2751 && tv->v_type != VAR_STRING)
2752 // issue errmsg for type error
2753 (void)tv_get_number(tv);
2754 return buf;
2755}
2756
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002757#endif // FEAT_EVAL