blob: d6357f789c76ac63ba12a5feea0323063597d8bc [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;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01001988 type_T *type = NULL;
1989 garray_T type_list;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001990
Bram Moolenaarea696852020-11-09 18:31:39 +01001991 // map() and filter() return the first argument, also on failure.
1992 if (filtermap != FILTERMAP_MAPNEW)
1993 copy_tv(&argvars[0], rettv);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01001994 if (filtermap == FILTERMAP_MAP && in_vim9script())
1995 {
1996 // Check that map() does not change the type of the dict.
1997 ga_init2(&type_list, sizeof(type_T *), 10);
1998 type = typval2type(argvars, &type_list);
1999 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002000
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002001 if (argvars[0].v_type == VAR_BLOB)
2002 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002003 if (filtermap == FILTERMAP_MAPNEW)
2004 {
2005 rettv->v_type = VAR_BLOB;
2006 rettv->vval.v_blob = NULL;
2007 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002008 if ((b = argvars[0].vval.v_blob) == NULL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002009 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002010 }
2011 else if (argvars[0].v_type == VAR_LIST)
2012 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002013 if (filtermap == FILTERMAP_MAPNEW)
2014 {
2015 rettv->v_type = VAR_LIST;
2016 rettv->vval.v_list = NULL;
2017 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002018 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002019 || (filtermap == FILTERMAP_FILTER
2020 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002021 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002022 }
2023 else if (argvars[0].v_type == VAR_DICT)
2024 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002025 if (filtermap == FILTERMAP_MAPNEW)
2026 {
2027 rettv->v_type = VAR_DICT;
2028 rettv->vval.v_dict = NULL;
2029 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002030 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002031 || (filtermap == FILTERMAP_FILTER
2032 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002033 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002034 }
2035 else
2036 {
Bram Moolenaarfcb0b612020-05-26 20:22:01 +02002037 semsg(_(e_listdictblobarg), ermsg);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002038 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002039 }
2040
2041 expr = &argvars[1];
2042 // On type errors, the preceding call has already displayed an error
2043 // message. Avoid a misleading error message for an empty string that
2044 // was not passed as argument.
2045 if (expr->v_type != VAR_UNKNOWN)
2046 {
2047 typval_T save_val;
2048 typval_T save_key;
2049
2050 prepare_vimvar(VV_VAL, &save_val);
2051 prepare_vimvar(VV_KEY, &save_key);
2052
2053 // We reset "did_emsg" to be able to detect whether an error
2054 // occurred during evaluation of the expression.
2055 save_did_emsg = did_emsg;
2056 did_emsg = FALSE;
2057
2058 if (argvars[0].v_type == VAR_DICT)
2059 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002060 int prev_lock = d->dv_lock;
Bram Moolenaarea696852020-11-09 18:31:39 +01002061 dict_T *d_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002062
Bram Moolenaarea696852020-11-09 18:31:39 +01002063 if (filtermap == FILTERMAP_MAPNEW)
2064 {
2065 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002066 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002067 d_ret = rettv->vval.v_dict;
2068 }
2069
2070 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002071 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002072 ht = &d->dv_hashtab;
2073 hash_lock(ht);
2074 todo = (int)ht->ht_used;
2075 for (hi = ht->ht_array; todo > 0; ++hi)
2076 {
2077 if (!HASHITEM_EMPTY(hi))
2078 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002079 int r;
2080 typval_T newtv;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002081
2082 --todo;
2083 di = HI2DI(hi);
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002084 if (filtermap == FILTERMAP_MAP
Bram Moolenaarea696852020-11-09 18:31:39 +01002085 && (value_check_lock(di->di_tv.v_lock,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002086 arg_errmsg, TRUE)
2087 || var_check_ro(di->di_flags,
2088 arg_errmsg, TRUE)))
2089 break;
2090 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaarea696852020-11-09 18:31:39 +01002091 r = filter_map_one(&di->di_tv, expr, filtermap,
2092 &newtv, &rem);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002093 clear_tv(get_vim_var_tv(VV_KEY));
2094 if (r == FAIL || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002095 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002096 clear_tv(&newtv);
2097 break;
2098 }
2099 if (filtermap == FILTERMAP_MAP)
2100 {
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002101 if (type != NULL && check_typval_type(type->tt_member,
2102 &newtv, 0) == FAIL)
2103 {
2104 clear_tv(&newtv);
2105 break;
2106 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002107 // map(): replace the dict item value
2108 clear_tv(&di->di_tv);
2109 newtv.v_lock = 0;
2110 di->di_tv = newtv;
2111 }
2112 else if (filtermap == FILTERMAP_MAPNEW)
2113 {
2114 // mapnew(): add the item value to the new dict
2115 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
2116 clear_tv(&newtv);
2117 if (r == FAIL)
2118 break;
2119 }
2120 else if (filtermap == FILTERMAP_FILTER && rem)
2121 {
2122 // filter(false): remove the item from the dict
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002123 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2124 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2125 break;
2126 dictitem_remove(d, di);
2127 }
2128 }
2129 }
2130 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002131 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002132 }
2133 else if (argvars[0].v_type == VAR_BLOB)
2134 {
2135 int i;
2136 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002137 varnumber_T val;
Bram Moolenaarea696852020-11-09 18:31:39 +01002138 blob_T *b_ret = b;
2139
2140 if (filtermap == FILTERMAP_MAPNEW)
2141 {
2142 if (blob_copy(b, rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002143 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002144 b_ret = rettv->vval.v_blob;
2145 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002146
2147 // set_vim_var_nr() doesn't set the type
2148 set_vim_var_type(VV_KEY, VAR_NUMBER);
2149
2150 for (i = 0; i < b->bv_ga.ga_len; i++)
2151 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002152 typval_T newtv;
2153
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002154 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002155 val = blob_get(b, i);
2156 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002157 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaarea696852020-11-09 18:31:39 +01002158 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
2159 || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002160 break;
Bram Moolenaarea696852020-11-09 18:31:39 +01002161 if (newtv.v_type != VAR_NUMBER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002162 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002163 clear_tv(&newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002164 emsg(_(e_invalblob));
2165 break;
2166 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002167 if (filtermap != FILTERMAP_FILTER)
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002168 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002169 if (newtv.vval.v_number != val)
2170 blob_set(b_ret, i, newtv.vval.v_number);
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002171 }
2172 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002173 {
2174 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2175
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002176 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002177 (size_t)b->bv_ga.ga_len - i - 1);
2178 --b->bv_ga.ga_len;
2179 --i;
2180 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002181 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002182 }
2183 }
2184 else // argvars[0].v_type == VAR_LIST
2185 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002186 int prev_lock = l->lv_lock;
2187 list_T *l_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002188
Bram Moolenaarea696852020-11-09 18:31:39 +01002189 if (filtermap == FILTERMAP_MAPNEW)
2190 {
2191 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002192 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002193 l_ret = rettv->vval.v_list;
2194 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002195 // set_vim_var_nr() doesn't set the type
2196 set_vim_var_type(VV_KEY, VAR_NUMBER);
2197
Bram Moolenaarea696852020-11-09 18:31:39 +01002198 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002199 l->lv_lock = VAR_LOCKED;
Bram Moolenaarea696852020-11-09 18:31:39 +01002200
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002201 if (l->lv_first == &range_list_item)
2202 {
2203 varnumber_T val = l->lv_u.nonmat.lv_start;
2204 int len = l->lv_len;
2205 int stride = l->lv_u.nonmat.lv_stride;
2206
2207 // List from range(): loop over the numbers
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002208 if (filtermap != FILTERMAP_MAPNEW)
2209 {
2210 l->lv_first = NULL;
2211 l->lv_u.mat.lv_last = NULL;
2212 l->lv_len = 0;
2213 l->lv_u.mat.lv_idx_item = NULL;
2214 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002215
2216 for (idx = 0; idx < len; ++idx)
Bram Moolenaarc56936e2020-11-10 11:43:56 +01002217 {
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002218 typval_T tv;
2219 typval_T newtv;
2220
2221 tv.v_type = VAR_NUMBER;
2222 tv.v_lock = 0;
2223 tv.vval.v_number = val;
2224 set_vim_var_nr(VV_KEY, idx);
2225 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2226 == FAIL)
Bram Moolenaarea696852020-11-09 18:31:39 +01002227 break;
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002228 if (did_emsg)
2229 {
2230 clear_tv(&newtv);
2231 break;
2232 }
2233 if (filtermap != FILTERMAP_FILTER)
2234 {
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002235 if (filtermap == FILTERMAP_MAP && type != NULL
2236 && check_typval_type(type->tt_member,
2237 &newtv, 0) == FAIL)
2238 {
2239 clear_tv(&newtv);
2240 break;
2241 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002242 // map(), mapnew(): always append the new value to the
2243 // list
2244 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2245 ? l : l_ret, &newtv) == FAIL)
2246 break;
2247 }
2248 else if (!rem)
2249 {
2250 // filter(): append the list item value when not rem
2251 if (list_append_tv_move(l, &tv) == FAIL)
2252 break;
2253 }
2254
2255 val += stride;
Bram Moolenaarea696852020-11-09 18:31:39 +01002256 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002257 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002258 else
2259 {
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002260 // Materialized list: loop over the items
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002261 for (li = l->lv_first; li != NULL; li = nli)
2262 {
2263 typval_T newtv;
2264
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002265 if (filtermap == FILTERMAP_MAP && value_check_lock(
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002266 li->li_tv.v_lock, arg_errmsg, TRUE))
2267 break;
2268 nli = li->li_next;
2269 set_vim_var_nr(VV_KEY, idx);
2270 if (filter_map_one(&li->li_tv, expr, filtermap,
2271 &newtv, &rem) == FAIL)
2272 break;
2273 if (did_emsg)
2274 {
2275 clear_tv(&newtv);
2276 break;
2277 }
2278 if (filtermap == FILTERMAP_MAP)
2279 {
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002280 if (type != NULL && check_typval_type(type->tt_member,
2281 &newtv, 0) == FAIL)
2282 {
2283 clear_tv(&newtv);
2284 break;
2285 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002286 // map(): replace the list item value
2287 clear_tv(&li->li_tv);
2288 newtv.v_lock = 0;
2289 li->li_tv = newtv;
2290 }
2291 else if (filtermap == FILTERMAP_MAPNEW)
2292 {
2293 // mapnew(): append the list item value
2294 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2295 break;
2296 }
2297 else if (filtermap == FILTERMAP_FILTER && rem)
2298 listitem_remove(l, li);
2299 ++idx;
2300 }
2301 }
2302
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002303 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002304 }
2305
2306 restore_vimvar(VV_KEY, &save_key);
2307 restore_vimvar(VV_VAL, &save_val);
2308
2309 did_emsg |= save_did_emsg;
2310 }
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002311
2312theend:
2313 if (type != NULL)
2314 clear_type_list(&type_list);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002315}
2316
2317/*
2318 * "filter()" function
2319 */
2320 void
2321f_filter(typval_T *argvars, typval_T *rettv)
2322{
Bram Moolenaarea696852020-11-09 18:31:39 +01002323 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002324}
2325
2326/*
2327 * "map()" function
2328 */
2329 void
2330f_map(typval_T *argvars, typval_T *rettv)
2331{
Bram Moolenaarea696852020-11-09 18:31:39 +01002332 filter_map(argvars, rettv, FILTERMAP_MAP);
2333}
2334
2335/*
2336 * "mapnew()" function
2337 */
2338 void
2339f_mapnew(typval_T *argvars, typval_T *rettv)
2340{
2341 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002342}
2343
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002344/*
2345 * "add(list, item)" function
2346 */
2347 void
2348f_add(typval_T *argvars, typval_T *rettv)
2349{
2350 list_T *l;
2351 blob_T *b;
2352
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002353 rettv->vval.v_number = 1; // Default: Failed
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002354 if (argvars[0].v_type == VAR_LIST)
2355 {
2356 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002357 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002358 (char_u *)N_("add() argument"), TRUE)
2359 && list_append_tv(l, &argvars[1]) == OK)
2360 copy_tv(&argvars[0], rettv);
2361 }
2362 else if (argvars[0].v_type == VAR_BLOB)
2363 {
2364 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002365 && !value_check_lock(b->bv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002366 (char_u *)N_("add() argument"), TRUE))
2367 {
2368 int error = FALSE;
2369 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2370
2371 if (!error)
2372 {
2373 ga_append(&b->bv_ga, (int)n);
2374 copy_tv(&argvars[0], rettv);
2375 }
2376 }
2377 }
2378 else
2379 emsg(_(e_listblobreq));
2380}
2381
2382/*
2383 * "count()" function
2384 */
2385 void
2386f_count(typval_T *argvars, typval_T *rettv)
2387{
2388 long n = 0;
2389 int ic = FALSE;
2390 int error = FALSE;
2391
2392 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002393 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002394
2395 if (argvars[0].v_type == VAR_STRING)
2396 {
2397 char_u *expr = tv_get_string_chk(&argvars[1]);
2398 char_u *p = argvars[0].vval.v_string;
2399 char_u *next;
2400
2401 if (!error && expr != NULL && *expr != NUL && p != NULL)
2402 {
2403 if (ic)
2404 {
2405 size_t len = STRLEN(expr);
2406
2407 while (*p != NUL)
2408 {
2409 if (MB_STRNICMP(p, expr, len) == 0)
2410 {
2411 ++n;
2412 p += len;
2413 }
2414 else
2415 MB_PTR_ADV(p);
2416 }
2417 }
2418 else
2419 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2420 != NULL)
2421 {
2422 ++n;
2423 p = next + STRLEN(expr);
2424 }
2425 }
2426
2427 }
2428 else if (argvars[0].v_type == VAR_LIST)
2429 {
2430 listitem_T *li;
2431 list_T *l;
2432 long idx;
2433
2434 if ((l = argvars[0].vval.v_list) != NULL)
2435 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002436 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002437 li = l->lv_first;
2438 if (argvars[2].v_type != VAR_UNKNOWN)
2439 {
2440 if (argvars[3].v_type != VAR_UNKNOWN)
2441 {
2442 idx = (long)tv_get_number_chk(&argvars[3], &error);
2443 if (!error)
2444 {
2445 li = list_find(l, idx);
2446 if (li == NULL)
2447 semsg(_(e_listidx), idx);
2448 }
2449 }
2450 if (error)
2451 li = NULL;
2452 }
2453
2454 for ( ; li != NULL; li = li->li_next)
2455 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2456 ++n;
2457 }
2458 }
2459 else if (argvars[0].v_type == VAR_DICT)
2460 {
2461 int todo;
2462 dict_T *d;
2463 hashitem_T *hi;
2464
2465 if ((d = argvars[0].vval.v_dict) != NULL)
2466 {
2467 if (argvars[2].v_type != VAR_UNKNOWN)
2468 {
2469 if (argvars[3].v_type != VAR_UNKNOWN)
2470 emsg(_(e_invarg));
2471 }
2472
2473 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2474 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2475 {
2476 if (!HASHITEM_EMPTY(hi))
2477 {
2478 --todo;
2479 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2480 ++n;
2481 }
2482 }
2483 }
2484 }
2485 else
2486 semsg(_(e_listdictarg), "count()");
2487 rettv->vval.v_number = n;
2488}
2489
2490/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002491 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002492 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002493 static void
2494extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002495{
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002496 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2497 {
2498 list_T *l1, *l2;
2499 listitem_T *item;
2500 long before;
2501 int error = FALSE;
2502
2503 l1 = argvars[0].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002504 if (l1 == NULL)
2505 {
2506 emsg(_(e_cannot_extend_null_list));
2507 return;
2508 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002509 l2 = argvars[1].vval.v_list;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002510 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
2511 && l2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002512 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002513 if (is_new)
2514 {
2515 l1 = list_copy(l1, FALSE, get_copyID());
2516 if (l1 == NULL)
2517 return;
2518 }
2519
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002520 if (argvars[2].v_type != VAR_UNKNOWN)
2521 {
2522 before = (long)tv_get_number_chk(&argvars[2], &error);
2523 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002524 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002525
2526 if (before == l1->lv_len)
2527 item = NULL;
2528 else
2529 {
2530 item = list_find(l1, before);
2531 if (item == NULL)
2532 {
2533 semsg(_(e_listidx), before);
2534 return;
2535 }
2536 }
2537 }
2538 else
2539 item = NULL;
2540 list_extend(l1, l2, item);
2541
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002542 if (is_new)
2543 {
2544 rettv->v_type = VAR_LIST;
2545 rettv->vval.v_list = l1;
2546 rettv->v_lock = FALSE;
2547 }
2548 else
2549 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002550 }
2551 }
2552 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2553 {
2554 dict_T *d1, *d2;
2555 char_u *action;
2556 int i;
2557
2558 d1 = argvars[0].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002559 if (d1 == NULL)
2560 {
2561 emsg(_(e_cannot_extend_null_dict));
2562 return;
2563 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002564 d2 = argvars[1].vval.v_dict;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002565 if ((is_new || !value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
2566 && d2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002567 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002568 if (is_new)
2569 {
2570 d1 = dict_copy(d1, FALSE, get_copyID());
2571 if (d1 == NULL)
2572 return;
2573 }
2574
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002575 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002576 if (argvars[2].v_type != VAR_UNKNOWN)
2577 {
2578 static char *(av[]) = {"keep", "force", "error"};
2579
2580 action = tv_get_string_chk(&argvars[2]);
2581 if (action == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002582 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002583 for (i = 0; i < 3; ++i)
2584 if (STRCMP(action, av[i]) == 0)
2585 break;
2586 if (i == 3)
2587 {
2588 semsg(_(e_invarg2), action);
2589 return;
2590 }
2591 }
2592 else
2593 action = (char_u *)"force";
2594
2595 dict_extend(d1, d2, action);
2596
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002597 if (is_new)
2598 {
2599 rettv->v_type = VAR_DICT;
2600 rettv->vval.v_dict = d1;
2601 rettv->v_lock = FALSE;
2602 }
2603 else
2604 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002605 }
2606 }
2607 else
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002608 semsg(_(e_listdictarg), is_new ? "extendnew()" : "extend()");
2609}
2610
2611/*
2612 * "extend(list, list [, idx])" function
2613 * "extend(dict, dict [, action])" function
2614 */
2615 void
2616f_extend(typval_T *argvars, typval_T *rettv)
2617{
2618 char_u *errmsg = (char_u *)N_("extend() argument");
2619
2620 extend(argvars, rettv, errmsg, FALSE);
2621}
2622
2623/*
2624 * "extendnew(list, list [, idx])" function
2625 * "extendnew(dict, dict [, action])" function
2626 */
2627 void
2628f_extendnew(typval_T *argvars, typval_T *rettv)
2629{
2630 char_u *errmsg = (char_u *)N_("extendnew() argument");
2631
2632 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002633}
2634
2635/*
2636 * "insert()" function
2637 */
2638 void
2639f_insert(typval_T *argvars, typval_T *rettv)
2640{
2641 long before = 0;
2642 listitem_T *item;
2643 list_T *l;
2644 int error = FALSE;
2645
2646 if (argvars[0].v_type == VAR_BLOB)
2647 {
2648 int val, len;
2649 char_u *p;
2650
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002651 if (argvars[0].vval.v_blob == NULL)
2652 return;
2653
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002654 len = blob_len(argvars[0].vval.v_blob);
2655 if (argvars[2].v_type != VAR_UNKNOWN)
2656 {
2657 before = (long)tv_get_number_chk(&argvars[2], &error);
2658 if (error)
2659 return; // type error; errmsg already given
2660 if (before < 0 || before > len)
2661 {
2662 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2663 return;
2664 }
2665 }
2666 val = tv_get_number_chk(&argvars[1], &error);
2667 if (error)
2668 return;
2669 if (val < 0 || val > 255)
2670 {
2671 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2672 return;
2673 }
2674
2675 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
2676 return;
2677 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2678 mch_memmove(p + before + 1, p + before, (size_t)len - before);
2679 *(p + before) = val;
2680 ++argvars[0].vval.v_blob->bv_ga.ga_len;
2681
2682 copy_tv(&argvars[0], rettv);
2683 }
2684 else if (argvars[0].v_type != VAR_LIST)
2685 semsg(_(e_listblobarg), "insert()");
2686 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002687 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002688 (char_u *)N_("insert() argument"), TRUE))
2689 {
2690 if (argvars[2].v_type != VAR_UNKNOWN)
2691 before = (long)tv_get_number_chk(&argvars[2], &error);
2692 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002693 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002694
2695 if (before == l->lv_len)
2696 item = NULL;
2697 else
2698 {
2699 item = list_find(l, before);
2700 if (item == NULL)
2701 {
2702 semsg(_(e_listidx), before);
2703 l = NULL;
2704 }
2705 }
2706 if (l != NULL)
2707 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02002708 (void)list_insert_tv(l, &argvars[1], item);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002709 copy_tv(&argvars[0], rettv);
2710 }
2711 }
2712}
2713
2714/*
2715 * "remove()" function
2716 */
2717 void
2718f_remove(typval_T *argvars, typval_T *rettv)
2719{
2720 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2721
2722 if (argvars[0].v_type == VAR_DICT)
2723 dict_remove(argvars, rettv, arg_errmsg);
2724 else if (argvars[0].v_type == VAR_BLOB)
2725 blob_remove(argvars, rettv);
2726 else if (argvars[0].v_type == VAR_LIST)
2727 list_remove(argvars, rettv, arg_errmsg);
2728 else
2729 semsg(_(e_listdictblobarg), "remove()");
2730}
2731
2732/*
2733 * "reverse({list})" function
2734 */
2735 void
2736f_reverse(typval_T *argvars, typval_T *rettv)
2737{
2738 list_T *l;
2739 listitem_T *li, *ni;
2740
2741 if (argvars[0].v_type == VAR_BLOB)
2742 {
2743 blob_T *b = argvars[0].vval.v_blob;
2744 int i, len = blob_len(b);
2745
2746 for (i = 0; i < len / 2; i++)
2747 {
2748 int tmp = blob_get(b, i);
2749
2750 blob_set(b, i, blob_get(b, len - i - 1));
2751 blob_set(b, len - i - 1, tmp);
2752 }
2753 rettv_blob_set(rettv, b);
2754 return;
2755 }
2756
2757 if (argvars[0].v_type != VAR_LIST)
2758 semsg(_(e_listblobarg), "reverse()");
2759 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002760 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002761 (char_u *)N_("reverse() argument"), TRUE))
2762 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002763 if (l->lv_first == &range_list_item)
2764 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002765 varnumber_T new_start = l->lv_u.nonmat.lv_start
2766 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2767 l->lv_u.nonmat.lv_end = new_start
2768 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2769 l->lv_u.nonmat.lv_start = new_start;
2770 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002771 rettv_list_set(rettv, l);
2772 return;
2773 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002774 li = l->lv_u.mat.lv_last;
2775 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002776 l->lv_len = 0;
2777 while (li != NULL)
2778 {
2779 ni = li->li_prev;
2780 list_append(l, li);
2781 li = ni;
2782 }
2783 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002784 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002785 }
2786}
2787
Bram Moolenaar85629982020-06-01 18:39:20 +02002788/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002789 * "reduce(list, { accumulator, element -> value } [, initial])" function
Bram Moolenaar85629982020-06-01 18:39:20 +02002790 */
2791 void
2792f_reduce(typval_T *argvars, typval_T *rettv)
2793{
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002794 typval_T initial;
Bram Moolenaar85629982020-06-01 18:39:20 +02002795 char_u *func_name;
2796 partial_T *partial = NULL;
2797 funcexe_T funcexe;
2798 typval_T argv[3];
2799
2800 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
2801 {
2802 emsg(_(e_listblobreq));
2803 return;
2804 }
2805
2806 if (argvars[1].v_type == VAR_FUNC)
2807 func_name = argvars[1].vval.v_string;
2808 else if (argvars[1].v_type == VAR_PARTIAL)
2809 {
2810 partial = argvars[1].vval.v_partial;
2811 func_name = partial_name(partial);
2812 }
2813 else
2814 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01002815 if (func_name == NULL || *func_name == NUL)
2816 {
2817 emsg(_(e_missing_function_argument));
2818 return;
2819 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002820
2821 vim_memset(&funcexe, 0, sizeof(funcexe));
2822 funcexe.evaluate = TRUE;
2823 funcexe.partial = partial;
2824
2825 if (argvars[0].v_type == VAR_LIST)
2826 {
2827 list_T *l = argvars[0].vval.v_list;
2828 listitem_T *li = NULL;
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002829 int r;
Bram Moolenaarca275a02020-06-24 22:07:46 +02002830 int called_emsg_start = called_emsg;
Bram Moolenaar85629982020-06-01 18:39:20 +02002831
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002832 if (l != NULL)
2833 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar85629982020-06-01 18:39:20 +02002834 if (argvars[2].v_type == VAR_UNKNOWN)
2835 {
2836 if (l == NULL || l->lv_first == NULL)
2837 {
2838 semsg(_(e_reduceempty), "List");
2839 return;
2840 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002841 initial = l->lv_first->li_tv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002842 li = l->lv_first->li_next;
2843 }
2844 else
2845 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002846 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002847 if (l != NULL)
2848 li = l->lv_first;
2849 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002850 copy_tv(&initial, rettv);
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002851
2852 if (l != NULL)
Bram Moolenaar85629982020-06-01 18:39:20 +02002853 {
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002854 int prev_locked = l->lv_lock;
2855
2856 l->lv_lock = VAR_FIXED; // disallow the list changing here
2857 for ( ; li != NULL; li = li->li_next)
2858 {
2859 argv[0] = *rettv;
2860 argv[1] = li->li_tv;
2861 rettv->v_type = VAR_UNKNOWN;
2862 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
2863 clear_tv(&argv[0]);
2864 if (r == FAIL || called_emsg != called_emsg_start)
2865 break;
2866 }
2867 l->lv_lock = prev_locked;
Bram Moolenaar85629982020-06-01 18:39:20 +02002868 }
2869 }
2870 else
2871 {
2872 blob_T *b = argvars[0].vval.v_blob;
2873 int i;
2874
2875 if (argvars[2].v_type == VAR_UNKNOWN)
2876 {
2877 if (b == NULL || b->bv_ga.ga_len == 0)
2878 {
2879 semsg(_(e_reduceempty), "Blob");
2880 return;
2881 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002882 initial.v_type = VAR_NUMBER;
2883 initial.vval.v_number = blob_get(b, 0);
Bram Moolenaar85629982020-06-01 18:39:20 +02002884 i = 1;
2885 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002886 else if (argvars[2].v_type != VAR_NUMBER)
2887 {
2888 emsg(_(e_number_exp));
2889 return;
2890 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002891 else
2892 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002893 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002894 i = 0;
2895 }
2896
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002897 copy_tv(&initial, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02002898 if (b != NULL)
2899 {
2900 for ( ; i < b->bv_ga.ga_len; i++)
2901 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002902 argv[0] = *rettv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002903 argv[1].v_type = VAR_NUMBER;
2904 argv[1].vval.v_number = blob_get(b, i);
2905 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
2906 return;
Bram Moolenaar85629982020-06-01 18:39:20 +02002907 }
2908 }
2909 }
2910}
2911
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002912#endif // defined(FEAT_EVAL)