blob: 0bca0b5530a7b7096c6e0dd1f8075faa3bbd5237 [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/*
534 * Locate "item" list "l" and return its index.
535 * Returns -1 when "item" is not in the list.
536 */
537 long
538list_idx_of_item(list_T *l, listitem_T *item)
539{
540 long idx = 0;
541 listitem_T *li;
542
543 if (l == NULL)
544 return -1;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200545 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200546 idx = 0;
547 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
548 ++idx;
549 if (li == NULL)
550 return -1;
551 return idx;
552}
553
554/*
555 * Append item "item" to the end of list "l".
556 */
557 void
558list_append(list_T *l, listitem_T *item)
559{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200560 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100561 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200562 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100563 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200564 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100565 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200566 item->li_prev = NULL;
567 }
568 else
569 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100570 l->lv_u.mat.lv_last->li_next = item;
571 item->li_prev = l->lv_u.mat.lv_last;
572 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200573 }
574 ++l->lv_len;
575 item->li_next = NULL;
576}
577
578/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200580 * Return FAIL when out of memory.
581 */
582 int
583list_append_tv(list_T *l, typval_T *tv)
584{
585 listitem_T *li = listitem_alloc();
586
587 if (li == NULL)
588 return FAIL;
589 copy_tv(tv, &li->li_tv);
590 list_append(l, li);
591 return OK;
592}
593
594/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595 * As list_append_tv() but move the value instead of copying it.
596 * Return FAIL when out of memory.
597 */
598 int
599list_append_tv_move(list_T *l, typval_T *tv)
600{
601 listitem_T *li = listitem_alloc();
602
603 if (li == NULL)
604 return FAIL;
605 li->li_tv = *tv;
606 list_append(l, li);
607 return OK;
608}
609
610/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200611 * Add a dictionary to a list. Used by getqflist().
612 * Return FAIL when out of memory.
613 */
614 int
615list_append_dict(list_T *list, dict_T *dict)
616{
617 listitem_T *li = listitem_alloc();
618
619 if (li == NULL)
620 return FAIL;
621 li->li_tv.v_type = VAR_DICT;
622 li->li_tv.v_lock = 0;
623 li->li_tv.vval.v_dict = dict;
624 list_append(list, li);
625 ++dict->dv_refcount;
626 return OK;
627}
628
629/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100630 * Append list2 to list1.
631 * Return FAIL when out of memory.
632 */
633 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200634list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100635{
636 listitem_T *li = listitem_alloc();
637
638 if (li == NULL)
639 return FAIL;
640 li->li_tv.v_type = VAR_LIST;
641 li->li_tv.v_lock = 0;
642 li->li_tv.vval.v_list = list2;
643 list_append(list1, li);
644 ++list2->lv_refcount;
645 return OK;
646}
647
648/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200649 * Make a copy of "str" and append it as an item to list "l".
650 * When "len" >= 0 use "str[len]".
651 * Returns FAIL when out of memory.
652 */
653 int
654list_append_string(list_T *l, char_u *str, int len)
655{
656 listitem_T *li = listitem_alloc();
657
658 if (li == NULL)
659 return FAIL;
660 list_append(l, li);
661 li->li_tv.v_type = VAR_STRING;
662 li->li_tv.v_lock = 0;
663 if (str == NULL)
664 li->li_tv.vval.v_string = NULL;
665 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
666 : vim_strsave(str))) == NULL)
667 return FAIL;
668 return OK;
669}
670
671/*
672 * Append "n" to list "l".
673 * Returns FAIL when out of memory.
674 */
675 int
676list_append_number(list_T *l, varnumber_T n)
677{
678 listitem_T *li;
679
680 li = listitem_alloc();
681 if (li == NULL)
682 return FAIL;
683 li->li_tv.v_type = VAR_NUMBER;
684 li->li_tv.v_lock = 0;
685 li->li_tv.vval.v_number = n;
686 list_append(l, li);
687 return OK;
688}
689
690/*
691 * Insert typval_T "tv" in list "l" before "item".
692 * If "item" is NULL append at the end.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100693 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200694 */
695 int
696list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
697{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100698 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200699
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100700 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
701 && check_typval_type(l->lv_type->tt_member, tv, 0) == FAIL)
702 return FAIL;
703 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200704 if (ni == NULL)
705 return FAIL;
706 copy_tv(tv, &ni->li_tv);
707 list_insert(l, ni, item);
708 return OK;
709}
710
711 void
712list_insert(list_T *l, listitem_T *ni, listitem_T *item)
713{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200714 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200715 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100716 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200717 list_append(l, ni);
718 else
719 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100720 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200721 ni->li_prev = item->li_prev;
722 ni->li_next = item;
723 if (item->li_prev == NULL)
724 {
725 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100726 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200727 }
728 else
729 {
730 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100731 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200732 }
733 item->li_prev = ni;
734 ++l->lv_len;
735 }
736}
737
738/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200739 * Flatten "list" to depth "maxdepth".
740 * It does nothing if "maxdepth" is 0.
741 * Returns FAIL when out of memory.
742 */
743 static int
744list_flatten(list_T *list, long maxdepth)
745{
746 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200747 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200748 int n;
749
750 if (maxdepth == 0)
751 return OK;
752 CHECK_LIST_MATERIALIZE(list);
753
754 n = 0;
755 item = list->lv_first;
756 while (item != NULL)
757 {
758 fast_breakcheck();
759 if (got_int)
760 return FAIL;
761
762 if (item->li_tv.v_type == VAR_LIST)
763 {
764 listitem_T *next = item->li_next;
765
766 vimlist_remove(list, item, item);
767 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
768 return FAIL;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200769 clear_tv(&item->li_tv);
770 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200771
772 if (item->li_prev == NULL)
773 item = list->lv_first;
774 else
775 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200776 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200777
778 if (++n >= maxdepth)
779 {
780 n = 0;
781 item = next;
782 }
783 }
784 else
785 {
786 n = 0;
787 item = item->li_next;
788 }
789 }
790
791 return OK;
792}
793
794/*
795 * "flatten(list[, {maxdepth}])" function
796 */
797 void
798f_flatten(typval_T *argvars, typval_T *rettv)
799{
800 list_T *l;
801 long maxdepth;
802 int error = FALSE;
803
804 if (argvars[0].v_type != VAR_LIST)
805 {
806 semsg(_(e_listarg), "flatten()");
807 return;
808 }
809
810 if (argvars[1].v_type == VAR_UNKNOWN)
811 maxdepth = 999999;
812 else
813 {
814 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
815 if (error)
816 return;
817 if (maxdepth < 0)
818 {
819 emsg(_("E900: maxdepth must be non-negative number"));
820 return;
821 }
822 }
823
824 l = argvars[0].vval.v_list;
Bram Moolenaara187c432020-09-16 21:08:28 +0200825 if (l != NULL && !value_check_lock(l->lv_lock,
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200826 (char_u *)N_("flatten() argument"), TRUE)
827 && list_flatten(l, maxdepth) == OK)
828 copy_tv(&argvars[0], rettv);
829}
830
831/*
Bram Moolenaar1a739232020-10-10 15:37:58 +0200832 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200833 * If "bef" is NULL append at the end, otherwise insert before this item.
834 * Returns FAIL when out of memory.
835 */
836 int
837list_extend(list_T *l1, list_T *l2, listitem_T *bef)
838{
839 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200840 int todo;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200841
Bram Moolenaar1a739232020-10-10 15:37:58 +0200842 // NULL list is equivalent to an empty list: nothing to do.
843 if (l2 == NULL || l2->lv_len == 0)
844 return OK;
845
846 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200847 CHECK_LIST_MATERIALIZE(l1);
848 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100850 // We also quit the loop when we have inserted the original item count of
851 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200852 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
853 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
854 return FAIL;
855 return OK;
856}
857
858/*
859 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
860 * Return FAIL when out of memory.
861 */
862 int
863list_concat(list_T *l1, list_T *l2, typval_T *tv)
864{
865 list_T *l;
866
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100867 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +0200868 if (l1 == NULL)
869 l = list_alloc();
870 else
871 l = list_copy(l1, FALSE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200872 if (l == NULL)
873 return FAIL;
874 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +0100875 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200876 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200877 if (l1 == NULL)
878 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200879
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100880 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200881 return list_extend(l, l2, NULL);
882}
883
Bram Moolenaar9af78762020-06-16 11:34:42 +0200884 list_T *
885list_slice(list_T *ol, long n1, long n2)
886{
887 listitem_T *item;
888 list_T *l = list_alloc();
889
890 if (l == NULL)
891 return NULL;
892 for (item = list_find(ol, n1); n1 <= n2; ++n1)
893 {
894 if (list_append_tv(l, &item->li_tv) == FAIL)
895 {
896 list_free(l);
897 return NULL;
898 }
899 item = item->li_next;
900 }
901 return l;
902}
903
Bram Moolenaared591872020-08-15 22:14:53 +0200904 int
905list_slice_or_index(
906 list_T *list,
907 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +0100908 varnumber_T n1_arg,
909 varnumber_T n2_arg,
910 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +0200911 typval_T *rettv,
912 int verbose)
913{
914 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +0100915 varnumber_T n1 = n1_arg;
916 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +0200917 typval_T var1;
918
919 if (n1 < 0)
920 n1 = len + n1;
921 if (n1 < 0 || n1 >= len)
922 {
923 // For a range we allow invalid values and return an empty
924 // list. A list index out of range is an error.
925 if (!range)
926 {
927 if (verbose)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100928 semsg(_(e_listidx), n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +0200929 return FAIL;
930 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200931 n1 = n1 < 0 ? 0 : len;
Bram Moolenaared591872020-08-15 22:14:53 +0200932 }
933 if (range)
934 {
935 list_T *l;
936
937 if (n2 < 0)
938 n2 = len + n2;
939 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +0100940 n2 = len - (exclusive ? 0 : 1);
941 if (exclusive)
942 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +0200943 if (n2 < 0 || n2 + 1 < n1)
944 n2 = -1;
945 l = list_slice(list, n1, n2);
946 if (l == NULL)
947 return FAIL;
948 clear_tv(rettv);
949 rettv_list_set(rettv, l);
950 }
951 else
952 {
953 // copy the item to "var1" to avoid that freeing the list makes it
954 // invalid.
955 copy_tv(&list_find(list, n1)->li_tv, &var1);
956 clear_tv(rettv);
957 *rettv = var1;
958 }
959 return OK;
960}
961
Bram Moolenaarda861d62016-07-17 15:46:27 +0200962/*
963 * Make a copy of list "orig". Shallow if "deep" is FALSE.
964 * The refcount of the new list is set to 1.
965 * See item_copy() for "copyID".
966 * Returns NULL when out of memory.
967 */
968 list_T *
969list_copy(list_T *orig, int deep, int copyID)
970{
971 list_T *copy;
972 listitem_T *item;
973 listitem_T *ni;
974
975 if (orig == NULL)
976 return NULL;
977
978 copy = list_alloc();
979 if (copy != NULL)
980 {
981 if (copyID != 0)
982 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100983 // Do this before adding the items, because one of the items may
984 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200985 orig->lv_copyID = copyID;
986 orig->lv_copylist = copy;
987 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200988 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200989 for (item = orig->lv_first; item != NULL && !got_int;
990 item = item->li_next)
991 {
992 ni = listitem_alloc();
993 if (ni == NULL)
994 break;
995 if (deep)
996 {
997 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
998 {
999 vim_free(ni);
1000 break;
1001 }
1002 }
1003 else
1004 copy_tv(&item->li_tv, &ni->li_tv);
1005 list_append(copy, ni);
1006 }
1007 ++copy->lv_refcount;
1008 if (item != NULL)
1009 {
1010 list_unref(copy);
1011 copy = NULL;
1012 }
1013 }
1014
1015 return copy;
1016}
1017
1018/*
1019 * Remove items "item" to "item2" from list "l".
1020 * Does not free the listitem or the value!
1021 * This used to be called list_remove, but that conflicts with a Sun header
1022 * file.
1023 */
1024 void
1025vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1026{
1027 listitem_T *ip;
1028
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001029 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001031 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001032 for (ip = item; ip != NULL; ip = ip->li_next)
1033 {
1034 --l->lv_len;
1035 list_fix_watch(l, ip);
1036 if (ip == item2)
1037 break;
1038 }
1039
1040 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001041 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001042 else
1043 item2->li_next->li_prev = item->li_prev;
1044 if (item->li_prev == NULL)
1045 l->lv_first = item2->li_next;
1046 else
1047 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001048 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001049}
1050
1051/*
1052 * Return an allocated string with the string representation of a list.
1053 * May return NULL.
1054 */
1055 char_u *
1056list2string(typval_T *tv, int copyID, int restore_copyID)
1057{
1058 garray_T ga;
1059
1060 if (tv->vval.v_list == NULL)
1061 return NULL;
1062 ga_init2(&ga, (int)sizeof(char), 80);
1063 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001064 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001065 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1066 FALSE, restore_copyID, copyID) == FAIL)
1067 {
1068 vim_free(ga.ga_data);
1069 return NULL;
1070 }
1071 ga_append(&ga, ']');
1072 ga_append(&ga, NUL);
1073 return (char_u *)ga.ga_data;
1074}
1075
1076typedef struct join_S {
1077 char_u *s;
1078 char_u *tofree;
1079} join_T;
1080
1081 static int
1082list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001083 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001084 list_T *l,
1085 char_u *sep,
1086 int echo_style,
1087 int restore_copyID,
1088 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001089 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001090{
1091 int i;
1092 join_T *p;
1093 int len;
1094 int sumlen = 0;
1095 int first = TRUE;
1096 char_u *tofree;
1097 char_u numbuf[NUMBUFLEN];
1098 listitem_T *item;
1099 char_u *s;
1100
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001101 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001102 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001103 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1104 {
1105 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001106 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001107 if (s == NULL)
1108 return FAIL;
1109
1110 len = (int)STRLEN(s);
1111 sumlen += len;
1112
1113 (void)ga_grow(join_gap, 1);
1114 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1115 if (tofree != NULL || s != numbuf)
1116 {
1117 p->s = s;
1118 p->tofree = tofree;
1119 }
1120 else
1121 {
1122 p->s = vim_strnsave(s, len);
1123 p->tofree = p->s;
1124 }
1125
1126 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001127 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001128 break;
1129 }
1130
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001131 // Allocate result buffer with its total size, avoid re-allocation and
1132 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001133 if (join_gap->ga_len >= 2)
1134 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1135 if (ga_grow(gap, sumlen + 2) == FAIL)
1136 return FAIL;
1137
1138 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1139 {
1140 if (first)
1141 first = FALSE;
1142 else
1143 ga_concat(gap, sep);
1144 p = ((join_T *)join_gap->ga_data) + i;
1145
1146 if (p->s != NULL)
1147 ga_concat(gap, p->s);
1148 line_breakcheck();
1149 }
1150
1151 return OK;
1152}
1153
1154/*
1155 * Join list "l" into a string in "*gap", using separator "sep".
1156 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1157 * Return FAIL or OK.
1158 */
1159 int
1160list_join(
1161 garray_T *gap,
1162 list_T *l,
1163 char_u *sep,
1164 int echo_style,
1165 int restore_copyID,
1166 int copyID)
1167{
1168 garray_T join_ga;
1169 int retval;
1170 join_T *p;
1171 int i;
1172
1173 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001174 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001175 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1176 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1177 copyID, &join_ga);
1178
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001179 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001180 if (join_ga.ga_data != NULL)
1181 {
1182 p = (join_T *)join_ga.ga_data;
1183 for (i = 0; i < join_ga.ga_len; ++i)
1184 {
1185 vim_free(p->tofree);
1186 ++p;
1187 }
1188 ga_clear(&join_ga);
1189 }
1190
1191 return retval;
1192}
1193
1194/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001195 * "join()" function
1196 */
1197 void
1198f_join(typval_T *argvars, typval_T *rettv)
1199{
1200 garray_T ga;
1201 char_u *sep;
1202
1203 if (argvars[0].v_type != VAR_LIST)
1204 {
1205 emsg(_(e_listreq));
1206 return;
1207 }
1208 if (argvars[0].vval.v_list == NULL)
1209 return;
1210 if (argvars[1].v_type == VAR_UNKNOWN)
1211 sep = (char_u *)" ";
1212 else
1213 sep = tv_get_string_chk(&argvars[1]);
1214
1215 rettv->v_type = VAR_STRING;
1216
1217 if (sep != NULL)
1218 {
1219 ga_init2(&ga, (int)sizeof(char), 80);
1220 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1221 ga_append(&ga, NUL);
1222 rettv->vval.v_string = (char_u *)ga.ga_data;
1223 }
1224 else
1225 rettv->vval.v_string = NULL;
1226}
1227
1228/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001229 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001230 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001231 * Return OK or FAIL.
1232 */
1233 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001234eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001235{
Bram Moolenaar71478202020-06-26 22:46:27 +02001236 int evaluate = evalarg == NULL ? FALSE
1237 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001238 list_T *l = NULL;
1239 typval_T tv;
1240 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001241 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001242 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001243
1244 if (evaluate)
1245 {
1246 l = list_alloc();
1247 if (l == NULL)
1248 return FAIL;
1249 }
1250
Bram Moolenaar962d7212020-07-04 14:15:00 +02001251 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001252 while (**arg != ']' && **arg != NUL)
1253 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001254 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001255 goto failret;
1256 if (evaluate)
1257 {
1258 item = listitem_alloc();
1259 if (item != NULL)
1260 {
1261 item->li_tv = tv;
1262 item->li_tv.v_lock = 0;
1263 list_append(l, item);
1264 }
1265 else
1266 clear_tv(&tv);
1267 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001268 // Legacy Vim script allowed a space before the comma.
1269 if (!vim9script)
1270 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001271
Bram Moolenaare6e03172020-06-27 16:36:05 +02001272 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001273 had_comma = **arg == ',';
1274 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001275 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001276 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001277 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001278 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaare6e03172020-06-27 16:36:05 +02001279 goto failret;
1280 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001281 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001282 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001283
Bram Moolenaare6b53242020-07-01 17:28:33 +02001284 // The "]" can be on the next line. But a double quoted string may
1285 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001286 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001287 if (**arg == ']')
1288 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001289
1290 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001291 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001293 {
1294 if (**arg == ',')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001295 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02001296 else
1297 semsg(_("E696: Missing comma in List: %s"), *arg);
1298 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001299 goto failret;
1300 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001301 }
1302
1303 if (**arg != ']')
1304 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001306 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001307failret:
1308 if (evaluate)
1309 list_free(l);
1310 return FAIL;
1311 }
1312
Bram Moolenaar9d489562020-07-30 20:08:50 +02001313 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001314 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001315 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001316
1317 return OK;
1318}
1319
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001320/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001321 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001322 */
1323 int
1324write_list(FILE *fd, list_T *list, int binary)
1325{
1326 listitem_T *li;
1327 int c;
1328 int ret = OK;
1329 char_u *s;
1330
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001331 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001332 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001333 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001334 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001335 {
1336 if (*s == '\n')
1337 c = putc(NUL, fd);
1338 else
1339 c = putc(*s, fd);
1340 if (c == EOF)
1341 {
1342 ret = FAIL;
1343 break;
1344 }
1345 }
1346 if (!binary || li->li_next != NULL)
1347 if (putc('\n', fd) == EOF)
1348 {
1349 ret = FAIL;
1350 break;
1351 }
1352 if (ret == FAIL)
1353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001354 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001355 break;
1356 }
1357 }
1358 return ret;
1359}
1360
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001361/*
1362 * Initialize a static list with 10 items.
1363 */
1364 void
1365init_static_list(staticList10_T *sl)
1366{
1367 list_T *l = &sl->sl_list;
1368 int i;
1369
1370 memset(sl, 0, sizeof(staticList10_T));
1371 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001372 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001373 l->lv_refcount = DO_NOT_FREE_CNT;
1374 l->lv_lock = VAR_FIXED;
1375 sl->sl_list.lv_len = 10;
1376
1377 for (i = 0; i < 10; ++i)
1378 {
1379 listitem_T *li = &sl->sl_items[i];
1380
1381 if (i == 0)
1382 li->li_prev = NULL;
1383 else
1384 li->li_prev = li - 1;
1385 if (i == 9)
1386 li->li_next = NULL;
1387 else
1388 li->li_next = li + 1;
1389 }
1390}
1391
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001392/*
1393 * "list2str()" function
1394 */
1395 void
1396f_list2str(typval_T *argvars, typval_T *rettv)
1397{
1398 list_T *l;
1399 listitem_T *li;
1400 garray_T ga;
1401 int utf8 = FALSE;
1402
1403 rettv->v_type = VAR_STRING;
1404 rettv->vval.v_string = NULL;
1405 if (argvars[0].v_type != VAR_LIST)
1406 {
1407 emsg(_(e_invarg));
1408 return;
1409 }
1410
1411 l = argvars[0].vval.v_list;
1412 if (l == NULL)
1413 return; // empty list results in empty string
1414
1415 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001416 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001417
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001418 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001419 ga_init2(&ga, 1, 80);
1420 if (has_mbyte || utf8)
1421 {
1422 char_u buf[MB_MAXBYTES + 1];
1423 int (*char2bytes)(int, char_u *);
1424
1425 if (utf8 || enc_utf8)
1426 char2bytes = utf_char2bytes;
1427 else
1428 char2bytes = mb_char2bytes;
1429
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001430 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001431 {
1432 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1433 ga_concat(&ga, buf);
1434 }
1435 ga_append(&ga, NUL);
1436 }
1437 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1438 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001439 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001440 ga_append(&ga, tv_get_number(&li->li_tv));
1441 ga_append(&ga, NUL);
1442 }
1443
1444 rettv->v_type = VAR_STRING;
1445 rettv->vval.v_string = ga.ga_data;
1446}
1447
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001448 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001449list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1450{
1451 list_T *l;
1452 listitem_T *item, *item2;
1453 listitem_T *li;
1454 int error = FALSE;
1455 int idx;
1456
1457 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001458 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001459 return;
1460
1461 idx = (long)tv_get_number_chk(&argvars[1], &error);
1462 if (error)
1463 ; // type error: do nothing, errmsg already given
1464 else if ((item = list_find(l, idx)) == NULL)
1465 semsg(_(e_listidx), idx);
1466 else
1467 {
1468 if (argvars[2].v_type == VAR_UNKNOWN)
1469 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001470 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001471 vimlist_remove(l, item, item);
1472 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001474 }
1475 else
1476 {
1477 // Remove range of items, return list with values.
1478 int end = (long)tv_get_number_chk(&argvars[2], &error);
1479
1480 if (error)
1481 ; // type error: do nothing
1482 else if ((item2 = list_find(l, end)) == NULL)
1483 semsg(_(e_listidx), end);
1484 else
1485 {
1486 int cnt = 0;
1487
1488 for (li = item; li != NULL; li = li->li_next)
1489 {
1490 ++cnt;
1491 if (li == item2)
1492 break;
1493 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001494 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001495 emsg(_(e_invrange));
1496 else
1497 {
1498 vimlist_remove(l, item, item2);
1499 if (rettv_list_alloc(rettv) == OK)
1500 {
1501 l = rettv->vval.v_list;
1502 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001503 l->lv_u.mat.lv_last = item2;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001504 item->li_prev = NULL;
1505 item2->li_next = NULL;
1506 l->lv_len = cnt;
1507 }
1508 }
1509 }
1510 }
1511 }
1512}
1513
1514static int item_compare(const void *s1, const void *s2);
1515static int item_compare2(const void *s1, const void *s2);
1516
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001517// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001518typedef struct
1519{
1520 listitem_T *item;
1521 int idx;
1522} sortItem_T;
1523
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001524// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001525typedef struct
1526{
1527 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001528 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001529 int item_compare_numeric;
1530 int item_compare_numbers;
1531#ifdef FEAT_FLOAT
1532 int item_compare_float;
1533#endif
1534 char_u *item_compare_func;
1535 partial_T *item_compare_partial;
1536 dict_T *item_compare_selfdict;
1537 int item_compare_func_err;
1538 int item_compare_keep_zero;
1539} sortinfo_T;
1540static sortinfo_T *sortinfo = NULL;
1541#define ITEM_COMPARE_FAIL 999
1542
1543/*
1544 * Compare functions for f_sort() and f_uniq() below.
1545 */
1546 static int
1547item_compare(const void *s1, const void *s2)
1548{
1549 sortItem_T *si1, *si2;
1550 typval_T *tv1, *tv2;
1551 char_u *p1, *p2;
1552 char_u *tofree1 = NULL, *tofree2 = NULL;
1553 int res;
1554 char_u numbuf1[NUMBUFLEN];
1555 char_u numbuf2[NUMBUFLEN];
1556
1557 si1 = (sortItem_T *)s1;
1558 si2 = (sortItem_T *)s2;
1559 tv1 = &si1->item->li_tv;
1560 tv2 = &si2->item->li_tv;
1561
1562 if (sortinfo->item_compare_numbers)
1563 {
1564 varnumber_T v1 = tv_get_number(tv1);
1565 varnumber_T v2 = tv_get_number(tv2);
1566
1567 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1568 }
1569
1570#ifdef FEAT_FLOAT
1571 if (sortinfo->item_compare_float)
1572 {
1573 float_T v1 = tv_get_float(tv1);
1574 float_T v2 = tv_get_float(tv2);
1575
1576 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1577 }
1578#endif
1579
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001580 // tv2string() puts quotes around a string and allocates memory. Don't do
1581 // that for string variables. Use a single quote when comparing with a
1582 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001583 if (tv1->v_type == VAR_STRING)
1584 {
1585 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1586 p1 = (char_u *)"'";
1587 else
1588 p1 = tv1->vval.v_string;
1589 }
1590 else
1591 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1592 if (tv2->v_type == VAR_STRING)
1593 {
1594 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1595 p2 = (char_u *)"'";
1596 else
1597 p2 = tv2->vval.v_string;
1598 }
1599 else
1600 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1601 if (p1 == NULL)
1602 p1 = (char_u *)"";
1603 if (p2 == NULL)
1604 p2 = (char_u *)"";
1605 if (!sortinfo->item_compare_numeric)
1606 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001607 if (sortinfo->item_compare_lc)
1608 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001609 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001610 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001611 }
1612 else
1613 {
1614 double n1, n2;
1615 n1 = strtod((char *)p1, (char **)&p1);
1616 n2 = strtod((char *)p2, (char **)&p2);
1617 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1618 }
1619
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001620 // When the result would be zero, compare the item indexes. Makes the
1621 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001622 if (res == 0 && !sortinfo->item_compare_keep_zero)
1623 res = si1->idx > si2->idx ? 1 : -1;
1624
1625 vim_free(tofree1);
1626 vim_free(tofree2);
1627 return res;
1628}
1629
1630 static int
1631item_compare2(const void *s1, const void *s2)
1632{
1633 sortItem_T *si1, *si2;
1634 int res;
1635 typval_T rettv;
1636 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001637 char_u *func_name;
1638 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001639 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001640
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001641 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001642 if (sortinfo->item_compare_func_err)
1643 return 0;
1644
1645 si1 = (sortItem_T *)s1;
1646 si2 = (sortItem_T *)s2;
1647
1648 if (partial == NULL)
1649 func_name = sortinfo->item_compare_func;
1650 else
1651 func_name = partial_name(partial);
1652
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001653 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1654 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001655 copy_tv(&si1->item->li_tv, &argv[0]);
1656 copy_tv(&si2->item->li_tv, &argv[1]);
1657
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001658 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001659 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001660 funcexe.evaluate = TRUE;
1661 funcexe.partial = partial;
1662 funcexe.selfdict = sortinfo->item_compare_selfdict;
1663 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001664 clear_tv(&argv[0]);
1665 clear_tv(&argv[1]);
1666
1667 if (res == FAIL)
1668 res = ITEM_COMPARE_FAIL;
1669 else
1670 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1671 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001672 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001673 clear_tv(&rettv);
1674
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001675 // When the result would be zero, compare the pointers themselves. Makes
1676 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001677 if (res == 0 && !sortinfo->item_compare_keep_zero)
1678 res = si1->idx > si2->idx ? 1 : -1;
1679
1680 return res;
1681}
1682
1683/*
1684 * "sort()" or "uniq()" function
1685 */
1686 static void
1687do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1688{
1689 list_T *l;
1690 listitem_T *li;
1691 sortItem_T *ptrs;
1692 sortinfo_T *old_sortinfo;
1693 sortinfo_T info;
1694 long len;
1695 long i;
1696
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001697 // Pointer to current info struct used in compare function. Save and
1698 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001699 old_sortinfo = sortinfo;
1700 sortinfo = &info;
1701
1702 if (argvars[0].v_type != VAR_LIST)
1703 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1704 else
1705 {
1706 l = argvars[0].vval.v_list;
Bram Moolenaara187c432020-09-16 21:08:28 +02001707 if (l == NULL || value_check_lock(l->lv_lock,
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001708 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1709 TRUE))
1710 goto theend;
1711 rettv_list_set(rettv, l);
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001712 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001713
1714 len = list_len(l);
1715 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001716 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001717
1718 info.item_compare_ic = FALSE;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001719 info.item_compare_lc = FALSE;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001720 info.item_compare_numeric = FALSE;
1721 info.item_compare_numbers = FALSE;
1722#ifdef FEAT_FLOAT
1723 info.item_compare_float = FALSE;
1724#endif
1725 info.item_compare_func = NULL;
1726 info.item_compare_partial = NULL;
1727 info.item_compare_selfdict = NULL;
1728 if (argvars[1].v_type != VAR_UNKNOWN)
1729 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001730 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001731 if (argvars[1].v_type == VAR_FUNC)
1732 info.item_compare_func = argvars[1].vval.v_string;
1733 else if (argvars[1].v_type == VAR_PARTIAL)
1734 info.item_compare_partial = argvars[1].vval.v_partial;
1735 else
1736 {
1737 int error = FALSE;
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001738 int nr = 0;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001739
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001740 if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001741 {
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001742 nr = tv_get_number_chk(&argvars[1], &error);
1743 if (error)
1744 goto theend; // type error; errmsg already given
1745 if (nr == 1)
1746 info.item_compare_ic = TRUE;
1747 }
1748 if (nr != 1)
1749 {
1750 if (argvars[1].v_type != VAR_NUMBER)
1751 info.item_compare_func = tv_get_string(&argvars[1]);
1752 else if (nr != 0)
1753 {
1754 emsg(_(e_invarg));
1755 goto theend;
1756 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001757 }
1758 if (info.item_compare_func != NULL)
1759 {
1760 if (*info.item_compare_func == NUL)
1761 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001762 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001763 info.item_compare_func = NULL;
1764 }
1765 else if (STRCMP(info.item_compare_func, "n") == 0)
1766 {
1767 info.item_compare_func = NULL;
1768 info.item_compare_numeric = TRUE;
1769 }
1770 else if (STRCMP(info.item_compare_func, "N") == 0)
1771 {
1772 info.item_compare_func = NULL;
1773 info.item_compare_numbers = TRUE;
1774 }
1775#ifdef FEAT_FLOAT
1776 else if (STRCMP(info.item_compare_func, "f") == 0)
1777 {
1778 info.item_compare_func = NULL;
1779 info.item_compare_float = TRUE;
1780 }
1781#endif
1782 else if (STRCMP(info.item_compare_func, "i") == 0)
1783 {
1784 info.item_compare_func = NULL;
1785 info.item_compare_ic = TRUE;
1786 }
Bram Moolenaar55e29612020-11-01 13:57:44 +01001787 else if (STRCMP(info.item_compare_func, "l") == 0)
1788 {
1789 info.item_compare_func = NULL;
1790 info.item_compare_lc = TRUE;
1791 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001792 }
1793 }
1794
1795 if (argvars[2].v_type != VAR_UNKNOWN)
1796 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001797 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001798 if (argvars[2].v_type != VAR_DICT)
1799 {
1800 emsg(_(e_dictreq));
1801 goto theend;
1802 }
1803 info.item_compare_selfdict = argvars[2].vval.v_dict;
1804 }
1805 }
1806
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001807 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001808 ptrs = ALLOC_MULT(sortItem_T, len);
1809 if (ptrs == NULL)
1810 goto theend;
1811
1812 i = 0;
1813 if (sort)
1814 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001815 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001816 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001817 {
1818 ptrs[i].item = li;
1819 ptrs[i].idx = i;
1820 ++i;
1821 }
1822
1823 info.item_compare_func_err = FALSE;
1824 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001825 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001826 if ((info.item_compare_func != NULL
1827 || info.item_compare_partial != NULL)
1828 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
1829 == ITEM_COMPARE_FAIL)
1830 emsg(_("E702: Sort compare function failed"));
1831 else
1832 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001833 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001834 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
1835 info.item_compare_func == NULL
1836 && info.item_compare_partial == NULL
1837 ? item_compare : item_compare2);
1838
1839 if (!info.item_compare_func_err)
1840 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001841 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001842 l->lv_first = l->lv_u.mat.lv_last
1843 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001844 l->lv_len = 0;
1845 for (i = 0; i < len; ++i)
1846 list_append(l, ptrs[i].item);
1847 }
1848 }
1849 }
1850 else
1851 {
1852 int (*item_compare_func_ptr)(const void *, const void *);
1853
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001854 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001855 info.item_compare_func_err = FALSE;
1856 info.item_compare_keep_zero = TRUE;
1857 item_compare_func_ptr = info.item_compare_func != NULL
1858 || info.item_compare_partial != NULL
1859 ? item_compare2 : item_compare;
1860
1861 for (li = l->lv_first; li != NULL && li->li_next != NULL;
1862 li = li->li_next)
1863 {
1864 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
1865 == 0)
1866 ptrs[i++].item = li;
1867 if (info.item_compare_func_err)
1868 {
1869 emsg(_("E882: Uniq compare function failed"));
1870 break;
1871 }
1872 }
1873
1874 if (!info.item_compare_func_err)
1875 {
1876 while (--i >= 0)
1877 {
1878 li = ptrs[i].item->li_next;
1879 ptrs[i].item->li_next = li->li_next;
1880 if (li->li_next != NULL)
1881 li->li_next->li_prev = ptrs[i].item;
1882 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001883 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001884 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001886 l->lv_len--;
1887 }
1888 }
1889 }
1890
1891 vim_free(ptrs);
1892 }
1893theend:
1894 sortinfo = old_sortinfo;
1895}
1896
1897/*
1898 * "sort({list})" function
1899 */
1900 void
1901f_sort(typval_T *argvars, typval_T *rettv)
1902{
1903 do_sort_uniq(argvars, rettv, TRUE);
1904}
1905
1906/*
1907 * "uniq({list})" function
1908 */
1909 void
1910f_uniq(typval_T *argvars, typval_T *rettv)
1911{
1912 do_sort_uniq(argvars, rettv, FALSE);
1913}
1914
Bram Moolenaarea696852020-11-09 18:31:39 +01001915typedef enum {
1916 FILTERMAP_FILTER,
1917 FILTERMAP_MAP,
1918 FILTERMAP_MAPNEW
1919} filtermap_T;
1920
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001921/*
1922 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01001923 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001924 */
1925 static int
Bram Moolenaarea696852020-11-09 18:31:39 +01001926filter_map_one(
1927 typval_T *tv, // original value
1928 typval_T *expr, // callback
1929 filtermap_T filtermap,
1930 typval_T *newtv, // for map() and mapnew(): new value
1931 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001932{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001933 typval_T argv[3];
1934 int retval = FAIL;
1935
1936 copy_tv(tv, get_vim_var_tv(VV_VAL));
1937 argv[0] = *get_vim_var_tv(VV_KEY);
1938 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01001939 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001940 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01001941 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001942 {
1943 int error = FALSE;
1944
1945 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02001946 if (in_vim9script())
Bram Moolenaarea696852020-11-09 18:31:39 +01001947 *remp = !tv2bool(newtv);
Bram Moolenaar56acb092020-08-16 14:48:19 +02001948 else
Bram Moolenaarea696852020-11-09 18:31:39 +01001949 *remp = (tv_get_number_chk(newtv, &error) == 0);
1950 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001951 // On type error, nothing has been removed; return FAIL to stop the
1952 // loop. The error message was given by tv_get_number_chk().
1953 if (error)
1954 goto theend;
1955 }
1956 retval = OK;
1957theend:
1958 clear_tv(get_vim_var_tv(VV_VAL));
1959 return retval;
1960}
1961
1962/*
1963 * Implementation of map() and filter().
1964 */
1965 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01001966filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001967{
1968 typval_T *expr;
1969 listitem_T *li, *nli;
1970 list_T *l = NULL;
1971 dictitem_T *di;
1972 hashtab_T *ht;
1973 hashitem_T *hi;
1974 dict_T *d = NULL;
1975 blob_T *b = NULL;
1976 int rem;
1977 int todo;
Bram Moolenaarea696852020-11-09 18:31:39 +01001978 char_u *ermsg = (char_u *)(filtermap == FILTERMAP_MAP ? "map()"
1979 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
1980 : "filter()");
1981 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
1982 ? N_("map() argument")
1983 : filtermap == FILTERMAP_MAPNEW
1984 ? N_("mapnew() argument")
1985 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001986 int save_did_emsg;
1987 int idx = 0;
1988
Bram Moolenaarea696852020-11-09 18:31:39 +01001989 // map() and filter() return the first argument, also on failure.
1990 if (filtermap != FILTERMAP_MAPNEW)
1991 copy_tv(&argvars[0], rettv);
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02001992
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001993 if (argvars[0].v_type == VAR_BLOB)
1994 {
Bram Moolenaarea696852020-11-09 18:31:39 +01001995 if (filtermap == FILTERMAP_MAPNEW)
1996 {
1997 rettv->v_type = VAR_BLOB;
1998 rettv->vval.v_blob = NULL;
1999 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002000 if ((b = argvars[0].vval.v_blob) == NULL)
2001 return;
2002 }
2003 else if (argvars[0].v_type == VAR_LIST)
2004 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002005 if (filtermap == FILTERMAP_MAPNEW)
2006 {
2007 rettv->v_type = VAR_LIST;
2008 rettv->vval.v_list = NULL;
2009 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002010 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002011 || (filtermap == FILTERMAP_FILTER
2012 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002013 return;
2014 }
2015 else if (argvars[0].v_type == VAR_DICT)
2016 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002017 if (filtermap == FILTERMAP_MAPNEW)
2018 {
2019 rettv->v_type = VAR_DICT;
2020 rettv->vval.v_dict = NULL;
2021 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002022 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002023 || (filtermap == FILTERMAP_FILTER
2024 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002025 return;
2026 }
2027 else
2028 {
Bram Moolenaarfcb0b612020-05-26 20:22:01 +02002029 semsg(_(e_listdictblobarg), ermsg);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002030 return;
2031 }
2032
2033 expr = &argvars[1];
2034 // On type errors, the preceding call has already displayed an error
2035 // message. Avoid a misleading error message for an empty string that
2036 // was not passed as argument.
2037 if (expr->v_type != VAR_UNKNOWN)
2038 {
2039 typval_T save_val;
2040 typval_T save_key;
2041
2042 prepare_vimvar(VV_VAL, &save_val);
2043 prepare_vimvar(VV_KEY, &save_key);
2044
2045 // We reset "did_emsg" to be able to detect whether an error
2046 // occurred during evaluation of the expression.
2047 save_did_emsg = did_emsg;
2048 did_emsg = FALSE;
2049
2050 if (argvars[0].v_type == VAR_DICT)
2051 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002052 int prev_lock = d->dv_lock;
Bram Moolenaarea696852020-11-09 18:31:39 +01002053 dict_T *d_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002054
Bram Moolenaarea696852020-11-09 18:31:39 +01002055 if (filtermap == FILTERMAP_MAPNEW)
2056 {
2057 if (rettv_dict_alloc(rettv) == FAIL)
2058 return;
2059 d_ret = rettv->vval.v_dict;
2060 }
2061
2062 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002063 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002064 ht = &d->dv_hashtab;
2065 hash_lock(ht);
2066 todo = (int)ht->ht_used;
2067 for (hi = ht->ht_array; todo > 0; ++hi)
2068 {
2069 if (!HASHITEM_EMPTY(hi))
2070 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002071 int r;
2072 typval_T newtv;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002073
2074 --todo;
2075 di = HI2DI(hi);
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002076 if (filtermap == FILTERMAP_MAP
Bram Moolenaarea696852020-11-09 18:31:39 +01002077 && (value_check_lock(di->di_tv.v_lock,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002078 arg_errmsg, TRUE)
2079 || var_check_ro(di->di_flags,
2080 arg_errmsg, TRUE)))
2081 break;
2082 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaarea696852020-11-09 18:31:39 +01002083 r = filter_map_one(&di->di_tv, expr, filtermap,
2084 &newtv, &rem);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002085 clear_tv(get_vim_var_tv(VV_KEY));
2086 if (r == FAIL || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002087 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002088 clear_tv(&newtv);
2089 break;
2090 }
2091 if (filtermap == FILTERMAP_MAP)
2092 {
2093 // map(): replace the dict item value
2094 clear_tv(&di->di_tv);
2095 newtv.v_lock = 0;
2096 di->di_tv = newtv;
2097 }
2098 else if (filtermap == FILTERMAP_MAPNEW)
2099 {
2100 // mapnew(): add the item value to the new dict
2101 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
2102 clear_tv(&newtv);
2103 if (r == FAIL)
2104 break;
2105 }
2106 else if (filtermap == FILTERMAP_FILTER && rem)
2107 {
2108 // filter(false): remove the item from the dict
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002109 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2110 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2111 break;
2112 dictitem_remove(d, di);
2113 }
2114 }
2115 }
2116 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002117 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002118 }
2119 else if (argvars[0].v_type == VAR_BLOB)
2120 {
2121 int i;
2122 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002123 varnumber_T val;
Bram Moolenaarea696852020-11-09 18:31:39 +01002124 blob_T *b_ret = b;
2125
2126 if (filtermap == FILTERMAP_MAPNEW)
2127 {
2128 if (blob_copy(b, rettv) == FAIL)
2129 return;
2130 b_ret = rettv->vval.v_blob;
2131 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002132
2133 // set_vim_var_nr() doesn't set the type
2134 set_vim_var_type(VV_KEY, VAR_NUMBER);
2135
2136 for (i = 0; i < b->bv_ga.ga_len; i++)
2137 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002138 typval_T newtv;
2139
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002140 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002141 val = blob_get(b, i);
2142 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002143 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaarea696852020-11-09 18:31:39 +01002144 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
2145 || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002146 break;
Bram Moolenaarea696852020-11-09 18:31:39 +01002147 if (newtv.v_type != VAR_NUMBER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002148 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002149 clear_tv(&newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002150 emsg(_(e_invalblob));
2151 break;
2152 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002153 if (filtermap != FILTERMAP_FILTER)
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002154 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002155 if (newtv.vval.v_number != val)
2156 blob_set(b_ret, i, newtv.vval.v_number);
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002157 }
2158 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002159 {
2160 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2161
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002162 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002163 (size_t)b->bv_ga.ga_len - i - 1);
2164 --b->bv_ga.ga_len;
2165 --i;
2166 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002167 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002168 }
2169 }
2170 else // argvars[0].v_type == VAR_LIST
2171 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002172 int prev_lock = l->lv_lock;
2173 list_T *l_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002174
Bram Moolenaarea696852020-11-09 18:31:39 +01002175 if (filtermap == FILTERMAP_MAPNEW)
2176 {
2177 if (rettv_list_alloc(rettv) == FAIL)
2178 return;
2179 l_ret = rettv->vval.v_list;
2180 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002181 // set_vim_var_nr() doesn't set the type
2182 set_vim_var_type(VV_KEY, VAR_NUMBER);
2183
Bram Moolenaarea696852020-11-09 18:31:39 +01002184 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002185 l->lv_lock = VAR_LOCKED;
Bram Moolenaarea696852020-11-09 18:31:39 +01002186
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002187 if (l->lv_first == &range_list_item)
2188 {
2189 varnumber_T val = l->lv_u.nonmat.lv_start;
2190 int len = l->lv_len;
2191 int stride = l->lv_u.nonmat.lv_stride;
2192
2193 // List from range(): loop over the numbers
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002194 if (filtermap != FILTERMAP_MAPNEW)
2195 {
2196 l->lv_first = NULL;
2197 l->lv_u.mat.lv_last = NULL;
2198 l->lv_len = 0;
2199 l->lv_u.mat.lv_idx_item = NULL;
2200 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002201
2202 for (idx = 0; idx < len; ++idx)
Bram Moolenaarc56936e2020-11-10 11:43:56 +01002203 {
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002204 typval_T tv;
2205 typval_T newtv;
2206
2207 tv.v_type = VAR_NUMBER;
2208 tv.v_lock = 0;
2209 tv.vval.v_number = val;
2210 set_vim_var_nr(VV_KEY, idx);
2211 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2212 == FAIL)
Bram Moolenaarea696852020-11-09 18:31:39 +01002213 break;
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002214 if (did_emsg)
2215 {
2216 clear_tv(&newtv);
2217 break;
2218 }
2219 if (filtermap != FILTERMAP_FILTER)
2220 {
2221 // map(), mapnew(): always append the new value to the
2222 // list
2223 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2224 ? l : l_ret, &newtv) == FAIL)
2225 break;
2226 }
2227 else if (!rem)
2228 {
2229 // filter(): append the list item value when not rem
2230 if (list_append_tv_move(l, &tv) == FAIL)
2231 break;
2232 }
2233
2234 val += stride;
Bram Moolenaarea696852020-11-09 18:31:39 +01002235 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002236 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002237 else
2238 {
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002239 // Materialized list: loop over the items
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002240 for (li = l->lv_first; li != NULL; li = nli)
2241 {
2242 typval_T newtv;
2243
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002244 if (filtermap == FILTERMAP_MAP && value_check_lock(
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002245 li->li_tv.v_lock, arg_errmsg, TRUE))
2246 break;
2247 nli = li->li_next;
2248 set_vim_var_nr(VV_KEY, idx);
2249 if (filter_map_one(&li->li_tv, expr, filtermap,
2250 &newtv, &rem) == FAIL)
2251 break;
2252 if (did_emsg)
2253 {
2254 clear_tv(&newtv);
2255 break;
2256 }
2257 if (filtermap == FILTERMAP_MAP)
2258 {
2259 // map(): replace the list item value
2260 clear_tv(&li->li_tv);
2261 newtv.v_lock = 0;
2262 li->li_tv = newtv;
2263 }
2264 else if (filtermap == FILTERMAP_MAPNEW)
2265 {
2266 // mapnew(): append the list item value
2267 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2268 break;
2269 }
2270 else if (filtermap == FILTERMAP_FILTER && rem)
2271 listitem_remove(l, li);
2272 ++idx;
2273 }
2274 }
2275
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002276 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002277 }
2278
2279 restore_vimvar(VV_KEY, &save_key);
2280 restore_vimvar(VV_VAL, &save_val);
2281
2282 did_emsg |= save_did_emsg;
2283 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002284}
2285
2286/*
2287 * "filter()" function
2288 */
2289 void
2290f_filter(typval_T *argvars, typval_T *rettv)
2291{
Bram Moolenaarea696852020-11-09 18:31:39 +01002292 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002293}
2294
2295/*
2296 * "map()" function
2297 */
2298 void
2299f_map(typval_T *argvars, typval_T *rettv)
2300{
Bram Moolenaarea696852020-11-09 18:31:39 +01002301 filter_map(argvars, rettv, FILTERMAP_MAP);
2302}
2303
2304/*
2305 * "mapnew()" function
2306 */
2307 void
2308f_mapnew(typval_T *argvars, typval_T *rettv)
2309{
2310 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002311}
2312
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002313/*
2314 * "add(list, item)" function
2315 */
2316 void
2317f_add(typval_T *argvars, typval_T *rettv)
2318{
2319 list_T *l;
2320 blob_T *b;
2321
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002322 rettv->vval.v_number = 1; // Default: Failed
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002323 if (argvars[0].v_type == VAR_LIST)
2324 {
2325 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002326 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002327 (char_u *)N_("add() argument"), TRUE)
2328 && list_append_tv(l, &argvars[1]) == OK)
2329 copy_tv(&argvars[0], rettv);
2330 }
2331 else if (argvars[0].v_type == VAR_BLOB)
2332 {
2333 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002334 && !value_check_lock(b->bv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002335 (char_u *)N_("add() argument"), TRUE))
2336 {
2337 int error = FALSE;
2338 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2339
2340 if (!error)
2341 {
2342 ga_append(&b->bv_ga, (int)n);
2343 copy_tv(&argvars[0], rettv);
2344 }
2345 }
2346 }
2347 else
2348 emsg(_(e_listblobreq));
2349}
2350
2351/*
2352 * "count()" function
2353 */
2354 void
2355f_count(typval_T *argvars, typval_T *rettv)
2356{
2357 long n = 0;
2358 int ic = FALSE;
2359 int error = FALSE;
2360
2361 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002362 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002363
2364 if (argvars[0].v_type == VAR_STRING)
2365 {
2366 char_u *expr = tv_get_string_chk(&argvars[1]);
2367 char_u *p = argvars[0].vval.v_string;
2368 char_u *next;
2369
2370 if (!error && expr != NULL && *expr != NUL && p != NULL)
2371 {
2372 if (ic)
2373 {
2374 size_t len = STRLEN(expr);
2375
2376 while (*p != NUL)
2377 {
2378 if (MB_STRNICMP(p, expr, len) == 0)
2379 {
2380 ++n;
2381 p += len;
2382 }
2383 else
2384 MB_PTR_ADV(p);
2385 }
2386 }
2387 else
2388 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2389 != NULL)
2390 {
2391 ++n;
2392 p = next + STRLEN(expr);
2393 }
2394 }
2395
2396 }
2397 else if (argvars[0].v_type == VAR_LIST)
2398 {
2399 listitem_T *li;
2400 list_T *l;
2401 long idx;
2402
2403 if ((l = argvars[0].vval.v_list) != NULL)
2404 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002405 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002406 li = l->lv_first;
2407 if (argvars[2].v_type != VAR_UNKNOWN)
2408 {
2409 if (argvars[3].v_type != VAR_UNKNOWN)
2410 {
2411 idx = (long)tv_get_number_chk(&argvars[3], &error);
2412 if (!error)
2413 {
2414 li = list_find(l, idx);
2415 if (li == NULL)
2416 semsg(_(e_listidx), idx);
2417 }
2418 }
2419 if (error)
2420 li = NULL;
2421 }
2422
2423 for ( ; li != NULL; li = li->li_next)
2424 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2425 ++n;
2426 }
2427 }
2428 else if (argvars[0].v_type == VAR_DICT)
2429 {
2430 int todo;
2431 dict_T *d;
2432 hashitem_T *hi;
2433
2434 if ((d = argvars[0].vval.v_dict) != NULL)
2435 {
2436 if (argvars[2].v_type != VAR_UNKNOWN)
2437 {
2438 if (argvars[3].v_type != VAR_UNKNOWN)
2439 emsg(_(e_invarg));
2440 }
2441
2442 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2443 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2444 {
2445 if (!HASHITEM_EMPTY(hi))
2446 {
2447 --todo;
2448 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2449 ++n;
2450 }
2451 }
2452 }
2453 }
2454 else
2455 semsg(_(e_listdictarg), "count()");
2456 rettv->vval.v_number = n;
2457}
2458
2459/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002460 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002461 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002462 static void
2463extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002464{
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002465 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2466 {
2467 list_T *l1, *l2;
2468 listitem_T *item;
2469 long before;
2470 int error = FALSE;
2471
2472 l1 = argvars[0].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002473 if (l1 == NULL)
2474 {
2475 emsg(_(e_cannot_extend_null_list));
2476 return;
2477 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002478 l2 = argvars[1].vval.v_list;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002479 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
2480 && l2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002481 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002482 if (is_new)
2483 {
2484 l1 = list_copy(l1, FALSE, get_copyID());
2485 if (l1 == NULL)
2486 return;
2487 }
2488
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002489 if (argvars[2].v_type != VAR_UNKNOWN)
2490 {
2491 before = (long)tv_get_number_chk(&argvars[2], &error);
2492 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002493 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002494
2495 if (before == l1->lv_len)
2496 item = NULL;
2497 else
2498 {
2499 item = list_find(l1, before);
2500 if (item == NULL)
2501 {
2502 semsg(_(e_listidx), before);
2503 return;
2504 }
2505 }
2506 }
2507 else
2508 item = NULL;
2509 list_extend(l1, l2, item);
2510
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002511 if (is_new)
2512 {
2513 rettv->v_type = VAR_LIST;
2514 rettv->vval.v_list = l1;
2515 rettv->v_lock = FALSE;
2516 }
2517 else
2518 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002519 }
2520 }
2521 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2522 {
2523 dict_T *d1, *d2;
2524 char_u *action;
2525 int i;
2526
2527 d1 = argvars[0].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002528 if (d1 == NULL)
2529 {
2530 emsg(_(e_cannot_extend_null_dict));
2531 return;
2532 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002533 d2 = argvars[1].vval.v_dict;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002534 if ((is_new || !value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
2535 && d2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002536 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002537 if (is_new)
2538 {
2539 d1 = dict_copy(d1, FALSE, get_copyID());
2540 if (d1 == NULL)
2541 return;
2542 }
2543
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002544 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002545 if (argvars[2].v_type != VAR_UNKNOWN)
2546 {
2547 static char *(av[]) = {"keep", "force", "error"};
2548
2549 action = tv_get_string_chk(&argvars[2]);
2550 if (action == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002551 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002552 for (i = 0; i < 3; ++i)
2553 if (STRCMP(action, av[i]) == 0)
2554 break;
2555 if (i == 3)
2556 {
2557 semsg(_(e_invarg2), action);
2558 return;
2559 }
2560 }
2561 else
2562 action = (char_u *)"force";
2563
2564 dict_extend(d1, d2, action);
2565
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002566 if (is_new)
2567 {
2568 rettv->v_type = VAR_DICT;
2569 rettv->vval.v_dict = d1;
2570 rettv->v_lock = FALSE;
2571 }
2572 else
2573 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002574 }
2575 }
2576 else
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002577 semsg(_(e_listdictarg), is_new ? "extendnew()" : "extend()");
2578}
2579
2580/*
2581 * "extend(list, list [, idx])" function
2582 * "extend(dict, dict [, action])" function
2583 */
2584 void
2585f_extend(typval_T *argvars, typval_T *rettv)
2586{
2587 char_u *errmsg = (char_u *)N_("extend() argument");
2588
2589 extend(argvars, rettv, errmsg, FALSE);
2590}
2591
2592/*
2593 * "extendnew(list, list [, idx])" function
2594 * "extendnew(dict, dict [, action])" function
2595 */
2596 void
2597f_extendnew(typval_T *argvars, typval_T *rettv)
2598{
2599 char_u *errmsg = (char_u *)N_("extendnew() argument");
2600
2601 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002602}
2603
2604/*
2605 * "insert()" function
2606 */
2607 void
2608f_insert(typval_T *argvars, typval_T *rettv)
2609{
2610 long before = 0;
2611 listitem_T *item;
2612 list_T *l;
2613 int error = FALSE;
2614
2615 if (argvars[0].v_type == VAR_BLOB)
2616 {
2617 int val, len;
2618 char_u *p;
2619
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002620 if (argvars[0].vval.v_blob == NULL)
2621 return;
2622
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002623 len = blob_len(argvars[0].vval.v_blob);
2624 if (argvars[2].v_type != VAR_UNKNOWN)
2625 {
2626 before = (long)tv_get_number_chk(&argvars[2], &error);
2627 if (error)
2628 return; // type error; errmsg already given
2629 if (before < 0 || before > len)
2630 {
2631 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2632 return;
2633 }
2634 }
2635 val = tv_get_number_chk(&argvars[1], &error);
2636 if (error)
2637 return;
2638 if (val < 0 || val > 255)
2639 {
2640 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2641 return;
2642 }
2643
2644 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
2645 return;
2646 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2647 mch_memmove(p + before + 1, p + before, (size_t)len - before);
2648 *(p + before) = val;
2649 ++argvars[0].vval.v_blob->bv_ga.ga_len;
2650
2651 copy_tv(&argvars[0], rettv);
2652 }
2653 else if (argvars[0].v_type != VAR_LIST)
2654 semsg(_(e_listblobarg), "insert()");
2655 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002656 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002657 (char_u *)N_("insert() argument"), TRUE))
2658 {
2659 if (argvars[2].v_type != VAR_UNKNOWN)
2660 before = (long)tv_get_number_chk(&argvars[2], &error);
2661 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002662 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002663
2664 if (before == l->lv_len)
2665 item = NULL;
2666 else
2667 {
2668 item = list_find(l, before);
2669 if (item == NULL)
2670 {
2671 semsg(_(e_listidx), before);
2672 l = NULL;
2673 }
2674 }
2675 if (l != NULL)
2676 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02002677 (void)list_insert_tv(l, &argvars[1], item);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002678 copy_tv(&argvars[0], rettv);
2679 }
2680 }
2681}
2682
2683/*
2684 * "remove()" function
2685 */
2686 void
2687f_remove(typval_T *argvars, typval_T *rettv)
2688{
2689 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2690
2691 if (argvars[0].v_type == VAR_DICT)
2692 dict_remove(argvars, rettv, arg_errmsg);
2693 else if (argvars[0].v_type == VAR_BLOB)
2694 blob_remove(argvars, rettv);
2695 else if (argvars[0].v_type == VAR_LIST)
2696 list_remove(argvars, rettv, arg_errmsg);
2697 else
2698 semsg(_(e_listdictblobarg), "remove()");
2699}
2700
2701/*
2702 * "reverse({list})" function
2703 */
2704 void
2705f_reverse(typval_T *argvars, typval_T *rettv)
2706{
2707 list_T *l;
2708 listitem_T *li, *ni;
2709
2710 if (argvars[0].v_type == VAR_BLOB)
2711 {
2712 blob_T *b = argvars[0].vval.v_blob;
2713 int i, len = blob_len(b);
2714
2715 for (i = 0; i < len / 2; i++)
2716 {
2717 int tmp = blob_get(b, i);
2718
2719 blob_set(b, i, blob_get(b, len - i - 1));
2720 blob_set(b, len - i - 1, tmp);
2721 }
2722 rettv_blob_set(rettv, b);
2723 return;
2724 }
2725
2726 if (argvars[0].v_type != VAR_LIST)
2727 semsg(_(e_listblobarg), "reverse()");
2728 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002729 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002730 (char_u *)N_("reverse() argument"), TRUE))
2731 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002732 if (l->lv_first == &range_list_item)
2733 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002734 varnumber_T new_start = l->lv_u.nonmat.lv_start
2735 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2736 l->lv_u.nonmat.lv_end = new_start
2737 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2738 l->lv_u.nonmat.lv_start = new_start;
2739 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002740 rettv_list_set(rettv, l);
2741 return;
2742 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002743 li = l->lv_u.mat.lv_last;
2744 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002745 l->lv_len = 0;
2746 while (li != NULL)
2747 {
2748 ni = li->li_prev;
2749 list_append(l, li);
2750 li = ni;
2751 }
2752 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002753 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002754 }
2755}
2756
Bram Moolenaar85629982020-06-01 18:39:20 +02002757/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002758 * "reduce(list, { accumulator, element -> value } [, initial])" function
Bram Moolenaar85629982020-06-01 18:39:20 +02002759 */
2760 void
2761f_reduce(typval_T *argvars, typval_T *rettv)
2762{
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002763 typval_T initial;
Bram Moolenaar85629982020-06-01 18:39:20 +02002764 char_u *func_name;
2765 partial_T *partial = NULL;
2766 funcexe_T funcexe;
2767 typval_T argv[3];
2768
2769 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
2770 {
2771 emsg(_(e_listblobreq));
2772 return;
2773 }
2774
2775 if (argvars[1].v_type == VAR_FUNC)
2776 func_name = argvars[1].vval.v_string;
2777 else if (argvars[1].v_type == VAR_PARTIAL)
2778 {
2779 partial = argvars[1].vval.v_partial;
2780 func_name = partial_name(partial);
2781 }
2782 else
2783 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01002784 if (func_name == NULL || *func_name == NUL)
2785 {
2786 emsg(_(e_missing_function_argument));
2787 return;
2788 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002789
2790 vim_memset(&funcexe, 0, sizeof(funcexe));
2791 funcexe.evaluate = TRUE;
2792 funcexe.partial = partial;
2793
2794 if (argvars[0].v_type == VAR_LIST)
2795 {
2796 list_T *l = argvars[0].vval.v_list;
2797 listitem_T *li = NULL;
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002798 int r;
Bram Moolenaarca275a02020-06-24 22:07:46 +02002799 int called_emsg_start = called_emsg;
Bram Moolenaar85629982020-06-01 18:39:20 +02002800
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002801 if (l != NULL)
2802 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar85629982020-06-01 18:39:20 +02002803 if (argvars[2].v_type == VAR_UNKNOWN)
2804 {
2805 if (l == NULL || l->lv_first == NULL)
2806 {
2807 semsg(_(e_reduceempty), "List");
2808 return;
2809 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002810 initial = l->lv_first->li_tv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002811 li = l->lv_first->li_next;
2812 }
2813 else
2814 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002815 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002816 if (l != NULL)
2817 li = l->lv_first;
2818 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002819 copy_tv(&initial, rettv);
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002820
2821 if (l != NULL)
Bram Moolenaar85629982020-06-01 18:39:20 +02002822 {
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002823 int prev_locked = l->lv_lock;
2824
2825 l->lv_lock = VAR_FIXED; // disallow the list changing here
2826 for ( ; li != NULL; li = li->li_next)
2827 {
2828 argv[0] = *rettv;
2829 argv[1] = li->li_tv;
2830 rettv->v_type = VAR_UNKNOWN;
2831 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
2832 clear_tv(&argv[0]);
2833 if (r == FAIL || called_emsg != called_emsg_start)
2834 break;
2835 }
2836 l->lv_lock = prev_locked;
Bram Moolenaar85629982020-06-01 18:39:20 +02002837 }
2838 }
2839 else
2840 {
2841 blob_T *b = argvars[0].vval.v_blob;
2842 int i;
2843
2844 if (argvars[2].v_type == VAR_UNKNOWN)
2845 {
2846 if (b == NULL || b->bv_ga.ga_len == 0)
2847 {
2848 semsg(_(e_reduceempty), "Blob");
2849 return;
2850 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002851 initial.v_type = VAR_NUMBER;
2852 initial.vval.v_number = blob_get(b, 0);
Bram Moolenaar85629982020-06-01 18:39:20 +02002853 i = 1;
2854 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002855 else if (argvars[2].v_type != VAR_NUMBER)
2856 {
2857 emsg(_(e_number_exp));
2858 return;
2859 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002860 else
2861 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002862 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002863 i = 0;
2864 }
2865
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002866 copy_tv(&initial, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02002867 if (b != NULL)
2868 {
2869 for ( ; i < b->bv_ga.ga_len; i++)
2870 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002871 argv[0] = *rettv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002872 argv[1].v_type = VAR_NUMBER;
2873 argv[1].vval.v_number = blob_get(b, i);
2874 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
2875 return;
Bram Moolenaar85629982020-06-01 18:39:20 +02002876 }
2877 }
2878 }
2879}
2880
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002881#endif // defined(FEAT_EVAL)