blob: 75cc336f87a34ec3105ed491f6a3dd48d7373e60 [file] [log] [blame]
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * typval.c: functions that deal with a typval
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
18/*
19 * Allocate memory for a variable type-value, and make it empty (0 or NULL
20 * value).
21 */
22 typval_T *
23alloc_tv(void)
24{
25 return ALLOC_CLEAR_ONE(typval_T);
26}
27
28/*
29 * Allocate memory for a variable type-value, and assign a string to it.
30 * The string "s" must have been allocated, it is consumed.
31 * Return NULL for out of memory, the variable otherwise.
32 */
33 typval_T *
34alloc_string_tv(char_u *s)
35{
36 typval_T *rettv;
37
38 rettv = alloc_tv();
39 if (rettv != NULL)
40 {
41 rettv->v_type = VAR_STRING;
42 rettv->vval.v_string = s;
43 }
44 else
45 vim_free(s);
46 return rettv;
47}
48
49/*
50 * Free the memory for a variable type-value.
51 */
52 void
53free_tv(typval_T *varp)
54{
55 if (varp != NULL)
56 {
57 switch (varp->v_type)
58 {
59 case VAR_FUNC:
60 func_unref(varp->vval.v_string);
61 // FALLTHROUGH
62 case VAR_STRING:
63 vim_free(varp->vval.v_string);
64 break;
65 case VAR_PARTIAL:
66 partial_unref(varp->vval.v_partial);
67 break;
68 case VAR_BLOB:
69 blob_unref(varp->vval.v_blob);
70 break;
71 case VAR_LIST:
72 list_unref(varp->vval.v_list);
73 break;
74 case VAR_DICT:
75 dict_unref(varp->vval.v_dict);
76 break;
77 case VAR_JOB:
78#ifdef FEAT_JOB_CHANNEL
79 job_unref(varp->vval.v_job);
80 break;
81#endif
82 case VAR_CHANNEL:
83#ifdef FEAT_JOB_CHANNEL
84 channel_unref(varp->vval.v_channel);
85 break;
86#endif
87 case VAR_NUMBER:
88 case VAR_FLOAT:
89 case VAR_ANY:
90 case VAR_UNKNOWN:
91 case VAR_VOID:
92 case VAR_BOOL:
93 case VAR_SPECIAL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +020094 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +020095 break;
96 }
97 vim_free(varp);
98 }
99}
100
101/*
102 * Free the memory for a variable value and set the value to NULL or 0.
103 */
104 void
105clear_tv(typval_T *varp)
106{
107 if (varp != NULL)
108 {
109 switch (varp->v_type)
110 {
111 case VAR_FUNC:
112 func_unref(varp->vval.v_string);
113 // FALLTHROUGH
114 case VAR_STRING:
115 VIM_CLEAR(varp->vval.v_string);
116 break;
117 case VAR_PARTIAL:
118 partial_unref(varp->vval.v_partial);
119 varp->vval.v_partial = NULL;
120 break;
121 case VAR_BLOB:
122 blob_unref(varp->vval.v_blob);
123 varp->vval.v_blob = NULL;
124 break;
125 case VAR_LIST:
126 list_unref(varp->vval.v_list);
127 varp->vval.v_list = NULL;
128 break;
129 case VAR_DICT:
130 dict_unref(varp->vval.v_dict);
131 varp->vval.v_dict = NULL;
132 break;
133 case VAR_NUMBER:
134 case VAR_BOOL:
135 case VAR_SPECIAL:
136 varp->vval.v_number = 0;
137 break;
138 case VAR_FLOAT:
139#ifdef FEAT_FLOAT
140 varp->vval.v_float = 0.0;
141 break;
142#endif
143 case VAR_JOB:
144#ifdef FEAT_JOB_CHANNEL
145 job_unref(varp->vval.v_job);
146 varp->vval.v_job = NULL;
147#endif
148 break;
149 case VAR_CHANNEL:
150#ifdef FEAT_JOB_CHANNEL
151 channel_unref(varp->vval.v_channel);
152 varp->vval.v_channel = NULL;
153#endif
Bram Moolenaar24f72092021-05-07 20:43:54 +0200154 break;
155 case VAR_INSTR:
156 VIM_CLEAR(varp->vval.v_instr);
157 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200158 case VAR_UNKNOWN:
159 case VAR_ANY:
160 case VAR_VOID:
161 break;
162 }
163 varp->v_lock = 0;
164 }
165}
166
167/*
168 * Set the value of a variable to NULL without freeing items.
169 */
170 void
171init_tv(typval_T *varp)
172{
173 if (varp != NULL)
174 CLEAR_POINTER(varp);
175}
176
Bram Moolenaar36967b32020-08-17 21:41:02 +0200177 static varnumber_T
178tv_get_bool_or_number_chk(typval_T *varp, int *denote, int want_bool)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200179{
180 varnumber_T n = 0L;
181
182 switch (varp->v_type)
183 {
184 case VAR_NUMBER:
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200185 if (in_vim9script() && want_bool && varp->vval.v_number != 0
Bram Moolenaard70840e2020-08-22 15:06:35 +0200186 && varp->vval.v_number != 1)
187 {
188 semsg(_(e_using_number_as_bool_nr), varp->vval.v_number);
189 break;
190 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200191 return varp->vval.v_number;
192 case VAR_FLOAT:
193#ifdef FEAT_FLOAT
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000194 emsg(_(e_using_float_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200195 break;
196#endif
197 case VAR_FUNC:
198 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000199 emsg(_(e_using_funcref_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200200 break;
201 case VAR_STRING:
Bram Moolenaar56acb092020-08-16 14:48:19 +0200202 if (in_vim9script())
203 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +0100204 emsg_using_string_as(varp, !want_bool);
Bram Moolenaar56acb092020-08-16 14:48:19 +0200205 break;
206 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200207 if (varp->vval.v_string != NULL)
208 vim_str2nr(varp->vval.v_string, NULL, NULL,
209 STR2NR_ALL, &n, NULL, 0, FALSE);
210 return n;
211 case VAR_LIST:
Bram Moolenaar677658a2022-01-05 16:09:06 +0000212 emsg(_(e_using_list_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200213 break;
214 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000215 emsg(_(e_using_dictionary_as_number));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200216 break;
217 case VAR_BOOL:
218 case VAR_SPECIAL:
Bram Moolenaar36967b32020-08-17 21:41:02 +0200219 if (!want_bool && in_vim9script())
Bram Moolenaar56acb092020-08-16 14:48:19 +0200220 {
Bram Moolenaard92cc132020-11-18 17:17:15 +0100221 if (varp->v_type == VAR_BOOL)
222 emsg(_(e_using_bool_as_number));
223 else
Bram Moolenaard88be5b2022-01-04 19:57:55 +0000224 emsg(_(e_using_special_as_number));
Bram Moolenaar56acb092020-08-16 14:48:19 +0200225 break;
226 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200227 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
228 case VAR_JOB:
229#ifdef FEAT_JOB_CHANNEL
230 emsg(_("E910: Using a Job as a Number"));
231 break;
232#endif
233 case VAR_CHANNEL:
234#ifdef FEAT_JOB_CHANNEL
235 emsg(_("E913: Using a Channel as a Number"));
236 break;
237#endif
238 case VAR_BLOB:
239 emsg(_("E974: Using a Blob as a Number"));
240 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200241 case VAR_VOID:
242 emsg(_(e_cannot_use_void_value));
243 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200244 case VAR_UNKNOWN:
245 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200246 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200247 internal_error_no_abort("tv_get_number(UNKNOWN)");
248 break;
249 }
250 if (denote == NULL) // useful for values that must be unsigned
251 n = -1;
252 else
253 *denote = TRUE;
254 return n;
255}
256
Bram Moolenaar36967b32020-08-17 21:41:02 +0200257/*
258 * Get the number value of a variable.
259 * If it is a String variable, uses vim_str2nr().
260 * For incompatible types, return 0.
261 * tv_get_number_chk() is similar to tv_get_number(), but informs the
262 * caller of incompatible types: it sets *denote to TRUE if "denote"
263 * is not NULL or returns -1 otherwise.
264 */
265 varnumber_T
266tv_get_number(typval_T *varp)
267{
268 int error = FALSE;
269
270 return tv_get_number_chk(varp, &error); // return 0L on error
271}
272
273 varnumber_T
274tv_get_number_chk(typval_T *varp, int *denote)
275{
276 return tv_get_bool_or_number_chk(varp, denote, FALSE);
277}
278
279/*
280 * Get the boolean value of "varp". This is like tv_get_number_chk(),
Bram Moolenaard70840e2020-08-22 15:06:35 +0200281 * but in Vim9 script accepts Number (0 and 1) and Bool/Special.
Bram Moolenaar36967b32020-08-17 21:41:02 +0200282 */
283 varnumber_T
284tv_get_bool(typval_T *varp)
285{
286 return tv_get_bool_or_number_chk(varp, NULL, TRUE);
Bram Moolenaar36967b32020-08-17 21:41:02 +0200287}
288
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200289/*
290 * Get the boolean value of "varp". This is like tv_get_number_chk(),
291 * but in Vim9 script accepts Number and Bool.
292 */
293 varnumber_T
294tv_get_bool_chk(typval_T *varp, int *denote)
295{
296 return tv_get_bool_or_number_chk(varp, denote, TRUE);
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200297}
298
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200299#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000300 static float_T
301tv_get_float_chk(typval_T *varp, int *error)
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200302{
303 switch (varp->v_type)
304 {
305 case VAR_NUMBER:
306 return (float_T)(varp->vval.v_number);
307 case VAR_FLOAT:
308 return varp->vval.v_float;
309 case VAR_FUNC:
310 case VAR_PARTIAL:
311 emsg(_("E891: Using a Funcref as a Float"));
312 break;
313 case VAR_STRING:
314 emsg(_("E892: Using a String as a Float"));
315 break;
316 case VAR_LIST:
317 emsg(_("E893: Using a List as a Float"));
318 break;
319 case VAR_DICT:
320 emsg(_("E894: Using a Dictionary as a Float"));
321 break;
322 case VAR_BOOL:
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000323 emsg(_(e_using_boolean_valud_as_float));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200324 break;
325 case VAR_SPECIAL:
326 emsg(_("E907: Using a special value as a Float"));
327 break;
328 case VAR_JOB:
329# ifdef FEAT_JOB_CHANNEL
330 emsg(_("E911: Using a Job as a Float"));
331 break;
332# endif
333 case VAR_CHANNEL:
334# ifdef FEAT_JOB_CHANNEL
335 emsg(_("E914: Using a Channel as a Float"));
336 break;
337# endif
338 case VAR_BLOB:
339 emsg(_("E975: Using a Blob as a Float"));
340 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200341 case VAR_VOID:
342 emsg(_(e_cannot_use_void_value));
343 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200344 case VAR_UNKNOWN:
345 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200346 case VAR_INSTR:
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200347 internal_error_no_abort("tv_get_float(UNKNOWN)");
348 break;
349 }
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000350 if (error != NULL)
351 *error = TRUE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200352 return 0;
353}
Bram Moolenaar28fbbea2021-12-22 21:40:33 +0000354
355 float_T
356tv_get_float(typval_T *varp)
357{
358 return tv_get_float_chk(varp, NULL);
359}
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200360#endif
361
362/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200363 * Give an error and return FAIL unless "args[idx]" is a string.
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100364 */
365 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100366check_for_string_arg(typval_T *args, int idx)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100367{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100368 if (args[idx].v_type != VAR_STRING)
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100369 {
rbtnnddc80af2021-12-17 18:01:31 +0000370 semsg(_(e_string_required_for_argument_nr), idx + 1);
Bram Moolenaar7bb4e742020-12-09 12:41:50 +0100371 return FAIL;
372 }
373 return OK;
374}
375
376/*
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100377 * Give an error and return FAIL unless "args[idx]" is a non-empty string.
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100378 */
379 int
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100380check_for_nonempty_string_arg(typval_T *args, int idx)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100381{
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100382 if (check_for_string_arg(args, idx) == FAIL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100383 return FAIL;
Bram Moolenaar32105ae2021-03-27 18:59:25 +0100384 if (args[idx].vval.v_string == NULL || *args[idx].vval.v_string == NUL)
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100385 {
Bram Moolenaare8e30782021-04-10 21:46:05 +0200386 semsg(_(e_non_empty_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2a9d5d32020-12-12 18:58:40 +0100387 return FAIL;
388 }
389 return OK;
390}
391
392/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200393 * Check for an optional string argument at 'idx'
394 */
395 int
396check_for_opt_string_arg(typval_T *args, int idx)
397{
398 return (args[idx].v_type == VAR_UNKNOWN
399 || check_for_string_arg(args, idx) != FAIL);
400}
401
402/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200403 * Give an error and return FAIL unless "args[idx]" is a number.
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200404 */
405 int
406check_for_number_arg(typval_T *args, int idx)
407{
408 if (args[idx].v_type != VAR_NUMBER)
409 {
rbtnnddc80af2021-12-17 18:01:31 +0000410 semsg(_(e_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200411 return FAIL;
412 }
413 return OK;
414}
415
416/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200417 * Check for an optional number argument at 'idx'
418 */
419 int
420check_for_opt_number_arg(typval_T *args, int idx)
421{
422 return (args[idx].v_type == VAR_UNKNOWN
423 || check_for_number_arg(args, idx) != FAIL);
424}
425
426/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200427 * Give an error and return FAIL unless "args[idx]" is a float or a number.
428 */
429 int
430check_for_float_or_nr_arg(typval_T *args, int idx)
431{
432 if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER)
433 {
rbtnnddc80af2021-12-17 18:01:31 +0000434 semsg(_(e_float_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200435 return FAIL;
436 }
437 return OK;
438}
439
440/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200441 * Give an error and return FAIL unless "args[idx]" is a bool.
442 */
443 int
444check_for_bool_arg(typval_T *args, int idx)
445{
446 if (args[idx].v_type != VAR_BOOL
447 && !(args[idx].v_type == VAR_NUMBER
448 && (args[idx].vval.v_number == 0
449 || args[idx].vval.v_number == 1)))
450 {
rbtnnddc80af2021-12-17 18:01:31 +0000451 semsg(_(e_bool_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200452 return FAIL;
453 }
454 return OK;
455}
456
457/*
Bram Moolenaara29856f2021-09-13 21:36:27 +0200458 * Check for an optional bool argument at 'idx'.
459 * Return FAIL if the type is wrong.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200460 */
461 int
462check_for_opt_bool_arg(typval_T *args, int idx)
463{
Bram Moolenaara29856f2021-09-13 21:36:27 +0200464 if (args[idx].v_type == VAR_UNKNOWN)
465 return OK;
466 return check_for_bool_arg(args, idx);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200467}
468
469/*
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200470 * Give an error and return FAIL unless "args[idx]" is a blob.
471 */
472 int
473check_for_blob_arg(typval_T *args, int idx)
474{
475 if (args[idx].v_type != VAR_BLOB)
476 {
rbtnnddc80af2021-12-17 18:01:31 +0000477 semsg(_(e_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5dfe4672021-09-14 17:54:30 +0200478 return FAIL;
479 }
480 return OK;
481}
482
483/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200484 * Give an error and return FAIL unless "args[idx]" is a list.
485 */
486 int
487check_for_list_arg(typval_T *args, int idx)
488{
489 if (args[idx].v_type != VAR_LIST)
490 {
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200491 semsg(_(e_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200492 return FAIL;
493 }
494 return OK;
495}
496
497/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200498 * Check for an optional list argument at 'idx'
499 */
500 int
501check_for_opt_list_arg(typval_T *args, int idx)
502{
503 return (args[idx].v_type == VAR_UNKNOWN
504 || check_for_list_arg(args, idx) != FAIL);
505}
506
507/*
Yegappan Lakshmanana9a7c0c2021-07-17 19:11:07 +0200508 * Give an error and return FAIL unless "args[idx]" is a dict.
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200509 */
510 int
511check_for_dict_arg(typval_T *args, int idx)
512{
513 if (args[idx].v_type != VAR_DICT)
514 {
rbtnnddc80af2021-12-17 18:01:31 +0000515 semsg(_(e_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200516 return FAIL;
517 }
518 return OK;
519}
520
521/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200522 * Check for an optional dict argument at 'idx'
523 */
524 int
525check_for_opt_dict_arg(typval_T *args, int idx)
526{
527 return (args[idx].v_type == VAR_UNKNOWN
528 || check_for_dict_arg(args, idx) != FAIL);
529}
530
531/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200532 * Give an error and return FAIL unless "args[idx]" is a channel or a job.
533 */
534 int
535check_for_chan_or_job_arg(typval_T *args, int idx)
536{
537 if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
538 {
rbtnnddc80af2021-12-17 18:01:31 +0000539 semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200540 return FAIL;
541 }
542 return OK;
543}
544
545/*
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +0200546 * Give an error and return FAIL unless "args[idx]" is an optional channel or a
547 * job.
548 */
549 int
550check_for_opt_chan_or_job_arg(typval_T *args, int idx)
551{
552 return (args[idx].v_type == VAR_UNKNOWN
553 || check_for_chan_or_job_arg(args, idx) != FAIL);
554}
555
556/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200557 * Give an error and return FAIL unless "args[idx]" is a job.
558 */
559 int
560check_for_job_arg(typval_T *args, int idx)
561{
562 if (args[idx].v_type != VAR_JOB)
563 {
rbtnnddc80af2021-12-17 18:01:31 +0000564 semsg(_(e_job_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200565 return FAIL;
566 }
567 return OK;
568}
569
570/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200571 * Check for an optional job argument at 'idx'.
572 */
573 int
574check_for_opt_job_arg(typval_T *args, int idx)
575{
576 return (args[idx].v_type == VAR_UNKNOWN
577 || check_for_job_arg(args, idx) != FAIL);
578}
579
580/*
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200581 * Give an error and return FAIL unless "args[idx]" is a string or
582 * a number.
583 */
584 int
585check_for_string_or_number_arg(typval_T *args, int idx)
586{
587 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
588 {
rbtnnddc80af2021-12-17 18:01:31 +0000589 semsg(_(e_string_or_number_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200590 return FAIL;
591 }
592 return OK;
593}
594
595/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200596 * Check for an optional string or number argument at 'idx'.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200597 */
598 int
599check_for_opt_string_or_number_arg(typval_T *args, int idx)
600{
601 return (args[idx].v_type == VAR_UNKNOWN
602 || check_for_string_or_number_arg(args, idx) != FAIL);
603}
604
605/*
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200606 * Give an error and return FAIL unless "args[idx]" is a buffer number.
607 * Buffer number can be a number or a string.
608 */
609 int
610check_for_buffer_arg(typval_T *args, int idx)
611{
612 return check_for_string_or_number_arg(args, idx);
613}
614
615/*
616 * Check for an optional buffer argument at 'idx'
617 */
618 int
619check_for_opt_buffer_arg(typval_T *args, int idx)
620{
621 return (args[idx].v_type == VAR_UNKNOWN
622 || check_for_buffer_arg(args, idx));
623}
624
625/*
626 * Give an error and return FAIL unless "args[idx]" is a line number.
627 * Line number can be a number or a string.
628 */
629 int
630check_for_lnum_arg(typval_T *args, int idx)
631{
632 return check_for_string_or_number_arg(args, idx);
633}
634
635/*
636 * Check for an optional line number argument at 'idx'
637 */
638 int
639check_for_opt_lnum_arg(typval_T *args, int idx)
640{
641 return (args[idx].v_type == VAR_UNKNOWN
642 || check_for_lnum_arg(args, idx));
643}
644
645/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200646 * Give an error and return FAIL unless "args[idx]" is a string or a blob.
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200647 */
648 int
649check_for_string_or_blob_arg(typval_T *args, int idx)
650{
651 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
652 {
rbtnnddc80af2021-12-17 18:01:31 +0000653 semsg(_(e_string_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200654 return FAIL;
655 }
656 return OK;
657}
658
659/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200660 * Give an error and return FAIL unless "args[idx]" is a string or a list.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200661 */
662 int
663check_for_string_or_list_arg(typval_T *args, int idx)
664{
665 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_LIST)
666 {
rbtnnddc80af2021-12-17 18:01:31 +0000667 semsg(_(e_string_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200668 return FAIL;
669 }
670 return OK;
671}
672
673/*
rbtnn0ccb5842021-12-18 18:33:46 +0000674 * Give an error and return FAIL unless "args[idx]" is a string, a list or a
675 * blob.
676 */
677 int
678check_for_string_or_list_or_blob_arg(typval_T *args, int idx)
679{
680 if (args[idx].v_type != VAR_STRING
681 && args[idx].v_type != VAR_LIST
682 && args[idx].v_type != VAR_BLOB)
683 {
684 semsg(_(e_string_list_or_blob_required_for_argument_nr), idx + 1);
685 return FAIL;
686 }
687 return OK;
688}
689
690/*
Yegappan Lakshmanana764e732021-07-25 15:57:32 +0200691 * Check for an optional string or list argument at 'idx'
692 */
693 int
694check_for_opt_string_or_list_arg(typval_T *args, int idx)
695{
696 return (args[idx].v_type == VAR_UNKNOWN
697 || check_for_string_or_list_arg(args, idx));
698}
699
700/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200701 * Give an error and return FAIL unless "args[idx]" is a string or a dict.
702 */
703 int
704check_for_string_or_dict_arg(typval_T *args, int idx)
705{
706 if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_DICT)
707 {
rbtnnddc80af2021-12-17 18:01:31 +0000708 semsg(_(e_string_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200709 return FAIL;
710 }
711 return OK;
712}
713
714/*
715 * Give an error and return FAIL unless "args[idx]" is a string or a number
716 * or a list.
717 */
718 int
719check_for_string_or_number_or_list_arg(typval_T *args, int idx)
720{
721 if (args[idx].v_type != VAR_STRING
722 && args[idx].v_type != VAR_NUMBER
723 && args[idx].v_type != VAR_LIST)
724 {
rbtnn0ccb5842021-12-18 18:33:46 +0000725 semsg(_(e_string_number_or_list_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200726 return FAIL;
727 }
728 return OK;
729}
730
731/*
Yegappan Lakshmanan7e6a2a62021-07-28 11:51:48 +0200732 * Give an error and return FAIL unless "args[idx]" is an optional string
733 * or number or a list
734 */
735 int
736check_for_opt_string_or_number_or_list_arg(typval_T *args, int idx)
737{
738 return (args[idx].v_type == VAR_UNKNOWN
739 || check_for_string_or_number_or_list_arg(args, idx) != FAIL);
740}
741
742/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200743 * Give an error and return FAIL unless "args[idx]" is a string or a list
744 * or a dict.
745 */
746 int
747check_for_string_or_list_or_dict_arg(typval_T *args, int idx)
748{
749 if (args[idx].v_type != VAR_STRING
750 && args[idx].v_type != VAR_LIST
751 && args[idx].v_type != VAR_DICT)
752 {
rbtnnddc80af2021-12-17 18:01:31 +0000753 semsg(_(e_string_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200754 return FAIL;
755 }
756 return OK;
757}
758
759/*
Bram Moolenaar223d0a62021-12-25 19:29:21 +0000760 * Give an error and return FAIL unless "args[idx]" is a string
761 * or a function reference.
762 */
763 int
764check_for_string_or_func_arg(typval_T *args, int idx)
765{
766 if (args[idx].v_type != VAR_PARTIAL
767 && args[idx].v_type != VAR_FUNC
768 && args[idx].v_type != VAR_STRING)
769 {
770 semsg(_(e_string_or_function_required_for_argument_nr), idx + 1);
771 return FAIL;
772 }
773 return OK;
774}
775
776/*
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200777 * Give an error and return FAIL unless "args[idx]" is a list or a blob.
778 */
779 int
780check_for_list_or_blob_arg(typval_T *args, int idx)
781{
782 if (args[idx].v_type != VAR_LIST && args[idx].v_type != VAR_BLOB)
783 {
rbtnn0ccb5842021-12-18 18:33:46 +0000784 semsg(_(e_list_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200785 return FAIL;
786 }
787 return OK;
788}
789
790/*
791 * Give an error and return FAIL unless "args[idx]" is a list or dict
792 */
793 int
794check_for_list_or_dict_arg(typval_T *args, int idx)
795{
796 if (args[idx].v_type != VAR_LIST
797 && args[idx].v_type != VAR_DICT)
798 {
rbtnnddc80af2021-12-17 18:01:31 +0000799 semsg(_(e_list_or_dict_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200800 return FAIL;
801 }
802 return OK;
803}
804
805/*
806 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
807 * blob.
808 */
809 int
810check_for_list_or_dict_or_blob_arg(typval_T *args, int idx)
811{
812 if (args[idx].v_type != VAR_LIST
813 && args[idx].v_type != VAR_DICT
814 && args[idx].v_type != VAR_BLOB)
815 {
rbtnnddc80af2021-12-17 18:01:31 +0000816 semsg(_(e_list_dict_or_blob_required_for_argument_nr), idx + 1);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200817 return FAIL;
818 }
819 return OK;
820}
821
822/*
Bram Moolenaar2d877592021-12-16 08:21:09 +0000823 * Give an error and return FAIL unless "args[idx]" is a list or dict or a
824 * blob or a string.
825 */
826 int
827check_for_list_or_dict_or_blob_or_string_arg(typval_T *args, int idx)
828{
829 if (args[idx].v_type != VAR_LIST
830 && args[idx].v_type != VAR_DICT
831 && args[idx].v_type != VAR_BLOB
832 && args[idx].v_type != VAR_STRING)
833 {
rbtnnddc80af2021-12-17 18:01:31 +0000834 semsg(_(e_list_dict_blob_or_string_required_for_argument_nr), idx + 1);
Bram Moolenaar2d877592021-12-16 08:21:09 +0000835 return FAIL;
836 }
837 return OK;
838}
839
840/*
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200841 * Give an error and return FAIL unless "args[idx]" is an optional buffer
842 * number or a dict.
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200843 */
844 int
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200845check_for_opt_buffer_or_dict_arg(typval_T *args, int idx)
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200846{
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200847 if (args[idx].v_type != VAR_UNKNOWN
848 && args[idx].v_type != VAR_STRING
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200849 && args[idx].v_type != VAR_NUMBER
850 && args[idx].v_type != VAR_DICT)
851 {
rbtnnddc80af2021-12-17 18:01:31 +0000852 semsg(_(e_string_required_for_argument_nr), idx + 1);
Yegappan Lakshmanancd917202021-07-21 19:09:09 +0200853 return FAIL;
854 }
855 return OK;
856}
857
858/*
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200859 * Get the string value of a variable.
860 * If it is a Number variable, the number is converted into a string.
861 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
862 * tv_get_string_buf() uses a given buffer.
863 * If the String variable has never been set, return an empty string.
864 * Never returns NULL;
865 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
866 * NULL on error.
867 */
868 char_u *
869tv_get_string(typval_T *varp)
870{
871 static char_u mybuf[NUMBUFLEN];
872
873 return tv_get_string_buf(varp, mybuf);
874}
875
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100876/*
877 * Like tv_get_string() but don't allow number to string conversion for Vim9.
878 */
879 char_u *
880tv_get_string_strict(typval_T *varp)
881{
882 static char_u mybuf[NUMBUFLEN];
883 char_u *res = tv_get_string_buf_chk_strict(
884 varp, mybuf, in_vim9script());
885
886 return res != NULL ? res : (char_u *)"";
887}
888
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200889 char_u *
890tv_get_string_buf(typval_T *varp, char_u *buf)
891{
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200892 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200893
894 return res != NULL ? res : (char_u *)"";
895}
896
897/*
898 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
899 */
900 char_u *
901tv_get_string_chk(typval_T *varp)
902{
903 static char_u mybuf[NUMBUFLEN];
904
905 return tv_get_string_buf_chk(varp, mybuf);
906}
907
908 char_u *
909tv_get_string_buf_chk(typval_T *varp, char_u *buf)
910{
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100911 return tv_get_string_buf_chk_strict(varp, buf, FALSE);
912}
913
914 char_u *
915tv_get_string_buf_chk_strict(typval_T *varp, char_u *buf, int strict)
916{
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200917 switch (varp->v_type)
918 {
919 case VAR_NUMBER:
Bram Moolenaarf2b26bc2021-01-30 23:05:11 +0100920 if (strict)
921 {
922 emsg(_(e_using_number_as_string));
923 break;
924 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200925 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
926 (varnumber_T)varp->vval.v_number);
927 return buf;
928 case VAR_FUNC:
929 case VAR_PARTIAL:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000930 emsg(_(e_using_funcref_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200931 break;
932 case VAR_LIST:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000933 emsg(_(e_using_list_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200934 break;
935 case VAR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +0000936 emsg(_(e_using_dictionary_as_string));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200937 break;
938 case VAR_FLOAT:
939#ifdef FEAT_FLOAT
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200940 if (strict)
941 {
Bram Moolenaar0699b042022-01-01 16:01:23 +0000942 emsg(_(e_using_float_as_string));
Bram Moolenaar7a2217b2021-06-06 12:33:49 +0200943 break;
944 }
945 vim_snprintf((char *)buf, NUMBUFLEN, "%g", varp->vval.v_float);
946 return buf;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200947#endif
948 case VAR_STRING:
949 if (varp->vval.v_string != NULL)
950 return varp->vval.v_string;
951 return (char_u *)"";
952 case VAR_BOOL:
953 case VAR_SPECIAL:
954 STRCPY(buf, get_var_special_name(varp->vval.v_number));
955 return buf;
956 case VAR_BLOB:
Yegappan Lakshmanan5b739922021-07-10 13:15:41 +0200957 emsg(_("E976: Using a Blob as a String"));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200958 break;
959 case VAR_JOB:
960#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200961 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200962 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200963 semsg(_(e_using_invalid_value_as_string_str), "job");
964 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200965 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200966 return job_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200967#endif
968 break;
969 case VAR_CHANNEL:
970#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200971 if (in_vim9script())
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200972 {
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200973 semsg(_(e_using_invalid_value_as_string_str), "channel");
974 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200975 }
Bram Moolenaar1328bde2021-06-05 20:51:38 +0200976 return channel_to_string_buf(varp, buf);
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200977#endif
978 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +0200979 case VAR_VOID:
980 emsg(_(e_cannot_use_void_value));
981 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200982 case VAR_UNKNOWN:
983 case VAR_ANY:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200984 case VAR_INSTR:
Bram Moolenaar68db9962021-05-09 23:19:22 +0200985 semsg(_(e_using_invalid_value_as_string_str),
986 vartype_name(varp->v_type));
Bram Moolenaar367d59e2020-05-30 17:06:14 +0200987 break;
988 }
989 return NULL;
990}
991
992/*
993 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
994 * string() on Dict, List, etc.
995 */
996 char_u *
997tv_stringify(typval_T *varp, char_u *buf)
998{
999 if (varp->v_type == VAR_LIST
1000 || varp->v_type == VAR_DICT
1001 || varp->v_type == VAR_BLOB
1002 || varp->v_type == VAR_FUNC
1003 || varp->v_type == VAR_PARTIAL
1004 || varp->v_type == VAR_FLOAT)
1005 {
1006 typval_T tmp;
1007
1008 f_string(varp, &tmp);
1009 tv_get_string_buf(&tmp, buf);
1010 clear_tv(varp);
1011 *varp = tmp;
1012 return tmp.vval.v_string;
1013 }
1014 return tv_get_string_buf(varp, buf);
1015}
1016
1017/*
1018 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
1019 * Also give an error message, using "name" or _("name") when use_gettext is
1020 * TRUE.
1021 */
1022 int
1023tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
1024{
1025 int lock = 0;
1026
1027 switch (tv->v_type)
1028 {
1029 case VAR_BLOB:
1030 if (tv->vval.v_blob != NULL)
1031 lock = tv->vval.v_blob->bv_lock;
1032 break;
1033 case VAR_LIST:
1034 if (tv->vval.v_list != NULL)
1035 lock = tv->vval.v_list->lv_lock;
1036 break;
1037 case VAR_DICT:
1038 if (tv->vval.v_dict != NULL)
1039 lock = tv->vval.v_dict->dv_lock;
1040 break;
1041 default:
1042 break;
1043 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001044 return value_check_lock(tv->v_lock, name, use_gettext)
1045 || (lock != 0 && value_check_lock(lock, name, use_gettext));
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001046}
1047
1048/*
1049 * Copy the values from typval_T "from" to typval_T "to".
1050 * When needed allocates string or increases reference count.
1051 * Does not make a copy of a list, blob or dict but copies the reference!
1052 * It is OK for "from" and "to" to point to the same item. This is used to
1053 * make a copy later.
1054 */
1055 void
1056copy_tv(typval_T *from, typval_T *to)
1057{
1058 to->v_type = from->v_type;
1059 to->v_lock = 0;
1060 switch (from->v_type)
1061 {
1062 case VAR_NUMBER:
1063 case VAR_BOOL:
1064 case VAR_SPECIAL:
1065 to->vval.v_number = from->vval.v_number;
1066 break;
1067 case VAR_FLOAT:
1068#ifdef FEAT_FLOAT
1069 to->vval.v_float = from->vval.v_float;
1070 break;
1071#endif
1072 case VAR_JOB:
1073#ifdef FEAT_JOB_CHANNEL
1074 to->vval.v_job = from->vval.v_job;
1075 if (to->vval.v_job != NULL)
1076 ++to->vval.v_job->jv_refcount;
1077 break;
1078#endif
1079 case VAR_CHANNEL:
1080#ifdef FEAT_JOB_CHANNEL
1081 to->vval.v_channel = from->vval.v_channel;
1082 if (to->vval.v_channel != NULL)
1083 ++to->vval.v_channel->ch_refcount;
1084 break;
1085#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001086 case VAR_INSTR:
1087 to->vval.v_instr = from->vval.v_instr;
1088 break;
1089
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001090 case VAR_STRING:
1091 case VAR_FUNC:
1092 if (from->vval.v_string == NULL)
1093 to->vval.v_string = NULL;
1094 else
1095 {
1096 to->vval.v_string = vim_strsave(from->vval.v_string);
1097 if (from->v_type == VAR_FUNC)
1098 func_ref(to->vval.v_string);
1099 }
1100 break;
1101 case VAR_PARTIAL:
1102 if (from->vval.v_partial == NULL)
1103 to->vval.v_partial = NULL;
1104 else
1105 {
1106 to->vval.v_partial = from->vval.v_partial;
1107 ++to->vval.v_partial->pt_refcount;
1108 }
1109 break;
1110 case VAR_BLOB:
1111 if (from->vval.v_blob == NULL)
1112 to->vval.v_blob = NULL;
1113 else
1114 {
1115 to->vval.v_blob = from->vval.v_blob;
1116 ++to->vval.v_blob->bv_refcount;
1117 }
1118 break;
1119 case VAR_LIST:
1120 if (from->vval.v_list == NULL)
1121 to->vval.v_list = NULL;
1122 else
1123 {
1124 to->vval.v_list = from->vval.v_list;
1125 ++to->vval.v_list->lv_refcount;
1126 }
1127 break;
1128 case VAR_DICT:
1129 if (from->vval.v_dict == NULL)
1130 to->vval.v_dict = NULL;
1131 else
1132 {
1133 to->vval.v_dict = from->vval.v_dict;
1134 ++to->vval.v_dict->dv_refcount;
1135 }
1136 break;
Bram Moolenaar61a417b2021-06-15 22:54:28 +02001137 case VAR_VOID:
1138 emsg(_(e_cannot_use_void_value));
1139 break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001140 case VAR_UNKNOWN:
1141 case VAR_ANY:
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001142 internal_error_no_abort("copy_tv(UNKNOWN)");
1143 break;
1144 }
1145}
1146
1147/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001148 * Compare "tv1" and "tv2".
1149 * Put the result in "tv1". Caller should clear "tv2".
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001150 */
1151 int
1152typval_compare(
Bram Moolenaar265f8112021-12-19 12:33:05 +00001153 typval_T *tv1, // first operand
1154 typval_T *tv2, // second operand
1155 exprtype_T type, // operator
1156 int ic) // ignore case
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001157{
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001158 varnumber_T n1, n2;
Bram Moolenaar265f8112021-12-19 12:33:05 +00001159 int res = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001160 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
1161
Bram Moolenaar265f8112021-12-19 12:33:05 +00001162 if (type_is && tv1->v_type != tv2->v_type)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001163 {
1164 // For "is" a different type always means FALSE, for "notis"
1165 // it means TRUE.
1166 n1 = (type == EXPR_ISNOT);
1167 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001168 else if (tv1->v_type == VAR_BLOB || tv2->v_type == VAR_BLOB)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001169 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001170 if (typval_compare_blob(tv1, tv2, type, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001171 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001172 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001173 return FAIL;
1174 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001175 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001176 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001177 else if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001178 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001179 if (typval_compare_list(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001180 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001181 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001182 return FAIL;
1183 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001184 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001185 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001186 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001187 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001188 if (typval_compare_dict(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001189 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001190 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001191 return FAIL;
1192 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001193 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001194 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001195 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC
1196 || tv1->v_type == VAR_PARTIAL || tv2->v_type == VAR_PARTIAL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001197 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001198 if (typval_compare_func(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001199 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001200 clear_tv(tv1);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001201 return FAIL;
1202 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001203 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001204 }
1205
1206#ifdef FEAT_FLOAT
1207 // If one of the two variables is a float, compare as a float.
1208 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001209 else if ((tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001210 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1211 {
1212 float_T f1, f2;
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001213 int error = FALSE;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001214
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001215 f1 = tv_get_float_chk(tv1, &error);
1216 if (!error)
1217 f2 = tv_get_float_chk(tv2, &error);
1218 if (error)
1219 {
1220 clear_tv(tv1);
1221 return FAIL;
1222 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001223 n1 = FALSE;
1224 switch (type)
1225 {
1226 case EXPR_IS:
1227 case EXPR_EQUAL: n1 = (f1 == f2); break;
1228 case EXPR_ISNOT:
1229 case EXPR_NEQUAL: n1 = (f1 != f2); break;
1230 case EXPR_GREATER: n1 = (f1 > f2); break;
1231 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
1232 case EXPR_SMALLER: n1 = (f1 < f2); break;
1233 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
1234 case EXPR_UNKNOWN:
1235 case EXPR_MATCH:
1236 default: break; // avoid gcc warning
1237 }
1238 }
1239#endif
1240
1241 // If one of the two variables is a number, compare as a number.
1242 // When using "=~" or "!~", always compare as string.
Bram Moolenaar265f8112021-12-19 12:33:05 +00001243 else if ((tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001244 && type != EXPR_MATCH && type != EXPR_NOMATCH)
1245 {
Bram Moolenaar28fbbea2021-12-22 21:40:33 +00001246 int error = FALSE;
1247
1248 n1 = tv_get_number_chk(tv1, &error);
1249 if (!error)
1250 n2 = tv_get_number_chk(tv2, &error);
1251 if (error)
1252 {
1253 clear_tv(tv1);
1254 return FAIL;
1255 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001256 switch (type)
1257 {
1258 case EXPR_IS:
1259 case EXPR_EQUAL: n1 = (n1 == n2); break;
1260 case EXPR_ISNOT:
1261 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1262 case EXPR_GREATER: n1 = (n1 > n2); break;
1263 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
1264 case EXPR_SMALLER: n1 = (n1 < n2); break;
1265 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
1266 case EXPR_UNKNOWN:
1267 case EXPR_MATCH:
1268 default: break; // avoid gcc warning
1269 }
1270 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001271 else if (in_vim9script() && (tv1->v_type == VAR_BOOL
1272 || tv2->v_type == VAR_BOOL
1273 || (tv1->v_type == VAR_SPECIAL
1274 && tv2->v_type == VAR_SPECIAL)))
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001275 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001276 if (tv1->v_type != tv2->v_type)
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001277 {
1278 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001279 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1280 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001281 return FAIL;
1282 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001283 n1 = tv1->vval.v_number;
1284 n2 = tv2->vval.v_number;
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001285 switch (type)
1286 {
1287 case EXPR_IS:
1288 case EXPR_EQUAL: n1 = (n1 == n2); break;
1289 case EXPR_ISNOT:
1290 case EXPR_NEQUAL: n1 = (n1 != n2); break;
1291 default:
Bram Moolenaar0c357522021-07-18 14:43:43 +02001292 semsg(_(e_invalid_operation_for_str),
Bram Moolenaar265f8112021-12-19 12:33:05 +00001293 vartype_name(tv1->v_type));
1294 clear_tv(tv1);
Bram Moolenaarcff40ff2021-01-09 16:21:37 +01001295 return FAIL;
1296 }
1297 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001298 else
1299 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001300 if (typval_compare_string(tv1, tv2, type, ic, &res) == FAIL)
Bram Moolenaar0c357522021-07-18 14:43:43 +02001301 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001302 clear_tv(tv1);
Bram Moolenaar0c357522021-07-18 14:43:43 +02001303 return FAIL;
1304 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001305 n1 = res;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001306 }
Bram Moolenaar265f8112021-12-19 12:33:05 +00001307 clear_tv(tv1);
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001308 if (in_vim9script())
1309 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001310 tv1->v_type = VAR_BOOL;
1311 tv1->vval.v_number = n1 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001312 }
1313 else
1314 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00001315 tv1->v_type = VAR_NUMBER;
1316 tv1->vval.v_number = n1;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02001317 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001318
1319 return OK;
1320}
1321
Bram Moolenaar34453202021-01-31 13:08:38 +01001322/*
Bram Moolenaar265f8112021-12-19 12:33:05 +00001323 * Compare "tv1" to "tv2" as lists acording to "type" and "ic".
1324 * Put the result, false or true, in "res".
1325 * Return FAIL and give an error message when the comparison can't be done.
1326 */
1327 int
1328typval_compare_list(
1329 typval_T *tv1,
1330 typval_T *tv2,
1331 exprtype_T type,
1332 int ic,
1333 int *res)
1334{
1335 int val = 0;
1336
1337 if (type == EXPR_IS || type == EXPR_ISNOT)
1338 {
1339 val = (tv1->v_type == tv2->v_type
1340 && tv1->vval.v_list == tv2->vval.v_list);
1341 if (type == EXPR_ISNOT)
1342 val = !val;
1343 }
1344 else if (tv1->v_type != tv2->v_type
1345 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1346 {
1347 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001348 emsg(_(e_can_only_compare_list_with_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001349 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001350 emsg(_(e_invalid_operation_for_list));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001351 return FAIL;
1352 }
1353 else
1354 {
1355 val = list_equal(tv1->vval.v_list, tv2->vval.v_list,
1356 ic, FALSE);
1357 if (type == EXPR_NEQUAL)
1358 val = !val;
1359 }
1360 *res = val;
1361 return OK;
1362}
1363
1364/*
1365 * Compare "tv1" to "tv2" as blobs acording to "type".
1366 * Put the result, false or true, in "res".
1367 * Return FAIL and give an error message when the comparison can't be done.
1368 */
1369 int
1370typval_compare_blob(
1371 typval_T *tv1,
1372 typval_T *tv2,
1373 exprtype_T type,
1374 int *res)
1375{
1376 int val = 0;
1377
1378 if (type == EXPR_IS || type == EXPR_ISNOT)
1379 {
1380 val = (tv1->v_type == tv2->v_type
1381 && tv1->vval.v_blob == tv2->vval.v_blob);
1382 if (type == EXPR_ISNOT)
1383 val = !val;
1384 }
1385 else if (tv1->v_type != tv2->v_type
1386 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1387 {
1388 if (tv1->v_type != tv2->v_type)
1389 emsg(_("E977: Can only compare Blob with Blob"));
1390 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001391 emsg(_(e_invalid_operation_for_blob));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001392 return FAIL;
1393 }
1394 else
1395 {
1396 val = blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1397 if (type == EXPR_NEQUAL)
1398 val = !val;
1399 }
1400 *res = val;
1401 return OK;
1402}
1403
1404/*
1405 * Compare "tv1" to "tv2" as dictionaries acording to "type" and "ic".
1406 * Put the result, false or true, in "res".
1407 * Return FAIL and give an error message when the comparison can't be done.
1408 */
1409 int
1410typval_compare_dict(
1411 typval_T *tv1,
1412 typval_T *tv2,
1413 exprtype_T type,
1414 int ic,
1415 int *res)
1416{
1417 int val;
1418
1419 if (type == EXPR_IS || type == EXPR_ISNOT)
1420 {
1421 val = (tv1->v_type == tv2->v_type
1422 && tv1->vval.v_dict == tv2->vval.v_dict);
1423 if (type == EXPR_ISNOT)
1424 val = !val;
1425 }
1426 else if (tv1->v_type != tv2->v_type
1427 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
1428 {
1429 if (tv1->v_type != tv2->v_type)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001430 emsg(_(e_can_only_compare_dictionary_with_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001431 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001432 emsg(_(e_invalid_operation_for_dictionary));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001433 return FAIL;
1434 }
1435 else
1436 {
1437 val = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, FALSE);
1438 if (type == EXPR_NEQUAL)
1439 val = !val;
1440 }
1441 *res = val;
1442 return OK;
1443}
1444
1445/*
1446 * Compare "tv1" to "tv2" as funcrefs acording to "type" and "ic".
1447 * Put the result, false or true, in "res".
1448 * Return FAIL and give an error message when the comparison can't be done.
1449 */
1450 int
1451typval_compare_func(
1452 typval_T *tv1,
1453 typval_T *tv2,
1454 exprtype_T type,
1455 int ic,
1456 int *res)
1457{
1458 int val = 0;
1459
1460 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
1461 && type != EXPR_IS && type != EXPR_ISNOT)
1462 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001463 emsg(_(e_invalid_operation_for_funcrefs));
Bram Moolenaar265f8112021-12-19 12:33:05 +00001464 return FAIL;
1465 }
1466 if ((tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial == NULL)
1467 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial == NULL))
1468 // When both partials are NULL, then they are equal.
1469 // Otherwise they are not equal.
1470 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1471 else if (type == EXPR_IS || type == EXPR_ISNOT)
1472 {
1473 if (tv1->v_type == VAR_FUNC && tv2->v_type == VAR_FUNC)
1474 // strings are considered the same if their value is
1475 // the same
1476 val = tv_equal(tv1, tv2, ic, FALSE);
1477 else if (tv1->v_type == VAR_PARTIAL && tv2->v_type == VAR_PARTIAL)
1478 val = (tv1->vval.v_partial == tv2->vval.v_partial);
1479 else
1480 val = FALSE;
1481 }
1482 else
1483 val = tv_equal(tv1, tv2, ic, FALSE);
1484 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
1485 val = !val;
1486 *res = val;
1487 return OK;
1488}
1489
1490/*
1491 * Compare "tv1" to "tv2" as strings according to "type" and "ic".
1492 * Put the result, false or true, in "res".
1493 * Return FAIL and give an error message when the comparison can't be done.
1494 */
1495 int
1496typval_compare_string(
1497 typval_T *tv1,
1498 typval_T *tv2,
1499 exprtype_T type,
1500 int ic,
1501 int *res)
1502{
1503 int i = 0;
1504 int val = FALSE;
1505 char_u *s1, *s2;
1506 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1507
1508 if (in_vim9script()
1509 && ((tv1->v_type != VAR_STRING && tv1->v_type != VAR_SPECIAL)
1510 || (tv2->v_type != VAR_STRING && tv2->v_type != VAR_SPECIAL)))
1511 {
1512 semsg(_(e_cannot_compare_str_with_str),
1513 vartype_name(tv1->v_type), vartype_name(tv2->v_type));
1514 return FAIL;
1515 }
1516 s1 = tv_get_string_buf(tv1, buf1);
1517 s2 = tv_get_string_buf(tv2, buf2);
1518 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
1519 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
1520 switch (type)
1521 {
1522 case EXPR_IS:
1523 case EXPR_EQUAL: val = (i == 0); break;
1524 case EXPR_ISNOT:
1525 case EXPR_NEQUAL: val = (i != 0); break;
1526 case EXPR_GREATER: val = (i > 0); break;
1527 case EXPR_GEQUAL: val = (i >= 0); break;
1528 case EXPR_SMALLER: val = (i < 0); break;
1529 case EXPR_SEQUAL: val = (i <= 0); break;
1530
1531 case EXPR_MATCH:
1532 case EXPR_NOMATCH:
1533 val = pattern_match(s2, s1, ic);
1534 if (type == EXPR_NOMATCH)
1535 val = !val;
1536 break;
1537
1538 default: break; // avoid gcc warning
1539 }
1540 *res = val;
1541 return OK;
1542}
1543/*
Bram Moolenaar34453202021-01-31 13:08:38 +01001544 * Convert any type to a string, never give an error.
1545 * When "quotes" is TRUE add quotes to a string.
1546 * Returns an allocated string.
1547 */
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001548 char_u *
Bram Moolenaar34453202021-01-31 13:08:38 +01001549typval_tostring(typval_T *arg, int quotes)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001550{
1551 char_u *tofree;
1552 char_u numbuf[NUMBUFLEN];
1553 char_u *ret = NULL;
1554
1555 if (arg == NULL)
1556 return vim_strsave((char_u *)"(does not exist)");
Bram Moolenaar34453202021-01-31 13:08:38 +01001557 if (!quotes && arg->v_type == VAR_STRING)
1558 {
1559 ret = vim_strsave(arg->vval.v_string == NULL ? (char_u *)""
1560 : arg->vval.v_string);
1561 }
1562 else
1563 {
1564 ret = tv2string(arg, &tofree, numbuf, 0);
1565 // Make a copy if we have a value but it's not in allocated memory.
1566 if (ret != NULL && tofree == NULL)
1567 ret = vim_strsave(ret);
1568 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001569 return ret;
1570}
1571
1572/*
1573 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
1574 * or it refers to a List or Dictionary that is locked.
1575 */
1576 int
1577tv_islocked(typval_T *tv)
1578{
1579 return (tv->v_lock & VAR_LOCKED)
1580 || (tv->v_type == VAR_LIST
1581 && tv->vval.v_list != NULL
1582 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
1583 || (tv->v_type == VAR_DICT
1584 && tv->vval.v_dict != NULL
1585 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
1586}
1587
1588 static int
1589func_equal(
1590 typval_T *tv1,
1591 typval_T *tv2,
1592 int ic) // ignore case
1593{
1594 char_u *s1, *s2;
1595 dict_T *d1, *d2;
1596 int a1, a2;
1597 int i;
1598
1599 // empty and NULL function name considered the same
1600 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
1601 : partial_name(tv1->vval.v_partial);
1602 if (s1 != NULL && *s1 == NUL)
1603 s1 = NULL;
1604 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
1605 : partial_name(tv2->vval.v_partial);
1606 if (s2 != NULL && *s2 == NUL)
1607 s2 = NULL;
1608 if (s1 == NULL || s2 == NULL)
1609 {
1610 if (s1 != s2)
1611 return FALSE;
1612 }
1613 else if (STRCMP(s1, s2) != 0)
1614 return FALSE;
1615
1616 // empty dict and NULL dict is different
1617 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
1618 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
1619 if (d1 == NULL || d2 == NULL)
1620 {
1621 if (d1 != d2)
1622 return FALSE;
1623 }
1624 else if (!dict_equal(d1, d2, ic, TRUE))
1625 return FALSE;
1626
1627 // empty list and no list considered the same
1628 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
1629 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
1630 if (a1 != a2)
1631 return FALSE;
1632 for (i = 0; i < a1; ++i)
1633 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
1634 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
1635 return FALSE;
1636
1637 return TRUE;
1638}
1639
1640/*
1641 * Return TRUE if "tv1" and "tv2" have the same value.
1642 * Compares the items just like "==" would compare them, but strings and
1643 * numbers are different. Floats and numbers are also different.
1644 */
1645 int
1646tv_equal(
1647 typval_T *tv1,
1648 typval_T *tv2,
1649 int ic, // ignore case
1650 int recursive) // TRUE when used recursively
1651{
1652 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
1653 char_u *s1, *s2;
1654 static int recursive_cnt = 0; // catch recursive loops
1655 int r;
1656 static int tv_equal_recurse_limit;
1657
1658 // Catch lists and dicts that have an endless loop by limiting
1659 // recursiveness to a limit. We guess they are equal then.
1660 // A fixed limit has the problem of still taking an awful long time.
1661 // Reduce the limit every time running into it. That should work fine for
1662 // deeply linked structures that are not recursively linked and catch
1663 // recursiveness quickly.
1664 if (!recursive)
1665 tv_equal_recurse_limit = 1000;
1666 if (recursive_cnt >= tv_equal_recurse_limit)
1667 {
1668 --tv_equal_recurse_limit;
1669 return TRUE;
1670 }
1671
1672 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
1673 // arguments.
1674 if ((tv1->v_type == VAR_FUNC
1675 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
1676 && (tv2->v_type == VAR_FUNC
1677 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
1678 {
1679 ++recursive_cnt;
1680 r = func_equal(tv1, tv2, ic);
1681 --recursive_cnt;
1682 return r;
1683 }
1684
Bram Moolenaar418a29f2021-02-10 22:23:41 +01001685 if (tv1->v_type != tv2->v_type
1686 && ((tv1->v_type != VAR_BOOL && tv1->v_type != VAR_SPECIAL)
1687 || (tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001688 return FALSE;
1689
1690 switch (tv1->v_type)
1691 {
1692 case VAR_LIST:
1693 ++recursive_cnt;
1694 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
1695 --recursive_cnt;
1696 return r;
1697
1698 case VAR_DICT:
1699 ++recursive_cnt;
1700 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
1701 --recursive_cnt;
1702 return r;
1703
1704 case VAR_BLOB:
1705 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
1706
1707 case VAR_NUMBER:
1708 case VAR_BOOL:
1709 case VAR_SPECIAL:
1710 return tv1->vval.v_number == tv2->vval.v_number;
1711
1712 case VAR_STRING:
1713 s1 = tv_get_string_buf(tv1, buf1);
1714 s2 = tv_get_string_buf(tv2, buf2);
1715 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
1716
1717 case VAR_FLOAT:
1718#ifdef FEAT_FLOAT
1719 return tv1->vval.v_float == tv2->vval.v_float;
1720#endif
1721 case VAR_JOB:
1722#ifdef FEAT_JOB_CHANNEL
1723 return tv1->vval.v_job == tv2->vval.v_job;
1724#endif
1725 case VAR_CHANNEL:
1726#ifdef FEAT_JOB_CHANNEL
1727 return tv1->vval.v_channel == tv2->vval.v_channel;
1728#endif
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001729 case VAR_INSTR:
1730 return tv1->vval.v_instr == tv2->vval.v_instr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001731
1732 case VAR_PARTIAL:
1733 return tv1->vval.v_partial == tv2->vval.v_partial;
1734
1735 case VAR_FUNC:
1736 return tv1->vval.v_string == tv2->vval.v_string;
1737
1738 case VAR_UNKNOWN:
1739 case VAR_ANY:
1740 case VAR_VOID:
1741 break;
1742 }
1743
1744 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
1745 // does not equal anything, not even itself.
1746 return FALSE;
1747}
1748
1749/*
1750 * Get an option value.
1751 * "arg" points to the '&' or '+' before the option name.
1752 * "arg" is advanced to character after the option name.
1753 * Return OK or FAIL.
1754 */
1755 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001756eval_option(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001757 char_u **arg,
1758 typval_T *rettv, // when NULL, only check if option exists
1759 int evaluate)
1760{
1761 char_u *option_end;
1762 long numval;
1763 char_u *stringval;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001764 getoption_T opt_type;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001765 int c;
1766 int working = (**arg == '+'); // has("+option")
1767 int ret = OK;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001768 int scope;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001769
1770 // Isolate the option name and find its value.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001771 option_end = find_option_end(arg, &scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001772 if (option_end == NULL)
1773 {
1774 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001775 semsg(_(e_option_name_missing_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001776 return FAIL;
1777 }
1778
1779 if (!evaluate)
1780 {
1781 *arg = option_end;
1782 return OK;
1783 }
1784
1785 c = *option_end;
1786 *option_end = NUL;
1787 opt_type = get_option_value(*arg, &numval,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001788 rettv == NULL ? NULL : &stringval, NULL, scope);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001789
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001790 if (opt_type == gov_unknown)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001791 {
1792 if (rettv != NULL)
Bram Moolenaare1242042021-12-16 20:56:57 +00001793 semsg(_(e_unknown_option_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001794 ret = FAIL;
1795 }
1796 else if (rettv != NULL)
1797 {
Bram Moolenaara79925a2021-01-05 17:50:28 +01001798 rettv->v_lock = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001799 if (opt_type == gov_hidden_string)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001800 {
1801 rettv->v_type = VAR_STRING;
1802 rettv->vval.v_string = NULL;
1803 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001804 else if (opt_type == gov_hidden_bool || opt_type == gov_hidden_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001805 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001806 rettv->v_type = in_vim9script() && opt_type == gov_hidden_bool
1807 ? VAR_BOOL : VAR_NUMBER;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001808 rettv->vval.v_number = 0;
1809 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001810 else if (opt_type == gov_bool || opt_type == gov_number)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001811 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001812 if (in_vim9script() && opt_type == gov_bool)
1813 {
1814 rettv->v_type = VAR_BOOL;
1815 rettv->vval.v_number = numval ? VVAL_TRUE : VVAL_FALSE;
1816 }
1817 else
1818 {
1819 rettv->v_type = VAR_NUMBER;
1820 rettv->vval.v_number = numval;
1821 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001822 }
1823 else // string option
1824 {
1825 rettv->v_type = VAR_STRING;
1826 rettv->vval.v_string = stringval;
1827 }
1828 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001829 else if (working && (opt_type == gov_hidden_bool
1830 || opt_type == gov_hidden_number
1831 || opt_type == gov_hidden_string))
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001832 ret = FAIL;
1833
1834 *option_end = c; // put back for error messages
1835 *arg = option_end;
1836
1837 return ret;
1838}
1839
1840/*
1841 * Allocate a variable for a number constant. Also deals with "0z" for blob.
1842 * Return OK or FAIL.
1843 */
1844 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001845eval_number(
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001846 char_u **arg,
1847 typval_T *rettv,
1848 int evaluate,
1849 int want_string UNUSED)
1850{
1851 int len;
Bram Moolenaardd9de502021-08-15 13:49:42 +02001852 int skip_quotes = !in_old_script(4);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001853#ifdef FEAT_FLOAT
1854 char_u *p;
1855 int get_float = FALSE;
1856
1857 // We accept a float when the format matches
1858 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
1859 // strict to avoid backwards compatibility problems.
1860 // With script version 2 and later the leading digit can be
1861 // omitted.
1862 // Don't look for a float after the "." operator, so that
1863 // ":let vers = 1.2.3" doesn't fail.
1864 if (**arg == '.')
1865 p = *arg;
1866 else
Bram Moolenaar29500652021-08-08 15:43:34 +02001867 {
1868 p = *arg + 1;
1869 if (skip_quotes)
1870 for (;;)
1871 {
1872 if (*p == '\'')
1873 ++p;
1874 if (!vim_isdigit(*p))
1875 break;
1876 p = skipdigits(p);
1877 }
1878 else
1879 p = skipdigits(p);
1880 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001881 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
1882 {
1883 get_float = TRUE;
1884 p = skipdigits(p + 2);
1885 if (*p == 'e' || *p == 'E')
1886 {
1887 ++p;
1888 if (*p == '-' || *p == '+')
1889 ++p;
1890 if (!vim_isdigit(*p))
1891 get_float = FALSE;
1892 else
1893 p = skipdigits(p + 1);
1894 }
1895 if (ASCII_ISALPHA(*p) || *p == '.')
1896 get_float = FALSE;
1897 }
1898 if (get_float)
1899 {
1900 float_T f;
1901
Bram Moolenaar29500652021-08-08 15:43:34 +02001902 *arg += string2float(*arg, &f, skip_quotes);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001903 if (evaluate)
1904 {
1905 rettv->v_type = VAR_FLOAT;
1906 rettv->vval.v_float = f;
1907 }
1908 }
1909 else
1910#endif
1911 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
1912 {
1913 char_u *bp;
1914 blob_T *blob = NULL; // init for gcc
1915
1916 // Blob constant: 0z0123456789abcdef
1917 if (evaluate)
1918 blob = blob_alloc();
1919 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
1920 {
1921 if (!vim_isxdigit(bp[1]))
1922 {
1923 if (blob != NULL)
1924 {
1925 emsg(_("E973: Blob literal should have an even number of hex characters"));
1926 ga_clear(&blob->bv_ga);
1927 VIM_CLEAR(blob);
1928 }
1929 return FAIL;
1930 }
1931 if (blob != NULL)
1932 ga_append(&blob->bv_ga,
1933 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
1934 if (bp[2] == '.' && vim_isxdigit(bp[3]))
1935 ++bp;
1936 }
1937 if (blob != NULL)
1938 rettv_blob_set(rettv, blob);
1939 *arg = bp;
1940 }
1941 else
1942 {
1943 varnumber_T n;
1944
1945 // decimal, hex or octal number
Bram Moolenaar29500652021-08-08 15:43:34 +02001946 vim_str2nr(*arg, NULL, &len, skip_quotes
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001947 ? STR2NR_NO_OCT + STR2NR_QUOTE
1948 : STR2NR_ALL, &n, NULL, 0, TRUE);
1949 if (len == 0)
1950 {
Bram Moolenaar98cb90e2021-11-30 11:56:22 +00001951 if (evaluate)
1952 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001953 return FAIL;
1954 }
1955 *arg += len;
1956 if (evaluate)
1957 {
1958 rettv->v_type = VAR_NUMBER;
1959 rettv->vval.v_number = n;
1960 }
1961 }
1962 return OK;
1963}
1964
1965/*
1966 * Allocate a variable for a string constant.
1967 * Return OK or FAIL.
1968 */
1969 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001970eval_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001971{
1972 char_u *p;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02001973 char_u *end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001974 int extra = 0;
1975 int len;
1976
1977 // Find the end of the string, skipping backslashed characters.
1978 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
1979 {
1980 if (*p == '\\' && p[1] != NUL)
1981 {
1982 ++p;
1983 // A "\<x>" form occupies at least 4 characters, and produces up
1984 // to 21 characters (3 * 6 for the char and 3 for a modifier):
1985 // reserve space for 18 extra.
1986 // Each byte in the char could be encoded as K_SPECIAL K_EXTRA x.
1987 if (*p == '<')
1988 extra += 18;
1989 }
1990 }
1991
1992 if (*p != '"')
1993 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001994 semsg(_(e_missing_double_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02001995 return FAIL;
1996 }
1997
1998 // If only parsing, set *arg and return here
1999 if (!evaluate)
2000 {
2001 *arg = p + 1;
2002 return OK;
2003 }
2004
2005 // Copy the string into allocated memory, handling backslashed
2006 // characters.
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002007 rettv->v_type = VAR_STRING;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002008 len = (int)(p - *arg + extra);
2009 rettv->vval.v_string = alloc(len);
2010 if (rettv->vval.v_string == NULL)
2011 return FAIL;
2012 end = rettv->vval.v_string;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002013
2014 for (p = *arg + 1; *p != NUL && *p != '"'; )
2015 {
2016 if (*p == '\\')
2017 {
2018 switch (*++p)
2019 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002020 case 'b': *end++ = BS; ++p; break;
2021 case 'e': *end++ = ESC; ++p; break;
2022 case 'f': *end++ = FF; ++p; break;
2023 case 'n': *end++ = NL; ++p; break;
2024 case 'r': *end++ = CAR; ++p; break;
2025 case 't': *end++ = TAB; ++p; break;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002026
2027 case 'X': // hex: "\x1", "\x12"
2028 case 'x':
2029 case 'u': // Unicode: "\u0023"
2030 case 'U':
2031 if (vim_isxdigit(p[1]))
2032 {
2033 int n, nr;
2034 int c = toupper(*p);
2035
2036 if (c == 'X')
2037 n = 2;
2038 else if (*p == 'u')
2039 n = 4;
2040 else
2041 n = 8;
2042 nr = 0;
2043 while (--n >= 0 && vim_isxdigit(p[1]))
2044 {
2045 ++p;
2046 nr = (nr << 4) + hex2nr(*p);
2047 }
2048 ++p;
2049 // For "\u" store the number according to
2050 // 'encoding'.
2051 if (c != 'X')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002052 end += (*mb_char2bytes)(nr, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002053 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002054 *end++ = nr;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002055 }
2056 break;
2057
2058 // octal: "\1", "\12", "\123"
2059 case '0':
2060 case '1':
2061 case '2':
2062 case '3':
2063 case '4':
2064 case '5':
2065 case '6':
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002066 case '7': *end = *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002067 if (*p >= '0' && *p <= '7')
2068 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002069 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002070 if (*p >= '0' && *p <= '7')
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002071 *end = (*end << 3) + *p++ - '0';
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002072 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002073 ++end;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002074 break;
2075
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002076 // Special key, e.g.: "\<C-W>"
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002077 case '<':
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002078 {
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002079 int flags = FSK_KEYCODE | FSK_IN_STRING;
2080
Bram Moolenaarfccd93f2020-05-31 22:06:51 +02002081 if (p[1] != '*')
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002082 flags |= FSK_SIMPLIFY;
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002083 extra = trans_special(&p, end, flags, NULL);
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002084 if (extra != 0)
2085 {
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002086 end += extra;
2087 if (end >= rettv->vval.v_string + len)
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002088 iemsg("eval_string() used more space than allocated");
Bram Moolenaarebe9d342020-05-30 21:52:54 +02002089 break;
2090 }
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002091 }
2092 // FALLTHROUGH
2093
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002094 default: MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002095 break;
2096 }
2097 }
2098 else
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002099 MB_COPY_CHAR(p, end);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002100 }
Bram Moolenaar1e0b7b12020-06-19 19:30:53 +02002101 *end = NUL;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002102 if (*p != NUL) // just in case
2103 ++p;
2104 *arg = p;
2105
2106 return OK;
2107}
2108
2109/*
2110 * Allocate a variable for a 'str''ing' constant.
2111 * Return OK or FAIL.
2112 */
2113 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002114eval_lit_string(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002115{
2116 char_u *p;
2117 char_u *str;
2118 int reduce = 0;
2119
2120 // Find the end of the string, skipping ''.
2121 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
2122 {
2123 if (*p == '\'')
2124 {
2125 if (p[1] != '\'')
2126 break;
2127 ++reduce;
2128 ++p;
2129 }
2130 }
2131
2132 if (*p != '\'')
2133 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002134 semsg(_(e_missing_single_quote_str), *arg);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002135 return FAIL;
2136 }
2137
2138 // If only parsing return after setting "*arg"
2139 if (!evaluate)
2140 {
2141 *arg = p + 1;
2142 return OK;
2143 }
2144
2145 // Copy the string into allocated memory, handling '' to ' reduction.
2146 str = alloc((p - *arg) - reduce);
2147 if (str == NULL)
2148 return FAIL;
2149 rettv->v_type = VAR_STRING;
2150 rettv->vval.v_string = str;
2151
2152 for (p = *arg + 1; *p != NUL; )
2153 {
2154 if (*p == '\'')
2155 {
2156 if (p[1] != '\'')
2157 break;
2158 ++p;
2159 }
2160 MB_COPY_CHAR(p, str);
2161 }
2162 *str = NUL;
2163 *arg = p + 1;
2164
2165 return OK;
2166}
2167
2168/*
2169 * Return a string with the string representation of a variable.
2170 * If the memory is allocated "tofree" is set to it, otherwise NULL.
2171 * "numbuf" is used for a number.
2172 * Puts quotes around strings, so that they can be parsed back by eval().
2173 * May return NULL.
2174 */
2175 char_u *
2176tv2string(
2177 typval_T *tv,
2178 char_u **tofree,
2179 char_u *numbuf,
2180 int copyID)
2181{
2182 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
2183}
2184
2185/*
2186 * Get the value of an environment variable.
2187 * "arg" is pointing to the '$'. It is advanced to after the name.
2188 * If the environment variable was not set, silently assume it is empty.
2189 * Return FAIL if the name is invalid.
2190 */
2191 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002192eval_env_var(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002193{
2194 char_u *string = NULL;
2195 int len;
2196 int cc;
2197 char_u *name;
2198 int mustfree = FALSE;
2199
2200 ++*arg;
2201 name = *arg;
2202 len = get_env_len(arg);
2203 if (evaluate)
2204 {
2205 if (len == 0)
2206 return FAIL; // invalid empty name
2207
2208 cc = name[len];
2209 name[len] = NUL;
2210 // first try vim_getenv(), fast for normal environment vars
2211 string = vim_getenv(name, &mustfree);
2212 if (string != NULL && *string != NUL)
2213 {
2214 if (!mustfree)
2215 string = vim_strsave(string);
2216 }
2217 else
2218 {
2219 if (mustfree)
2220 vim_free(string);
2221
2222 // next try expanding things like $VIM and ${HOME}
2223 string = expand_env_save(name - 1);
2224 if (string != NULL && *string == '$')
2225 VIM_CLEAR(string);
2226 }
2227 name[len] = cc;
2228
2229 rettv->v_type = VAR_STRING;
2230 rettv->vval.v_string = string;
Bram Moolenaar16e63e62021-08-11 16:47:26 +02002231 rettv->v_lock = 0;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002232 }
2233
2234 return OK;
2235}
2236
2237/*
2238 * Get the lnum from the first argument.
2239 * Also accepts ".", "$", etc., but that only works for the current buffer.
2240 * Returns -1 on error.
2241 */
2242 linenr_T
2243tv_get_lnum(typval_T *argvars)
2244{
Bram Moolenaar9a963372020-12-21 21:58:46 +01002245 linenr_T lnum = -1;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002246
Bram Moolenaar56acb092020-08-16 14:48:19 +02002247 if (argvars[0].v_type != VAR_STRING || !in_vim9script())
2248 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002249 if (lnum <= 0 && argvars[0].v_type != VAR_NUMBER)
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002250 {
2251 int fnum;
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002252 pos_T *fp;
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002253
Bram Moolenaarf6bdd822021-03-28 16:26:41 +02002254 // no valid number, try using arg like line()
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002255 fp = var2fpos(&argvars[0], TRUE, &fnum, FALSE);
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002256 if (fp != NULL)
2257 lnum = fp->lnum;
2258 }
2259 return lnum;
2260}
2261
2262/*
2263 * Get the lnum from the first argument.
2264 * Also accepts "$", then "buf" is used.
2265 * Returns 0 on error.
2266 */
2267 linenr_T
2268tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
2269{
2270 if (argvars[0].v_type == VAR_STRING
2271 && argvars[0].vval.v_string != NULL
2272 && argvars[0].vval.v_string[0] == '$'
Bram Moolenaar8b6256f2021-12-28 11:24:49 +00002273 && argvars[0].vval.v_string[1] == NUL
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002274 && buf != NULL)
2275 return buf->b_ml.ml_line_count;
2276 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
2277}
2278
2279/*
2280 * Get buffer by number or pattern.
2281 */
2282 buf_T *
2283tv_get_buf(typval_T *tv, int curtab_only)
2284{
2285 char_u *name = tv->vval.v_string;
2286 buf_T *buf;
2287
2288 if (tv->v_type == VAR_NUMBER)
2289 return buflist_findnr((int)tv->vval.v_number);
2290 if (tv->v_type != VAR_STRING)
2291 return NULL;
2292 if (name == NULL || *name == NUL)
2293 return curbuf;
2294 if (name[0] == '$' && name[1] == NUL)
2295 return lastbuf;
2296
2297 buf = buflist_find_by_name(name, curtab_only);
2298
2299 // If not found, try expanding the name, like done for bufexists().
2300 if (buf == NULL)
2301 buf = find_buffer(tv);
2302
2303 return buf;
2304}
2305
Bram Moolenaar3767e3a2020-09-01 23:06:01 +02002306/*
2307 * Like tv_get_buf() but give an error message is the type is wrong.
2308 */
2309 buf_T *
2310tv_get_buf_from_arg(typval_T *tv)
2311{
2312 buf_T *buf;
2313
2314 ++emsg_off;
2315 buf = tv_get_buf(tv, FALSE);
2316 --emsg_off;
2317 if (buf == NULL
2318 && tv->v_type != VAR_NUMBER
2319 && tv->v_type != VAR_STRING)
2320 // issue errmsg for type error
2321 (void)tv_get_number(tv);
2322 return buf;
2323}
2324
Bram Moolenaar367d59e2020-05-30 17:06:14 +02002325#endif // FEAT_EVAL