blob: 336bf3ba76d5416b11f115619f71503cd3b382bd [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarda861d62016-07-17 15:46:27 +02002 *
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/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +020011 * list.c: List support and container (List, Dict, Blob) functions.
Bram Moolenaarda861d62016-07-17 15:46:27 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar08c308a2019-09-04 17:48:15 +020018static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
19
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010020// List heads for garbage collection.
21static list_T *first_list = NULL; // list of all lists
Bram Moolenaarda861d62016-07-17 15:46:27 +020022
Bram Moolenaar00d253e2020-04-06 22:13:01 +020023#define FOR_ALL_WATCHERS(l, lw) \
24 for ((lw) = (l)->lv_watch; (lw) != NULL; (lw) = (lw)->lw_next)
25
Bram Moolenaarbdff0122020-04-05 18:56:05 +020026static void list_free_item(list_T *l, listitem_T *item);
27
Bram Moolenaarda861d62016-07-17 15:46:27 +020028/*
29 * Add a watcher to a list.
30 */
31 void
32list_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
43list_rem_watch(list_T *l, listwatch_T *lwrem)
44{
45 listwatch_T *lw, **lwp;
46
47 lwp = &l->lv_watch;
Bram Moolenaar00d253e2020-04-06 22:13:01 +020048 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020049 {
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 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020063 static void
Bram Moolenaarda861d62016-07-17 15:46:27 +020064list_fix_watch(list_T *l, listitem_T *item)
65{
66 listwatch_T *lw;
67
Bram Moolenaar00d253e2020-04-06 22:13:01 +020068 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020069 if (lw->lw_item == item)
70 lw->lw_item = item->li_next;
71}
72
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073 static void
74list_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
Bram Moolenaarda861d62016-07-17 15:46:27 +020084/*
85 * Allocate an empty header for a list.
86 * Caller should take care of the reference count.
87 */
88 list_T *
89list_alloc(void)
90{
91 list_T *l;
92
Bram Moolenaarc799fe22019-05-28 23:08:19 +020093 l = ALLOC_CLEAR_ONE(list_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +020094 if (l != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095 list_init(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +020096 return l;
97}
98
99/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100100 * list_alloc() with an ID for alloc_fail().
101 */
102 list_T *
103list_alloc_id(alloc_id_T id UNUSED)
104{
105#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200106 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100107 return NULL;
108#endif
109 return (list_alloc());
110}
111
112/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 * Allocate space for a list, plus "count" items.
114 * Next list_set_item() must be called for each item.
115 */
116 list_T *
117list_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;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100134 l->lv_u.mat.lv_last = li + count - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 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
158list_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/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200166 * Allocate an empty list for a return value, with reference count set.
167 * Returns OK or FAIL.
168 */
169 int
170rettv_list_alloc(typval_T *rettv)
171{
172 list_T *l = list_alloc();
173
174 if (l == NULL)
175 return FAIL;
176
Bram Moolenaarda861d62016-07-17 15:46:27 +0200177 rettv->v_lock = 0;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200178 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200179 return OK;
180}
181
182/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100183 * Same as rettv_list_alloc() but uses an allocation id for testing.
184 */
185 int
186rettv_list_alloc_id(typval_T *rettv, alloc_id_T id UNUSED)
187{
188#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200189 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaar162b7142018-12-21 15:17:36 +0100190 return FAIL;
191#endif
192 return rettv_list_alloc(rettv);
193}
194
195
196/*
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200197 * Set a list as the return value. Increments the reference count.
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200198 */
199 void
200rettv_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/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200209 * Unreference a list: decrement the reference count and free it when it
210 * becomes zero.
211 */
212 void
213list_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
224list_free_contents(list_T *l)
225{
226 listitem_T *item;
227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 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 }
Bram Moolenaarda861d62016-07-17 15:46:27 +0200236}
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
244list_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 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100253 // 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.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200256 list_free_contents(ll);
257 did_free = TRUE;
258 }
259 return did_free;
260}
261
262 static void
263list_free_list(list_T *l)
264{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100265 // Remove the list from the list of lists for garbage collection.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200266 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
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100273 free_type(l->lv_type);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200274 vim_free(l);
275}
276
277 void
278list_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 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100288 // 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.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200291 list_free_list(ll);
292 }
293 }
294}
295
296 void
297list_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 *
311listitem_alloc(void)
312{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200313 return ALLOC_ONE(listitem_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200314}
315
316/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100317 * Free a list item, unless it was allocated together with the list itself.
318 * Does not clear the value. Does not notify watchers.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200319 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200320 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100321list_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
333listitem_free(list_T *l, listitem_T *item)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200334{
335 clear_tv(&item->li_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 list_free_item(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200337}
338
339/*
340 * Remove a list item from a List and free it. Also clears the value.
341 */
342 void
343listitem_remove(list_T *l, listitem_T *item)
344{
345 vimlist_remove(l, item, item);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 listitem_free(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200347}
348
349/*
350 * Get the number of items in a list.
351 */
352 long
353list_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
364list_equal(
365 list_T *l1,
366 list_T *l2,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100367 int ic, // ignore case for strings
368 int recursive) // TRUE when used recursively
Bram Moolenaarda861d62016-07-17 15:46:27 +0200369{
370 listitem_T *item1, *item2;
371
Bram Moolenaarda861d62016-07-17 15:46:27 +0200372 if (l1 == l2)
373 return TRUE;
374 if (list_len(l1) != list_len(l2))
375 return FALSE;
Bram Moolenaar7b293c72020-04-09 21:33:22 +0200376 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;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200381
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200382 CHECK_LIST_MATERIALIZE(l1);
383 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384
Bram Moolenaarda861d62016-07-17 15:46:27 +0200385 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 *
399list_find(list_T *l, long n)
400{
401 listitem_T *item;
402 long idx;
403
404 if (l == NULL)
405 return NULL;
406
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100407 // Negative index is relative to the end.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200408 if (n < 0)
409 n = l->lv_len + n;
410
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100411 // Check for index out of range.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200412 if (n < 0 || n >= l->lv_len)
413 return NULL;
414
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200415 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100417 // When there is a cached index may start search from there.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100418 if (l->lv_u.mat.lv_idx_item != NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200419 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100420 if (n < l->lv_u.mat.lv_idx / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200421 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100422 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200423 item = l->lv_first;
424 idx = 0;
425 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100426 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200427 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100428 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100429 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200430 idx = l->lv_len - 1;
431 }
432 else
433 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100434 // closest to the cached index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100435 item = l->lv_u.mat.lv_idx_item;
436 idx = l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200437 }
438 }
439 else
440 {
441 if (n < l->lv_len / 2)
442 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100443 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200444 item = l->lv_first;
445 idx = 0;
446 }
447 else
448 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100449 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100450 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200451 idx = l->lv_len - 1;
452 }
453 }
454
455 while (n > idx)
456 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100457 // search forward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200458 item = item->li_next;
459 ++idx;
460 }
461 while (n < idx)
462 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100463 // search backward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200464 item = item->li_prev;
465 --idx;
466 }
467
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100468 // cache the used index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100469 l->lv_u.mat.lv_idx = idx;
470 l->lv_u.mat.lv_idx_item = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200471
472 return item;
473}
474
475/*
476 * Get list item "l[idx]" as a number.
477 */
478 long
479list_find_nr(
480 list_T *l,
481 long idx,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100482 int *errorp) // set to TRUE when something wrong
Bram Moolenaarda861d62016-07-17 15:46:27 +0200483{
484 listitem_T *li;
485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486 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
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100503 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 }
505
Bram Moolenaarda861d62016-07-17 15:46:27 +0200506 li = list_find(l, idx);
507 if (li == NULL)
508 {
509 if (errorp != NULL)
510 *errorp = TRUE;
511 return -1L;
512 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100513 return (long)tv_get_number_chk(&li->li_tv, errorp);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200514}
515
516/*
517 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
518 */
519 char_u *
520list_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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 semsg(_(e_listidx), idx);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200528 return NULL;
529 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100530 return tv_get_string(&li->li_tv);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200531}
532
533/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100534 * 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 *
538list_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/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200554 * Locate "item" list "l" and return its index.
555 * Returns -1 when "item" is not in the list.
556 */
557 long
558list_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;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200565 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200566 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
578list_append(list_T *l, listitem_T *item)
579{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200580 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100581 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200582 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100583 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200584 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100585 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200586 item->li_prev = NULL;
587 }
588 else
589 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100590 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;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200593 }
594 ++l->lv_len;
595 item->li_next = NULL;
596}
597
598/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200600 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200601 */
602 int
603list_append_tv(list_T *l, typval_T *tv)
604{
Bram Moolenaar90fba562021-07-09 19:17:55 +0200605 listitem_T *li;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200606
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200607 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200608 && check_typval_arg_type(l->lv_type->tt_member, tv,
609 NULL, 0) == FAIL)
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200610 return FAIL;
Bram Moolenaar90fba562021-07-09 19:17:55 +0200611 li = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200612 if (li == NULL)
613 return FAIL;
614 copy_tv(tv, &li->li_tv);
615 list_append(l, li);
616 return OK;
617}
618
619/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620 * As list_append_tv() but move the value instead of copying it.
621 * Return FAIL when out of memory.
622 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200623 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624list_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/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200636 * Add a dictionary to a list. Used by getqflist().
637 * Return FAIL when out of memory.
638 */
639 int
640list_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/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100655 * Append list2 to list1.
656 * Return FAIL when out of memory.
657 */
658 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200659list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100660{
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/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200674 * 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
679list_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
701list_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.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100718 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200719 */
720 int
721list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
722{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100723 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200724
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100725 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200726 && check_typval_arg_type(l->lv_type->tt_member, tv,
727 NULL, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100728 return FAIL;
729 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200730 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
738list_insert(list_T *l, listitem_T *ni, listitem_T *item)
739{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200740 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200741 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100742 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200743 list_append(l, ni);
744 else
745 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100746 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200747 ni->li_prev = item->li_prev;
748 ni->li_next = item;
749 if (item->li_prev == NULL)
750 {
751 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100752 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200753 }
754 else
755 {
756 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100757 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200758 }
759 item->li_prev = ni;
760 ++l->lv_len;
761 }
762}
763
764/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200765 * 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 *
770check_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
799check_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
841list_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;
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200855 type_T *member_type = NULL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200856
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
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200873 if (in_vim9script() && dest->lv_type != NULL
874 && dest->lv_type->tt_member != NULL)
875 member_type = dest->lv_type->tt_member;
876
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200877 /*
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 {
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200888 if (member_type != NULL
889 && check_typval_arg_type(member_type, &src_li->li_tv,
890 NULL, 0) == FAIL)
891 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200892 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/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200926 * Flatten "list" to depth "maxdepth".
927 * It does nothing if "maxdepth" is 0.
928 * Returns FAIL when out of memory.
929 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100930 static void
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200931list_flatten(list_T *list, long maxdepth)
932{
933 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200934 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200935 int n;
936
937 if (maxdepth == 0)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100938 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200939 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)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100947 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200948
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)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100955 return;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200956 clear_tv(&item->li_tv);
957 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200958
959 if (item->li_prev == NULL)
960 item = list->lv_first;
961 else
962 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200963 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200964
965 if (++n >= maxdepth)
966 {
967 n = 0;
968 item = next;
969 }
970 }
971 else
972 {
973 n = 0;
974 item = item->li_next;
975 }
976 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200977}
978
979/*
Bram Moolenaar3b690062021-02-01 20:14:51 +0100980 * "flatten()" and "flattennew()" functions
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200981 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100982 static void
983flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200984{
985 list_T *l;
986 long maxdepth;
987 int error = FALSE;
988
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200989 if (in_vim9script()
990 && (check_for_list_arg(argvars, 0) == FAIL
991 || check_for_opt_number_arg(argvars, 1) == FAIL))
992 return;
993
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200994 if (argvars[0].v_type != VAR_LIST)
995 {
996 semsg(_(e_listarg), "flatten()");
997 return;
998 }
999
1000 if (argvars[1].v_type == VAR_UNKNOWN)
1001 maxdepth = 999999;
1002 else
1003 {
1004 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
1005 if (error)
1006 return;
1007 if (maxdepth < 0)
1008 {
1009 emsg(_("E900: maxdepth must be non-negative number"));
1010 return;
1011 }
1012 }
1013
1014 l = argvars[0].vval.v_list;
Bram Moolenaar3b690062021-02-01 20:14:51 +01001015 rettv->v_type = VAR_LIST;
1016 rettv->vval.v_list = l;
1017 if (l == NULL)
1018 return;
1019
1020 if (make_copy)
1021 {
1022 l = list_copy(l, TRUE, get_copyID());
1023 rettv->vval.v_list = l;
1024 if (l == NULL)
1025 return;
1026 }
1027 else
1028 {
1029 if (value_check_lock(l->lv_lock,
1030 (char_u *)N_("flatten() argument"), TRUE))
1031 return;
1032 ++l->lv_refcount;
1033 }
1034
1035 list_flatten(l, maxdepth);
1036}
1037
1038/*
1039 * "flatten(list[, {maxdepth}])" function
1040 */
1041 void
1042f_flatten(typval_T *argvars, typval_T *rettv)
1043{
1044 if (in_vim9script())
1045 emsg(_(e_cannot_use_flatten_in_vim9_script));
1046 else
1047 flatten_common(argvars, rettv, FALSE);
1048}
1049
1050/*
1051 * "flattennew(list[, {maxdepth}])" function
1052 */
1053 void
1054f_flattennew(typval_T *argvars, typval_T *rettv)
1055{
1056 flatten_common(argvars, rettv, TRUE);
Bram Moolenaar077a1e62020-06-08 20:50:43 +02001057}
1058
1059/*
Bram Moolenaar1a739232020-10-10 15:37:58 +02001060 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001061 * If "bef" is NULL append at the end, otherwise insert before this item.
1062 * Returns FAIL when out of memory.
1063 */
1064 int
1065list_extend(list_T *l1, list_T *l2, listitem_T *bef)
1066{
1067 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001068 int todo;
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001069 listitem_T *bef_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001070
Bram Moolenaar1a739232020-10-10 15:37:58 +02001071 // NULL list is equivalent to an empty list: nothing to do.
1072 if (l2 == NULL || l2->lv_len == 0)
1073 return OK;
1074
1075 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001076 CHECK_LIST_MATERIALIZE(l1);
1077 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001079 // When exending a list with itself, at some point we run into the item
1080 // that was before "bef" and need to skip over the already inserted items
1081 // to "bef".
1082 bef_prev = bef == NULL ? NULL : bef->li_prev;
1083
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001084 // We also quit the loop when we have inserted the original item count of
1085 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001086 for (item = l2->lv_first; item != NULL && --todo >= 0;
1087 item = item == bef_prev ? bef : item->li_next)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001088 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
1089 return FAIL;
1090 return OK;
1091}
1092
1093/*
1094 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
1095 * Return FAIL when out of memory.
1096 */
1097 int
1098list_concat(list_T *l1, list_T *l2, typval_T *tv)
1099{
1100 list_T *l;
1101
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001102 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +02001103 if (l1 == NULL)
1104 l = list_alloc();
1105 else
1106 l = list_copy(l1, FALSE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001107 if (l == NULL)
1108 return FAIL;
1109 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +01001110 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001111 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001112 if (l1 == NULL)
1113 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001114
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001115 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +02001116 return list_extend(l, l2, NULL);
1117}
1118
Bram Moolenaar9af78762020-06-16 11:34:42 +02001119 list_T *
1120list_slice(list_T *ol, long n1, long n2)
1121{
1122 listitem_T *item;
1123 list_T *l = list_alloc();
1124
1125 if (l == NULL)
1126 return NULL;
1127 for (item = list_find(ol, n1); n1 <= n2; ++n1)
1128 {
1129 if (list_append_tv(l, &item->li_tv) == FAIL)
1130 {
1131 list_free(l);
1132 return NULL;
1133 }
1134 item = item->li_next;
1135 }
1136 return l;
1137}
1138
Bram Moolenaared591872020-08-15 22:14:53 +02001139 int
1140list_slice_or_index(
1141 list_T *list,
1142 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01001143 varnumber_T n1_arg,
1144 varnumber_T n2_arg,
1145 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +02001146 typval_T *rettv,
1147 int verbose)
1148{
1149 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001150 varnumber_T n1 = n1_arg;
1151 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +02001152 typval_T var1;
1153
1154 if (n1 < 0)
1155 n1 = len + n1;
1156 if (n1 < 0 || n1 >= len)
1157 {
Bram Moolenaar92f05f22021-08-12 21:12:56 +02001158 // For a range we allow invalid values and for legacy script return an
1159 // empty list, for Vim9 script start at the first item.
1160 // A list index out of range is an error.
Bram Moolenaared591872020-08-15 22:14:53 +02001161 if (!range)
1162 {
1163 if (verbose)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001164 semsg(_(e_listidx), (long)n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +02001165 return FAIL;
1166 }
Bram Moolenaar92f05f22021-08-12 21:12:56 +02001167 if (in_vim9script())
1168 n1 = n1 < 0 ? 0 : len;
1169 else
1170 n1 = len;
Bram Moolenaared591872020-08-15 22:14:53 +02001171 }
1172 if (range)
1173 {
1174 list_T *l;
1175
1176 if (n2 < 0)
1177 n2 = len + n2;
1178 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01001179 n2 = len - (exclusive ? 0 : 1);
1180 if (exclusive)
1181 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +02001182 if (n2 < 0 || n2 + 1 < n1)
1183 n2 = -1;
1184 l = list_slice(list, n1, n2);
1185 if (l == NULL)
1186 return FAIL;
1187 clear_tv(rettv);
1188 rettv_list_set(rettv, l);
1189 }
1190 else
1191 {
1192 // copy the item to "var1" to avoid that freeing the list makes it
1193 // invalid.
1194 copy_tv(&list_find(list, n1)->li_tv, &var1);
1195 clear_tv(rettv);
1196 *rettv = var1;
1197 }
1198 return OK;
1199}
1200
Bram Moolenaarda861d62016-07-17 15:46:27 +02001201/*
1202 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1203 * The refcount of the new list is set to 1.
1204 * See item_copy() for "copyID".
1205 * Returns NULL when out of memory.
1206 */
1207 list_T *
1208list_copy(list_T *orig, int deep, int copyID)
1209{
1210 list_T *copy;
1211 listitem_T *item;
1212 listitem_T *ni;
1213
1214 if (orig == NULL)
1215 return NULL;
1216
1217 copy = list_alloc();
1218 if (copy != NULL)
1219 {
Bram Moolenaarefc084e2021-09-09 22:30:52 +02001220 copy->lv_type = orig->lv_type;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001221 if (copyID != 0)
1222 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001223 // Do this before adding the items, because one of the items may
1224 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001225 orig->lv_copyID = copyID;
1226 orig->lv_copylist = copy;
1227 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001228 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001229 for (item = orig->lv_first; item != NULL && !got_int;
1230 item = item->li_next)
1231 {
1232 ni = listitem_alloc();
1233 if (ni == NULL)
1234 break;
1235 if (deep)
1236 {
1237 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
1238 {
1239 vim_free(ni);
1240 break;
1241 }
1242 }
1243 else
1244 copy_tv(&item->li_tv, &ni->li_tv);
1245 list_append(copy, ni);
1246 }
1247 ++copy->lv_refcount;
1248 if (item != NULL)
1249 {
1250 list_unref(copy);
1251 copy = NULL;
1252 }
1253 }
1254
1255 return copy;
1256}
1257
1258/*
1259 * Remove items "item" to "item2" from list "l".
1260 * Does not free the listitem or the value!
1261 * This used to be called list_remove, but that conflicts with a Sun header
1262 * file.
1263 */
1264 void
1265vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1266{
1267 listitem_T *ip;
1268
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001269 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001271 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001272 for (ip = item; ip != NULL; ip = ip->li_next)
1273 {
1274 --l->lv_len;
1275 list_fix_watch(l, ip);
1276 if (ip == item2)
1277 break;
1278 }
1279
1280 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001281 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001282 else
1283 item2->li_next->li_prev = item->li_prev;
1284 if (item->li_prev == NULL)
1285 l->lv_first = item2->li_next;
1286 else
1287 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001288 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001289}
1290
1291/*
1292 * Return an allocated string with the string representation of a list.
1293 * May return NULL.
1294 */
1295 char_u *
1296list2string(typval_T *tv, int copyID, int restore_copyID)
1297{
1298 garray_T ga;
1299
1300 if (tv->vval.v_list == NULL)
1301 return NULL;
1302 ga_init2(&ga, (int)sizeof(char), 80);
1303 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001304 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001305 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1306 FALSE, restore_copyID, copyID) == FAIL)
1307 {
1308 vim_free(ga.ga_data);
1309 return NULL;
1310 }
1311 ga_append(&ga, ']');
1312 ga_append(&ga, NUL);
1313 return (char_u *)ga.ga_data;
1314}
1315
1316typedef struct join_S {
1317 char_u *s;
1318 char_u *tofree;
1319} join_T;
1320
1321 static int
1322list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001323 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001324 list_T *l,
1325 char_u *sep,
1326 int echo_style,
1327 int restore_copyID,
1328 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001329 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001330{
1331 int i;
1332 join_T *p;
1333 int len;
1334 int sumlen = 0;
1335 int first = TRUE;
1336 char_u *tofree;
1337 char_u numbuf[NUMBUFLEN];
1338 listitem_T *item;
1339 char_u *s;
1340
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001341 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001342 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001343 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1344 {
1345 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001346 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001347 if (s == NULL)
1348 return FAIL;
1349
1350 len = (int)STRLEN(s);
1351 sumlen += len;
1352
1353 (void)ga_grow(join_gap, 1);
1354 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1355 if (tofree != NULL || s != numbuf)
1356 {
1357 p->s = s;
1358 p->tofree = tofree;
1359 }
1360 else
1361 {
1362 p->s = vim_strnsave(s, len);
1363 p->tofree = p->s;
1364 }
1365
1366 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001367 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001368 break;
1369 }
1370
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001371 // Allocate result buffer with its total size, avoid re-allocation and
1372 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001373 if (join_gap->ga_len >= 2)
1374 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1375 if (ga_grow(gap, sumlen + 2) == FAIL)
1376 return FAIL;
1377
1378 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1379 {
1380 if (first)
1381 first = FALSE;
1382 else
1383 ga_concat(gap, sep);
1384 p = ((join_T *)join_gap->ga_data) + i;
1385
1386 if (p->s != NULL)
1387 ga_concat(gap, p->s);
1388 line_breakcheck();
1389 }
1390
1391 return OK;
1392}
1393
1394/*
1395 * Join list "l" into a string in "*gap", using separator "sep".
1396 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1397 * Return FAIL or OK.
1398 */
1399 int
1400list_join(
1401 garray_T *gap,
1402 list_T *l,
1403 char_u *sep,
1404 int echo_style,
1405 int restore_copyID,
1406 int copyID)
1407{
1408 garray_T join_ga;
1409 int retval;
1410 join_T *p;
1411 int i;
1412
1413 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001414 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001415 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1416 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1417 copyID, &join_ga);
1418
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001419 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001420 if (join_ga.ga_data != NULL)
1421 {
1422 p = (join_T *)join_ga.ga_data;
1423 for (i = 0; i < join_ga.ga_len; ++i)
1424 {
1425 vim_free(p->tofree);
1426 ++p;
1427 }
1428 ga_clear(&join_ga);
1429 }
1430
1431 return retval;
1432}
1433
1434/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001435 * "join()" function
1436 */
1437 void
1438f_join(typval_T *argvars, typval_T *rettv)
1439{
1440 garray_T ga;
1441 char_u *sep;
1442
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001443 if (in_vim9script()
1444 && (check_for_list_arg(argvars, 0) == FAIL
1445 || check_for_opt_string_arg(argvars, 1) == FAIL))
1446 return;
1447
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001448 if (argvars[0].v_type != VAR_LIST)
1449 {
1450 emsg(_(e_listreq));
1451 return;
1452 }
Bram Moolenaaref982572021-08-12 19:27:57 +02001453 rettv->v_type = VAR_STRING;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001454 if (argvars[0].vval.v_list == NULL)
1455 return;
Bram Moolenaaref982572021-08-12 19:27:57 +02001456
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001457 if (argvars[1].v_type == VAR_UNKNOWN)
1458 sep = (char_u *)" ";
1459 else
1460 sep = tv_get_string_chk(&argvars[1]);
1461
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001462 if (sep != NULL)
1463 {
1464 ga_init2(&ga, (int)sizeof(char), 80);
1465 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1466 ga_append(&ga, NUL);
1467 rettv->vval.v_string = (char_u *)ga.ga_data;
1468 }
1469 else
1470 rettv->vval.v_string = NULL;
1471}
1472
1473/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001474 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001475 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001476 * Return OK or FAIL.
1477 */
1478 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001479eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001480{
Bram Moolenaar71478202020-06-26 22:46:27 +02001481 int evaluate = evalarg == NULL ? FALSE
1482 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001483 list_T *l = NULL;
1484 typval_T tv;
1485 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001486 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001487 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001488
1489 if (evaluate)
1490 {
1491 l = list_alloc();
1492 if (l == NULL)
1493 return FAIL;
1494 }
1495
Bram Moolenaar962d7212020-07-04 14:15:00 +02001496 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001497 while (**arg != ']' && **arg != NUL)
1498 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001499 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001500 goto failret;
1501 if (evaluate)
1502 {
1503 item = listitem_alloc();
1504 if (item != NULL)
1505 {
1506 item->li_tv = tv;
1507 item->li_tv.v_lock = 0;
1508 list_append(l, item);
1509 }
1510 else
1511 clear_tv(&tv);
1512 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001513 // Legacy Vim script allowed a space before the comma.
1514 if (!vim9script)
1515 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001516
Bram Moolenaare6e03172020-06-27 16:36:05 +02001517 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001518 had_comma = **arg == ',';
1519 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001520 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001521 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001522 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001523 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001524 goto failret;
1525 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001526 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001527 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001528
Bram Moolenaare6b53242020-07-01 17:28:33 +02001529 // The "]" can be on the next line. But a double quoted string may
1530 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001531 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001532 if (**arg == ']')
1533 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001534
1535 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001536 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001538 {
1539 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001540 semsg(_(e_no_white_space_allowed_before_str_str),
1541 ",", *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001542 else
1543 semsg(_("E696: Missing comma in List: %s"), *arg);
1544 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001545 goto failret;
1546 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001547 }
1548
1549 if (**arg != ']')
1550 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001552 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001553failret:
1554 if (evaluate)
1555 list_free(l);
1556 return FAIL;
1557 }
1558
Bram Moolenaar9d489562020-07-30 20:08:50 +02001559 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001560 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001561 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001562
1563 return OK;
1564}
1565
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001566/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001567 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001568 */
1569 int
1570write_list(FILE *fd, list_T *list, int binary)
1571{
1572 listitem_T *li;
1573 int c;
1574 int ret = OK;
1575 char_u *s;
1576
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001577 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001578 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001579 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001580 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581 {
1582 if (*s == '\n')
1583 c = putc(NUL, fd);
1584 else
1585 c = putc(*s, fd);
1586 if (c == EOF)
1587 {
1588 ret = FAIL;
1589 break;
1590 }
1591 }
1592 if (!binary || li->li_next != NULL)
1593 if (putc('\n', fd) == EOF)
1594 {
1595 ret = FAIL;
1596 break;
1597 }
1598 if (ret == FAIL)
1599 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001600 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001601 break;
1602 }
1603 }
1604 return ret;
1605}
1606
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001607/*
1608 * Initialize a static list with 10 items.
1609 */
1610 void
1611init_static_list(staticList10_T *sl)
1612{
1613 list_T *l = &sl->sl_list;
1614 int i;
1615
1616 memset(sl, 0, sizeof(staticList10_T));
1617 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001618 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001619 l->lv_refcount = DO_NOT_FREE_CNT;
1620 l->lv_lock = VAR_FIXED;
1621 sl->sl_list.lv_len = 10;
1622
1623 for (i = 0; i < 10; ++i)
1624 {
1625 listitem_T *li = &sl->sl_items[i];
1626
1627 if (i == 0)
1628 li->li_prev = NULL;
1629 else
1630 li->li_prev = li - 1;
1631 if (i == 9)
1632 li->li_next = NULL;
1633 else
1634 li->li_next = li + 1;
1635 }
1636}
1637
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001638/*
1639 * "list2str()" function
1640 */
1641 void
1642f_list2str(typval_T *argvars, typval_T *rettv)
1643{
1644 list_T *l;
1645 listitem_T *li;
1646 garray_T ga;
1647 int utf8 = FALSE;
1648
1649 rettv->v_type = VAR_STRING;
1650 rettv->vval.v_string = NULL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001651
1652 if (in_vim9script()
1653 && (check_for_list_arg(argvars, 0) == FAIL
1654 || check_for_opt_bool_arg(argvars, 1) == FAIL))
1655 return;
1656
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001657 if (argvars[0].v_type != VAR_LIST)
1658 {
1659 emsg(_(e_invarg));
1660 return;
1661 }
1662
1663 l = argvars[0].vval.v_list;
1664 if (l == NULL)
1665 return; // empty list results in empty string
1666
1667 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001668 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001669
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001670 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001671 ga_init2(&ga, 1, 80);
1672 if (has_mbyte || utf8)
1673 {
1674 char_u buf[MB_MAXBYTES + 1];
1675 int (*char2bytes)(int, char_u *);
1676
1677 if (utf8 || enc_utf8)
1678 char2bytes = utf_char2bytes;
1679 else
1680 char2bytes = mb_char2bytes;
1681
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001682 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001683 {
1684 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1685 ga_concat(&ga, buf);
1686 }
1687 ga_append(&ga, NUL);
1688 }
1689 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1690 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001691 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001692 ga_append(&ga, tv_get_number(&li->li_tv));
1693 ga_append(&ga, NUL);
1694 }
1695
1696 rettv->v_type = VAR_STRING;
1697 rettv->vval.v_string = ga.ga_data;
1698}
1699
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001700 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001701list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1702{
1703 list_T *l;
1704 listitem_T *item, *item2;
1705 listitem_T *li;
1706 int error = FALSE;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001707 long idx;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001708
1709 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001710 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001711 return;
1712
1713 idx = (long)tv_get_number_chk(&argvars[1], &error);
1714 if (error)
1715 ; // type error: do nothing, errmsg already given
1716 else if ((item = list_find(l, idx)) == NULL)
1717 semsg(_(e_listidx), idx);
1718 else
1719 {
1720 if (argvars[2].v_type == VAR_UNKNOWN)
1721 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001722 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001723 vimlist_remove(l, item, item);
1724 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001726 }
1727 else
1728 {
1729 // Remove range of items, return list with values.
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001730 long end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001731
1732 if (error)
1733 ; // type error: do nothing
1734 else if ((item2 = list_find(l, end)) == NULL)
1735 semsg(_(e_listidx), end);
1736 else
1737 {
1738 int cnt = 0;
1739
1740 for (li = item; li != NULL; li = li->li_next)
1741 {
1742 ++cnt;
1743 if (li == item2)
1744 break;
1745 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001746 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar108010a2021-06-27 22:03:33 +02001747 emsg(_(e_invalid_range));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001748 else
1749 {
1750 vimlist_remove(l, item, item2);
1751 if (rettv_list_alloc(rettv) == OK)
1752 {
Bram Moolenaar885971e2021-07-18 22:25:29 +02001753 list_T *rl = rettv->vval.v_list;
1754
1755 if (l->lv_with_items > 0)
1756 {
1757 // need to copy the list items and move the value
1758 while (item != NULL)
1759 {
1760 li = listitem_alloc();
1761 if (li == NULL)
1762 return;
1763 li->li_tv = item->li_tv;
1764 init_tv(&item->li_tv);
1765 list_append(rl, li);
1766 if (item == item2)
1767 break;
1768 item = item->li_next;
1769 }
1770 }
1771 else
1772 {
1773 rl->lv_first = item;
1774 rl->lv_u.mat.lv_last = item2;
1775 item->li_prev = NULL;
1776 item2->li_next = NULL;
1777 rl->lv_len = cnt;
1778 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001779 }
1780 }
1781 }
1782 }
1783 }
1784}
1785
1786static int item_compare(const void *s1, const void *s2);
1787static int item_compare2(const void *s1, const void *s2);
1788
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001789// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001790typedef struct
1791{
1792 listitem_T *item;
1793 int idx;
1794} sortItem_T;
1795
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001796// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001797typedef struct
1798{
1799 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001800 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001801 int item_compare_numeric;
1802 int item_compare_numbers;
1803#ifdef FEAT_FLOAT
1804 int item_compare_float;
1805#endif
1806 char_u *item_compare_func;
1807 partial_T *item_compare_partial;
1808 dict_T *item_compare_selfdict;
1809 int item_compare_func_err;
1810 int item_compare_keep_zero;
1811} sortinfo_T;
1812static sortinfo_T *sortinfo = NULL;
1813#define ITEM_COMPARE_FAIL 999
1814
1815/*
1816 * Compare functions for f_sort() and f_uniq() below.
1817 */
1818 static int
1819item_compare(const void *s1, const void *s2)
1820{
1821 sortItem_T *si1, *si2;
1822 typval_T *tv1, *tv2;
1823 char_u *p1, *p2;
1824 char_u *tofree1 = NULL, *tofree2 = NULL;
1825 int res;
1826 char_u numbuf1[NUMBUFLEN];
1827 char_u numbuf2[NUMBUFLEN];
1828
1829 si1 = (sortItem_T *)s1;
1830 si2 = (sortItem_T *)s2;
1831 tv1 = &si1->item->li_tv;
1832 tv2 = &si2->item->li_tv;
1833
1834 if (sortinfo->item_compare_numbers)
1835 {
1836 varnumber_T v1 = tv_get_number(tv1);
1837 varnumber_T v2 = tv_get_number(tv2);
1838
1839 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1840 }
1841
1842#ifdef FEAT_FLOAT
1843 if (sortinfo->item_compare_float)
1844 {
1845 float_T v1 = tv_get_float(tv1);
1846 float_T v2 = tv_get_float(tv2);
1847
1848 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1849 }
1850#endif
1851
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001852 // tv2string() puts quotes around a string and allocates memory. Don't do
1853 // that for string variables. Use a single quote when comparing with a
1854 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001855 if (tv1->v_type == VAR_STRING)
1856 {
1857 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1858 p1 = (char_u *)"'";
1859 else
1860 p1 = tv1->vval.v_string;
1861 }
1862 else
1863 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1864 if (tv2->v_type == VAR_STRING)
1865 {
1866 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1867 p2 = (char_u *)"'";
1868 else
1869 p2 = tv2->vval.v_string;
1870 }
1871 else
1872 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1873 if (p1 == NULL)
1874 p1 = (char_u *)"";
1875 if (p2 == NULL)
1876 p2 = (char_u *)"";
1877 if (!sortinfo->item_compare_numeric)
1878 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001879 if (sortinfo->item_compare_lc)
1880 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001881 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001882 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001883 }
1884 else
1885 {
1886 double n1, n2;
1887 n1 = strtod((char *)p1, (char **)&p1);
1888 n2 = strtod((char *)p2, (char **)&p2);
1889 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1890 }
1891
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001892 // When the result would be zero, compare the item indexes. Makes the
1893 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001894 if (res == 0 && !sortinfo->item_compare_keep_zero)
1895 res = si1->idx > si2->idx ? 1 : -1;
1896
1897 vim_free(tofree1);
1898 vim_free(tofree2);
1899 return res;
1900}
1901
1902 static int
1903item_compare2(const void *s1, const void *s2)
1904{
1905 sortItem_T *si1, *si2;
1906 int res;
1907 typval_T rettv;
1908 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001909 char_u *func_name;
1910 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001911 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001912
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001913 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001914 if (sortinfo->item_compare_func_err)
1915 return 0;
1916
1917 si1 = (sortItem_T *)s1;
1918 si2 = (sortItem_T *)s2;
1919
1920 if (partial == NULL)
1921 func_name = sortinfo->item_compare_func;
1922 else
1923 func_name = partial_name(partial);
1924
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001925 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1926 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001927 copy_tv(&si1->item->li_tv, &argv[0]);
1928 copy_tv(&si2->item->li_tv, &argv[1]);
1929
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001930 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001931 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001932 funcexe.evaluate = TRUE;
1933 funcexe.partial = partial;
1934 funcexe.selfdict = sortinfo->item_compare_selfdict;
1935 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001936 clear_tv(&argv[0]);
1937 clear_tv(&argv[1]);
1938
1939 if (res == FAIL)
1940 res = ITEM_COMPARE_FAIL;
1941 else
1942 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1943 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001944 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001945 clear_tv(&rettv);
1946
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001947 // When the result would be zero, compare the pointers themselves. Makes
1948 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001949 if (res == 0 && !sortinfo->item_compare_keep_zero)
1950 res = si1->idx > si2->idx ? 1 : -1;
1951
1952 return res;
1953}
1954
1955/*
1956 * "sort()" or "uniq()" function
1957 */
1958 static void
1959do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1960{
1961 list_T *l;
1962 listitem_T *li;
1963 sortItem_T *ptrs;
1964 sortinfo_T *old_sortinfo;
1965 sortinfo_T info;
1966 long len;
1967 long i;
1968
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001969 if (in_vim9script()
1970 && (check_for_list_arg(argvars, 0) == FAIL
1971 || (argvars[1].v_type != VAR_UNKNOWN
1972 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
1973 return;
1974
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001975 // Pointer to current info struct used in compare function. Save and
1976 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001977 old_sortinfo = sortinfo;
1978 sortinfo = &info;
1979
1980 if (argvars[0].v_type != VAR_LIST)
1981 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1982 else
1983 {
1984 l = argvars[0].vval.v_list;
Bram Moolenaaref982572021-08-12 19:27:57 +02001985 if (l != NULL && value_check_lock(l->lv_lock,
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001986 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1987 TRUE))
1988 goto theend;
1989 rettv_list_set(rettv, l);
Bram Moolenaaref982572021-08-12 19:27:57 +02001990 if (l == NULL)
1991 goto theend;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001992 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001993
1994 len = list_len(l);
1995 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001996 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001997
1998 info.item_compare_ic = FALSE;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001999 info.item_compare_lc = FALSE;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002000 info.item_compare_numeric = FALSE;
2001 info.item_compare_numbers = FALSE;
2002#ifdef FEAT_FLOAT
2003 info.item_compare_float = FALSE;
2004#endif
2005 info.item_compare_func = NULL;
2006 info.item_compare_partial = NULL;
2007 info.item_compare_selfdict = NULL;
2008 if (argvars[1].v_type != VAR_UNKNOWN)
2009 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002010 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002011 if (argvars[1].v_type == VAR_FUNC)
2012 info.item_compare_func = argvars[1].vval.v_string;
2013 else if (argvars[1].v_type == VAR_PARTIAL)
2014 info.item_compare_partial = argvars[1].vval.v_partial;
2015 else
2016 {
2017 int error = FALSE;
Bram Moolenaar08e51f42020-09-16 23:23:36 +02002018 int nr = 0;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002019
Bram Moolenaar08e51f42020-09-16 23:23:36 +02002020 if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002021 {
Bram Moolenaar08e51f42020-09-16 23:23:36 +02002022 nr = tv_get_number_chk(&argvars[1], &error);
2023 if (error)
2024 goto theend; // type error; errmsg already given
2025 if (nr == 1)
2026 info.item_compare_ic = TRUE;
2027 }
2028 if (nr != 1)
2029 {
2030 if (argvars[1].v_type != VAR_NUMBER)
2031 info.item_compare_func = tv_get_string(&argvars[1]);
2032 else if (nr != 0)
2033 {
2034 emsg(_(e_invarg));
2035 goto theend;
2036 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002037 }
2038 if (info.item_compare_func != NULL)
2039 {
2040 if (*info.item_compare_func == NUL)
2041 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002042 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002043 info.item_compare_func = NULL;
2044 }
2045 else if (STRCMP(info.item_compare_func, "n") == 0)
2046 {
2047 info.item_compare_func = NULL;
2048 info.item_compare_numeric = TRUE;
2049 }
2050 else if (STRCMP(info.item_compare_func, "N") == 0)
2051 {
2052 info.item_compare_func = NULL;
2053 info.item_compare_numbers = TRUE;
2054 }
2055#ifdef FEAT_FLOAT
2056 else if (STRCMP(info.item_compare_func, "f") == 0)
2057 {
2058 info.item_compare_func = NULL;
2059 info.item_compare_float = TRUE;
2060 }
2061#endif
2062 else if (STRCMP(info.item_compare_func, "i") == 0)
2063 {
2064 info.item_compare_func = NULL;
2065 info.item_compare_ic = TRUE;
2066 }
Bram Moolenaar55e29612020-11-01 13:57:44 +01002067 else if (STRCMP(info.item_compare_func, "l") == 0)
2068 {
2069 info.item_compare_func = NULL;
2070 info.item_compare_lc = TRUE;
2071 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002072 }
2073 }
2074
2075 if (argvars[2].v_type != VAR_UNKNOWN)
2076 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002077 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002078 if (argvars[2].v_type != VAR_DICT)
2079 {
2080 emsg(_(e_dictreq));
2081 goto theend;
2082 }
2083 info.item_compare_selfdict = argvars[2].vval.v_dict;
2084 }
2085 }
2086
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002087 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002088 ptrs = ALLOC_MULT(sortItem_T, len);
2089 if (ptrs == NULL)
2090 goto theend;
2091
2092 i = 0;
2093 if (sort)
2094 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002095 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002096 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002097 {
2098 ptrs[i].item = li;
2099 ptrs[i].idx = i;
2100 ++i;
2101 }
2102
2103 info.item_compare_func_err = FALSE;
2104 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002105 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002106 if ((info.item_compare_func != NULL
2107 || info.item_compare_partial != NULL)
2108 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
2109 == ITEM_COMPARE_FAIL)
2110 emsg(_("E702: Sort compare function failed"));
2111 else
2112 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002113 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002114 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
2115 info.item_compare_func == NULL
2116 && info.item_compare_partial == NULL
2117 ? item_compare : item_compare2);
2118
2119 if (!info.item_compare_func_err)
2120 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002121 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002122 l->lv_first = l->lv_u.mat.lv_last
2123 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002124 l->lv_len = 0;
2125 for (i = 0; i < len; ++i)
2126 list_append(l, ptrs[i].item);
2127 }
2128 }
2129 }
2130 else
2131 {
2132 int (*item_compare_func_ptr)(const void *, const void *);
2133
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002134 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002135 info.item_compare_func_err = FALSE;
2136 info.item_compare_keep_zero = TRUE;
2137 item_compare_func_ptr = info.item_compare_func != NULL
2138 || info.item_compare_partial != NULL
2139 ? item_compare2 : item_compare;
2140
2141 for (li = l->lv_first; li != NULL && li->li_next != NULL;
2142 li = li->li_next)
2143 {
2144 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
2145 == 0)
2146 ptrs[i++].item = li;
2147 if (info.item_compare_func_err)
2148 {
2149 emsg(_("E882: Uniq compare function failed"));
2150 break;
2151 }
2152 }
2153
2154 if (!info.item_compare_func_err)
2155 {
2156 while (--i >= 0)
2157 {
2158 li = ptrs[i].item->li_next;
2159 ptrs[i].item->li_next = li->li_next;
2160 if (li->li_next != NULL)
2161 li->li_next->li_prev = ptrs[i].item;
2162 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002163 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002164 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002166 l->lv_len--;
2167 }
2168 }
2169 }
2170
2171 vim_free(ptrs);
2172 }
2173theend:
2174 sortinfo = old_sortinfo;
2175}
2176
2177/*
2178 * "sort({list})" function
2179 */
2180 void
2181f_sort(typval_T *argvars, typval_T *rettv)
2182{
2183 do_sort_uniq(argvars, rettv, TRUE);
2184}
2185
2186/*
2187 * "uniq({list})" function
2188 */
2189 void
2190f_uniq(typval_T *argvars, typval_T *rettv)
2191{
2192 do_sort_uniq(argvars, rettv, FALSE);
2193}
2194
Bram Moolenaarea696852020-11-09 18:31:39 +01002195typedef enum {
2196 FILTERMAP_FILTER,
2197 FILTERMAP_MAP,
2198 FILTERMAP_MAPNEW
2199} filtermap_T;
2200
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002201/*
2202 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01002203 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002204 */
2205 static int
Bram Moolenaarea696852020-11-09 18:31:39 +01002206filter_map_one(
2207 typval_T *tv, // original value
2208 typval_T *expr, // callback
2209 filtermap_T filtermap,
2210 typval_T *newtv, // for map() and mapnew(): new value
2211 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002212{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002213 typval_T argv[3];
2214 int retval = FAIL;
2215
2216 copy_tv(tv, get_vim_var_tv(VV_VAL));
2217 argv[0] = *get_vim_var_tv(VV_KEY);
2218 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01002219 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002220 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002221 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002222 {
2223 int error = FALSE;
2224
2225 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02002226 if (in_vim9script())
Bram Moolenaarea696852020-11-09 18:31:39 +01002227 *remp = !tv2bool(newtv);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002228 else
Bram Moolenaarea696852020-11-09 18:31:39 +01002229 *remp = (tv_get_number_chk(newtv, &error) == 0);
2230 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002231 // On type error, nothing has been removed; return FAIL to stop the
2232 // loop. The error message was given by tv_get_number_chk().
2233 if (error)
2234 goto theend;
2235 }
2236 retval = OK;
2237theend:
2238 clear_tv(get_vim_var_tv(VV_VAL));
2239 return retval;
2240}
2241
2242/*
2243 * Implementation of map() and filter().
2244 */
2245 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01002246filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002247{
2248 typval_T *expr;
2249 listitem_T *li, *nli;
2250 list_T *l = NULL;
2251 dictitem_T *di;
2252 hashtab_T *ht;
2253 hashitem_T *hi;
2254 dict_T *d = NULL;
2255 blob_T *b = NULL;
2256 int rem;
2257 int todo;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002258 char *func_name = filtermap == FILTERMAP_MAP ? "map()"
Bram Moolenaarea696852020-11-09 18:31:39 +01002259 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002260 : "filter()";
Bram Moolenaarea696852020-11-09 18:31:39 +01002261 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2262 ? N_("map() argument")
2263 : filtermap == FILTERMAP_MAPNEW
2264 ? N_("mapnew() argument")
2265 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002266 int save_did_emsg;
2267 int idx = 0;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002268 type_T *type = NULL;
2269 garray_T type_list;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002270
Bram Moolenaarea696852020-11-09 18:31:39 +01002271 // map() and filter() return the first argument, also on failure.
2272 if (filtermap != FILTERMAP_MAPNEW)
2273 copy_tv(&argvars[0], rettv);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002274
2275 if (in_vim9script()
2276 && (check_for_list_or_dict_or_blob_arg(argvars, 0) == FAIL))
2277 return;
2278
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002279 if (filtermap == FILTERMAP_MAP && in_vim9script())
2280 {
2281 // Check that map() does not change the type of the dict.
2282 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaarf2253962021-04-13 20:53:13 +02002283 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002284 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002285
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002286 if (argvars[0].v_type == VAR_BLOB)
2287 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002288 if (filtermap == FILTERMAP_MAPNEW)
2289 {
2290 rettv->v_type = VAR_BLOB;
2291 rettv->vval.v_blob = NULL;
2292 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002293 if ((b = argvars[0].vval.v_blob) == NULL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002294 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002295 }
2296 else if (argvars[0].v_type == VAR_LIST)
2297 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002298 if (filtermap == FILTERMAP_MAPNEW)
2299 {
2300 rettv->v_type = VAR_LIST;
2301 rettv->vval.v_list = NULL;
2302 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002303 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002304 || (filtermap == FILTERMAP_FILTER
2305 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002306 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002307 }
2308 else if (argvars[0].v_type == VAR_DICT)
2309 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002310 if (filtermap == FILTERMAP_MAPNEW)
2311 {
2312 rettv->v_type = VAR_DICT;
2313 rettv->vval.v_dict = NULL;
2314 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002315 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002316 || (filtermap == FILTERMAP_FILTER
2317 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002318 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002319 }
2320 else
2321 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002322 semsg(_(e_listdictblobarg), func_name);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002323 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002324 }
2325
2326 expr = &argvars[1];
2327 // On type errors, the preceding call has already displayed an error
2328 // message. Avoid a misleading error message for an empty string that
2329 // was not passed as argument.
2330 if (expr->v_type != VAR_UNKNOWN)
2331 {
2332 typval_T save_val;
2333 typval_T save_key;
2334
2335 prepare_vimvar(VV_VAL, &save_val);
2336 prepare_vimvar(VV_KEY, &save_key);
2337
2338 // We reset "did_emsg" to be able to detect whether an error
2339 // occurred during evaluation of the expression.
2340 save_did_emsg = did_emsg;
2341 did_emsg = FALSE;
2342
2343 if (argvars[0].v_type == VAR_DICT)
2344 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002345 int prev_lock = d->dv_lock;
Bram Moolenaarea696852020-11-09 18:31:39 +01002346 dict_T *d_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002347
Bram Moolenaarea696852020-11-09 18:31:39 +01002348 if (filtermap == FILTERMAP_MAPNEW)
2349 {
2350 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002351 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002352 d_ret = rettv->vval.v_dict;
2353 }
2354
2355 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002356 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002357 ht = &d->dv_hashtab;
2358 hash_lock(ht);
2359 todo = (int)ht->ht_used;
2360 for (hi = ht->ht_array; todo > 0; ++hi)
2361 {
2362 if (!HASHITEM_EMPTY(hi))
2363 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002364 int r;
2365 typval_T newtv;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002366
2367 --todo;
2368 di = HI2DI(hi);
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002369 if (filtermap == FILTERMAP_MAP
Bram Moolenaarea696852020-11-09 18:31:39 +01002370 && (value_check_lock(di->di_tv.v_lock,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002371 arg_errmsg, TRUE)
2372 || var_check_ro(di->di_flags,
2373 arg_errmsg, TRUE)))
2374 break;
2375 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaar027c4ab2021-02-21 16:20:18 +01002376 newtv.v_type = VAR_UNKNOWN;
Bram Moolenaarea696852020-11-09 18:31:39 +01002377 r = filter_map_one(&di->di_tv, expr, filtermap,
2378 &newtv, &rem);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002379 clear_tv(get_vim_var_tv(VV_KEY));
2380 if (r == FAIL || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002381 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002382 clear_tv(&newtv);
2383 break;
2384 }
2385 if (filtermap == FILTERMAP_MAP)
2386 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002387 if (type != NULL && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002388 type->tt_member, &newtv,
2389 func_name, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002390 {
2391 clear_tv(&newtv);
2392 break;
2393 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002394 // map(): replace the dict item value
2395 clear_tv(&di->di_tv);
2396 newtv.v_lock = 0;
2397 di->di_tv = newtv;
2398 }
2399 else if (filtermap == FILTERMAP_MAPNEW)
2400 {
2401 // mapnew(): add the item value to the new dict
2402 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
2403 clear_tv(&newtv);
2404 if (r == FAIL)
2405 break;
2406 }
2407 else if (filtermap == FILTERMAP_FILTER && rem)
2408 {
2409 // filter(false): remove the item from the dict
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002410 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2411 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2412 break;
2413 dictitem_remove(d, di);
2414 }
2415 }
2416 }
2417 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002418 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002419 }
2420 else if (argvars[0].v_type == VAR_BLOB)
2421 {
2422 int i;
2423 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002424 varnumber_T val;
Bram Moolenaarea696852020-11-09 18:31:39 +01002425 blob_T *b_ret = b;
2426
2427 if (filtermap == FILTERMAP_MAPNEW)
2428 {
2429 if (blob_copy(b, rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002430 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002431 b_ret = rettv->vval.v_blob;
2432 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002433
2434 // set_vim_var_nr() doesn't set the type
2435 set_vim_var_type(VV_KEY, VAR_NUMBER);
2436
2437 for (i = 0; i < b->bv_ga.ga_len; i++)
2438 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002439 typval_T newtv;
2440
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002441 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002442 val = blob_get(b, i);
2443 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002444 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaarea696852020-11-09 18:31:39 +01002445 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
2446 || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002447 break;
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002448 if (newtv.v_type != VAR_NUMBER && newtv.v_type != VAR_BOOL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002449 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002450 clear_tv(&newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002451 emsg(_(e_invalblob));
2452 break;
2453 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002454 if (filtermap != FILTERMAP_FILTER)
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002455 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002456 if (newtv.vval.v_number != val)
2457 blob_set(b_ret, i, newtv.vval.v_number);
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002458 }
2459 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002460 {
2461 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2462
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002463 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002464 (size_t)b->bv_ga.ga_len - i - 1);
2465 --b->bv_ga.ga_len;
2466 --i;
2467 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002468 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002469 }
2470 }
2471 else // argvars[0].v_type == VAR_LIST
2472 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002473 int prev_lock = l->lv_lock;
2474 list_T *l_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002475
Bram Moolenaarea696852020-11-09 18:31:39 +01002476 if (filtermap == FILTERMAP_MAPNEW)
2477 {
2478 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002479 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002480 l_ret = rettv->vval.v_list;
2481 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002482 // set_vim_var_nr() doesn't set the type
2483 set_vim_var_type(VV_KEY, VAR_NUMBER);
2484
Bram Moolenaarea696852020-11-09 18:31:39 +01002485 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002486 l->lv_lock = VAR_LOCKED;
Bram Moolenaarea696852020-11-09 18:31:39 +01002487
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002488 if (l->lv_first == &range_list_item)
2489 {
2490 varnumber_T val = l->lv_u.nonmat.lv_start;
2491 int len = l->lv_len;
2492 int stride = l->lv_u.nonmat.lv_stride;
2493
2494 // List from range(): loop over the numbers
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002495 if (filtermap != FILTERMAP_MAPNEW)
2496 {
2497 l->lv_first = NULL;
2498 l->lv_u.mat.lv_last = NULL;
2499 l->lv_len = 0;
2500 l->lv_u.mat.lv_idx_item = NULL;
2501 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002502
2503 for (idx = 0; idx < len; ++idx)
Bram Moolenaarc56936e2020-11-10 11:43:56 +01002504 {
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002505 typval_T tv;
2506 typval_T newtv;
2507
2508 tv.v_type = VAR_NUMBER;
2509 tv.v_lock = 0;
2510 tv.vval.v_number = val;
2511 set_vim_var_nr(VV_KEY, idx);
2512 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2513 == FAIL)
Bram Moolenaarea696852020-11-09 18:31:39 +01002514 break;
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002515 if (did_emsg)
2516 {
2517 clear_tv(&newtv);
2518 break;
2519 }
2520 if (filtermap != FILTERMAP_FILTER)
2521 {
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002522 if (filtermap == FILTERMAP_MAP && type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002523 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002524 type->tt_member, &newtv,
2525 func_name, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002526 {
2527 clear_tv(&newtv);
2528 break;
2529 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002530 // map(), mapnew(): always append the new value to the
2531 // list
2532 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2533 ? l : l_ret, &newtv) == FAIL)
2534 break;
2535 }
2536 else if (!rem)
2537 {
2538 // filter(): append the list item value when not rem
2539 if (list_append_tv_move(l, &tv) == FAIL)
2540 break;
2541 }
2542
2543 val += stride;
Bram Moolenaarea696852020-11-09 18:31:39 +01002544 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002545 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002546 else
2547 {
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002548 // Materialized list: loop over the items
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002549 for (li = l->lv_first; li != NULL; li = nli)
2550 {
2551 typval_T newtv;
2552
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002553 if (filtermap == FILTERMAP_MAP && value_check_lock(
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002554 li->li_tv.v_lock, arg_errmsg, TRUE))
2555 break;
2556 nli = li->li_next;
2557 set_vim_var_nr(VV_KEY, idx);
2558 if (filter_map_one(&li->li_tv, expr, filtermap,
2559 &newtv, &rem) == FAIL)
2560 break;
2561 if (did_emsg)
2562 {
2563 clear_tv(&newtv);
2564 break;
2565 }
2566 if (filtermap == FILTERMAP_MAP)
2567 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002568 if (type != NULL && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002569 type->tt_member, &newtv, func_name, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002570 {
2571 clear_tv(&newtv);
2572 break;
2573 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002574 // map(): replace the list item value
2575 clear_tv(&li->li_tv);
2576 newtv.v_lock = 0;
2577 li->li_tv = newtv;
2578 }
2579 else if (filtermap == FILTERMAP_MAPNEW)
2580 {
2581 // mapnew(): append the list item value
2582 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2583 break;
2584 }
2585 else if (filtermap == FILTERMAP_FILTER && rem)
2586 listitem_remove(l, li);
2587 ++idx;
2588 }
2589 }
2590
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002591 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002592 }
2593
2594 restore_vimvar(VV_KEY, &save_key);
2595 restore_vimvar(VV_VAL, &save_val);
2596
2597 did_emsg |= save_did_emsg;
2598 }
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002599
2600theend:
2601 if (type != NULL)
2602 clear_type_list(&type_list);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002603}
2604
2605/*
2606 * "filter()" function
2607 */
2608 void
2609f_filter(typval_T *argvars, typval_T *rettv)
2610{
Bram Moolenaarea696852020-11-09 18:31:39 +01002611 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002612}
2613
2614/*
2615 * "map()" function
2616 */
2617 void
2618f_map(typval_T *argvars, typval_T *rettv)
2619{
Bram Moolenaarea696852020-11-09 18:31:39 +01002620 filter_map(argvars, rettv, FILTERMAP_MAP);
2621}
2622
2623/*
2624 * "mapnew()" function
2625 */
2626 void
2627f_mapnew(typval_T *argvars, typval_T *rettv)
2628{
2629 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002630}
2631
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002632/*
2633 * "add(list, item)" function
2634 */
2635 void
2636f_add(typval_T *argvars, typval_T *rettv)
2637{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002638 rettv->vval.v_number = 1; // Default: Failed
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002639
2640 if (in_vim9script()
2641 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2642 || (argvars[0].v_type == VAR_BLOB
2643 && check_for_number_arg(argvars, 1) == FAIL)))
2644 return;
2645
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002646 if (argvars[0].v_type == VAR_LIST)
2647 {
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002648 list_T *l = argvars[0].vval.v_list;
2649
2650 if (l == NULL)
2651 {
2652 if (in_vim9script())
2653 emsg(_(e_cannot_add_to_null_list));
2654 }
2655 else if (!value_check_lock(l->lv_lock,
2656 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002657 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002658 {
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002659 copy_tv(&argvars[0], rettv);
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002660 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002661 }
2662 else if (argvars[0].v_type == VAR_BLOB)
2663 {
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002664 blob_T *b = argvars[0].vval.v_blob;
2665
2666 if (b == NULL)
2667 {
2668 if (in_vim9script())
2669 emsg(_(e_cannot_add_to_null_blob));
2670 }
2671 else if (!value_check_lock(b->bv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002672 (char_u *)N_("add() argument"), TRUE))
2673 {
2674 int error = FALSE;
2675 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2676
2677 if (!error)
2678 {
2679 ga_append(&b->bv_ga, (int)n);
2680 copy_tv(&argvars[0], rettv);
2681 }
2682 }
2683 }
2684 else
2685 emsg(_(e_listblobreq));
2686}
2687
2688/*
2689 * "count()" function
2690 */
2691 void
2692f_count(typval_T *argvars, typval_T *rettv)
2693{
2694 long n = 0;
2695 int ic = FALSE;
2696 int error = FALSE;
2697
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002698 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002699 && (check_for_string_or_list_or_dict_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002700 || check_for_opt_bool_arg(argvars, 2) == FAIL
2701 || (argvars[2].v_type != VAR_UNKNOWN
2702 && check_for_opt_number_arg(argvars, 3) == FAIL)))
2703 return;
2704
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002705 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002706 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002707
2708 if (argvars[0].v_type == VAR_STRING)
2709 {
2710 char_u *expr = tv_get_string_chk(&argvars[1]);
2711 char_u *p = argvars[0].vval.v_string;
2712 char_u *next;
2713
2714 if (!error && expr != NULL && *expr != NUL && p != NULL)
2715 {
2716 if (ic)
2717 {
2718 size_t len = STRLEN(expr);
2719
2720 while (*p != NUL)
2721 {
2722 if (MB_STRNICMP(p, expr, len) == 0)
2723 {
2724 ++n;
2725 p += len;
2726 }
2727 else
2728 MB_PTR_ADV(p);
2729 }
2730 }
2731 else
2732 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2733 != NULL)
2734 {
2735 ++n;
2736 p = next + STRLEN(expr);
2737 }
2738 }
2739
2740 }
2741 else if (argvars[0].v_type == VAR_LIST)
2742 {
2743 listitem_T *li;
2744 list_T *l;
2745 long idx;
2746
2747 if ((l = argvars[0].vval.v_list) != NULL)
2748 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002749 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002750 li = l->lv_first;
2751 if (argvars[2].v_type != VAR_UNKNOWN)
2752 {
2753 if (argvars[3].v_type != VAR_UNKNOWN)
2754 {
2755 idx = (long)tv_get_number_chk(&argvars[3], &error);
2756 if (!error)
2757 {
2758 li = list_find(l, idx);
2759 if (li == NULL)
2760 semsg(_(e_listidx), idx);
2761 }
2762 }
2763 if (error)
2764 li = NULL;
2765 }
2766
2767 for ( ; li != NULL; li = li->li_next)
2768 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2769 ++n;
2770 }
2771 }
2772 else if (argvars[0].v_type == VAR_DICT)
2773 {
2774 int todo;
2775 dict_T *d;
2776 hashitem_T *hi;
2777
2778 if ((d = argvars[0].vval.v_dict) != NULL)
2779 {
2780 if (argvars[2].v_type != VAR_UNKNOWN)
2781 {
2782 if (argvars[3].v_type != VAR_UNKNOWN)
2783 emsg(_(e_invarg));
2784 }
2785
2786 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2787 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2788 {
2789 if (!HASHITEM_EMPTY(hi))
2790 {
2791 --todo;
2792 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2793 ++n;
2794 }
2795 }
2796 }
2797 }
2798 else
2799 semsg(_(e_listdictarg), "count()");
2800 rettv->vval.v_number = n;
2801}
2802
2803/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002804 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002805 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002806 static void
2807extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002808{
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002809 type_T *type = NULL;
2810 garray_T type_list;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002811 char *func_name = is_new ? "extendnew()" : "extend()";
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002812
2813 if (!is_new && in_vim9script())
2814 {
2815 // Check that map() does not change the type of the dict.
2816 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaarf2253962021-04-13 20:53:13 +02002817 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002818 }
2819
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002820 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2821 {
2822 list_T *l1, *l2;
2823 listitem_T *item;
2824 long before;
2825 int error = FALSE;
2826
2827 l1 = argvars[0].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002828 if (l1 == NULL)
2829 {
2830 emsg(_(e_cannot_extend_null_list));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002831 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002832 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002833 l2 = argvars[1].vval.v_list;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002834 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
2835 && l2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002836 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002837 if (is_new)
2838 {
2839 l1 = list_copy(l1, FALSE, get_copyID());
2840 if (l1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002841 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002842 }
2843
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002844 if (argvars[2].v_type != VAR_UNKNOWN)
2845 {
2846 before = (long)tv_get_number_chk(&argvars[2], &error);
2847 if (error)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002848 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002849
2850 if (before == l1->lv_len)
2851 item = NULL;
2852 else
2853 {
2854 item = list_find(l1, before);
2855 if (item == NULL)
2856 {
2857 semsg(_(e_listidx), before);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002858 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002859 }
2860 }
2861 }
2862 else
2863 item = NULL;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002864 if (type != NULL && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002865 type, &argvars[1], func_name, 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002866 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002867 list_extend(l1, l2, item);
2868
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002869 if (is_new)
2870 {
2871 rettv->v_type = VAR_LIST;
2872 rettv->vval.v_list = l1;
2873 rettv->v_lock = FALSE;
2874 }
2875 else
2876 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002877 }
2878 }
2879 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2880 {
2881 dict_T *d1, *d2;
2882 char_u *action;
2883 int i;
2884
2885 d1 = argvars[0].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002886 if (d1 == NULL)
2887 {
2888 emsg(_(e_cannot_extend_null_dict));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002889 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002890 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002891 d2 = argvars[1].vval.v_dict;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002892 if ((is_new || !value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
2893 && d2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002894 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002895 if (is_new)
2896 {
2897 d1 = dict_copy(d1, FALSE, get_copyID());
2898 if (d1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002899 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002900 }
2901
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002902 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002903 if (argvars[2].v_type != VAR_UNKNOWN)
2904 {
2905 static char *(av[]) = {"keep", "force", "error"};
2906
2907 action = tv_get_string_chk(&argvars[2]);
2908 if (action == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002909 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002910 for (i = 0; i < 3; ++i)
2911 if (STRCMP(action, av[i]) == 0)
2912 break;
2913 if (i == 3)
2914 {
2915 semsg(_(e_invarg2), action);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002916 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002917 }
2918 }
2919 else
2920 action = (char_u *)"force";
2921
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002922 if (type != NULL && check_typval_arg_type(type, &argvars[1],
2923 func_name, 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002924 goto theend;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002925 dict_extend(d1, d2, action, func_name);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002926
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002927 if (is_new)
2928 {
2929 rettv->v_type = VAR_DICT;
2930 rettv->vval.v_dict = d1;
2931 rettv->v_lock = FALSE;
2932 }
2933 else
2934 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002935 }
2936 }
2937 else
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002938 semsg(_(e_listdictarg), func_name);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002939
2940theend:
2941 if (type != NULL)
2942 clear_type_list(&type_list);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002943}
2944
2945/*
2946 * "extend(list, list [, idx])" function
2947 * "extend(dict, dict [, action])" function
2948 */
2949 void
2950f_extend(typval_T *argvars, typval_T *rettv)
2951{
2952 char_u *errmsg = (char_u *)N_("extend() argument");
2953
2954 extend(argvars, rettv, errmsg, FALSE);
2955}
2956
2957/*
2958 * "extendnew(list, list [, idx])" function
2959 * "extendnew(dict, dict [, action])" function
2960 */
2961 void
2962f_extendnew(typval_T *argvars, typval_T *rettv)
2963{
2964 char_u *errmsg = (char_u *)N_("extendnew() argument");
2965
2966 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002967}
2968
2969/*
2970 * "insert()" function
2971 */
2972 void
2973f_insert(typval_T *argvars, typval_T *rettv)
2974{
2975 long before = 0;
2976 listitem_T *item;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002977 int error = FALSE;
2978
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002979 if (in_vim9script()
2980 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2981 || (argvars[0].v_type == VAR_BLOB
2982 && check_for_number_arg(argvars, 1) == FAIL)
2983 || check_for_opt_number_arg(argvars, 2) == FAIL))
2984 return;
2985
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002986 if (argvars[0].v_type == VAR_BLOB)
2987 {
Sean Dewar80d73952021-08-04 19:25:54 +02002988 blob_T *b = argvars[0].vval.v_blob;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002989
Sean Dewar80d73952021-08-04 19:25:54 +02002990 if (b == NULL)
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002991 {
2992 if (in_vim9script())
2993 emsg(_(e_cannot_add_to_null_blob));
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002994 }
Sean Dewar80d73952021-08-04 19:25:54 +02002995 else if (!value_check_lock(b->bv_lock,
2996 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002997 {
Sean Dewar80d73952021-08-04 19:25:54 +02002998 int val, len;
2999 char_u *p;
3000
3001 len = blob_len(b);
3002 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003003 {
Sean Dewar80d73952021-08-04 19:25:54 +02003004 before = (long)tv_get_number_chk(&argvars[2], &error);
3005 if (error)
3006 return; // type error; errmsg already given
3007 if (before < 0 || before > len)
3008 {
3009 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
3010 return;
3011 }
3012 }
3013 val = tv_get_number_chk(&argvars[1], &error);
3014 if (error)
3015 return;
3016 if (val < 0 || val > 255)
3017 {
3018 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003019 return;
3020 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003021
Sean Dewar80d73952021-08-04 19:25:54 +02003022 if (ga_grow(&b->bv_ga, 1) == FAIL)
3023 return;
3024 p = (char_u *)b->bv_ga.ga_data;
3025 mch_memmove(p + before + 1, p + before, (size_t)len - before);
3026 *(p + before) = val;
3027 ++b->bv_ga.ga_len;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003028
Sean Dewar80d73952021-08-04 19:25:54 +02003029 copy_tv(&argvars[0], rettv);
3030 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003031 }
3032 else if (argvars[0].v_type != VAR_LIST)
3033 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar39211cb2021-04-18 15:48:04 +02003034 else
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003035 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02003036 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003037
Bram Moolenaar39211cb2021-04-18 15:48:04 +02003038 if (l == NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003039 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02003040 if (in_vim9script())
3041 emsg(_(e_cannot_add_to_null_list));
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003042 }
Bram Moolenaar39211cb2021-04-18 15:48:04 +02003043 else if (!value_check_lock(l->lv_lock,
3044 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003045 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02003046 if (argvars[2].v_type != VAR_UNKNOWN)
3047 before = (long)tv_get_number_chk(&argvars[2], &error);
3048 if (error)
3049 return; // type error; errmsg already given
3050
3051 if (before == l->lv_len)
3052 item = NULL;
3053 else
3054 {
3055 item = list_find(l, before);
3056 if (item == NULL)
3057 {
3058 semsg(_(e_listidx), before);
3059 l = NULL;
3060 }
3061 }
3062 if (l != NULL)
3063 {
3064 (void)list_insert_tv(l, &argvars[1], item);
3065 copy_tv(&argvars[0], rettv);
3066 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003067 }
3068 }
3069}
3070
3071/*
3072 * "remove()" function
3073 */
3074 void
3075f_remove(typval_T *argvars, typval_T *rettv)
3076{
3077 char_u *arg_errmsg = (char_u *)N_("remove() argument");
3078
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02003079 if (in_vim9script()
3080 && (check_for_list_or_dict_or_blob_arg(argvars, 0) == FAIL
3081 || ((argvars[0].v_type == VAR_LIST
3082 || argvars[0].v_type == VAR_BLOB)
3083 && (check_for_number_arg(argvars, 1) == FAIL
3084 || check_for_opt_number_arg(argvars, 2) == FAIL))
3085 || (argvars[0].v_type == VAR_DICT
3086 && check_for_string_or_number_arg(argvars, 1) == FAIL)))
3087 return;
3088
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003089 if (argvars[0].v_type == VAR_DICT)
3090 dict_remove(argvars, rettv, arg_errmsg);
3091 else if (argvars[0].v_type == VAR_BLOB)
Sean Dewar80d73952021-08-04 19:25:54 +02003092 blob_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003093 else if (argvars[0].v_type == VAR_LIST)
3094 list_remove(argvars, rettv, arg_errmsg);
3095 else
3096 semsg(_(e_listdictblobarg), "remove()");
3097}
3098
3099/*
3100 * "reverse({list})" function
3101 */
3102 void
3103f_reverse(typval_T *argvars, typval_T *rettv)
3104{
3105 list_T *l;
3106 listitem_T *li, *ni;
3107
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02003108 if (in_vim9script() && check_for_list_or_blob_arg(argvars, 0) == FAIL)
3109 return;
3110
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003111 if (argvars[0].v_type == VAR_BLOB)
3112 {
3113 blob_T *b = argvars[0].vval.v_blob;
3114 int i, len = blob_len(b);
3115
3116 for (i = 0; i < len / 2; i++)
3117 {
3118 int tmp = blob_get(b, i);
3119
3120 blob_set(b, i, blob_get(b, len - i - 1));
3121 blob_set(b, len - i - 1, tmp);
3122 }
3123 rettv_blob_set(rettv, b);
3124 return;
3125 }
3126
3127 if (argvars[0].v_type != VAR_LIST)
3128 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaaref982572021-08-12 19:27:57 +02003129 else
3130 {
3131 l = argvars[0].vval.v_list;
3132 rettv_list_set(rettv, l);
3133 if (l != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02003134 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003135 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar89bfc822020-01-27 22:37:23 +01003136 {
Bram Moolenaaref982572021-08-12 19:27:57 +02003137 if (l->lv_first == &range_list_item)
3138 {
3139 varnumber_T new_start = l->lv_u.nonmat.lv_start
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003140 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
Bram Moolenaaref982572021-08-12 19:27:57 +02003141 l->lv_u.nonmat.lv_end = new_start
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003142 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
Bram Moolenaaref982572021-08-12 19:27:57 +02003143 l->lv_u.nonmat.lv_start = new_start;
3144 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
3145 return;
3146 }
3147 li = l->lv_u.mat.lv_last;
3148 l->lv_first = l->lv_u.mat.lv_last = NULL;
3149 l->lv_len = 0;
3150 while (li != NULL)
3151 {
3152 ni = li->li_prev;
3153 list_append(l, li);
3154 li = ni;
3155 }
3156 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01003157 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003158 }
3159}
3160
Bram Moolenaar85629982020-06-01 18:39:20 +02003161/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003162 * "reduce(list, { accumulator, element -> value } [, initial])" function
Bram Moolenaar85629982020-06-01 18:39:20 +02003163 */
3164 void
3165f_reduce(typval_T *argvars, typval_T *rettv)
3166{
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003167 typval_T initial;
Bram Moolenaar85629982020-06-01 18:39:20 +02003168 char_u *func_name;
3169 partial_T *partial = NULL;
3170 funcexe_T funcexe;
3171 typval_T argv[3];
3172
3173 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
3174 {
3175 emsg(_(e_listblobreq));
3176 return;
3177 }
3178
3179 if (argvars[1].v_type == VAR_FUNC)
3180 func_name = argvars[1].vval.v_string;
3181 else if (argvars[1].v_type == VAR_PARTIAL)
3182 {
3183 partial = argvars[1].vval.v_partial;
3184 func_name = partial_name(partial);
3185 }
3186 else
3187 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01003188 if (func_name == NULL || *func_name == NUL)
3189 {
3190 emsg(_(e_missing_function_argument));
3191 return;
3192 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003193
3194 vim_memset(&funcexe, 0, sizeof(funcexe));
3195 funcexe.evaluate = TRUE;
3196 funcexe.partial = partial;
3197
3198 if (argvars[0].v_type == VAR_LIST)
3199 {
3200 list_T *l = argvars[0].vval.v_list;
3201 listitem_T *li = NULL;
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003202 int r;
Bram Moolenaarca275a02020-06-24 22:07:46 +02003203 int called_emsg_start = called_emsg;
Bram Moolenaar85629982020-06-01 18:39:20 +02003204
Bram Moolenaarfda20c42020-06-29 20:09:36 +02003205 if (l != NULL)
3206 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar85629982020-06-01 18:39:20 +02003207 if (argvars[2].v_type == VAR_UNKNOWN)
3208 {
3209 if (l == NULL || l->lv_first == NULL)
3210 {
3211 semsg(_(e_reduceempty), "List");
3212 return;
3213 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003214 initial = l->lv_first->li_tv;
Bram Moolenaar85629982020-06-01 18:39:20 +02003215 li = l->lv_first->li_next;
3216 }
3217 else
3218 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003219 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02003220 if (l != NULL)
3221 li = l->lv_first;
3222 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003223 copy_tv(&initial, rettv);
Bram Moolenaarfda20c42020-06-29 20:09:36 +02003224
3225 if (l != NULL)
Bram Moolenaar85629982020-06-01 18:39:20 +02003226 {
Bram Moolenaarfda20c42020-06-29 20:09:36 +02003227 int prev_locked = l->lv_lock;
3228
3229 l->lv_lock = VAR_FIXED; // disallow the list changing here
3230 for ( ; li != NULL; li = li->li_next)
3231 {
3232 argv[0] = *rettv;
3233 argv[1] = li->li_tv;
3234 rettv->v_type = VAR_UNKNOWN;
3235 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
3236 clear_tv(&argv[0]);
3237 if (r == FAIL || called_emsg != called_emsg_start)
3238 break;
3239 }
3240 l->lv_lock = prev_locked;
Bram Moolenaar85629982020-06-01 18:39:20 +02003241 }
3242 }
3243 else
3244 {
3245 blob_T *b = argvars[0].vval.v_blob;
3246 int i;
3247
3248 if (argvars[2].v_type == VAR_UNKNOWN)
3249 {
3250 if (b == NULL || b->bv_ga.ga_len == 0)
3251 {
3252 semsg(_(e_reduceempty), "Blob");
3253 return;
3254 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003255 initial.v_type = VAR_NUMBER;
3256 initial.vval.v_number = blob_get(b, 0);
Bram Moolenaar85629982020-06-01 18:39:20 +02003257 i = 1;
3258 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003259 else if (argvars[2].v_type != VAR_NUMBER)
3260 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003261 emsg(_(e_number_expected));
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003262 return;
3263 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003264 else
3265 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003266 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02003267 i = 0;
3268 }
3269
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003270 copy_tv(&initial, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003271 if (b != NULL)
3272 {
3273 for ( ; i < b->bv_ga.ga_len; i++)
3274 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003275 argv[0] = *rettv;
Bram Moolenaar85629982020-06-01 18:39:20 +02003276 argv[1].v_type = VAR_NUMBER;
3277 argv[1].vval.v_number = blob_get(b, i);
3278 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
3279 return;
Bram Moolenaar85629982020-06-01 18:39:20 +02003280 }
3281 }
3282 }
3283}
3284
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02003285#endif // defined(FEAT_EVAL)