1 /* 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 * list.c: List support and container (List, Dict, Blob) functions.
12 */
13
14 #include "vim.h"
15
16 #if defined(FEAT_EVAL) || defined(PROTO)
17
18 static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
19
20 // List heads for garbage collection.
21 static list_T *first_list = NULL; // list of all lists
22
23 #define FOR_ALL_WATCHERS(l, lw) \
24 for ((lw) = (l)->lv_watch; (lw) != NULL; (lw) = (lw)->lw_next)
25
26 static void list_free_item(list_T *l, listitem_T *item);
27
28 /*
29 * Add a watcher to a list.
30 */
31 void
list_add_watch(list_T * l,listwatch_T * lw)32 list_add_watch(list_T *l, listwatch_T *lw)
33 {
34 lw->lw_next = l->lv_watch;
35 l->lv_watch = lw;
36 }
37
38 /*
39 * Remove a watcher from a list.
40 * No warning when it isn't found...
41 */
42 void
list_rem_watch(list_T * l,listwatch_T * lwrem)43 list_rem_watch(list_T *l, listwatch_T *lwrem)
44 {
45 listwatch_T *lw, **lwp;
46
47 lwp = &l->lv_watch;
48 FOR_ALL_WATCHERS(l, lw)
49 {
50 if (lw == lwrem)
51 {
52 *lwp = lw->lw_next;
53 break;
54 }
55 lwp = &lw->lw_next;
56 }
57 }
58
59 /*
60 * Just before removing an item from a list: advance watchers to the next
61 * item.
62 */
63 static void
list_fix_watch(list_T * l,listitem_T * item)64 list_fix_watch(list_T *l, listitem_T *item)
65 {
66 listwatch_T *lw;
67
68 FOR_ALL_WATCHERS(l, lw)
69 if (lw->lw_item == item)
70 lw->lw_item = item->li_next;
71 }
72
73 static void
list_init(list_T * l)74 list_init(list_T *l)
75 {
76 // Prepend the list to the list of lists for garbage collection.
77 if (first_list != NULL)
78 first_list->lv_used_prev = l;
79 l->lv_used_prev = NULL;
80 l->lv_used_next = first_list;
81 first_list = l;
82 }
83
84 /*
85 * Allocate an empty header for a list.
86 * Caller should take care of the reference count.
87 */
88 list_T *
list_alloc(void)89 list_alloc(void)
90 {
91 list_T *l;
92
93 l = ALLOC_CLEAR_ONE(list_T);
94 if (l != NULL)
95 list_init(l);
96 return l;
97 }
98
99 /*
100 * list_alloc() with an ID for alloc_fail().
101 */
102 list_T *
list_alloc_id(alloc_id_T id UNUSED)103 list_alloc_id(alloc_id_T id UNUSED)
104 {
105 #ifdef FEAT_EVAL
106 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
107 return NULL;
108 #endif
109 return (list_alloc());
110 }
111
112 /*
113 * Allocate space for a list, plus "count" items.
114 * Next list_set_item() must be called for each item.
115 */
116 list_T *
list_alloc_with_items(int count)117 list_alloc_with_items(int count)
118 {
119 list_T *l;
120
121 l = (list_T *)alloc_clear(sizeof(list_T) + count * sizeof(listitem_T));
122 if (l != NULL)
123 {
124 list_init(l);
125
126 if (count > 0)
127 {
128 listitem_T *li = (listitem_T *)(l + 1);
129 int i;
130
131 l->lv_len = count;
132 l->lv_with_items = count;
133 l->lv_first = li;
134 l->lv_u.mat.lv_last = li + count - 1;
135 for (i = 0; i < count; ++i)
136 {
137 if (i == 0)
138 li->li_prev = NULL;
139 else
140 li->li_prev = li - 1;
141 if (i == count - 1)
142 li->li_next = NULL;
143 else
144 li->li_next = li + 1;
145 ++li;
146 }
147 }
148 }
149 return l;
150 }
151
152 /*
153 * Set item "idx" for a list previously allocated with list_alloc_with_items().
154 * The contents of "tv" is moved into the list item.
155 * Each item must be set exactly once.
156 */
157 void
list_set_item(list_T * l,int idx,typval_T * tv)158 list_set_item(list_T *l, int idx, typval_T *tv)
159 {
160 listitem_T *li = (listitem_T *)(l + 1) + idx;
161
162 li->li_tv = *tv;
163 }
164
165 /*
166 * Allocate an empty list for a return value, with reference count set.
167 * Returns OK or FAIL.
168 */
169 int
rettv_list_alloc(typval_T * rettv)170 rettv_list_alloc(typval_T *rettv)
171 {
172 list_T *l = list_alloc();
173
174 if (l == NULL)
175 return FAIL;
176
177 rettv->v_lock = 0;
178 rettv_list_set(rettv, l);
179 return OK;
180 }
181
182 /*
183 * Same as rettv_list_alloc() but uses an allocation id for testing.
184 */
185 int
rettv_list_alloc_id(typval_T * rettv,alloc_id_T id UNUSED)186 rettv_list_alloc_id(typval_T *rettv, alloc_id_T id UNUSED)
187 {
188 #ifdef FEAT_EVAL
189 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
190 return FAIL;
191 #endif
192 return rettv_list_alloc(rettv);
193 }
194
195
196 /*
197 * Set a list as the return value. Increments the reference count.
198 */
199 void
rettv_list_set(typval_T * rettv,list_T * l)200 rettv_list_set(typval_T *rettv, list_T *l)
201 {
202 rettv->v_type = VAR_LIST;
203 rettv->vval.v_list = l;
204 if (l != NULL)
205 ++l->lv_refcount;
206 }
207
208 /*
209 * Unreference a list: decrement the reference count and free it when it
210 * becomes zero.
211 */
212 void
list_unref(list_T * l)213 list_unref(list_T *l)
214 {
215 if (l != NULL && --l->lv_refcount <= 0)
216 list_free(l);
217 }
218
219 /*
220 * Free a list, including all non-container items it points to.
221 * Ignores the reference count.
222 */
223 static void
list_free_contents(list_T * l)224 list_free_contents(list_T *l)
225 {
226 listitem_T *item;
227
228 if (l->lv_first != &range_list_item)
229 for (item = l->lv_first; item != NULL; item = l->lv_first)
230 {
231 // Remove the item before deleting it.
232 l->lv_first = item->li_next;
233 clear_tv(&item->li_tv);
234 list_free_item(l, item);
235 }
236 }
237
238 /*
239 * Go through the list of lists and free items without the copyID.
240 * But don't free a list that has a watcher (used in a for loop), these
241 * are not referenced anywhere.
242 */
243 int
list_free_nonref(int copyID)244 list_free_nonref(int copyID)
245 {
246 list_T *ll;
247 int did_free = FALSE;
248
249 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
250 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
251 && ll->lv_watch == NULL)
252 {
253 // Free the List and ordinary items it contains, but don't recurse
254 // into Lists and Dictionaries, they will be in the list of dicts
255 // or list of lists.
256 list_free_contents(ll);
257 did_free = TRUE;
258 }
259 return did_free;
260 }
261
262 static void
list_free_list(list_T * l)263 list_free_list(list_T *l)
264 {
265 // Remove the list from the list of lists for garbage collection.
266 if (l->lv_used_prev == NULL)
267 first_list = l->lv_used_next;
268 else
269 l->lv_used_prev->lv_used_next = l->lv_used_next;
270 if (l->lv_used_next != NULL)
271 l->lv_used_next->lv_used_prev = l->lv_used_prev;
272
273 free_type(l->lv_type);
274 vim_free(l);
275 }
276
277 void
list_free_items(int copyID)278 list_free_items(int copyID)
279 {
280 list_T *ll, *ll_next;
281
282 for (ll = first_list; ll != NULL; ll = ll_next)
283 {
284 ll_next = ll->lv_used_next;
285 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
286 && ll->lv_watch == NULL)
287 {
288 // Free the List and ordinary items it contains, but don't recurse
289 // into Lists and Dictionaries, they will be in the list of dicts
290 // or list of lists.
291 list_free_list(ll);
292 }
293 }
294 }
295
296 void
list_free(list_T * l)297 list_free(list_T *l)
298 {
299 if (!in_free_unref_items)
300 {
301 list_free_contents(l);
302 list_free_list(l);
303 }
304 }
305
306 /*
307 * Allocate a list item.
308 * It is not initialized, don't forget to set v_lock.
309 */
310 listitem_T *
listitem_alloc(void)311 listitem_alloc(void)
312 {
313 return ALLOC_ONE(listitem_T);
314 }
315
316 /*
317 * Free a list item, unless it was allocated together with the list itself.
318 * Does not clear the value. Does not notify watchers.
319 */
320 static void
list_free_item(list_T * l,listitem_T * item)321 list_free_item(list_T *l, listitem_T *item)
322 {
323 if (l->lv_with_items == 0 || item < (listitem_T *)l
324 || item >= (listitem_T *)(l + 1) + l->lv_with_items)
325 vim_free(item);
326 }
327
328 /*
329 * Free a list item, unless it was allocated together with the list itself.
330 * Also clears the value. Does not notify watchers.
331 */
332 void
listitem_free(list_T * l,listitem_T * item)333 listitem_free(list_T *l, listitem_T *item)
334 {
335 clear_tv(&item->li_tv);
336 list_free_item(l, item);
337 }
338
339 /*
340 * Remove a list item from a List and free it. Also clears the value.
341 */
342 void
listitem_remove(list_T * l,listitem_T * item)343 listitem_remove(list_T *l, listitem_T *item)
344 {
345 vimlist_remove(l, item, item);
346 listitem_free(l, item);
347 }
348
349 /*
350 * Get the number of items in a list.
351 */
352 long
list_len(list_T * l)353 list_len(list_T *l)
354 {
355 if (l == NULL)
356 return 0L;
357 return l->lv_len;
358 }
359
360 /*
361 * Return TRUE when two lists have exactly the same values.
362 */
363 int
list_equal(list_T * l1,list_T * l2,int ic,int recursive)364 list_equal(
365 list_T *l1,
366 list_T *l2,
367 int ic, // ignore case for strings
368 int recursive) // TRUE when used recursively
369 {
370 listitem_T *item1, *item2;
371
372 if (l1 == l2)
373 return TRUE;
374 if (list_len(l1) != list_len(l2))
375 return FALSE;
376 if (list_len(l1) == 0)
377 // empty and NULL list are considered equal
378 return TRUE;
379 if (l1 == NULL || l2 == NULL)
380 return FALSE;
381
382 CHECK_LIST_MATERIALIZE(l1);
383 CHECK_LIST_MATERIALIZE(l2);
384
385 for (item1 = l1->lv_first, item2 = l2->lv_first;
386 item1 != NULL && item2 != NULL;
387 item1 = item1->li_next, item2 = item2->li_next)
388 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
389 return FALSE;
390 return item1 == NULL && item2 == NULL;
391 }
392
393 /*
394 * Locate item with index "n" in list "l" and return it.
395 * A negative index is counted from the end; -1 is the last item.
396 * Returns NULL when "n" is out of range.
397 */
398 listitem_T *
list_find(list_T * l,long n)399 list_find(list_T *l, long n)
400 {
401 listitem_T *item;
402 long idx;
403
404 if (l == NULL)
405 return NULL;
406
407 // Negative index is relative to the end.
408 if (n < 0)
409 n = l->lv_len + n;
410
411 // Check for index out of range.
412 if (n < 0 || n >= l->lv_len)
413 return NULL;
414
415 CHECK_LIST_MATERIALIZE(l);
416
417 // When there is a cached index may start search from there.
418 if (l->lv_u.mat.lv_idx_item != NULL)
419 {
420 if (n < l->lv_u.mat.lv_idx / 2)
421 {
422 // closest to the start of the list
423 item = l->lv_first;
424 idx = 0;
425 }
426 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
427 {
428 // closest to the end of the list
429 item = l->lv_u.mat.lv_last;
430 idx = l->lv_len - 1;
431 }
432 else
433 {
434 // closest to the cached index
435 item = l->lv_u.mat.lv_idx_item;
436 idx = l->lv_u.mat.lv_idx;
437 }
438 }
439 else
440 {
441 if (n < l->lv_len / 2)
442 {
443 // closest to the start of the list
444 item = l->lv_first;
445 idx = 0;
446 }
447 else
448 {
449 // closest to the end of the list
450 item = l->lv_u.mat.lv_last;
451 idx = l->lv_len - 1;
452 }
453 }
454
455 while (n > idx)
456 {
457 // search forward
458 item = item->li_next;
459 ++idx;
460 }
461 while (n < idx)
462 {
463 // search backward
464 item = item->li_prev;
465 --idx;
466 }
467
468 // cache the used index
469 l->lv_u.mat.lv_idx = idx;
470 l->lv_u.mat.lv_idx_item = item;
471
472 return item;
473 }
474
475 /*
476 * Get list item "l[idx]" as a number.
477 */
478 long
list_find_nr(list_T * l,long idx,int * errorp)479 list_find_nr(
480 list_T *l,
481 long idx,
482 int *errorp) // set to TRUE when something wrong
483 {
484 listitem_T *li;
485
486 if (l != NULL && l->lv_first == &range_list_item)
487 {
488 long n = idx;
489
490 // not materialized range() list: compute the value.
491 // Negative index is relative to the end.
492 if (n < 0)
493 n = l->lv_len + n;
494
495 // Check for index out of range.
496 if (n < 0 || n >= l->lv_len)
497 {
498 if (errorp != NULL)
499 *errorp = TRUE;
500 return -1L;
501 }
502
503 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
504 }
505
506 li = list_find(l, idx);
507 if (li == NULL)
508 {
509 if (errorp != NULL)
510 *errorp = TRUE;
511 return -1L;
512 }
513 return (long)tv_get_number_chk(&li->li_tv, errorp);
514 }
515
516 /*
517 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
518 */
519 char_u *
list_find_str(list_T * l,long idx)520 list_find_str(list_T *l, long idx)
521 {
522 listitem_T *li;
523
524 li = list_find(l, idx - 1);
525 if (li == NULL)
526 {
527 semsg(_(e_listidx), idx);
528 return NULL;
529 }
530 return tv_get_string(&li->li_tv);
531 }
532
533 /*
534 * Like list_find() but when a negative index is used that is not found use
535 * zero and set "idx" to zero. Used for first index of a range.
536 */
537 listitem_T *
list_find_index(list_T * l,long * idx)538 list_find_index(list_T *l, long *idx)
539 {
540 listitem_T *li = list_find(l, *idx);
541
542 if (li == NULL)
543 {
544 if (*idx < 0)
545 {
546 *idx = 0;
547 li = list_find(l, *idx);
548 }
549 }
550 return li;
551 }
552
553 /*
554 * Locate "item" list "l" and return its index.
555 * Returns -1 when "item" is not in the list.
556 */
557 long
list_idx_of_item(list_T * l,listitem_T * item)558 list_idx_of_item(list_T *l, listitem_T *item)
559 {
560 long idx = 0;
561 listitem_T *li;
562
563 if (l == NULL)
564 return -1;
565 CHECK_LIST_MATERIALIZE(l);
566 idx = 0;
567 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
568 ++idx;
569 if (li == NULL)
570 return -1;
571 return idx;
572 }
573
574 /*
575 * Append item "item" to the end of list "l".
576 */
577 void
list_append(list_T * l,listitem_T * item)578 list_append(list_T *l, listitem_T *item)
579 {
580 CHECK_LIST_MATERIALIZE(l);
581 if (l->lv_u.mat.lv_last == NULL)
582 {
583 // empty list
584 l->lv_first = item;
585 l->lv_u.mat.lv_last = item;
586 item->li_prev = NULL;
587 }
588 else
589 {
590 l->lv_u.mat.lv_last->li_next = item;
591 item->li_prev = l->lv_u.mat.lv_last;
592 l->lv_u.mat.lv_last = item;
593 }
594 ++l->lv_len;
595 item->li_next = NULL;
596 }
597
598 /*
599 * Append typval_T "tv" to the end of list "l". "tv" is copied.
600 * Return FAIL when out of memory or the type is wrong.
601 */
602 int
list_append_tv(list_T * l,typval_T * tv)603 list_append_tv(list_T *l, typval_T *tv)
604 {
605 listitem_T *li;
606
607 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
608 && check_typval_arg_type(l->lv_type->tt_member, tv,
609 NULL, 0) == FAIL)
610 return FAIL;
611 li = listitem_alloc();
612 if (li == NULL)
613 return FAIL;
614 copy_tv(tv, &li->li_tv);
615 list_append(l, li);
616 return OK;
617 }
618
619 /*
620 * As list_append_tv() but move the value instead of copying it.
621 * Return FAIL when out of memory.
622 */
623 static int
list_append_tv_move(list_T * l,typval_T * tv)624 list_append_tv_move(list_T *l, typval_T *tv)
625 {
626 listitem_T *li = listitem_alloc();
627
628 if (li == NULL)
629 return FAIL;
630 li->li_tv = *tv;
631 list_append(l, li);
632 return OK;
633 }
634
635 /*
636 * Add a dictionary to a list. Used by getqflist().
637 * Return FAIL when out of memory.
638 */
639 int
list_append_dict(list_T * list,dict_T * dict)640 list_append_dict(list_T *list, dict_T *dict)
641 {
642 listitem_T *li = listitem_alloc();
643
644 if (li == NULL)
645 return FAIL;
646 li->li_tv.v_type = VAR_DICT;
647 li->li_tv.v_lock = 0;
648 li->li_tv.vval.v_dict = dict;
649 list_append(list, li);
650 ++dict->dv_refcount;
651 return OK;
652 }
653
654 /*
655 * Append list2 to list1.
656 * Return FAIL when out of memory.
657 */
658 int
list_append_list(list_T * list1,list_T * list2)659 list_append_list(list_T *list1, list_T *list2)
660 {
661 listitem_T *li = listitem_alloc();
662
663 if (li == NULL)
664 return FAIL;
665 li->li_tv.v_type = VAR_LIST;
666 li->li_tv.v_lock = 0;
667 li->li_tv.vval.v_list = list2;
668 list_append(list1, li);
669 ++list2->lv_refcount;
670 return OK;
671 }
672
673 /*
674 * Make a copy of "str" and append it as an item to list "l".
675 * When "len" >= 0 use "str[len]".
676 * Returns FAIL when out of memory.
677 */
678 int
list_append_string(list_T * l,char_u * str,int len)679 list_append_string(list_T *l, char_u *str, int len)
680 {
681 listitem_T *li = listitem_alloc();
682
683 if (li == NULL)
684 return FAIL;
685 list_append(l, li);
686 li->li_tv.v_type = VAR_STRING;
687 li->li_tv.v_lock = 0;
688 if (str == NULL)
689 li->li_tv.vval.v_string = NULL;
690 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
691 : vim_strsave(str))) == NULL)
692 return FAIL;
693 return OK;
694 }
695
696 /*
697 * Append "n" to list "l".
698 * Returns FAIL when out of memory.
699 */
700 int
list_append_number(list_T * l,varnumber_T n)701 list_append_number(list_T *l, varnumber_T n)
702 {
703 listitem_T *li;
704
705 li = listitem_alloc();
706 if (li == NULL)
707 return FAIL;
708 li->li_tv.v_type = VAR_NUMBER;
709 li->li_tv.v_lock = 0;
710 li->li_tv.vval.v_number = n;
711 list_append(l, li);
712 return OK;
713 }
714
715 /*
716 * Insert typval_T "tv" in list "l" before "item".
717 * If "item" is NULL append at the end.
718 * Return FAIL when out of memory or the type is wrong.
719 */
720 int
list_insert_tv(list_T * l,typval_T * tv,listitem_T * item)721 list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
722 {
723 listitem_T *ni;
724
725 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
726 && check_typval_arg_type(l->lv_type->tt_member, tv,
727 NULL, 0) == FAIL)
728 return FAIL;
729 ni = listitem_alloc();
730 if (ni == NULL)
731 return FAIL;
732 copy_tv(tv, &ni->li_tv);
733 list_insert(l, ni, item);
734 return OK;
735 }
736
737 void
list_insert(list_T * l,listitem_T * ni,listitem_T * item)738 list_insert(list_T *l, listitem_T *ni, listitem_T *item)
739 {
740 CHECK_LIST_MATERIALIZE(l);
741 if (item == NULL)
742 // Append new item at end of list.
743 list_append(l, ni);
744 else
745 {
746 // Insert new item before existing item.
747 ni->li_prev = item->li_prev;
748 ni->li_next = item;
749 if (item->li_prev == NULL)
750 {
751 l->lv_first = ni;
752 ++l->lv_u.mat.lv_idx;
753 }
754 else
755 {
756 item->li_prev->li_next = ni;
757 l->lv_u.mat.lv_idx_item = NULL;
758 }
759 item->li_prev = ni;
760 ++l->lv_len;
761 }
762 }
763
764 /*
765 * Get the list item in "l" with index "n1". "n1" is adjusted if needed.
766 * In Vim9, it is at the end of the list, add an item.
767 * Return NULL if there is no such item.
768 */
769 listitem_T *
check_range_index_one(list_T * l,long * n1,int quiet)770 check_range_index_one(list_T *l, long *n1, int quiet)
771 {
772 listitem_T *li = list_find_index(l, n1);
773
774 if (li == NULL)
775 {
776 // Vim9: Allow for adding an item at the end.
777 if (in_vim9script() && *n1 == l->lv_len && l->lv_lock == 0)
778 {
779 list_append_number(l, 0);
780 li = list_find_index(l, n1);
781 }
782 if (li == NULL)
783 {
784 if (!quiet)
785 semsg(_(e_listidx), *n1);
786 return NULL;
787 }
788 }
789 return li;
790 }
791
792 /*
793 * Check that "n2" can be used as the second index in a range of list "l".
794 * If "n1" or "n2" is negative it is changed to the positive index.
795 * "li1" is the item for item "n1".
796 * Return OK or FAIL.
797 */
798 int
check_range_index_two(list_T * l,long * n1,listitem_T * li1,long * n2,int quiet)799 check_range_index_two(
800 list_T *l,
801 long *n1,
802 listitem_T *li1,
803 long *n2,
804 int quiet)
805 {
806 if (*n2 < 0)
807 {
808 listitem_T *ni = list_find(l, *n2);
809
810 if (ni == NULL)
811 {
812 if (!quiet)
813 semsg(_(e_listidx), *n2);
814 return FAIL;
815 }
816 *n2 = list_idx_of_item(l, ni);
817 }
818
819 // Check that n2 isn't before n1.
820 if (*n1 < 0)
821 *n1 = list_idx_of_item(l, li1);
822 if (*n2 < *n1)
823 {
824 if (!quiet)
825 semsg(_(e_listidx), *n2);
826 return FAIL;
827 }
828 return OK;
829 }
830
831 /*
832 * Assign values from list "src" into a range of "dest".
833 * "idx1_arg" is the index of the first item in "dest" to be replaced.
834 * "idx2" is the index of last item to be replaced, but when "empty_idx2" is
835 * TRUE then replace all items after "idx1".
836 * "op" is the operator, normally "=" but can be "+=" and the like.
837 * "varname" is used for error messages.
838 * Returns OK or FAIL.
839 */
840 int
list_assign_range(list_T * dest,list_T * src,long idx1_arg,long idx2,int empty_idx2,char_u * op,char_u * varname)841 list_assign_range(
842 list_T *dest,
843 list_T *src,
844 long idx1_arg,
845 long idx2,
846 int empty_idx2,
847 char_u *op,
848 char_u *varname)
849 {
850 listitem_T *src_li;
851 listitem_T *dest_li;
852 long idx1 = idx1_arg;
853 listitem_T *first_li = list_find_index(dest, &idx1);
854 long idx;
855 type_T *member_type = NULL;
856
857 /*
858 * Check whether any of the list items is locked before making any changes.
859 */
860 idx = idx1;
861 dest_li = first_li;
862 for (src_li = src->lv_first; src_li != NULL && dest_li != NULL; )
863 {
864 if (value_check_lock(dest_li->li_tv.v_lock, varname, FALSE))
865 return FAIL;
866 src_li = src_li->li_next;
867 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
868 break;
869 dest_li = dest_li->li_next;
870 ++idx;
871 }
872
873 if (in_vim9script() && dest->lv_type != NULL
874 && dest->lv_type->tt_member != NULL)
875 member_type = dest->lv_type->tt_member;
876
877 /*
878 * Assign the List values to the list items.
879 */
880 idx = idx1;
881 dest_li = first_li;
882 for (src_li = src->lv_first; src_li != NULL; )
883 {
884 if (op != NULL && *op != '=')
885 tv_op(&dest_li->li_tv, &src_li->li_tv, op);
886 else
887 {
888 if (member_type != NULL
889 && check_typval_arg_type(member_type, &src_li->li_tv,
890 NULL, 0) == FAIL)
891 return FAIL;
892 clear_tv(&dest_li->li_tv);
893 copy_tv(&src_li->li_tv, &dest_li->li_tv);
894 }
895 src_li = src_li->li_next;
896 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
897 break;
898 if (dest_li->li_next == NULL)
899 {
900 // Need to add an empty item.
901 if (list_append_number(dest, 0) == FAIL)
902 {
903 src_li = NULL;
904 break;
905 }
906 }
907 dest_li = dest_li->li_next;
908 ++idx;
909 }
910 if (src_li != NULL)
911 {
912 emsg(_(e_list_value_has_more_items_than_targets));
913 return FAIL;
914 }
915 if (empty_idx2
916 ? (dest_li != NULL && dest_li->li_next != NULL)
917 : idx != idx2)
918 {
919 emsg(_(e_list_value_does_not_have_enough_items));
920 return FAIL;
921 }
922 return OK;
923 }
924
925 /*
926 * Flatten "list" to depth "maxdepth".
927 * It does nothing if "maxdepth" is 0.
928 * Returns FAIL when out of memory.
929 */
930 static void
list_flatten(list_T * list,long maxdepth)931 list_flatten(list_T *list, long maxdepth)
932 {
933 listitem_T *item;
934 listitem_T *tofree;
935 int n;
936
937 if (maxdepth == 0)
938 return;
939 CHECK_LIST_MATERIALIZE(list);
940
941 n = 0;
942 item = list->lv_first;
943 while (item != NULL)
944 {
945 fast_breakcheck();
946 if (got_int)
947 return;
948
949 if (item->li_tv.v_type == VAR_LIST)
950 {
951 listitem_T *next = item->li_next;
952
953 vimlist_remove(list, item, item);
954 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
955 {
956 list_free_item(list, item);
957 return;
958 }
959 clear_tv(&item->li_tv);
960 tofree = item;
961
962 if (item->li_prev == NULL)
963 item = list->lv_first;
964 else
965 item = item->li_prev->li_next;
966 list_free_item(list, tofree);
967
968 if (++n >= maxdepth)
969 {
970 n = 0;
971 item = next;
972 }
973 }
974 else
975 {
976 n = 0;
977 item = item->li_next;
978 }
979 }
980 }
981
982 /*
983 * "flatten()" and "flattennew()" functions
984 */
985 static void
flatten_common(typval_T * argvars,typval_T * rettv,int make_copy)986 flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
987 {
988 list_T *l;
989 long maxdepth;
990 int error = FALSE;
991
992 if (in_vim9script()
993 && (check_for_list_arg(argvars, 0) == FAIL
994 || check_for_opt_number_arg(argvars, 1) == FAIL))
995 return;
996
997 if (argvars[0].v_type != VAR_LIST)
998 {
999 semsg(_(e_listarg), "flatten()");
1000 return;
1001 }
1002
1003 if (argvars[1].v_type == VAR_UNKNOWN)
1004 maxdepth = 999999;
1005 else
1006 {
1007 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
1008 if (error)
1009 return;
1010 if (maxdepth < 0)
1011 {
1012 emsg(_("E900: maxdepth must be non-negative number"));
1013 return;
1014 }
1015 }
1016
1017 l = argvars[0].vval.v_list;
1018 rettv->v_type = VAR_LIST;
1019 rettv->vval.v_list = l;
1020 if (l == NULL)
1021 return;
1022
1023 if (make_copy)
1024 {
1025 l = list_copy(l, TRUE, get_copyID());
1026 rettv->vval.v_list = l;
1027 if (l == NULL)
1028 return;
1029 // The type will change.
1030 free_type(l->lv_type);
1031 l->lv_type = NULL;
1032 }
1033 else
1034 {
1035 if (value_check_lock(l->lv_lock,
1036 (char_u *)N_("flatten() argument"), TRUE))
1037 return;
1038 ++l->lv_refcount;
1039 }
1040
1041 list_flatten(l, maxdepth);
1042 }
1043
1044 /*
1045 * "flatten(list[, {maxdepth}])" function
1046 */
1047 void
f_flatten(typval_T * argvars,typval_T * rettv)1048 f_flatten(typval_T *argvars, typval_T *rettv)
1049 {
1050 if (in_vim9script())
1051 emsg(_(e_cannot_use_flatten_in_vim9_script));
1052 else
1053 flatten_common(argvars, rettv, FALSE);
1054 }
1055
1056 /*
1057 * "flattennew(list[, {maxdepth}])" function
1058 */
1059 void
f_flattennew(typval_T * argvars,typval_T * rettv)1060 f_flattennew(typval_T *argvars, typval_T *rettv)
1061 {
1062 flatten_common(argvars, rettv, TRUE);
1063 }
1064
1065 /*
1066 * Extend "l1" with "l2". "l1" must not be NULL.
1067 * If "bef" is NULL append at the end, otherwise insert before this item.
1068 * Returns FAIL when out of memory.
1069 */
1070 int
list_extend(list_T * l1,list_T * l2,listitem_T * bef)1071 list_extend(list_T *l1, list_T *l2, listitem_T *bef)
1072 {
1073 listitem_T *item;
1074 int todo;
1075 listitem_T *bef_prev;
1076
1077 // NULL list is equivalent to an empty list: nothing to do.
1078 if (l2 == NULL || l2->lv_len == 0)
1079 return OK;
1080
1081 todo = l2->lv_len;
1082 CHECK_LIST_MATERIALIZE(l1);
1083 CHECK_LIST_MATERIALIZE(l2);
1084
1085 // When exending a list with itself, at some point we run into the item
1086 // that was before "bef" and need to skip over the already inserted items
1087 // to "bef".
1088 bef_prev = bef == NULL ? NULL : bef->li_prev;
1089
1090 // We also quit the loop when we have inserted the original item count of
1091 // the list, avoid a hang when we extend a list with itself.
1092 for (item = l2->lv_first; item != NULL && --todo >= 0;
1093 item = item == bef_prev ? bef : item->li_next)
1094 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
1095 return FAIL;
1096 return OK;
1097 }
1098
1099 /*
1100 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
1101 * Return FAIL when out of memory.
1102 */
1103 int
list_concat(list_T * l1,list_T * l2,typval_T * tv)1104 list_concat(list_T *l1, list_T *l2, typval_T *tv)
1105 {
1106 list_T *l;
1107
1108 // make a copy of the first list.
1109 if (l1 == NULL)
1110 l = list_alloc();
1111 else
1112 l = list_copy(l1, FALSE, 0);
1113 if (l == NULL)
1114 return FAIL;
1115 tv->v_type = VAR_LIST;
1116 tv->v_lock = 0;
1117 tv->vval.v_list = l;
1118 if (l1 == NULL)
1119 ++l->lv_refcount;
1120
1121 // append all items from the second list
1122 return list_extend(l, l2, NULL);
1123 }
1124
1125 list_T *
list_slice(list_T * ol,long n1,long n2)1126 list_slice(list_T *ol, long n1, long n2)
1127 {
1128 listitem_T *item;
1129 list_T *l = list_alloc();
1130
1131 if (l == NULL)
1132 return NULL;
1133 for (item = list_find(ol, n1); n1 <= n2; ++n1)
1134 {
1135 if (list_append_tv(l, &item->li_tv) == FAIL)
1136 {
1137 list_free(l);
1138 return NULL;
1139 }
1140 item = item->li_next;
1141 }
1142 return l;
1143 }
1144
1145 int
list_slice_or_index(list_T * list,int range,varnumber_T n1_arg,varnumber_T n2_arg,int exclusive,typval_T * rettv,int verbose)1146 list_slice_or_index(
1147 list_T *list,
1148 int range,
1149 varnumber_T n1_arg,
1150 varnumber_T n2_arg,
1151 int exclusive,
1152 typval_T *rettv,
1153 int verbose)
1154 {
1155 long len = list_len(list);
1156 varnumber_T n1 = n1_arg;
1157 varnumber_T n2 = n2_arg;
1158 typval_T var1;
1159
1160 if (n1 < 0)
1161 n1 = len + n1;
1162 if (n1 < 0 || n1 >= len)
1163 {
1164 // For a range we allow invalid values and for legacy script return an
1165 // empty list, for Vim9 script start at the first item.
1166 // A list index out of range is an error.
1167 if (!range)
1168 {
1169 if (verbose)
1170 semsg(_(e_listidx), (long)n1_arg);
1171 return FAIL;
1172 }
1173 if (in_vim9script())
1174 n1 = n1 < 0 ? 0 : len;
1175 else
1176 n1 = len;
1177 }
1178 if (range)
1179 {
1180 list_T *l;
1181
1182 if (n2 < 0)
1183 n2 = len + n2;
1184 else if (n2 >= len)
1185 n2 = len - (exclusive ? 0 : 1);
1186 if (exclusive)
1187 --n2;
1188 if (n2 < 0 || n2 + 1 < n1)
1189 n2 = -1;
1190 l = list_slice(list, n1, n2);
1191 if (l == NULL)
1192 return FAIL;
1193 clear_tv(rettv);
1194 rettv_list_set(rettv, l);
1195 }
1196 else
1197 {
1198 // copy the item to "var1" to avoid that freeing the list makes it
1199 // invalid.
1200 copy_tv(&list_find(list, n1)->li_tv, &var1);
1201 clear_tv(rettv);
1202 *rettv = var1;
1203 }
1204 return OK;
1205 }
1206
1207 /*
1208 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1209 * The refcount of the new list is set to 1.
1210 * See item_copy() for "copyID".
1211 * Returns NULL when out of memory.
1212 */
1213 list_T *
list_copy(list_T * orig,int deep,int copyID)1214 list_copy(list_T *orig, int deep, int copyID)
1215 {
1216 list_T *copy;
1217 listitem_T *item;
1218 listitem_T *ni;
1219
1220 if (orig == NULL)
1221 return NULL;
1222
1223 copy = list_alloc();
1224 if (copy != NULL)
1225 {
1226 copy->lv_type = alloc_type(orig->lv_type);
1227 if (copyID != 0)
1228 {
1229 // Do this before adding the items, because one of the items may
1230 // refer back to this list.
1231 orig->lv_copyID = copyID;
1232 orig->lv_copylist = copy;
1233 }
1234 CHECK_LIST_MATERIALIZE(orig);
1235 for (item = orig->lv_first; item != NULL && !got_int;
1236 item = item->li_next)
1237 {
1238 ni = listitem_alloc();
1239 if (ni == NULL)
1240 break;
1241 if (deep)
1242 {
1243 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
1244 {
1245 vim_free(ni);
1246 break;
1247 }
1248 }
1249 else
1250 copy_tv(&item->li_tv, &ni->li_tv);
1251 list_append(copy, ni);
1252 }
1253 ++copy->lv_refcount;
1254 if (item != NULL)
1255 {
1256 list_unref(copy);
1257 copy = NULL;
1258 }
1259 }
1260
1261 return copy;
1262 }
1263
1264 /*
1265 * Remove items "item" to "item2" from list "l".
1266 * Does not free the listitem or the value!
1267 * This used to be called list_remove, but that conflicts with a Sun header
1268 * file.
1269 */
1270 void
vimlist_remove(list_T * l,listitem_T * item,listitem_T * item2)1271 vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1272 {
1273 listitem_T *ip;
1274
1275 CHECK_LIST_MATERIALIZE(l);
1276
1277 // notify watchers
1278 for (ip = item; ip != NULL; ip = ip->li_next)
1279 {
1280 --l->lv_len;
1281 list_fix_watch(l, ip);
1282 if (ip == item2)
1283 break;
1284 }
1285
1286 if (item2->li_next == NULL)
1287 l->lv_u.mat.lv_last = item->li_prev;
1288 else
1289 item2->li_next->li_prev = item->li_prev;
1290 if (item->li_prev == NULL)
1291 l->lv_first = item2->li_next;
1292 else
1293 item->li_prev->li_next = item2->li_next;
1294 l->lv_u.mat.lv_idx_item = NULL;
1295 }
1296
1297 /*
1298 * Return an allocated string with the string representation of a list.
1299 * May return NULL.
1300 */
1301 char_u *
list2string(typval_T * tv,int copyID,int restore_copyID)1302 list2string(typval_T *tv, int copyID, int restore_copyID)
1303 {
1304 garray_T ga;
1305
1306 if (tv->vval.v_list == NULL)
1307 return NULL;
1308 ga_init2(&ga, (int)sizeof(char), 80);
1309 ga_append(&ga, '[');
1310 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
1311 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1312 FALSE, restore_copyID, copyID) == FAIL)
1313 {
1314 vim_free(ga.ga_data);
1315 return NULL;
1316 }
1317 ga_append(&ga, ']');
1318 ga_append(&ga, NUL);
1319 return (char_u *)ga.ga_data;
1320 }
1321
1322 typedef struct join_S {
1323 char_u *s;
1324 char_u *tofree;
1325 } join_T;
1326
1327 static int
list_join_inner(garray_T * gap,list_T * l,char_u * sep,int echo_style,int restore_copyID,int copyID,garray_T * join_gap)1328 list_join_inner(
1329 garray_T *gap, // to store the result in
1330 list_T *l,
1331 char_u *sep,
1332 int echo_style,
1333 int restore_copyID,
1334 int copyID,
1335 garray_T *join_gap) // to keep each list item string
1336 {
1337 int i;
1338 join_T *p;
1339 int len;
1340 int sumlen = 0;
1341 int first = TRUE;
1342 char_u *tofree;
1343 char_u numbuf[NUMBUFLEN];
1344 listitem_T *item;
1345 char_u *s;
1346
1347 // Stringify each item in the list.
1348 CHECK_LIST_MATERIALIZE(l);
1349 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1350 {
1351 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
1352 echo_style, restore_copyID, !echo_style);
1353 if (s == NULL)
1354 return FAIL;
1355
1356 len = (int)STRLEN(s);
1357 sumlen += len;
1358
1359 (void)ga_grow(join_gap, 1);
1360 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1361 if (tofree != NULL || s != numbuf)
1362 {
1363 p->s = s;
1364 p->tofree = tofree;
1365 }
1366 else
1367 {
1368 p->s = vim_strnsave(s, len);
1369 p->tofree = p->s;
1370 }
1371
1372 line_breakcheck();
1373 if (did_echo_string_emsg) // recursion error, bail out
1374 break;
1375 }
1376
1377 // Allocate result buffer with its total size, avoid re-allocation and
1378 // multiple copy operations. Add 2 for a tailing ']' and NUL.
1379 if (join_gap->ga_len >= 2)
1380 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1381 if (ga_grow(gap, sumlen + 2) == FAIL)
1382 return FAIL;
1383
1384 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1385 {
1386 if (first)
1387 first = FALSE;
1388 else
1389 ga_concat(gap, sep);
1390 p = ((join_T *)join_gap->ga_data) + i;
1391
1392 if (p->s != NULL)
1393 ga_concat(gap, p->s);
1394 line_breakcheck();
1395 }
1396
1397 return OK;
1398 }
1399
1400 /*
1401 * Join list "l" into a string in "*gap", using separator "sep".
1402 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1403 * Return FAIL or OK.
1404 */
1405 int
list_join(garray_T * gap,list_T * l,char_u * sep,int echo_style,int restore_copyID,int copyID)1406 list_join(
1407 garray_T *gap,
1408 list_T *l,
1409 char_u *sep,
1410 int echo_style,
1411 int restore_copyID,
1412 int copyID)
1413 {
1414 garray_T join_ga;
1415 int retval;
1416 join_T *p;
1417 int i;
1418
1419 if (l->lv_len < 1)
1420 return OK; // nothing to do
1421 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1422 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1423 copyID, &join_ga);
1424
1425 // Dispose each item in join_ga.
1426 if (join_ga.ga_data != NULL)
1427 {
1428 p = (join_T *)join_ga.ga_data;
1429 for (i = 0; i < join_ga.ga_len; ++i)
1430 {
1431 vim_free(p->tofree);
1432 ++p;
1433 }
1434 ga_clear(&join_ga);
1435 }
1436
1437 return retval;
1438 }
1439
1440 /*
1441 * "join()" function
1442 */
1443 void
f_join(typval_T * argvars,typval_T * rettv)1444 f_join(typval_T *argvars, typval_T *rettv)
1445 {
1446 garray_T ga;
1447 char_u *sep;
1448
1449 if (in_vim9script()
1450 && (check_for_list_arg(argvars, 0) == FAIL
1451 || check_for_opt_string_arg(argvars, 1) == FAIL))
1452 return;
1453
1454 if (argvars[0].v_type != VAR_LIST)
1455 {
1456 emsg(_(e_listreq));
1457 return;
1458 }
1459 rettv->v_type = VAR_STRING;
1460 if (argvars[0].vval.v_list == NULL)
1461 return;
1462
1463 if (argvars[1].v_type == VAR_UNKNOWN)
1464 sep = (char_u *)" ";
1465 else
1466 sep = tv_get_string_chk(&argvars[1]);
1467
1468 if (sep != NULL)
1469 {
1470 ga_init2(&ga, (int)sizeof(char), 80);
1471 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1472 ga_append(&ga, NUL);
1473 rettv->vval.v_string = (char_u *)ga.ga_data;
1474 }
1475 else
1476 rettv->vval.v_string = NULL;
1477 }
1478
1479 /*
1480 * Allocate a variable for a List and fill it from "*arg".
1481 * "*arg" points to the "[".
1482 * Return OK or FAIL.
1483 */
1484 int
eval_list(char_u ** arg,typval_T * rettv,evalarg_T * evalarg,int do_error)1485 eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
1486 {
1487 int evaluate = evalarg == NULL ? FALSE
1488 : evalarg->eval_flags & EVAL_EVALUATE;
1489 list_T *l = NULL;
1490 typval_T tv;
1491 listitem_T *item;
1492 int vim9script = in_vim9script();
1493 int had_comma;
1494
1495 if (evaluate)
1496 {
1497 l = list_alloc();
1498 if (l == NULL)
1499 return FAIL;
1500 }
1501
1502 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
1503 while (**arg != ']' && **arg != NUL)
1504 {
1505 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
1506 goto failret;
1507 if (evaluate)
1508 {
1509 item = listitem_alloc();
1510 if (item != NULL)
1511 {
1512 item->li_tv = tv;
1513 item->li_tv.v_lock = 0;
1514 list_append(l, item);
1515 }
1516 else
1517 clear_tv(&tv);
1518 }
1519 // Legacy Vim script allowed a space before the comma.
1520 if (!vim9script)
1521 *arg = skipwhite(*arg);
1522
1523 // the comma must come after the value
1524 had_comma = **arg == ',';
1525 if (had_comma)
1526 {
1527 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
1528 {
1529 semsg(_(e_white_space_required_after_str_str), ",", *arg);
1530 goto failret;
1531 }
1532 *arg = skipwhite(*arg + 1);
1533 }
1534
1535 // The "]" can be on the next line. But a double quoted string may
1536 // follow, not a comment.
1537 *arg = skipwhite_and_linebreak(*arg, evalarg);
1538 if (**arg == ']')
1539 break;
1540
1541 if (!had_comma)
1542 {
1543 if (do_error)
1544 {
1545 if (**arg == ',')
1546 semsg(_(e_no_white_space_allowed_before_str_str),
1547 ",", *arg);
1548 else
1549 semsg(_("E696: Missing comma in List: %s"), *arg);
1550 }
1551 goto failret;
1552 }
1553 }
1554
1555 if (**arg != ']')
1556 {
1557 if (do_error)
1558 semsg(_(e_list_end), *arg);
1559 failret:
1560 if (evaluate)
1561 list_free(l);
1562 return FAIL;
1563 }
1564
1565 *arg += 1;
1566 if (evaluate)
1567 rettv_list_set(rettv, l);
1568
1569 return OK;
1570 }
1571
1572 /*
1573 * Write "list" of strings to file "fd".
1574 */
1575 int
write_list(FILE * fd,list_T * list,int binary)1576 write_list(FILE *fd, list_T *list, int binary)
1577 {
1578 listitem_T *li;
1579 int c;
1580 int ret = OK;
1581 char_u *s;
1582
1583 CHECK_LIST_MATERIALIZE(list);
1584 FOR_ALL_LIST_ITEMS(list, li)
1585 {
1586 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
1587 {
1588 if (*s == '\n')
1589 c = putc(NUL, fd);
1590 else
1591 c = putc(*s, fd);
1592 if (c == EOF)
1593 {
1594 ret = FAIL;
1595 break;
1596 }
1597 }
1598 if (!binary || li->li_next != NULL)
1599 if (putc('\n', fd) == EOF)
1600 {
1601 ret = FAIL;
1602 break;
1603 }
1604 if (ret == FAIL)
1605 {
1606 emsg(_(e_write));
1607 break;
1608 }
1609 }
1610 return ret;
1611 }
1612
1613 /*
1614 * Initialize a static list with 10 items.
1615 */
1616 void
init_static_list(staticList10_T * sl)1617 init_static_list(staticList10_T *sl)
1618 {
1619 list_T *l = &sl->sl_list;
1620 int i;
1621
1622 memset(sl, 0, sizeof(staticList10_T));
1623 l->lv_first = &sl->sl_items[0];
1624 l->lv_u.mat.lv_last = &sl->sl_items[9];
1625 l->lv_refcount = DO_NOT_FREE_CNT;
1626 l->lv_lock = VAR_FIXED;
1627 sl->sl_list.lv_len = 10;
1628
1629 for (i = 0; i < 10; ++i)
1630 {
1631 listitem_T *li = &sl->sl_items[i];
1632
1633 if (i == 0)
1634 li->li_prev = NULL;
1635 else
1636 li->li_prev = li - 1;
1637 if (i == 9)
1638 li->li_next = NULL;
1639 else
1640 li->li_next = li + 1;
1641 }
1642 }
1643
1644 /*
1645 * "list2str()" function
1646 */
1647 void
f_list2str(typval_T * argvars,typval_T * rettv)1648 f_list2str(typval_T *argvars, typval_T *rettv)
1649 {
1650 list_T *l;
1651 listitem_T *li;
1652 garray_T ga;
1653 int utf8 = FALSE;
1654
1655 rettv->v_type = VAR_STRING;
1656 rettv->vval.v_string = NULL;
1657
1658 if (in_vim9script()
1659 && (check_for_list_arg(argvars, 0) == FAIL
1660 || check_for_opt_bool_arg(argvars, 1) == FAIL))
1661 return;
1662
1663 if (argvars[0].v_type != VAR_LIST)
1664 {
1665 emsg(_(e_invarg));
1666 return;
1667 }
1668
1669 l = argvars[0].vval.v_list;
1670 if (l == NULL)
1671 return; // empty list results in empty string
1672
1673 if (argvars[1].v_type != VAR_UNKNOWN)
1674 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
1675
1676 CHECK_LIST_MATERIALIZE(l);
1677 ga_init2(&ga, 1, 80);
1678 if (has_mbyte || utf8)
1679 {
1680 char_u buf[MB_MAXBYTES + 1];
1681 int (*char2bytes)(int, char_u *);
1682
1683 if (utf8 || enc_utf8)
1684 char2bytes = utf_char2bytes;
1685 else
1686 char2bytes = mb_char2bytes;
1687
1688 FOR_ALL_LIST_ITEMS(l, li)
1689 {
1690 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1691 ga_concat(&ga, buf);
1692 }
1693 ga_append(&ga, NUL);
1694 }
1695 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1696 {
1697 FOR_ALL_LIST_ITEMS(l, li)
1698 ga_append(&ga, tv_get_number(&li->li_tv));
1699 ga_append(&ga, NUL);
1700 }
1701
1702 rettv->v_type = VAR_STRING;
1703 rettv->vval.v_string = ga.ga_data;
1704 }
1705
1706 static void
list_remove(typval_T * argvars,typval_T * rettv,char_u * arg_errmsg)1707 list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1708 {
1709 list_T *l;
1710 listitem_T *item, *item2;
1711 listitem_T *li;
1712 int error = FALSE;
1713 long idx;
1714
1715 if ((l = argvars[0].vval.v_list) == NULL
1716 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
1717 return;
1718
1719 idx = (long)tv_get_number_chk(&argvars[1], &error);
1720 if (error)
1721 ; // type error: do nothing, errmsg already given
1722 else if ((item = list_find(l, idx)) == NULL)
1723 semsg(_(e_listidx), idx);
1724 else
1725 {
1726 if (argvars[2].v_type == VAR_UNKNOWN)
1727 {
1728 // Remove one item, return its value.
1729 vimlist_remove(l, item, item);
1730 *rettv = item->li_tv;
1731 list_free_item(l, item);
1732 }
1733 else
1734 {
1735 // Remove range of items, return list with values.
1736 long end = (long)tv_get_number_chk(&argvars[2], &error);
1737
1738 if (error)
1739 ; // type error: do nothing
1740 else if ((item2 = list_find(l, end)) == NULL)
1741 semsg(_(e_listidx), end);
1742 else
1743 {
1744 int cnt = 0;
1745
1746 for (li = item; li != NULL; li = li->li_next)
1747 {
1748 ++cnt;
1749 if (li == item2)
1750 break;
1751 }
1752 if (li == NULL) // didn't find "item2" after "item"
1753 emsg(_(e_invalid_range));
1754 else
1755 {
1756 vimlist_remove(l, item, item2);
1757 if (rettv_list_alloc(rettv) == OK)
1758 {
1759 list_T *rl = rettv->vval.v_list;
1760
1761 if (l->lv_with_items > 0)
1762 {
1763 // need to copy the list items and move the value
1764 while (item != NULL)
1765 {
1766 li = listitem_alloc();
1767 if (li == NULL)
1768 return;
1769 li->li_tv = item->li_tv;
1770 init_tv(&item->li_tv);
1771 list_append(rl, li);
1772 if (item == item2)
1773 break;
1774 item = item->li_next;
1775 }
1776 }
1777 else
1778 {
1779 rl->lv_first = item;
1780 rl->lv_u.mat.lv_last = item2;
1781 item->li_prev = NULL;
1782 item2->li_next = NULL;
1783 rl->lv_len = cnt;
1784 }
1785 }
1786 }
1787 }
1788 }
1789 }
1790 }
1791
1792 static int item_compare(const void *s1, const void *s2);
1793 static int item_compare2(const void *s1, const void *s2);
1794
1795 // struct used in the array that's given to qsort()
1796 typedef struct
1797 {
1798 listitem_T *item;
1799 int idx;
1800 } sortItem_T;
1801
1802 // struct storing information about current sort
1803 typedef struct
1804 {
1805 int item_compare_ic;
1806 int item_compare_lc;
1807 int item_compare_numeric;
1808 int item_compare_numbers;
1809 #ifdef FEAT_FLOAT
1810 int item_compare_float;
1811 #endif
1812 char_u *item_compare_func;
1813 partial_T *item_compare_partial;
1814 dict_T *item_compare_selfdict;
1815 int item_compare_func_err;
1816 int item_compare_keep_zero;
1817 } sortinfo_T;
1818 static sortinfo_T *sortinfo = NULL;
1819 #define ITEM_COMPARE_FAIL 999
1820
1821 /*
1822 * Compare functions for f_sort() and f_uniq() below.
1823 */
1824 static int
item_compare(const void * s1,const void * s2)1825 item_compare(const void *s1, const void *s2)
1826 {
1827 sortItem_T *si1, *si2;
1828 typval_T *tv1, *tv2;
1829 char_u *p1, *p2;
1830 char_u *tofree1 = NULL, *tofree2 = NULL;
1831 int res;
1832 char_u numbuf1[NUMBUFLEN];
1833 char_u numbuf2[NUMBUFLEN];
1834
1835 si1 = (sortItem_T *)s1;
1836 si2 = (sortItem_T *)s2;
1837 tv1 = &si1->item->li_tv;
1838 tv2 = &si2->item->li_tv;
1839
1840 if (sortinfo->item_compare_numbers)
1841 {
1842 varnumber_T v1 = tv_get_number(tv1);
1843 varnumber_T v2 = tv_get_number(tv2);
1844
1845 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1846 }
1847
1848 #ifdef FEAT_FLOAT
1849 if (sortinfo->item_compare_float)
1850 {
1851 float_T v1 = tv_get_float(tv1);
1852 float_T v2 = tv_get_float(tv2);
1853
1854 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1855 }
1856 #endif
1857
1858 // tv2string() puts quotes around a string and allocates memory. Don't do
1859 // that for string variables. Use a single quote when comparing with a
1860 // non-string to do what the docs promise.
1861 if (tv1->v_type == VAR_STRING)
1862 {
1863 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1864 p1 = (char_u *)"'";
1865 else
1866 p1 = tv1->vval.v_string;
1867 }
1868 else
1869 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1870 if (tv2->v_type == VAR_STRING)
1871 {
1872 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1873 p2 = (char_u *)"'";
1874 else
1875 p2 = tv2->vval.v_string;
1876 }
1877 else
1878 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1879 if (p1 == NULL)
1880 p1 = (char_u *)"";
1881 if (p2 == NULL)
1882 p2 = (char_u *)"";
1883 if (!sortinfo->item_compare_numeric)
1884 {
1885 if (sortinfo->item_compare_lc)
1886 res = strcoll((char *)p1, (char *)p2);
1887 else
1888 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
1889 }
1890 else
1891 {
1892 double n1, n2;
1893 n1 = strtod((char *)p1, (char **)&p1);
1894 n2 = strtod((char *)p2, (char **)&p2);
1895 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1896 }
1897
1898 // When the result would be zero, compare the item indexes. Makes the
1899 // sort stable.
1900 if (res == 0 && !sortinfo->item_compare_keep_zero)
1901 res = si1->idx > si2->idx ? 1 : -1;
1902
1903 vim_free(tofree1);
1904 vim_free(tofree2);
1905 return res;
1906 }
1907
1908 static int
item_compare2(const void * s1,const void * s2)1909 item_compare2(const void *s1, const void *s2)
1910 {
1911 sortItem_T *si1, *si2;
1912 int res;
1913 typval_T rettv;
1914 typval_T argv[3];
1915 char_u *func_name;
1916 partial_T *partial = sortinfo->item_compare_partial;
1917 funcexe_T funcexe;
1918
1919 // shortcut after failure in previous call; compare all items equal
1920 if (sortinfo->item_compare_func_err)
1921 return 0;
1922
1923 si1 = (sortItem_T *)s1;
1924 si2 = (sortItem_T *)s2;
1925
1926 if (partial == NULL)
1927 func_name = sortinfo->item_compare_func;
1928 else
1929 func_name = partial_name(partial);
1930
1931 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1932 // in the copy without changing the original list items.
1933 copy_tv(&si1->item->li_tv, &argv[0]);
1934 copy_tv(&si2->item->li_tv, &argv[1]);
1935
1936 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
1937 CLEAR_FIELD(funcexe);
1938 funcexe.evaluate = TRUE;
1939 funcexe.partial = partial;
1940 funcexe.selfdict = sortinfo->item_compare_selfdict;
1941 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
1942 clear_tv(&argv[0]);
1943 clear_tv(&argv[1]);
1944
1945 if (res == FAIL)
1946 res = ITEM_COMPARE_FAIL;
1947 else
1948 {
1949 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1950 if (res > 0)
1951 res = 1;
1952 else if (res < 0)
1953 res = -1;
1954 }
1955 if (sortinfo->item_compare_func_err)
1956 res = ITEM_COMPARE_FAIL; // return value has wrong type
1957 clear_tv(&rettv);
1958
1959 // When the result would be zero, compare the pointers themselves. Makes
1960 // the sort stable.
1961 if (res == 0 && !sortinfo->item_compare_keep_zero)
1962 res = si1->idx > si2->idx ? 1 : -1;
1963
1964 return res;
1965 }
1966
1967 /*
1968 * "sort()" or "uniq()" function
1969 */
1970 static void
do_sort_uniq(typval_T * argvars,typval_T * rettv,int sort)1971 do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1972 {
1973 list_T *l;
1974 listitem_T *li;
1975 sortItem_T *ptrs;
1976 sortinfo_T *old_sortinfo;
1977 sortinfo_T info;
1978 long len;
1979 long i;
1980
1981 if (in_vim9script()
1982 && (check_for_list_arg(argvars, 0) == FAIL
1983 || (argvars[1].v_type != VAR_UNKNOWN
1984 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
1985 return;
1986
1987 // Pointer to current info struct used in compare function. Save and
1988 // restore the current one for nested calls.
1989 old_sortinfo = sortinfo;
1990 sortinfo = &info;
1991
1992 if (argvars[0].v_type != VAR_LIST)
1993 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1994 else
1995 {
1996 l = argvars[0].vval.v_list;
1997 if (l != NULL && value_check_lock(l->lv_lock,
1998 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1999 TRUE))
2000 goto theend;
2001 rettv_list_set(rettv, l);
2002 if (l == NULL)
2003 goto theend;
2004 CHECK_LIST_MATERIALIZE(l);
2005
2006 len = list_len(l);
2007 if (len <= 1)
2008 goto theend; // short list sorts pretty quickly
2009
2010 info.item_compare_ic = FALSE;
2011 info.item_compare_lc = FALSE;
2012 info.item_compare_numeric = FALSE;
2013 info.item_compare_numbers = FALSE;
2014 #ifdef FEAT_FLOAT
2015 info.item_compare_float = FALSE;
2016 #endif
2017 info.item_compare_func = NULL;
2018 info.item_compare_partial = NULL;
2019 info.item_compare_selfdict = NULL;
2020 if (argvars[1].v_type != VAR_UNKNOWN)
2021 {
2022 // optional second argument: {func}
2023 if (argvars[1].v_type == VAR_FUNC)
2024 info.item_compare_func = argvars[1].vval.v_string;
2025 else if (argvars[1].v_type == VAR_PARTIAL)
2026 info.item_compare_partial = argvars[1].vval.v_partial;
2027 else
2028 {
2029 int error = FALSE;
2030 int nr = 0;
2031
2032 if (argvars[1].v_type == VAR_NUMBER)
2033 {
2034 nr = tv_get_number_chk(&argvars[1], &error);
2035 if (error)
2036 goto theend; // type error; errmsg already given
2037 if (nr == 1)
2038 info.item_compare_ic = TRUE;
2039 }
2040 if (nr != 1)
2041 {
2042 if (argvars[1].v_type != VAR_NUMBER)
2043 info.item_compare_func = tv_get_string(&argvars[1]);
2044 else if (nr != 0)
2045 {
2046 emsg(_(e_invarg));
2047 goto theend;
2048 }
2049 }
2050 if (info.item_compare_func != NULL)
2051 {
2052 if (*info.item_compare_func == NUL)
2053 {
2054 // empty string means default sort
2055 info.item_compare_func = NULL;
2056 }
2057 else if (STRCMP(info.item_compare_func, "n") == 0)
2058 {
2059 info.item_compare_func = NULL;
2060 info.item_compare_numeric = TRUE;
2061 }
2062 else if (STRCMP(info.item_compare_func, "N") == 0)
2063 {
2064 info.item_compare_func = NULL;
2065 info.item_compare_numbers = TRUE;
2066 }
2067 #ifdef FEAT_FLOAT
2068 else if (STRCMP(info.item_compare_func, "f") == 0)
2069 {
2070 info.item_compare_func = NULL;
2071 info.item_compare_float = TRUE;
2072 }
2073 #endif
2074 else if (STRCMP(info.item_compare_func, "i") == 0)
2075 {
2076 info.item_compare_func = NULL;
2077 info.item_compare_ic = TRUE;
2078 }
2079 else if (STRCMP(info.item_compare_func, "l") == 0)
2080 {
2081 info.item_compare_func = NULL;
2082 info.item_compare_lc = TRUE;
2083 }
2084 }
2085 }
2086
2087 if (argvars[2].v_type != VAR_UNKNOWN)
2088 {
2089 // optional third argument: {dict}
2090 if (argvars[2].v_type != VAR_DICT)
2091 {
2092 emsg(_(e_dictreq));
2093 goto theend;
2094 }
2095 info.item_compare_selfdict = argvars[2].vval.v_dict;
2096 }
2097 }
2098
2099 // Make an array with each entry pointing to an item in the List.
2100 ptrs = ALLOC_MULT(sortItem_T, len);
2101 if (ptrs == NULL)
2102 goto theend;
2103
2104 i = 0;
2105 if (sort)
2106 {
2107 // sort(): ptrs will be the list to sort
2108 FOR_ALL_LIST_ITEMS(l, li)
2109 {
2110 ptrs[i].item = li;
2111 ptrs[i].idx = i;
2112 ++i;
2113 }
2114
2115 info.item_compare_func_err = FALSE;
2116 info.item_compare_keep_zero = FALSE;
2117 // test the compare function
2118 if ((info.item_compare_func != NULL
2119 || info.item_compare_partial != NULL)
2120 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
2121 == ITEM_COMPARE_FAIL)
2122 emsg(_("E702: Sort compare function failed"));
2123 else
2124 {
2125 // Sort the array with item pointers.
2126 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
2127 info.item_compare_func == NULL
2128 && info.item_compare_partial == NULL
2129 ? item_compare : item_compare2);
2130
2131 if (!info.item_compare_func_err)
2132 {
2133 // Clear the List and append the items in sorted order.
2134 l->lv_first = l->lv_u.mat.lv_last
2135 = l->lv_u.mat.lv_idx_item = NULL;
2136 l->lv_len = 0;
2137 for (i = 0; i < len; ++i)
2138 list_append(l, ptrs[i].item);
2139 }
2140 }
2141 }
2142 else
2143 {
2144 int (*item_compare_func_ptr)(const void *, const void *);
2145
2146 // f_uniq(): ptrs will be a stack of items to remove
2147 info.item_compare_func_err = FALSE;
2148 info.item_compare_keep_zero = TRUE;
2149 item_compare_func_ptr = info.item_compare_func != NULL
2150 || info.item_compare_partial != NULL
2151 ? item_compare2 : item_compare;
2152
2153 for (li = l->lv_first; li != NULL && li->li_next != NULL;
2154 li = li->li_next)
2155 {
2156 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
2157 == 0)
2158 ptrs[i++].item = li;
2159 if (info.item_compare_func_err)
2160 {
2161 emsg(_("E882: Uniq compare function failed"));
2162 break;
2163 }
2164 }
2165
2166 if (!info.item_compare_func_err)
2167 {
2168 while (--i >= 0)
2169 {
2170 li = ptrs[i].item->li_next;
2171 ptrs[i].item->li_next = li->li_next;
2172 if (li->li_next != NULL)
2173 li->li_next->li_prev = ptrs[i].item;
2174 else
2175 l->lv_u.mat.lv_last = ptrs[i].item;
2176 list_fix_watch(l, li);
2177 listitem_free(l, li);
2178 l->lv_len--;
2179 }
2180 }
2181 }
2182
2183 vim_free(ptrs);
2184 }
2185 theend:
2186 sortinfo = old_sortinfo;
2187 }
2188
2189 /*
2190 * "sort({list})" function
2191 */
2192 void
f_sort(typval_T * argvars,typval_T * rettv)2193 f_sort(typval_T *argvars, typval_T *rettv)
2194 {
2195 do_sort_uniq(argvars, rettv, TRUE);
2196 }
2197
2198 /*
2199 * "uniq({list})" function
2200 */
2201 void
f_uniq(typval_T * argvars,typval_T * rettv)2202 f_uniq(typval_T *argvars, typval_T *rettv)
2203 {
2204 do_sort_uniq(argvars, rettv, FALSE);
2205 }
2206
2207 typedef enum {
2208 FILTERMAP_FILTER,
2209 FILTERMAP_MAP,
2210 FILTERMAP_MAPNEW
2211 } filtermap_T;
2212
2213 /*
2214 * Handle one item for map() and filter().
2215 * Sets v:val to "tv". Caller must set v:key.
2216 */
2217 static int
filter_map_one(typval_T * tv,typval_T * expr,filtermap_T filtermap,typval_T * newtv,int * remp)2218 filter_map_one(
2219 typval_T *tv, // original value
2220 typval_T *expr, // callback
2221 filtermap_T filtermap,
2222 typval_T *newtv, // for map() and mapnew(): new value
2223 int *remp) // for filter(): remove flag
2224 {
2225 typval_T argv[3];
2226 int retval = FAIL;
2227
2228 copy_tv(tv, get_vim_var_tv(VV_VAL));
2229 argv[0] = *get_vim_var_tv(VV_KEY);
2230 argv[1] = *get_vim_var_tv(VV_VAL);
2231 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
2232 goto theend;
2233 if (filtermap == FILTERMAP_FILTER)
2234 {
2235 int error = FALSE;
2236
2237 // filter(): when expr is zero remove the item
2238 if (in_vim9script())
2239 *remp = !tv2bool(newtv);
2240 else
2241 *remp = (tv_get_number_chk(newtv, &error) == 0);
2242 clear_tv(newtv);
2243 // On type error, nothing has been removed; return FAIL to stop the
2244 // loop. The error message was given by tv_get_number_chk().
2245 if (error)
2246 goto theend;
2247 }
2248 retval = OK;
2249 theend:
2250 clear_tv(get_vim_var_tv(VV_VAL));
2251 return retval;
2252 }
2253
2254 /*
2255 * Implementation of map() and filter().
2256 */
2257 static void
filter_map(typval_T * argvars,typval_T * rettv,filtermap_T filtermap)2258 filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
2259 {
2260 typval_T *expr;
2261 listitem_T *li, *nli;
2262 list_T *l = NULL;
2263 dictitem_T *di;
2264 hashtab_T *ht;
2265 hashitem_T *hi;
2266 dict_T *d = NULL;
2267 blob_T *b = NULL;
2268 int rem;
2269 int todo;
2270 char *func_name = filtermap == FILTERMAP_MAP ? "map()"
2271 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
2272 : "filter()";
2273 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2274 ? N_("map() argument")
2275 : filtermap == FILTERMAP_MAPNEW
2276 ? N_("mapnew() argument")
2277 : N_("filter() argument"));
2278 int save_did_emsg;
2279 int idx = 0;
2280 type_T *type = NULL;
2281 garray_T type_list;
2282
2283 // map() and filter() return the first argument, also on failure.
2284 if (filtermap != FILTERMAP_MAPNEW)
2285 copy_tv(&argvars[0], rettv);
2286
2287 if (in_vim9script()
2288 && (check_for_list_or_dict_or_blob_arg(argvars, 0) == FAIL))
2289 return;
2290
2291 if (filtermap == FILTERMAP_MAP && in_vim9script())
2292 {
2293 // Check that map() does not change the type of the dict.
2294 ga_init2(&type_list, sizeof(type_T *), 10);
2295 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
2296 }
2297
2298 if (argvars[0].v_type == VAR_BLOB)
2299 {
2300 if (filtermap == FILTERMAP_MAPNEW)
2301 {
2302 rettv->v_type = VAR_BLOB;
2303 rettv->vval.v_blob = NULL;
2304 }
2305 if ((b = argvars[0].vval.v_blob) == NULL)
2306 goto theend;
2307 }
2308 else if (argvars[0].v_type == VAR_LIST)
2309 {
2310 if (filtermap == FILTERMAP_MAPNEW)
2311 {
2312 rettv->v_type = VAR_LIST;
2313 rettv->vval.v_list = NULL;
2314 }
2315 if ((l = argvars[0].vval.v_list) == NULL
2316 || (filtermap == FILTERMAP_FILTER
2317 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
2318 goto theend;
2319 }
2320 else if (argvars[0].v_type == VAR_DICT)
2321 {
2322 if (filtermap == FILTERMAP_MAPNEW)
2323 {
2324 rettv->v_type = VAR_DICT;
2325 rettv->vval.v_dict = NULL;
2326 }
2327 if ((d = argvars[0].vval.v_dict) == NULL
2328 || (filtermap == FILTERMAP_FILTER
2329 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
2330 goto theend;
2331 }
2332 else
2333 {
2334 semsg(_(e_listdictblobarg), func_name);
2335 goto theend;
2336 }
2337
2338 expr = &argvars[1];
2339 // On type errors, the preceding call has already displayed an error
2340 // message. Avoid a misleading error message for an empty string that
2341 // was not passed as argument.
2342 if (expr->v_type != VAR_UNKNOWN)
2343 {
2344 typval_T save_val;
2345 typval_T save_key;
2346
2347 prepare_vimvar(VV_VAL, &save_val);
2348 prepare_vimvar(VV_KEY, &save_key);
2349
2350 // We reset "did_emsg" to be able to detect whether an error
2351 // occurred during evaluation of the expression.
2352 save_did_emsg = did_emsg;
2353 did_emsg = FALSE;
2354
2355 if (argvars[0].v_type == VAR_DICT)
2356 {
2357 int prev_lock = d->dv_lock;
2358 dict_T *d_ret = NULL;
2359
2360 if (filtermap == FILTERMAP_MAPNEW)
2361 {
2362 if (rettv_dict_alloc(rettv) == FAIL)
2363 goto theend;
2364 d_ret = rettv->vval.v_dict;
2365 }
2366
2367 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
2368 d->dv_lock = VAR_LOCKED;
2369 ht = &d->dv_hashtab;
2370 hash_lock(ht);
2371 todo = (int)ht->ht_used;
2372 for (hi = ht->ht_array; todo > 0; ++hi)
2373 {
2374 if (!HASHITEM_EMPTY(hi))
2375 {
2376 int r;
2377 typval_T newtv;
2378
2379 --todo;
2380 di = HI2DI(hi);
2381 if (filtermap == FILTERMAP_MAP
2382 && (value_check_lock(di->di_tv.v_lock,
2383 arg_errmsg, TRUE)
2384 || var_check_ro(di->di_flags,
2385 arg_errmsg, TRUE)))
2386 break;
2387 set_vim_var_string(VV_KEY, di->di_key, -1);
2388 newtv.v_type = VAR_UNKNOWN;
2389 r = filter_map_one(&di->di_tv, expr, filtermap,
2390 &newtv, &rem);
2391 clear_tv(get_vim_var_tv(VV_KEY));
2392 if (r == FAIL || did_emsg)
2393 {
2394 clear_tv(&newtv);
2395 break;
2396 }
2397 if (filtermap == FILTERMAP_MAP)
2398 {
2399 if (type != NULL && check_typval_arg_type(
2400 type->tt_member, &newtv,
2401 func_name, 0) == FAIL)
2402 {
2403 clear_tv(&newtv);
2404 break;
2405 }
2406 // map(): replace the dict item value
2407 clear_tv(&di->di_tv);
2408 newtv.v_lock = 0;
2409 di->di_tv = newtv;
2410 }
2411 else if (filtermap == FILTERMAP_MAPNEW)
2412 {
2413 // mapnew(): add the item value to the new dict
2414 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
2415 clear_tv(&newtv);
2416 if (r == FAIL)
2417 break;
2418 }
2419 else if (filtermap == FILTERMAP_FILTER && rem)
2420 {
2421 // filter(false): remove the item from the dict
2422 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2423 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2424 break;
2425 dictitem_remove(d, di);
2426 }
2427 }
2428 }
2429 hash_unlock(ht);
2430 d->dv_lock = prev_lock;
2431 }
2432 else if (argvars[0].v_type == VAR_BLOB)
2433 {
2434 int i;
2435 typval_T tv;
2436 varnumber_T val;
2437 blob_T *b_ret = b;
2438
2439 if (filtermap == FILTERMAP_MAPNEW)
2440 {
2441 if (blob_copy(b, rettv) == FAIL)
2442 goto theend;
2443 b_ret = rettv->vval.v_blob;
2444 }
2445
2446 // set_vim_var_nr() doesn't set the type
2447 set_vim_var_type(VV_KEY, VAR_NUMBER);
2448
2449 for (i = 0; i < b->bv_ga.ga_len; i++)
2450 {
2451 typval_T newtv;
2452
2453 tv.v_type = VAR_NUMBER;
2454 val = blob_get(b, i);
2455 tv.vval.v_number = val;
2456 set_vim_var_nr(VV_KEY, idx);
2457 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
2458 || did_emsg)
2459 break;
2460 if (newtv.v_type != VAR_NUMBER && newtv.v_type != VAR_BOOL)
2461 {
2462 clear_tv(&newtv);
2463 emsg(_(e_invalblob));
2464 break;
2465 }
2466 if (filtermap != FILTERMAP_FILTER)
2467 {
2468 if (newtv.vval.v_number != val)
2469 blob_set(b_ret, i, newtv.vval.v_number);
2470 }
2471 else if (rem)
2472 {
2473 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2474
2475 mch_memmove(p + i, p + i + 1,
2476 (size_t)b->bv_ga.ga_len - i - 1);
2477 --b->bv_ga.ga_len;
2478 --i;
2479 }
2480 ++idx;
2481 }
2482 }
2483 else // argvars[0].v_type == VAR_LIST
2484 {
2485 int prev_lock = l->lv_lock;
2486 list_T *l_ret = NULL;
2487
2488 if (filtermap == FILTERMAP_MAPNEW)
2489 {
2490 if (rettv_list_alloc(rettv) == FAIL)
2491 goto theend;
2492 l_ret = rettv->vval.v_list;
2493 }
2494 // set_vim_var_nr() doesn't set the type
2495 set_vim_var_type(VV_KEY, VAR_NUMBER);
2496
2497 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
2498 l->lv_lock = VAR_LOCKED;
2499
2500 if (l->lv_first == &range_list_item)
2501 {
2502 varnumber_T val = l->lv_u.nonmat.lv_start;
2503 int len = l->lv_len;
2504 int stride = l->lv_u.nonmat.lv_stride;
2505
2506 // List from range(): loop over the numbers
2507 if (filtermap != FILTERMAP_MAPNEW)
2508 {
2509 l->lv_first = NULL;
2510 l->lv_u.mat.lv_last = NULL;
2511 l->lv_len = 0;
2512 l->lv_u.mat.lv_idx_item = NULL;
2513 }
2514
2515 for (idx = 0; idx < len; ++idx)
2516 {
2517 typval_T tv;
2518 typval_T newtv;
2519
2520 tv.v_type = VAR_NUMBER;
2521 tv.v_lock = 0;
2522 tv.vval.v_number = val;
2523 set_vim_var_nr(VV_KEY, idx);
2524 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2525 == FAIL)
2526 break;
2527 if (did_emsg)
2528 {
2529 clear_tv(&newtv);
2530 break;
2531 }
2532 if (filtermap != FILTERMAP_FILTER)
2533 {
2534 if (filtermap == FILTERMAP_MAP && type != NULL
2535 && check_typval_arg_type(
2536 type->tt_member, &newtv,
2537 func_name, 0) == FAIL)
2538 {
2539 clear_tv(&newtv);
2540 break;
2541 }
2542 // map(), mapnew(): always append the new value to the
2543 // list
2544 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2545 ? l : l_ret, &newtv) == FAIL)
2546 break;
2547 }
2548 else if (!rem)
2549 {
2550 // filter(): append the list item value when not rem
2551 if (list_append_tv_move(l, &tv) == FAIL)
2552 break;
2553 }
2554
2555 val += stride;
2556 }
2557 }
2558 else
2559 {
2560 // Materialized list: loop over the items
2561 for (li = l->lv_first; li != NULL; li = nli)
2562 {
2563 typval_T newtv;
2564
2565 if (filtermap == FILTERMAP_MAP && value_check_lock(
2566 li->li_tv.v_lock, arg_errmsg, TRUE))
2567 break;
2568 nli = li->li_next;
2569 set_vim_var_nr(VV_KEY, idx);
2570 if (filter_map_one(&li->li_tv, expr, filtermap,
2571 &newtv, &rem) == FAIL)
2572 break;
2573 if (did_emsg)
2574 {
2575 clear_tv(&newtv);
2576 break;
2577 }
2578 if (filtermap == FILTERMAP_MAP)
2579 {
2580 if (type != NULL && check_typval_arg_type(
2581 type->tt_member, &newtv, func_name, 0) == FAIL)
2582 {
2583 clear_tv(&newtv);
2584 break;
2585 }
2586 // map(): replace the list item value
2587 clear_tv(&li->li_tv);
2588 newtv.v_lock = 0;
2589 li->li_tv = newtv;
2590 }
2591 else if (filtermap == FILTERMAP_MAPNEW)
2592 {
2593 // mapnew(): append the list item value
2594 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2595 break;
2596 }
2597 else if (filtermap == FILTERMAP_FILTER && rem)
2598 listitem_remove(l, li);
2599 ++idx;
2600 }
2601 }
2602
2603 l->lv_lock = prev_lock;
2604 }
2605
2606 restore_vimvar(VV_KEY, &save_key);
2607 restore_vimvar(VV_VAL, &save_val);
2608
2609 did_emsg |= save_did_emsg;
2610 }
2611
2612 theend:
2613 if (type != NULL)
2614 clear_type_list(&type_list);
2615 }
2616
2617 /*
2618 * "filter()" function
2619 */
2620 void
f_filter(typval_T * argvars,typval_T * rettv)2621 f_filter(typval_T *argvars, typval_T *rettv)
2622 {
2623 filter_map(argvars, rettv, FILTERMAP_FILTER);
2624 }
2625
2626 /*
2627 * "map()" function
2628 */
2629 void
f_map(typval_T * argvars,typval_T * rettv)2630 f_map(typval_T *argvars, typval_T *rettv)
2631 {
2632 filter_map(argvars, rettv, FILTERMAP_MAP);
2633 }
2634
2635 /*
2636 * "mapnew()" function
2637 */
2638 void
f_mapnew(typval_T * argvars,typval_T * rettv)2639 f_mapnew(typval_T *argvars, typval_T *rettv)
2640 {
2641 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
2642 }
2643
2644 /*
2645 * "add(list, item)" function
2646 */
2647 void
f_add(typval_T * argvars,typval_T * rettv)2648 f_add(typval_T *argvars, typval_T *rettv)
2649 {
2650 rettv->vval.v_number = 1; // Default: Failed
2651
2652 if (in_vim9script()
2653 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2654 || (argvars[0].v_type == VAR_BLOB
2655 && check_for_number_arg(argvars, 1) == FAIL)))
2656 return;
2657
2658 if (argvars[0].v_type == VAR_LIST)
2659 {
2660 list_T *l = argvars[0].vval.v_list;
2661
2662 if (l == NULL)
2663 {
2664 if (in_vim9script())
2665 emsg(_(e_cannot_add_to_null_list));
2666 }
2667 else if (!value_check_lock(l->lv_lock,
2668 (char_u *)N_("add() argument"), TRUE)
2669 && list_append_tv(l, &argvars[1]) == OK)
2670 {
2671 copy_tv(&argvars[0], rettv);
2672 }
2673 }
2674 else if (argvars[0].v_type == VAR_BLOB)
2675 {
2676 blob_T *b = argvars[0].vval.v_blob;
2677
2678 if (b == NULL)
2679 {
2680 if (in_vim9script())
2681 emsg(_(e_cannot_add_to_null_blob));
2682 }
2683 else if (!value_check_lock(b->bv_lock,
2684 (char_u *)N_("add() argument"), TRUE))
2685 {
2686 int error = FALSE;
2687 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2688
2689 if (!error)
2690 {
2691 ga_append(&b->bv_ga, (int)n);
2692 copy_tv(&argvars[0], rettv);
2693 }
2694 }
2695 }
2696 else
2697 emsg(_(e_listblobreq));
2698 }
2699
2700 /*
2701 * "count()" function
2702 */
2703 void
f_count(typval_T * argvars,typval_T * rettv)2704 f_count(typval_T *argvars, typval_T *rettv)
2705 {
2706 long n = 0;
2707 int ic = FALSE;
2708 int error = FALSE;
2709
2710 if (in_vim9script()
2711 && (check_for_string_or_list_or_dict_arg(argvars, 0) == FAIL
2712 || check_for_opt_bool_arg(argvars, 2) == FAIL
2713 || (argvars[2].v_type != VAR_UNKNOWN
2714 && check_for_opt_number_arg(argvars, 3) == FAIL)))
2715 return;
2716
2717 if (argvars[2].v_type != VAR_UNKNOWN)
2718 ic = (int)tv_get_bool_chk(&argvars[2], &error);
2719
2720 if (argvars[0].v_type == VAR_STRING)
2721 {
2722 char_u *expr = tv_get_string_chk(&argvars[1]);
2723 char_u *p = argvars[0].vval.v_string;
2724 char_u *next;
2725
2726 if (!error && expr != NULL && *expr != NUL && p != NULL)
2727 {
2728 if (ic)
2729 {
2730 size_t len = STRLEN(expr);
2731
2732 while (*p != NUL)
2733 {
2734 if (MB_STRNICMP(p, expr, len) == 0)
2735 {
2736 ++n;
2737 p += len;
2738 }
2739 else
2740 MB_PTR_ADV(p);
2741 }
2742 }
2743 else
2744 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2745 != NULL)
2746 {
2747 ++n;
2748 p = next + STRLEN(expr);
2749 }
2750 }
2751
2752 }
2753 else if (argvars[0].v_type == VAR_LIST)
2754 {
2755 listitem_T *li;
2756 list_T *l;
2757 long idx;
2758
2759 if ((l = argvars[0].vval.v_list) != NULL)
2760 {
2761 CHECK_LIST_MATERIALIZE(l);
2762 li = l->lv_first;
2763 if (argvars[2].v_type != VAR_UNKNOWN)
2764 {
2765 if (argvars[3].v_type != VAR_UNKNOWN)
2766 {
2767 idx = (long)tv_get_number_chk(&argvars[3], &error);
2768 if (!error)
2769 {
2770 li = list_find(l, idx);
2771 if (li == NULL)
2772 semsg(_(e_listidx), idx);
2773 }
2774 }
2775 if (error)
2776 li = NULL;
2777 }
2778
2779 for ( ; li != NULL; li = li->li_next)
2780 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2781 ++n;
2782 }
2783 }
2784 else if (argvars[0].v_type == VAR_DICT)
2785 {
2786 int todo;
2787 dict_T *d;
2788 hashitem_T *hi;
2789
2790 if ((d = argvars[0].vval.v_dict) != NULL)
2791 {
2792 if (argvars[2].v_type != VAR_UNKNOWN)
2793 {
2794 if (argvars[3].v_type != VAR_UNKNOWN)
2795 emsg(_(e_invarg));
2796 }
2797
2798 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2799 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2800 {
2801 if (!HASHITEM_EMPTY(hi))
2802 {
2803 --todo;
2804 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2805 ++n;
2806 }
2807 }
2808 }
2809 }
2810 else
2811 semsg(_(e_listdictarg), "count()");
2812 rettv->vval.v_number = n;
2813 }
2814
2815 /*
2816 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
2817 */
2818 static void
extend(typval_T * argvars,typval_T * rettv,char_u * arg_errmsg,int is_new)2819 extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
2820 {
2821 type_T *type = NULL;
2822 garray_T type_list;
2823 char *func_name = is_new ? "extendnew()" : "extend()";
2824
2825 if (!is_new && in_vim9script())
2826 {
2827 // Check that map() does not change the type of the dict.
2828 ga_init2(&type_list, sizeof(type_T *), 10);
2829 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
2830 }
2831
2832 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2833 {
2834 list_T *l1, *l2;
2835 listitem_T *item;
2836 long before;
2837 int error = FALSE;
2838
2839 l1 = argvars[0].vval.v_list;
2840 if (l1 == NULL)
2841 {
2842 emsg(_(e_cannot_extend_null_list));
2843 goto theend;
2844 }
2845 l2 = argvars[1].vval.v_list;
2846 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
2847 && l2 != NULL)
2848 {
2849 if (is_new)
2850 {
2851 l1 = list_copy(l1, FALSE, get_copyID());
2852 if (l1 == NULL)
2853 goto theend;
2854 }
2855
2856 if (argvars[2].v_type != VAR_UNKNOWN)
2857 {
2858 before = (long)tv_get_number_chk(&argvars[2], &error);
2859 if (error)
2860 goto theend; // type error; errmsg already given
2861
2862 if (before == l1->lv_len)
2863 item = NULL;
2864 else
2865 {
2866 item = list_find(l1, before);
2867 if (item == NULL)
2868 {
2869 semsg(_(e_listidx), before);
2870 goto theend;
2871 }
2872 }
2873 }
2874 else
2875 item = NULL;
2876 if (type != NULL && check_typval_arg_type(
2877 type, &argvars[1], func_name, 2) == FAIL)
2878 goto theend;
2879 list_extend(l1, l2, item);
2880
2881 if (is_new)
2882 {
2883 rettv->v_type = VAR_LIST;
2884 rettv->vval.v_list = l1;
2885 rettv->v_lock = FALSE;
2886 }
2887 else
2888 copy_tv(&argvars[0], rettv);
2889 }
2890 }
2891 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2892 {
2893 dict_T *d1, *d2;
2894 char_u *action;
2895 int i;
2896
2897 d1 = argvars[0].vval.v_dict;
2898 if (d1 == NULL)
2899 {
2900 emsg(_(e_cannot_extend_null_dict));
2901 goto theend;
2902 }
2903 d2 = argvars[1].vval.v_dict;
2904 if ((is_new || !value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
2905 && d2 != NULL)
2906 {
2907 if (is_new)
2908 {
2909 d1 = dict_copy(d1, FALSE, get_copyID());
2910 if (d1 == NULL)
2911 goto theend;
2912 }
2913
2914 // Check the third argument.
2915 if (argvars[2].v_type != VAR_UNKNOWN)
2916 {
2917 static char *(av[]) = {"keep", "force", "error"};
2918
2919 action = tv_get_string_chk(&argvars[2]);
2920 if (action == NULL)
2921 goto theend; // type error; errmsg already given
2922 for (i = 0; i < 3; ++i)
2923 if (STRCMP(action, av[i]) == 0)
2924 break;
2925 if (i == 3)
2926 {
2927 semsg(_(e_invarg2), action);
2928 goto theend;
2929 }
2930 }
2931 else
2932 action = (char_u *)"force";
2933
2934 if (type != NULL && check_typval_arg_type(type, &argvars[1],
2935 func_name, 2) == FAIL)
2936 goto theend;
2937 dict_extend(d1, d2, action, func_name);
2938
2939 if (is_new)
2940 {
2941 rettv->v_type = VAR_DICT;
2942 rettv->vval.v_dict = d1;
2943 rettv->v_lock = FALSE;
2944 }
2945 else
2946 copy_tv(&argvars[0], rettv);
2947 }
2948 }
2949 else
2950 semsg(_(e_listdictarg), func_name);
2951
2952 theend:
2953 if (type != NULL)
2954 clear_type_list(&type_list);
2955 }
2956
2957 /*
2958 * "extend(list, list [, idx])" function
2959 * "extend(dict, dict [, action])" function
2960 */
2961 void
f_extend(typval_T * argvars,typval_T * rettv)2962 f_extend(typval_T *argvars, typval_T *rettv)
2963 {
2964 char_u *errmsg = (char_u *)N_("extend() argument");
2965
2966 extend(argvars, rettv, errmsg, FALSE);
2967 }
2968
2969 /*
2970 * "extendnew(list, list [, idx])" function
2971 * "extendnew(dict, dict [, action])" function
2972 */
2973 void
f_extendnew(typval_T * argvars,typval_T * rettv)2974 f_extendnew(typval_T *argvars, typval_T *rettv)
2975 {
2976 char_u *errmsg = (char_u *)N_("extendnew() argument");
2977
2978 extend(argvars, rettv, errmsg, TRUE);
2979 }
2980
2981 /*
2982 * "insert()" function
2983 */
2984 void
f_insert(typval_T * argvars,typval_T * rettv)2985 f_insert(typval_T *argvars, typval_T *rettv)
2986 {
2987 long before = 0;
2988 listitem_T *item;
2989 int error = FALSE;
2990
2991 if (in_vim9script()
2992 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2993 || (argvars[0].v_type == VAR_BLOB
2994 && check_for_number_arg(argvars, 1) == FAIL)
2995 || check_for_opt_number_arg(argvars, 2) == FAIL))
2996 return;
2997
2998 if (argvars[0].v_type == VAR_BLOB)
2999 {
3000 blob_T *b = argvars[0].vval.v_blob;
3001
3002 if (b == NULL)
3003 {
3004 if (in_vim9script())
3005 emsg(_(e_cannot_add_to_null_blob));
3006 }
3007 else if (!value_check_lock(b->bv_lock,
3008 (char_u *)N_("insert() argument"), TRUE))
3009 {
3010 int val, len;
3011 char_u *p;
3012
3013 len = blob_len(b);
3014 if (argvars[2].v_type != VAR_UNKNOWN)
3015 {
3016 before = (long)tv_get_number_chk(&argvars[2], &error);
3017 if (error)
3018 return; // type error; errmsg already given
3019 if (before < 0 || before > len)
3020 {
3021 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
3022 return;
3023 }
3024 }
3025 val = tv_get_number_chk(&argvars[1], &error);
3026 if (error)
3027 return;
3028 if (val < 0 || val > 255)
3029 {
3030 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
3031 return;
3032 }
3033
3034 if (ga_grow(&b->bv_ga, 1) == FAIL)
3035 return;
3036 p = (char_u *)b->bv_ga.ga_data;
3037 mch_memmove(p + before + 1, p + before, (size_t)len - before);
3038 *(p + before) = val;
3039 ++b->bv_ga.ga_len;
3040
3041 copy_tv(&argvars[0], rettv);
3042 }
3043 }
3044 else if (argvars[0].v_type != VAR_LIST)
3045 semsg(_(e_listblobarg), "insert()");
3046 else
3047 {
3048 list_T *l = argvars[0].vval.v_list;
3049
3050 if (l == NULL)
3051 {
3052 if (in_vim9script())
3053 emsg(_(e_cannot_add_to_null_list));
3054 }
3055 else if (!value_check_lock(l->lv_lock,
3056 (char_u *)N_("insert() argument"), TRUE))
3057 {
3058 if (argvars[2].v_type != VAR_UNKNOWN)
3059 before = (long)tv_get_number_chk(&argvars[2], &error);
3060 if (error)
3061 return; // type error; errmsg already given
3062
3063 if (before == l->lv_len)
3064 item = NULL;
3065 else
3066 {
3067 item = list_find(l, before);
3068 if (item == NULL)
3069 {
3070 semsg(_(e_listidx), before);
3071 l = NULL;
3072 }
3073 }
3074 if (l != NULL)
3075 {
3076 (void)list_insert_tv(l, &argvars[1], item);
3077 copy_tv(&argvars[0], rettv);
3078 }
3079 }
3080 }
3081 }
3082
3083 /*
3084 * "remove()" function
3085 */
3086 void
f_remove(typval_T * argvars,typval_T * rettv)3087 f_remove(typval_T *argvars, typval_T *rettv)
3088 {
3089 char_u *arg_errmsg = (char_u *)N_("remove() argument");
3090
3091 if (in_vim9script()
3092 && (check_for_list_or_dict_or_blob_arg(argvars, 0) == FAIL
3093 || ((argvars[0].v_type == VAR_LIST
3094 || argvars[0].v_type == VAR_BLOB)
3095 && (check_for_number_arg(argvars, 1) == FAIL
3096 || check_for_opt_number_arg(argvars, 2) == FAIL))
3097 || (argvars[0].v_type == VAR_DICT
3098 && check_for_string_or_number_arg(argvars, 1) == FAIL)))
3099 return;
3100
3101 if (argvars[0].v_type == VAR_DICT)
3102 dict_remove(argvars, rettv, arg_errmsg);
3103 else if (argvars[0].v_type == VAR_BLOB)
3104 blob_remove(argvars, rettv, arg_errmsg);
3105 else if (argvars[0].v_type == VAR_LIST)
3106 list_remove(argvars, rettv, arg_errmsg);
3107 else
3108 semsg(_(e_listdictblobarg), "remove()");
3109 }
3110
3111 /*
3112 * "reverse({list})" function
3113 */
3114 void
f_reverse(typval_T * argvars,typval_T * rettv)3115 f_reverse(typval_T *argvars, typval_T *rettv)
3116 {
3117 list_T *l;
3118 listitem_T *li, *ni;
3119
3120 if (in_vim9script() && check_for_list_or_blob_arg(argvars, 0) == FAIL)
3121 return;
3122
3123 if (argvars[0].v_type == VAR_BLOB)
3124 {
3125 blob_T *b = argvars[0].vval.v_blob;
3126 int i, len = blob_len(b);
3127
3128 for (i = 0; i < len / 2; i++)
3129 {
3130 int tmp = blob_get(b, i);
3131
3132 blob_set(b, i, blob_get(b, len - i - 1));
3133 blob_set(b, len - i - 1, tmp);
3134 }
3135 rettv_blob_set(rettv, b);
3136 return;
3137 }
3138
3139 if (argvars[0].v_type != VAR_LIST)
3140 semsg(_(e_listblobarg), "reverse()");
3141 else
3142 {
3143 l = argvars[0].vval.v_list;
3144 rettv_list_set(rettv, l);
3145 if (l != NULL
3146 && !value_check_lock(l->lv_lock,
3147 (char_u *)N_("reverse() argument"), TRUE))
3148 {
3149 if (l->lv_first == &range_list_item)
3150 {
3151 varnumber_T new_start = l->lv_u.nonmat.lv_start
3152 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
3153 l->lv_u.nonmat.lv_end = new_start
3154 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
3155 l->lv_u.nonmat.lv_start = new_start;
3156 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
3157 return;
3158 }
3159 li = l->lv_u.mat.lv_last;
3160 l->lv_first = l->lv_u.mat.lv_last = NULL;
3161 l->lv_len = 0;
3162 while (li != NULL)
3163 {
3164 ni = li->li_prev;
3165 list_append(l, li);
3166 li = ni;
3167 }
3168 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
3169 }
3170 }
3171 }
3172
3173 /*
3174 * "reduce(list, { accumulator, element -> value } [, initial])" function
3175 */
3176 void
f_reduce(typval_T * argvars,typval_T * rettv)3177 f_reduce(typval_T *argvars, typval_T *rettv)
3178 {
3179 typval_T initial;
3180 char_u *func_name;
3181 partial_T *partial = NULL;
3182 funcexe_T funcexe;
3183 typval_T argv[3];
3184
3185 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
3186 {
3187 emsg(_(e_listblobreq));
3188 return;
3189 }
3190
3191 if (argvars[1].v_type == VAR_FUNC)
3192 func_name = argvars[1].vval.v_string;
3193 else if (argvars[1].v_type == VAR_PARTIAL)
3194 {
3195 partial = argvars[1].vval.v_partial;
3196 func_name = partial_name(partial);
3197 }
3198 else
3199 func_name = tv_get_string(&argvars[1]);
3200 if (func_name == NULL || *func_name == NUL)
3201 {
3202 emsg(_(e_missing_function_argument));
3203 return;
3204 }
3205
3206 vim_memset(&funcexe, 0, sizeof(funcexe));
3207 funcexe.evaluate = TRUE;
3208 funcexe.partial = partial;
3209
3210 if (argvars[0].v_type == VAR_LIST)
3211 {
3212 list_T *l = argvars[0].vval.v_list;
3213 listitem_T *li = NULL;
3214 int r;
3215 int called_emsg_start = called_emsg;
3216
3217 if (l != NULL)
3218 CHECK_LIST_MATERIALIZE(l);
3219 if (argvars[2].v_type == VAR_UNKNOWN)
3220 {
3221 if (l == NULL || l->lv_first == NULL)
3222 {
3223 semsg(_(e_reduceempty), "List");
3224 return;
3225 }
3226 initial = l->lv_first->li_tv;
3227 li = l->lv_first->li_next;
3228 }
3229 else
3230 {
3231 initial = argvars[2];
3232 if (l != NULL)
3233 li = l->lv_first;
3234 }
3235 copy_tv(&initial, rettv);
3236
3237 if (l != NULL)
3238 {
3239 int prev_locked = l->lv_lock;
3240
3241 l->lv_lock = VAR_FIXED; // disallow the list changing here
3242 for ( ; li != NULL; li = li->li_next)
3243 {
3244 argv[0] = *rettv;
3245 argv[1] = li->li_tv;
3246 rettv->v_type = VAR_UNKNOWN;
3247 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
3248 clear_tv(&argv[0]);
3249 if (r == FAIL || called_emsg != called_emsg_start)
3250 break;
3251 }
3252 l->lv_lock = prev_locked;
3253 }
3254 }
3255 else
3256 {
3257 blob_T *b = argvars[0].vval.v_blob;
3258 int i;
3259
3260 if (argvars[2].v_type == VAR_UNKNOWN)
3261 {
3262 if (b == NULL || b->bv_ga.ga_len == 0)
3263 {
3264 semsg(_(e_reduceempty), "Blob");
3265 return;
3266 }
3267 initial.v_type = VAR_NUMBER;
3268 initial.vval.v_number = blob_get(b, 0);
3269 i = 1;
3270 }
3271 else if (argvars[2].v_type != VAR_NUMBER)
3272 {
3273 emsg(_(e_number_expected));
3274 return;
3275 }
3276 else
3277 {
3278 initial = argvars[2];
3279 i = 0;
3280 }
3281
3282 copy_tv(&initial, rettv);
3283 if (b != NULL)
3284 {
3285 for ( ; i < b->bv_ga.ga_len; i++)
3286 {
3287 argv[0] = *rettv;
3288 argv[1].v_type = VAR_NUMBER;
3289 argv[1].vval.v_number = blob_get(b, i);
3290 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
3291 return;
3292 }
3293 }
3294 }
3295 }
3296
3297 #endif // defined(FEAT_EVAL)
3298