blob: b50cb03c98560b1546b68ac92110c7a55f52370d [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 Moolenaar4ba37b52019-12-04 21:57:43 +010018// List heads for garbage collection.
19static list_T *first_list = NULL; // list of all lists
Bram Moolenaarda861d62016-07-17 15:46:27 +020020
Bram Moolenaar00d253e2020-04-06 22:13:01 +020021#define FOR_ALL_WATCHERS(l, lw) \
22 for ((lw) = (l)->lv_watch; (lw) != NULL; (lw) = (lw)->lw_next)
23
Bram Moolenaarbdff0122020-04-05 18:56:05 +020024static void list_free_item(list_T *l, listitem_T *item);
25
Bram Moolenaarda861d62016-07-17 15:46:27 +020026/*
27 * Add a watcher to a list.
28 */
29 void
30list_add_watch(list_T *l, listwatch_T *lw)
31{
32 lw->lw_next = l->lv_watch;
33 l->lv_watch = lw;
34}
35
36/*
37 * Remove a watcher from a list.
38 * No warning when it isn't found...
39 */
40 void
41list_rem_watch(list_T *l, listwatch_T *lwrem)
42{
43 listwatch_T *lw, **lwp;
44
45 lwp = &l->lv_watch;
Bram Moolenaar00d253e2020-04-06 22:13:01 +020046 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020047 {
48 if (lw == lwrem)
49 {
50 *lwp = lw->lw_next;
51 break;
52 }
53 lwp = &lw->lw_next;
54 }
55}
56
57/*
58 * Just before removing an item from a list: advance watchers to the next
59 * item.
60 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020061 static void
Bram Moolenaarda861d62016-07-17 15:46:27 +020062list_fix_watch(list_T *l, listitem_T *item)
63{
64 listwatch_T *lw;
65
Bram Moolenaar00d253e2020-04-06 22:13:01 +020066 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020067 if (lw->lw_item == item)
68 lw->lw_item = item->li_next;
69}
70
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010071 static void
72list_init(list_T *l)
73{
74 // Prepend the list to the list of lists for garbage collection.
75 if (first_list != NULL)
76 first_list->lv_used_prev = l;
77 l->lv_used_prev = NULL;
78 l->lv_used_next = first_list;
79 first_list = l;
80}
81
Bram Moolenaarda861d62016-07-17 15:46:27 +020082/*
83 * Allocate an empty header for a list.
84 * Caller should take care of the reference count.
85 */
86 list_T *
87list_alloc(void)
88{
89 list_T *l;
90
Bram Moolenaarc799fe22019-05-28 23:08:19 +020091 l = ALLOC_CLEAR_ONE(list_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +020092 if (l != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010093 list_init(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +020094 return l;
95}
96
97/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010098 * list_alloc() with an ID for alloc_fail().
99 */
100 list_T *
101list_alloc_id(alloc_id_T id UNUSED)
102{
103#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200104 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100105 return NULL;
106#endif
107 return (list_alloc());
108}
109
110/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100111 * Allocate space for a list, plus "count" items.
Bram Moolenaar24fe33a2022-11-24 00:09:02 +0000112 * This uses one allocation for efficiency.
113 * The reference count is not set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100114 * 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));
Bram Moolenaar24fe33a2022-11-24 00:09:02 +0000122 if (l == NULL)
123 return NULL;
124
125 list_init(l);
126
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000127 if (count <= 0)
128 return l;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000130 listitem_T *li = (listitem_T *)(l + 1);
131 int i;
132
133 l->lv_len = count;
134 l->lv_with_items = count;
135 l->lv_first = li;
136 l->lv_u.mat.lv_last = li + count - 1;
137 for (i = 0; i < count; ++i)
138 {
139 if (i == 0)
140 li->li_prev = NULL;
141 else
142 li->li_prev = li - 1;
143 if (i == count - 1)
144 li->li_next = NULL;
145 else
146 li->li_next = li + 1;
147 ++li;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148 }
Bram Moolenaar24fe33a2022-11-24 00:09:02 +0000149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 return l;
151}
152
153/*
154 * Set item "idx" for a list previously allocated with list_alloc_with_items().
155 * The contents of "tv" is moved into the list item.
156 * Each item must be set exactly once.
157 */
158 void
159list_set_item(list_T *l, int idx, typval_T *tv)
160{
161 listitem_T *li = (listitem_T *)(l + 1) + idx;
162
163 li->li_tv = *tv;
164}
165
166/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200167 * Allocate an empty list for a return value, with reference count set.
168 * Returns OK or FAIL.
169 */
170 int
171rettv_list_alloc(typval_T *rettv)
172{
173 list_T *l = list_alloc();
174
175 if (l == NULL)
176 return FAIL;
177
Bram Moolenaarda861d62016-07-17 15:46:27 +0200178 rettv->v_lock = 0;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200179 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200180 return OK;
181}
182
183/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100184 * Same as rettv_list_alloc() but uses an allocation id for testing.
185 */
186 int
187rettv_list_alloc_id(typval_T *rettv, alloc_id_T id UNUSED)
188{
189#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200190 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaar162b7142018-12-21 15:17:36 +0100191 return FAIL;
192#endif
193 return rettv_list_alloc(rettv);
194}
195
196
197/*
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200198 * Set a list as the return value. Increments the reference count.
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200199 */
200 void
201rettv_list_set(typval_T *rettv, list_T *l)
202{
203 rettv->v_type = VAR_LIST;
204 rettv->vval.v_list = l;
205 if (l != NULL)
206 ++l->lv_refcount;
207}
208
209/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200210 * Unreference a list: decrement the reference count and free it when it
211 * becomes zero.
212 */
213 void
214list_unref(list_T *l)
215{
216 if (l != NULL && --l->lv_refcount <= 0)
217 list_free(l);
218}
219
220/*
221 * Free a list, including all non-container items it points to.
222 * Ignores the reference count.
223 */
224 static void
225list_free_contents(list_T *l)
226{
227 listitem_T *item;
228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229 if (l->lv_first != &range_list_item)
230 for (item = l->lv_first; item != NULL; item = l->lv_first)
231 {
232 // Remove the item before deleting it.
233 l->lv_first = item->li_next;
234 clear_tv(&item->li_tv);
235 list_free_item(l, item);
236 }
Bram Moolenaarda861d62016-07-17 15:46:27 +0200237}
238
239/*
240 * Go through the list of lists and free items without the copyID.
241 * But don't free a list that has a watcher (used in a for loop), these
242 * are not referenced anywhere.
243 */
244 int
245list_free_nonref(int copyID)
246{
247 list_T *ll;
248 int did_free = FALSE;
249
250 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
251 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
252 && ll->lv_watch == NULL)
253 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100254 // Free the List and ordinary items it contains, but don't recurse
255 // into Lists and Dictionaries, they will be in the list of dicts
256 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200257 list_free_contents(ll);
258 did_free = TRUE;
259 }
260 return did_free;
261}
262
263 static void
264list_free_list(list_T *l)
265{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100266 // Remove the list from the list of lists for garbage collection.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200267 if (l->lv_used_prev == NULL)
268 first_list = l->lv_used_next;
269 else
270 l->lv_used_prev->lv_used_next = l->lv_used_next;
271 if (l->lv_used_next != NULL)
272 l->lv_used_next->lv_used_prev = l->lv_used_prev;
273
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100274 free_type(l->lv_type);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200275 vim_free(l);
276}
277
278 void
279list_free_items(int copyID)
280{
281 list_T *ll, *ll_next;
282
283 for (ll = first_list; ll != NULL; ll = ll_next)
284 {
285 ll_next = ll->lv_used_next;
286 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
287 && ll->lv_watch == NULL)
288 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100289 // Free the List and ordinary items it contains, but don't recurse
290 // into Lists and Dictionaries, they will be in the list of dicts
291 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200292 list_free_list(ll);
293 }
294 }
295}
296
297 void
298list_free(list_T *l)
299{
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000300 if (in_free_unref_items)
301 return;
302
303 list_free_contents(l);
304 list_free_list(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200305}
306
307/*
308 * Allocate a list item.
309 * It is not initialized, don't forget to set v_lock.
310 */
311 listitem_T *
312listitem_alloc(void)
313{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200314 return ALLOC_ONE(listitem_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200315}
316
317/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318 * Free a list item, unless it was allocated together with the list itself.
319 * Does not clear the value. Does not notify watchers.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200320 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200321 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100322list_free_item(list_T *l, listitem_T *item)
323{
324 if (l->lv_with_items == 0 || item < (listitem_T *)l
325 || item >= (listitem_T *)(l + 1) + l->lv_with_items)
326 vim_free(item);
327}
328
329/*
330 * Free a list item, unless it was allocated together with the list itself.
331 * Also clears the value. Does not notify watchers.
332 */
333 void
334listitem_free(list_T *l, listitem_T *item)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200335{
336 clear_tv(&item->li_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100337 list_free_item(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200338}
339
340/*
341 * Remove a list item from a List and free it. Also clears the value.
342 */
343 void
344listitem_remove(list_T *l, listitem_T *item)
345{
346 vimlist_remove(l, item, item);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100347 listitem_free(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200348}
349
350/*
351 * Get the number of items in a list.
352 */
353 long
354list_len(list_T *l)
355{
356 if (l == NULL)
357 return 0L;
358 return l->lv_len;
359}
360
361/*
362 * Return TRUE when two lists have exactly the same values.
363 */
364 int
365list_equal(
366 list_T *l1,
367 list_T *l2,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100368 int ic, // ignore case for strings
369 int recursive) // TRUE when used recursively
Bram Moolenaarda861d62016-07-17 15:46:27 +0200370{
371 listitem_T *item1, *item2;
372
Bram Moolenaarda861d62016-07-17 15:46:27 +0200373 if (l1 == l2)
374 return TRUE;
375 if (list_len(l1) != list_len(l2))
376 return FALSE;
Bram Moolenaar7b293c72020-04-09 21:33:22 +0200377 if (list_len(l1) == 0)
378 // empty and NULL list are considered equal
379 return TRUE;
380 if (l1 == NULL || l2 == NULL)
381 return FALSE;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200382
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200383 CHECK_LIST_MATERIALIZE(l1);
384 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100385
Bram Moolenaarda861d62016-07-17 15:46:27 +0200386 for (item1 = l1->lv_first, item2 = l2->lv_first;
387 item1 != NULL && item2 != NULL;
388 item1 = item1->li_next, item2 = item2->li_next)
389 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
390 return FALSE;
391 return item1 == NULL && item2 == NULL;
392}
393
394/*
395 * Locate item with index "n" in list "l" and return it.
396 * A negative index is counted from the end; -1 is the last item.
397 * Returns NULL when "n" is out of range.
398 */
399 listitem_T *
400list_find(list_T *l, long n)
401{
402 listitem_T *item;
403 long idx;
404
405 if (l == NULL)
406 return NULL;
407
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100408 // Negative index is relative to the end.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200409 if (n < 0)
410 n = l->lv_len + n;
411
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100412 // Check for index out of range.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200413 if (n < 0 || n >= l->lv_len)
414 return NULL;
415
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200416 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100417
Christian Brabandtdf63da92023-11-23 20:14:28 +0100418 // range_list_materialize may reset l->lv_len
419 if (n >= l->lv_len)
420 return NULL;
421
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100422 // When there is a cached index may start search from there.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100423 if (l->lv_u.mat.lv_idx_item != NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200424 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100425 if (n < l->lv_u.mat.lv_idx / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200426 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100427 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200428 item = l->lv_first;
429 idx = 0;
430 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100431 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200432 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100433 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100434 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200435 idx = l->lv_len - 1;
436 }
437 else
438 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100439 // closest to the cached index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100440 item = l->lv_u.mat.lv_idx_item;
441 idx = l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200442 }
443 }
444 else
445 {
446 if (n < l->lv_len / 2)
447 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100448 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200449 item = l->lv_first;
450 idx = 0;
451 }
452 else
453 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100454 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100455 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200456 idx = l->lv_len - 1;
457 }
458 }
459
460 while (n > idx)
461 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100462 // search forward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200463 item = item->li_next;
464 ++idx;
465 }
466 while (n < idx)
467 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100468 // search backward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200469 item = item->li_prev;
470 --idx;
471 }
472
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100473 // cache the used index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100474 l->lv_u.mat.lv_idx = idx;
475 l->lv_u.mat.lv_idx_item = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200476
477 return item;
478}
479
480/*
481 * Get list item "l[idx]" as a number.
482 */
483 long
484list_find_nr(
485 list_T *l,
486 long idx,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100487 int *errorp) // set to TRUE when something wrong
Bram Moolenaarda861d62016-07-17 15:46:27 +0200488{
489 listitem_T *li;
490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491 if (l != NULL && l->lv_first == &range_list_item)
492 {
493 long n = idx;
494
495 // not materialized range() list: compute the value.
496 // Negative index is relative to the end.
497 if (n < 0)
498 n = l->lv_len + n;
499
500 // Check for index out of range.
501 if (n < 0 || n >= l->lv_len)
502 {
503 if (errorp != NULL)
504 *errorp = TRUE;
505 return -1L;
506 }
507
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100508 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509 }
510
Bram Moolenaarda861d62016-07-17 15:46:27 +0200511 li = list_find(l, idx);
512 if (li == NULL)
513 {
514 if (errorp != NULL)
515 *errorp = TRUE;
516 return -1L;
517 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100518 return (long)tv_get_number_chk(&li->li_tv, errorp);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200519}
520
521/*
522 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
523 */
524 char_u *
525list_find_str(list_T *l, long idx)
526{
527 listitem_T *li;
528
529 li = list_find(l, idx - 1);
530 if (li == NULL)
531 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000532 semsg(_(e_list_index_out_of_range_nr), idx);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200533 return NULL;
534 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100535 return tv_get_string(&li->li_tv);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200536}
537
538/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100539 * Like list_find() but when a negative index is used that is not found use
540 * zero and set "idx" to zero. Used for first index of a range.
541 */
542 listitem_T *
543list_find_index(list_T *l, long *idx)
544{
545 listitem_T *li = list_find(l, *idx);
546
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000547 if (li != NULL)
548 return li;
549
550 if (*idx < 0)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100551 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000552 *idx = 0;
553 li = list_find(l, *idx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100554 }
555 return li;
556}
557
558/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200559 * Locate "item" list "l" and return its index.
560 * Returns -1 when "item" is not in the list.
561 */
562 long
563list_idx_of_item(list_T *l, listitem_T *item)
564{
565 long idx = 0;
566 listitem_T *li;
567
568 if (l == NULL)
569 return -1;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200570 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200571 idx = 0;
572 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
573 ++idx;
574 if (li == NULL)
575 return -1;
576 return idx;
577}
578
579/*
580 * Append item "item" to the end of list "l".
581 */
582 void
583list_append(list_T *l, listitem_T *item)
584{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200585 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100586 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200587 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100588 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200589 l->lv_first = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200590 item->li_prev = NULL;
591 }
592 else
593 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100594 l->lv_u.mat.lv_last->li_next = item;
595 item->li_prev = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200596 }
Bram Moolenaar35c807d2022-01-27 16:36:29 +0000597 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200598 ++l->lv_len;
599 item->li_next = NULL;
600}
601
602/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200604 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200605 */
606 int
607list_append_tv(list_T *l, typval_T *tv)
608{
Bram Moolenaar90fba562021-07-09 19:17:55 +0200609 listitem_T *li;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200610
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200611 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200612 && check_typval_arg_type(l->lv_type->tt_member, tv,
613 NULL, 0) == FAIL)
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200614 return FAIL;
Bram Moolenaar90fba562021-07-09 19:17:55 +0200615 li = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200616 if (li == NULL)
617 return FAIL;
618 copy_tv(tv, &li->li_tv);
619 list_append(l, li);
620 return OK;
621}
622
623/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 * As list_append_tv() but move the value instead of copying it.
625 * Return FAIL when out of memory.
626 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200627 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100628list_append_tv_move(list_T *l, typval_T *tv)
629{
630 listitem_T *li = listitem_alloc();
631
632 if (li == NULL)
633 return FAIL;
634 li->li_tv = *tv;
635 list_append(l, li);
636 return OK;
637}
638
639/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200640 * Add a dictionary to a list. Used by getqflist().
641 * Return FAIL when out of memory.
642 */
643 int
644list_append_dict(list_T *list, dict_T *dict)
645{
646 listitem_T *li = listitem_alloc();
647
648 if (li == NULL)
649 return FAIL;
650 li->li_tv.v_type = VAR_DICT;
651 li->li_tv.v_lock = 0;
652 li->li_tv.vval.v_dict = dict;
653 list_append(list, li);
654 ++dict->dv_refcount;
655 return OK;
656}
657
658/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100659 * Append list2 to list1.
660 * Return FAIL when out of memory.
661 */
662 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200663list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100664{
665 listitem_T *li = listitem_alloc();
666
667 if (li == NULL)
668 return FAIL;
669 li->li_tv.v_type = VAR_LIST;
670 li->li_tv.v_lock = 0;
671 li->li_tv.vval.v_list = list2;
672 list_append(list1, li);
673 ++list2->lv_refcount;
674 return OK;
675}
676
677/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200678 * Make a copy of "str" and append it as an item to list "l".
679 * When "len" >= 0 use "str[len]".
680 * Returns FAIL when out of memory.
681 */
682 int
683list_append_string(list_T *l, char_u *str, int len)
684{
685 listitem_T *li = listitem_alloc();
686
687 if (li == NULL)
688 return FAIL;
689 list_append(l, li);
690 li->li_tv.v_type = VAR_STRING;
691 li->li_tv.v_lock = 0;
692 if (str == NULL)
693 li->li_tv.vval.v_string = NULL;
694 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
695 : vim_strsave(str))) == NULL)
696 return FAIL;
697 return OK;
698}
699
700/*
701 * Append "n" to list "l".
702 * Returns FAIL when out of memory.
703 */
704 int
705list_append_number(list_T *l, varnumber_T n)
706{
707 listitem_T *li;
708
709 li = listitem_alloc();
710 if (li == NULL)
711 return FAIL;
712 li->li_tv.v_type = VAR_NUMBER;
713 li->li_tv.v_lock = 0;
714 li->li_tv.vval.v_number = n;
715 list_append(l, li);
716 return OK;
717}
718
719/*
720 * Insert typval_T "tv" in list "l" before "item".
721 * If "item" is NULL append at the end.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100722 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200723 */
724 int
725list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
726{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100727 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200728
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100729 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200730 && check_typval_arg_type(l->lv_type->tt_member, tv,
731 NULL, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100732 return FAIL;
733 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200734 if (ni == NULL)
735 return FAIL;
736 copy_tv(tv, &ni->li_tv);
737 list_insert(l, ni, item);
738 return OK;
739}
740
741 void
742list_insert(list_T *l, listitem_T *ni, listitem_T *item)
743{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200744 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200745 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100746 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200747 list_append(l, ni);
748 else
749 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100750 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200751 ni->li_prev = item->li_prev;
752 ni->li_next = item;
753 if (item->li_prev == NULL)
754 {
755 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100756 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200757 }
758 else
759 {
760 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100761 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200762 }
763 item->li_prev = ni;
764 ++l->lv_len;
765 }
766}
767
768/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200769 * Get the list item in "l" with index "n1". "n1" is adjusted if needed.
Bram Moolenaar22ebd172022-04-01 15:26:58 +0100770 * In Vim9, it is at the end of the list, add an item if "can_append" is TRUE.
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200771 * Return NULL if there is no such item.
772 */
773 listitem_T *
Bram Moolenaar22ebd172022-04-01 15:26:58 +0100774check_range_index_one(list_T *l, long *n1, int can_append, int quiet)
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200775{
Bram Moolenaar22ebd172022-04-01 15:26:58 +0100776 long orig_n1 = *n1;
777 listitem_T *li = list_find_index(l, n1);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200778
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000779 if (li != NULL)
780 return li;
781
782 // Vim9: Allow for adding an item at the end.
783 if (can_append && in_vim9script()
784 && *n1 == l->lv_len && l->lv_lock == 0)
785 {
786 list_append_number(l, 0);
787 li = list_find_index(l, n1);
788 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200789 if (li == NULL)
790 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +0000791 if (!quiet)
792 semsg(_(e_list_index_out_of_range_nr), orig_n1);
793 return NULL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200794 }
795 return li;
796}
797
798/*
799 * Check that "n2" can be used as the second index in a range of list "l".
800 * If "n1" or "n2" is negative it is changed to the positive index.
801 * "li1" is the item for item "n1".
802 * Return OK or FAIL.
803 */
804 int
805check_range_index_two(
806 list_T *l,
807 long *n1,
808 listitem_T *li1,
809 long *n2,
810 int quiet)
811{
812 if (*n2 < 0)
813 {
814 listitem_T *ni = list_find(l, *n2);
815
816 if (ni == NULL)
817 {
818 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000819 semsg(_(e_list_index_out_of_range_nr), *n2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200820 return FAIL;
821 }
822 *n2 = list_idx_of_item(l, ni);
823 }
824
825 // Check that n2 isn't before n1.
826 if (*n1 < 0)
827 *n1 = list_idx_of_item(l, li1);
828 if (*n2 < *n1)
829 {
830 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000831 semsg(_(e_list_index_out_of_range_nr), *n2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200832 return FAIL;
833 }
834 return OK;
835}
836
837/*
838 * Assign values from list "src" into a range of "dest".
839 * "idx1_arg" is the index of the first item in "dest" to be replaced.
840 * "idx2" is the index of last item to be replaced, but when "empty_idx2" is
841 * TRUE then replace all items after "idx1".
842 * "op" is the operator, normally "=" but can be "+=" and the like.
843 * "varname" is used for error messages.
844 * Returns OK or FAIL.
845 */
846 int
847list_assign_range(
848 list_T *dest,
849 list_T *src,
850 long idx1_arg,
851 long idx2,
852 int empty_idx2,
853 char_u *op,
854 char_u *varname)
855{
856 listitem_T *src_li;
857 listitem_T *dest_li;
858 long idx1 = idx1_arg;
859 listitem_T *first_li = list_find_index(dest, &idx1);
860 long idx;
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200861 type_T *member_type = NULL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200862
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000863 // Check whether any of the list items is locked before making any changes.
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200864 idx = idx1;
865 dest_li = first_li;
866 for (src_li = src->lv_first; src_li != NULL && dest_li != NULL; )
867 {
868 if (value_check_lock(dest_li->li_tv.v_lock, varname, FALSE))
869 return FAIL;
870 src_li = src_li->li_next;
871 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
872 break;
873 dest_li = dest_li->li_next;
874 ++idx;
875 }
876
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200877 if (in_vim9script() && dest->lv_type != NULL
878 && dest->lv_type->tt_member != NULL)
879 member_type = dest->lv_type->tt_member;
880
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000881 // Assign the List values to the list items.
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200882 idx = idx1;
883 dest_li = first_li;
884 for (src_li = src->lv_first; src_li != NULL; )
885 {
886 if (op != NULL && *op != '=')
887 tv_op(&dest_li->li_tv, &src_li->li_tv, op);
888 else
889 {
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200890 if (member_type != NULL
891 && check_typval_arg_type(member_type, &src_li->li_tv,
892 NULL, 0) == FAIL)
893 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200894 clear_tv(&dest_li->li_tv);
895 copy_tv(&src_li->li_tv, &dest_li->li_tv);
896 }
897 src_li = src_li->li_next;
898 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
899 break;
900 if (dest_li->li_next == NULL)
901 {
902 // Need to add an empty item.
903 if (list_append_number(dest, 0) == FAIL)
904 {
905 src_li = NULL;
906 break;
907 }
908 }
909 dest_li = dest_li->li_next;
910 ++idx;
911 }
912 if (src_li != NULL)
913 {
914 emsg(_(e_list_value_has_more_items_than_targets));
915 return FAIL;
916 }
917 if (empty_idx2
918 ? (dest_li != NULL && dest_li->li_next != NULL)
919 : idx != idx2)
920 {
921 emsg(_(e_list_value_does_not_have_enough_items));
922 return FAIL;
923 }
924 return OK;
925}
926
927/*
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000928 * Flatten up to "maxitems" in "list", starting at "first" to depth "maxdepth".
929 * When "first" is NULL use the first item.
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200930 * It does nothing if "maxdepth" is 0.
931 * Returns FAIL when out of memory.
932 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100933 static void
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000934list_flatten(list_T *list, listitem_T *first, long maxitems, long maxdepth)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200935{
936 listitem_T *item;
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000937 int done = 0;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200938
939 if (maxdepth == 0)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100940 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200941 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000942 if (first == NULL)
943 item = list->lv_first;
944 else
945 item = first;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200946
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000947 while (item != NULL && done < maxitems)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200948 {
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000949 listitem_T *next = item->li_next;
950
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200951 fast_breakcheck();
952 if (got_int)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100953 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200954
955 if (item->li_tv.v_type == VAR_LIST)
956 {
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000957 list_T *itemlist = item->li_tv.vval.v_list;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200958
959 vimlist_remove(list, item, item);
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000960 if (list_extend(list, itemlist, next) == FAIL)
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +0200961 {
962 list_free_item(list, item);
Bram Moolenaar3b690062021-02-01 20:14:51 +0100963 return;
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +0200964 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200965
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000966 if (maxdepth > 0)
967 list_flatten(list, item->li_prev == NULL
968 ? list->lv_first : item->li_prev->li_next,
969 itemlist->lv_len, maxdepth - 1);
Bram Moolenaarf3980dc2022-03-26 16:42:23 +0000970 clear_tv(&item->li_tv);
Bram Moolenaarc6c1ec42022-03-26 10:50:11 +0000971 list_free_item(list, item);
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000972 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200973
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000974 ++done;
975 item = next;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200976 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200977}
978
979/*
Bram Moolenaar3b690062021-02-01 20:14:51 +0100980 * "flatten()" and "flattennew()" functions
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200981 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100982 static void
983flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200984{
985 list_T *l;
986 long maxdepth;
987 int error = FALSE;
988
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200989 if (in_vim9script()
990 && (check_for_list_arg(argvars, 0) == FAIL
991 || check_for_opt_number_arg(argvars, 1) == FAIL))
992 return;
993
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200994 if (argvars[0].v_type != VAR_LIST)
995 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +0000996 semsg(_(e_argument_of_str_must_be_list), "flatten()");
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200997 return;
998 }
999
1000 if (argvars[1].v_type == VAR_UNKNOWN)
1001 maxdepth = 999999;
1002 else
1003 {
1004 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
1005 if (error)
1006 return;
1007 if (maxdepth < 0)
1008 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001009 emsg(_(e_maxdepth_must_be_non_negative_number));
Bram Moolenaar077a1e62020-06-08 20:50:43 +02001010 return;
1011 }
1012 }
1013
1014 l = argvars[0].vval.v_list;
Bram Moolenaar3b690062021-02-01 20:14:51 +01001015 rettv->v_type = VAR_LIST;
1016 rettv->vval.v_list = l;
1017 if (l == NULL)
1018 return;
1019
1020 if (make_copy)
1021 {
Bram Moolenaarc6c1ec42022-03-26 10:50:11 +00001022 l = list_copy(l, FALSE, TRUE, get_copyID());
Bram Moolenaar3b690062021-02-01 20:14:51 +01001023 rettv->vval.v_list = l;
1024 if (l == NULL)
1025 return;
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +02001026 // The type will change.
1027 free_type(l->lv_type);
1028 l->lv_type = NULL;
Bram Moolenaar3b690062021-02-01 20:14:51 +01001029 }
1030 else
1031 {
1032 if (value_check_lock(l->lv_lock,
1033 (char_u *)N_("flatten() argument"), TRUE))
1034 return;
1035 ++l->lv_refcount;
1036 }
1037
Bram Moolenaaracf7d732022-03-25 19:50:57 +00001038 list_flatten(l, NULL, l->lv_len, maxdepth);
Bram Moolenaar3b690062021-02-01 20:14:51 +01001039}
1040
1041/*
1042 * "flatten(list[, {maxdepth}])" function
1043 */
1044 void
1045f_flatten(typval_T *argvars, typval_T *rettv)
1046{
1047 if (in_vim9script())
1048 emsg(_(e_cannot_use_flatten_in_vim9_script));
1049 else
1050 flatten_common(argvars, rettv, FALSE);
1051}
1052
1053/*
1054 * "flattennew(list[, {maxdepth}])" function
1055 */
1056 void
1057f_flattennew(typval_T *argvars, typval_T *rettv)
1058{
1059 flatten_common(argvars, rettv, TRUE);
Bram Moolenaar077a1e62020-06-08 20:50:43 +02001060}
1061
1062/*
Bram Moolenaar976f8592022-08-30 14:34:52 +01001063 * "items(list)" function
1064 * Caller must have already checked that argvars[0] is a List.
1065 */
1066 void
1067list2items(typval_T *argvars, typval_T *rettv)
1068{
1069 list_T *l = argvars[0].vval.v_list;
1070 listitem_T *li;
1071 varnumber_T idx;
1072
1073 if (rettv_list_alloc(rettv) == FAIL)
1074 return;
Bram Moolenaar976f8592022-08-30 14:34:52 +01001075 if (l == NULL)
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001076 return; // null list behaves like an empty list
Bram Moolenaar976f8592022-08-30 14:34:52 +01001077
1078 // TODO: would be more efficient to not materialize the argument
1079 CHECK_LIST_MATERIALIZE(l);
1080 for (idx = 0, li = l->lv_first; li != NULL; li = li->li_next, ++idx)
1081 {
1082 list_T *l2 = list_alloc();
1083
1084 if (l2 == NULL)
1085 break;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01001086 if (list_append_list(rettv->vval.v_list, l2) == FAIL)
1087 {
1088 vim_free(l2);
1089 break;
1090 }
1091 if (list_append_number(l2, idx) == FAIL
Bram Moolenaar976f8592022-08-30 14:34:52 +01001092 || list_append_tv(l2, &li->li_tv) == FAIL)
1093 break;
1094 }
1095}
1096
1097/*
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001098 * "items(string)" function
1099 * Caller must have already checked that argvars[0] is a String.
1100 */
1101 void
1102string2items(typval_T *argvars, typval_T *rettv)
1103{
1104 char_u *p = argvars[0].vval.v_string;
1105 varnumber_T idx;
1106
1107 if (rettv_list_alloc(rettv) == FAIL)
1108 return;
1109 if (p == NULL)
1110 return; // null string behaves like an empty string
1111
1112 for (idx = 0; *p != NUL; ++idx)
1113 {
1114 int len = mb_ptr2len(p);
1115 list_T *l2;
1116
1117 if (len == 0)
1118 break;
1119 l2 = list_alloc();
1120 if (l2 == NULL)
1121 break;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01001122 if (list_append_list(rettv->vval.v_list, l2) == FAIL)
1123 {
1124 vim_free(l2);
1125 break;
1126 }
1127 if (list_append_number(l2, idx) == FAIL
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001128 || list_append_string(l2, p, len) == FAIL)
1129 break;
1130 p += len;
1131 }
1132}
1133
1134/*
Bram Moolenaar1a739232020-10-10 15:37:58 +02001135 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001136 * If "bef" is NULL append at the end, otherwise insert before this item.
1137 * Returns FAIL when out of memory.
1138 */
1139 int
1140list_extend(list_T *l1, list_T *l2, listitem_T *bef)
1141{
1142 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001143 int todo;
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001144 listitem_T *bef_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001145
Bram Moolenaar1a739232020-10-10 15:37:58 +02001146 // NULL list is equivalent to an empty list: nothing to do.
1147 if (l2 == NULL || l2->lv_len == 0)
1148 return OK;
1149
1150 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001151 CHECK_LIST_MATERIALIZE(l1);
1152 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001154 // When exending a list with itself, at some point we run into the item
1155 // that was before "bef" and need to skip over the already inserted items
1156 // to "bef".
1157 bef_prev = bef == NULL ? NULL : bef->li_prev;
1158
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001159 // We also quit the loop when we have inserted the original item count of
1160 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001161 for (item = l2->lv_first; item != NULL && --todo >= 0;
1162 item = item == bef_prev ? bef : item->li_next)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001163 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
1164 return FAIL;
1165 return OK;
1166}
1167
1168/*
1169 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
1170 * Return FAIL when out of memory.
1171 */
1172 int
1173list_concat(list_T *l1, list_T *l2, typval_T *tv)
1174{
1175 list_T *l;
1176
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001177 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +02001178 if (l1 == NULL)
1179 l = list_alloc();
1180 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00001181 l = list_copy(l1, FALSE, TRUE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001182 if (l == NULL)
1183 return FAIL;
1184 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +01001185 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001186 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001187 if (l1 == NULL)
1188 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001189
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001190 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +02001191 return list_extend(l, l2, NULL);
1192}
1193
Bram Moolenaar9af78762020-06-16 11:34:42 +02001194 list_T *
1195list_slice(list_T *ol, long n1, long n2)
1196{
1197 listitem_T *item;
1198 list_T *l = list_alloc();
1199
1200 if (l == NULL)
1201 return NULL;
1202 for (item = list_find(ol, n1); n1 <= n2; ++n1)
1203 {
1204 if (list_append_tv(l, &item->li_tv) == FAIL)
1205 {
1206 list_free(l);
1207 return NULL;
1208 }
1209 item = item->li_next;
1210 }
1211 return l;
1212}
1213
Bram Moolenaared591872020-08-15 22:14:53 +02001214 int
1215list_slice_or_index(
1216 list_T *list,
1217 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01001218 varnumber_T n1_arg,
1219 varnumber_T n2_arg,
1220 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +02001221 typval_T *rettv,
1222 int verbose)
1223{
1224 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001225 varnumber_T n1 = n1_arg;
1226 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +02001227 typval_T var1;
1228
1229 if (n1 < 0)
1230 n1 = len + n1;
1231 if (n1 < 0 || n1 >= len)
1232 {
Bram Moolenaar92f05f22021-08-12 21:12:56 +02001233 // For a range we allow invalid values and for legacy script return an
1234 // empty list, for Vim9 script start at the first item.
1235 // A list index out of range is an error.
Bram Moolenaared591872020-08-15 22:14:53 +02001236 if (!range)
1237 {
1238 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001239 semsg(_(e_list_index_out_of_range_nr), (long)n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +02001240 return FAIL;
1241 }
Bram Moolenaar92f05f22021-08-12 21:12:56 +02001242 if (in_vim9script())
1243 n1 = n1 < 0 ? 0 : len;
1244 else
1245 n1 = len;
Bram Moolenaared591872020-08-15 22:14:53 +02001246 }
1247 if (range)
1248 {
1249 list_T *l;
1250
1251 if (n2 < 0)
1252 n2 = len + n2;
1253 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01001254 n2 = len - (exclusive ? 0 : 1);
1255 if (exclusive)
1256 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +02001257 if (n2 < 0 || n2 + 1 < n1)
1258 n2 = -1;
1259 l = list_slice(list, n1, n2);
1260 if (l == NULL)
1261 return FAIL;
1262 clear_tv(rettv);
1263 rettv_list_set(rettv, l);
1264 }
1265 else
1266 {
1267 // copy the item to "var1" to avoid that freeing the list makes it
1268 // invalid.
1269 copy_tv(&list_find(list, n1)->li_tv, &var1);
1270 clear_tv(rettv);
1271 *rettv = var1;
1272 }
1273 return OK;
1274}
1275
Bram Moolenaarda861d62016-07-17 15:46:27 +02001276/*
1277 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1278 * The refcount of the new list is set to 1.
Bram Moolenaar381692b2022-02-02 20:01:27 +00001279 * See item_copy() for "top" and "copyID".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001280 * Returns NULL when out of memory.
1281 */
1282 list_T *
Bram Moolenaar381692b2022-02-02 20:01:27 +00001283list_copy(list_T *orig, int deep, int top, int copyID)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001284{
1285 list_T *copy;
1286 listitem_T *item;
1287 listitem_T *ni;
1288
1289 if (orig == NULL)
1290 return NULL;
1291
1292 copy = list_alloc();
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001293 if (copy == NULL)
1294 return NULL;
1295
1296 if (orig->lv_type == NULL || top || deep)
1297 copy->lv_type = NULL;
1298 else
1299 copy->lv_type = alloc_type(orig->lv_type);
1300 if (copyID != 0)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001301 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001302 // Do this before adding the items, because one of the items may
1303 // refer back to this list.
1304 orig->lv_copyID = copyID;
1305 orig->lv_copylist = copy;
1306 }
1307 CHECK_LIST_MATERIALIZE(orig);
1308 for (item = orig->lv_first; item != NULL && !got_int;
1309 item = item->li_next)
1310 {
1311 ni = listitem_alloc();
1312 if (ni == NULL)
1313 break;
1314 if (deep)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001315 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001316 if (item_copy(&item->li_tv, &ni->li_tv,
1317 deep, FALSE, copyID) == FAIL)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001318 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001319 vim_free(ni);
1320 break;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001321 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001322 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001323 else
1324 copy_tv(&item->li_tv, &ni->li_tv);
1325 list_append(copy, ni);
1326 }
1327 ++copy->lv_refcount;
1328 if (item != NULL)
1329 {
1330 list_unref(copy);
1331 copy = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001332 }
1333
1334 return copy;
1335}
1336
1337/*
1338 * Remove items "item" to "item2" from list "l".
1339 * Does not free the listitem or the value!
1340 * This used to be called list_remove, but that conflicts with a Sun header
1341 * file.
1342 */
1343 void
1344vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1345{
1346 listitem_T *ip;
1347
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001348 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001350 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001351 for (ip = item; ip != NULL; ip = ip->li_next)
1352 {
1353 --l->lv_len;
1354 list_fix_watch(l, ip);
1355 if (ip == item2)
1356 break;
1357 }
1358
1359 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001360 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001361 else
1362 item2->li_next->li_prev = item->li_prev;
1363 if (item->li_prev == NULL)
1364 l->lv_first = item2->li_next;
1365 else
1366 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001367 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001368}
1369
1370/*
1371 * Return an allocated string with the string representation of a list.
1372 * May return NULL.
1373 */
1374 char_u *
1375list2string(typval_T *tv, int copyID, int restore_copyID)
1376{
1377 garray_T ga;
1378
1379 if (tv->vval.v_list == NULL)
1380 return NULL;
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001381 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001382 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001383 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001384 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1385 FALSE, restore_copyID, copyID) == FAIL)
1386 {
1387 vim_free(ga.ga_data);
1388 return NULL;
1389 }
1390 ga_append(&ga, ']');
1391 ga_append(&ga, NUL);
1392 return (char_u *)ga.ga_data;
1393}
1394
1395typedef struct join_S {
1396 char_u *s;
1397 char_u *tofree;
1398} join_T;
1399
1400 static int
1401list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001402 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001403 list_T *l,
1404 char_u *sep,
1405 int echo_style,
1406 int restore_copyID,
1407 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001408 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001409{
1410 int i;
1411 join_T *p;
1412 int len;
1413 int sumlen = 0;
1414 int first = TRUE;
1415 char_u *tofree;
1416 char_u numbuf[NUMBUFLEN];
1417 listitem_T *item;
1418 char_u *s;
1419
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001420 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001421 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001422 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1423 {
1424 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001425 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001426 if (s == NULL)
1427 return FAIL;
1428
1429 len = (int)STRLEN(s);
1430 sumlen += len;
1431
1432 (void)ga_grow(join_gap, 1);
1433 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1434 if (tofree != NULL || s != numbuf)
1435 {
1436 p->s = s;
1437 p->tofree = tofree;
1438 }
1439 else
1440 {
1441 p->s = vim_strnsave(s, len);
1442 p->tofree = p->s;
1443 }
1444
1445 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001446 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001447 break;
1448 }
1449
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001450 // Allocate result buffer with its total size, avoid re-allocation and
1451 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001452 if (join_gap->ga_len >= 2)
1453 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1454 if (ga_grow(gap, sumlen + 2) == FAIL)
1455 return FAIL;
1456
1457 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1458 {
1459 if (first)
1460 first = FALSE;
1461 else
1462 ga_concat(gap, sep);
1463 p = ((join_T *)join_gap->ga_data) + i;
1464
1465 if (p->s != NULL)
1466 ga_concat(gap, p->s);
1467 line_breakcheck();
1468 }
1469
1470 return OK;
1471}
1472
1473/*
1474 * Join list "l" into a string in "*gap", using separator "sep".
1475 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1476 * Return FAIL or OK.
1477 */
1478 int
1479list_join(
1480 garray_T *gap,
1481 list_T *l,
1482 char_u *sep,
1483 int echo_style,
1484 int restore_copyID,
1485 int copyID)
1486{
1487 garray_T join_ga;
1488 int retval;
1489 join_T *p;
1490 int i;
1491
1492 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001493 return OK; // nothing to do
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001494 ga_init2(&join_ga, sizeof(join_T), l->lv_len);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001495 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1496 copyID, &join_ga);
1497
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001498 if (join_ga.ga_data == NULL)
1499 return retval;
1500
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001501 // Dispose each item in join_ga.
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001502 p = (join_T *)join_ga.ga_data;
1503 for (i = 0; i < join_ga.ga_len; ++i)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001504 {
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001505 vim_free(p->tofree);
1506 ++p;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001507 }
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00001508 ga_clear(&join_ga);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001509
1510 return retval;
1511}
1512
1513/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001514 * "join()" function
1515 */
1516 void
1517f_join(typval_T *argvars, typval_T *rettv)
1518{
1519 garray_T ga;
1520 char_u *sep;
1521
Yegappan Lakshmananca195cc2022-06-14 13:42:26 +01001522 rettv->v_type = VAR_STRING;
1523
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001524 if (in_vim9script()
1525 && (check_for_list_arg(argvars, 0) == FAIL
1526 || check_for_opt_string_arg(argvars, 1) == FAIL))
1527 return;
1528
Bram Moolenaard83392a2022-09-01 12:22:46 +01001529 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001530 return;
Yegappan Lakshmananca195cc2022-06-14 13:42:26 +01001531
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001532 if (argvars[0].vval.v_list == NULL)
1533 return;
Bram Moolenaaref982572021-08-12 19:27:57 +02001534
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001535 if (argvars[1].v_type == VAR_UNKNOWN)
1536 sep = (char_u *)" ";
1537 else
1538 sep = tv_get_string_chk(&argvars[1]);
1539
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001540 if (sep != NULL)
1541 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001542 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001543 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1544 ga_append(&ga, NUL);
1545 rettv->vval.v_string = (char_u *)ga.ga_data;
1546 }
1547 else
1548 rettv->vval.v_string = NULL;
1549}
1550
1551/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001552 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001553 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001554 * Return OK or FAIL.
1555 */
1556 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001557eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001558{
Bram Moolenaar71478202020-06-26 22:46:27 +02001559 int evaluate = evalarg == NULL ? FALSE
1560 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001561 list_T *l = NULL;
1562 typval_T tv;
1563 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001564 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001565 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001566
1567 if (evaluate)
1568 {
1569 l = list_alloc();
1570 if (l == NULL)
1571 return FAIL;
1572 }
1573
Bram Moolenaar962d7212020-07-04 14:15:00 +02001574 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001575 while (**arg != ']' && **arg != NUL)
1576 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001577 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001578 goto failret;
Ernie Raelfa831102023-12-14 20:06:39 +01001579 if (check_typval_is_value(&tv) == FAIL)
1580 {
1581 if (evaluate)
1582 clear_tv(&tv);
1583 goto failret;
1584 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001585 if (evaluate)
1586 {
1587 item = listitem_alloc();
1588 if (item != NULL)
1589 {
1590 item->li_tv = tv;
1591 item->li_tv.v_lock = 0;
1592 list_append(l, item);
1593 }
1594 else
1595 clear_tv(&tv);
1596 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001597 // Legacy Vim script allowed a space before the comma.
1598 if (!vim9script)
1599 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001600
Bram Moolenaare6e03172020-06-27 16:36:05 +02001601 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001602 had_comma = **arg == ',';
1603 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001604 {
Christian Brabandt00cb2472023-09-05 20:46:25 +02001605 if (vim9script && !IS_WHITE_NL_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001606 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001607 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001608 goto failret;
1609 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001610 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001611 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001612
Bram Moolenaare6b53242020-07-01 17:28:33 +02001613 // The "]" can be on the next line. But a double quoted string may
1614 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001615 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001616 if (**arg == ']')
1617 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001618
1619 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001620 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001622 {
1623 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001624 semsg(_(e_no_white_space_allowed_before_str_str),
1625 ",", *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001626 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001627 semsg(_(e_missing_comma_in_list_str), *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001628 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001629 goto failret;
1630 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001631 }
1632
1633 if (**arg != ']')
1634 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 if (do_error)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001636 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001637failret:
1638 if (evaluate)
1639 list_free(l);
1640 return FAIL;
1641 }
1642
Bram Moolenaar9d489562020-07-30 20:08:50 +02001643 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001644 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001645 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001646
1647 return OK;
1648}
1649
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001650/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001651 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001652 */
1653 int
1654write_list(FILE *fd, list_T *list, int binary)
1655{
1656 listitem_T *li;
1657 int c;
1658 int ret = OK;
1659 char_u *s;
1660
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001661 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001662 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001663 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001664 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001665 {
1666 if (*s == '\n')
1667 c = putc(NUL, fd);
1668 else
1669 c = putc(*s, fd);
1670 if (c == EOF)
1671 {
1672 ret = FAIL;
1673 break;
1674 }
1675 }
1676 if (!binary || li->li_next != NULL)
1677 if (putc('\n', fd) == EOF)
1678 {
1679 ret = FAIL;
1680 break;
1681 }
1682 if (ret == FAIL)
1683 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001684 emsg(_(e_error_while_writing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001685 break;
1686 }
1687 }
1688 return ret;
1689}
1690
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001691/*
1692 * Initialize a static list with 10 items.
1693 */
1694 void
1695init_static_list(staticList10_T *sl)
1696{
1697 list_T *l = &sl->sl_list;
1698 int i;
1699
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00001700 CLEAR_POINTER(sl);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001701 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001702 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001703 l->lv_refcount = DO_NOT_FREE_CNT;
1704 l->lv_lock = VAR_FIXED;
1705 sl->sl_list.lv_len = 10;
1706
1707 for (i = 0; i < 10; ++i)
1708 {
1709 listitem_T *li = &sl->sl_items[i];
1710
1711 if (i == 0)
1712 li->li_prev = NULL;
1713 else
1714 li->li_prev = li - 1;
1715 if (i == 9)
1716 li->li_next = NULL;
1717 else
1718 li->li_next = li + 1;
1719 }
1720}
1721
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001722/*
1723 * "list2str()" function
1724 */
1725 void
1726f_list2str(typval_T *argvars, typval_T *rettv)
1727{
1728 list_T *l;
1729 listitem_T *li;
1730 garray_T ga;
1731 int utf8 = FALSE;
1732
1733 rettv->v_type = VAR_STRING;
1734 rettv->vval.v_string = NULL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001735
1736 if (in_vim9script()
1737 && (check_for_list_arg(argvars, 0) == FAIL
1738 || check_for_opt_bool_arg(argvars, 1) == FAIL))
1739 return;
1740
Bram Moolenaard83392a2022-09-01 12:22:46 +01001741 if (check_for_list_arg(argvars, 0) == FAIL)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001742 return;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001743
1744 l = argvars[0].vval.v_list;
1745 if (l == NULL)
1746 return; // empty list results in empty string
1747
1748 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001749 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001750
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001751 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001752 ga_init2(&ga, 1, 80);
1753 if (has_mbyte || utf8)
1754 {
1755 char_u buf[MB_MAXBYTES + 1];
1756 int (*char2bytes)(int, char_u *);
1757
1758 if (utf8 || enc_utf8)
1759 char2bytes = utf_char2bytes;
1760 else
1761 char2bytes = mb_char2bytes;
1762
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001763 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001764 {
1765 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1766 ga_concat(&ga, buf);
1767 }
1768 ga_append(&ga, NUL);
1769 }
1770 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1771 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001772 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001773 ga_append(&ga, tv_get_number(&li->li_tv));
1774 ga_append(&ga, NUL);
1775 }
1776
1777 rettv->v_type = VAR_STRING;
1778 rettv->vval.v_string = ga.ga_data;
1779}
1780
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001781/*
1782 * Remove item argvars[1] from List argvars[0]. If argvars[2] is supplied, then
1783 * remove the range of items from argvars[1] to argvars[2] (inclusive).
1784 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001785 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001786list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1787{
1788 list_T *l;
1789 listitem_T *item, *item2;
1790 listitem_T *li;
1791 int error = FALSE;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001792 long idx;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001793 long end;
1794 int cnt = 0;
1795 list_T *rl;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001796
1797 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001798 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001799 return;
1800
1801 idx = (long)tv_get_number_chk(&argvars[1], &error);
1802 if (error)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001803 return; // type error: do nothing, errmsg already given
1804
1805 if ((item = list_find(l, idx)) == NULL)
1806 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001807 semsg(_(e_list_index_out_of_range_nr), idx);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001808 return;
1809 }
1810
1811 if (argvars[2].v_type == VAR_UNKNOWN)
1812 {
1813 // Remove one item, return its value.
1814 vimlist_remove(l, item, item);
1815 *rettv = item->li_tv;
1816 list_free_item(l, item);
1817 return;
1818 }
1819
1820 // Remove range of items, return list with values.
1821 end = (long)tv_get_number_chk(&argvars[2], &error);
1822 if (error)
1823 return; // type error: do nothing
1824
1825 if ((item2 = list_find(l, end)) == NULL)
1826 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001827 semsg(_(e_list_index_out_of_range_nr), end);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001828 return;
1829 }
1830
1831 for (li = item; li != NULL; li = li->li_next)
1832 {
1833 ++cnt;
1834 if (li == item2)
1835 break;
1836 }
1837 if (li == NULL) // didn't find "item2" after "item"
1838 {
1839 emsg(_(e_invalid_range));
1840 return;
1841 }
1842
1843 vimlist_remove(l, item, item2);
Bram Moolenaar93a10962022-06-16 11:42:09 +01001844 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001845 return;
1846
1847 rl = rettv->vval.v_list;
1848
1849 if (l->lv_with_items > 0)
1850 {
1851 // need to copy the list items and move the value
1852 while (item != NULL)
1853 {
1854 li = listitem_alloc();
1855 if (li == NULL)
1856 return;
1857 li->li_tv = item->li_tv;
1858 init_tv(&item->li_tv);
1859 list_append(rl, li);
1860 if (item == item2)
1861 break;
1862 item = item->li_next;
1863 }
1864 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001865 else
1866 {
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001867 rl->lv_first = item;
1868 rl->lv_u.mat.lv_last = item2;
1869 item->li_prev = NULL;
1870 item2->li_next = NULL;
1871 rl->lv_len = cnt;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001872 }
1873}
1874
1875static int item_compare(const void *s1, const void *s2);
1876static int item_compare2(const void *s1, const void *s2);
1877
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001878// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001879typedef struct
1880{
1881 listitem_T *item;
1882 int idx;
1883} sortItem_T;
1884
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001885// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001886typedef struct
1887{
1888 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001889 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001890 int item_compare_numeric;
1891 int item_compare_numbers;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001892 int item_compare_float;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001893 char_u *item_compare_func;
1894 partial_T *item_compare_partial;
1895 dict_T *item_compare_selfdict;
1896 int item_compare_func_err;
1897 int item_compare_keep_zero;
1898} sortinfo_T;
1899static sortinfo_T *sortinfo = NULL;
1900#define ITEM_COMPARE_FAIL 999
1901
1902/*
1903 * Compare functions for f_sort() and f_uniq() below.
1904 */
1905 static int
1906item_compare(const void *s1, const void *s2)
1907{
1908 sortItem_T *si1, *si2;
1909 typval_T *tv1, *tv2;
1910 char_u *p1, *p2;
1911 char_u *tofree1 = NULL, *tofree2 = NULL;
1912 int res;
1913 char_u numbuf1[NUMBUFLEN];
1914 char_u numbuf2[NUMBUFLEN];
1915
1916 si1 = (sortItem_T *)s1;
1917 si2 = (sortItem_T *)s2;
1918 tv1 = &si1->item->li_tv;
1919 tv2 = &si2->item->li_tv;
1920
1921 if (sortinfo->item_compare_numbers)
1922 {
Bram Moolenaarbe19d782023-03-09 22:06:49 +00001923 varnumber_T v1 = tv_to_number(tv1);
1924 varnumber_T v2 = tv_to_number(tv2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001925
1926 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1927 }
1928
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001929 if (sortinfo->item_compare_float)
1930 {
1931 float_T v1 = tv_get_float(tv1);
1932 float_T v2 = tv_get_float(tv2);
1933
1934 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1935 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001936
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001937 // tv2string() puts quotes around a string and allocates memory. Don't do
1938 // that for string variables. Use a single quote when comparing with a
1939 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001940 if (tv1->v_type == VAR_STRING)
1941 {
1942 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1943 p1 = (char_u *)"'";
1944 else
1945 p1 = tv1->vval.v_string;
1946 }
1947 else
1948 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1949 if (tv2->v_type == VAR_STRING)
1950 {
1951 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1952 p2 = (char_u *)"'";
1953 else
1954 p2 = tv2->vval.v_string;
1955 }
1956 else
1957 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1958 if (p1 == NULL)
1959 p1 = (char_u *)"";
1960 if (p2 == NULL)
1961 p2 = (char_u *)"";
1962 if (!sortinfo->item_compare_numeric)
1963 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001964 if (sortinfo->item_compare_lc)
1965 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001966 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001967 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001968 }
1969 else
1970 {
1971 double n1, n2;
1972 n1 = strtod((char *)p1, (char **)&p1);
1973 n2 = strtod((char *)p2, (char **)&p2);
1974 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1975 }
1976
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001977 // When the result would be zero, compare the item indexes. Makes the
1978 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001979 if (res == 0 && !sortinfo->item_compare_keep_zero)
1980 res = si1->idx > si2->idx ? 1 : -1;
1981
1982 vim_free(tofree1);
1983 vim_free(tofree2);
1984 return res;
1985}
1986
1987 static int
1988item_compare2(const void *s1, const void *s2)
1989{
1990 sortItem_T *si1, *si2;
1991 int res;
1992 typval_T rettv;
1993 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001994 char_u *func_name;
1995 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001996 funcexe_T funcexe;
Bram Moolenaar23018f22021-12-27 11:54:37 +00001997 int did_emsg_before = did_emsg;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001998
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001999 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002000 if (sortinfo->item_compare_func_err)
2001 return 0;
2002
2003 si1 = (sortItem_T *)s1;
2004 si2 = (sortItem_T *)s2;
2005
2006 if (partial == NULL)
2007 func_name = sortinfo->item_compare_func;
2008 else
2009 func_name = partial_name(partial);
2010
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002011 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
2012 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002013 copy_tv(&si1->item->li_tv, &argv[0]);
2014 copy_tv(&si2->item->li_tv, &argv[1]);
2015
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002016 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02002017 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002018 funcexe.fe_evaluate = TRUE;
2019 funcexe.fe_partial = partial;
2020 funcexe.fe_selfdict = sortinfo->item_compare_selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002021 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002022 clear_tv(&argv[0]);
2023 clear_tv(&argv[1]);
2024
Bram Moolenaar23018f22021-12-27 11:54:37 +00002025 if (res == FAIL || did_emsg > did_emsg_before)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002026 res = ITEM_COMPARE_FAIL;
2027 else
Yasuhiro Matsumotoc04f6232021-09-19 17:01:39 +02002028 {
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002029 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Yasuhiro Matsumotoc04f6232021-09-19 17:01:39 +02002030 if (res > 0)
2031 res = 1;
2032 else if (res < 0)
2033 res = -1;
2034 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002035 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002036 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002037 clear_tv(&rettv);
2038
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002039 // When the result would be zero, compare the pointers themselves. Makes
2040 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002041 if (res == 0 && !sortinfo->item_compare_keep_zero)
2042 res = si1->idx > si2->idx ? 1 : -1;
2043
2044 return res;
2045}
2046
2047/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002048 * sort() List "l"
2049 */
2050 static void
2051do_sort(list_T *l, sortinfo_T *info)
2052{
2053 long len;
2054 sortItem_T *ptrs;
2055 long i = 0;
2056 listitem_T *li;
2057
2058 len = list_len(l);
2059
2060 // Make an array with each entry pointing to an item in the List.
2061 ptrs = ALLOC_MULT(sortItem_T, len);
2062 if (ptrs == NULL)
2063 return;
2064
2065 // sort(): ptrs will be the list to sort
2066 FOR_ALL_LIST_ITEMS(l, li)
2067 {
2068 ptrs[i].item = li;
2069 ptrs[i].idx = i;
2070 ++i;
2071 }
2072
2073 info->item_compare_func_err = FALSE;
2074 info->item_compare_keep_zero = FALSE;
2075 // test the compare function
2076 if ((info->item_compare_func != NULL
2077 || info->item_compare_partial != NULL)
2078 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
2079 == ITEM_COMPARE_FAIL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002080 emsg(_(e_sort_compare_function_failed));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002081 else
2082 {
2083 // Sort the array with item pointers.
2084 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
2085 info->item_compare_func == NULL
2086 && info->item_compare_partial == NULL
2087 ? item_compare : item_compare2);
2088
2089 if (!info->item_compare_func_err)
2090 {
2091 // Clear the List and append the items in sorted order.
2092 l->lv_first = l->lv_u.mat.lv_last
2093 = l->lv_u.mat.lv_idx_item = NULL;
2094 l->lv_len = 0;
2095 for (i = 0; i < len; ++i)
2096 list_append(l, ptrs[i].item);
2097 }
2098 }
2099
2100 vim_free(ptrs);
2101}
2102
2103/*
2104 * uniq() List "l"
2105 */
2106 static void
2107do_uniq(list_T *l, sortinfo_T *info)
2108{
2109 long len;
2110 sortItem_T *ptrs;
2111 long i = 0;
2112 listitem_T *li;
2113 int (*item_compare_func_ptr)(const void *, const void *);
2114
2115 len = list_len(l);
2116
2117 // Make an array with each entry pointing to an item in the List.
2118 ptrs = ALLOC_MULT(sortItem_T, len);
2119 if (ptrs == NULL)
2120 return;
2121
2122 // f_uniq(): ptrs will be a stack of items to remove
2123 info->item_compare_func_err = FALSE;
2124 info->item_compare_keep_zero = TRUE;
2125 item_compare_func_ptr = info->item_compare_func != NULL
2126 || info->item_compare_partial != NULL
2127 ? item_compare2 : item_compare;
2128
2129 for (li = l->lv_first; li != NULL && li->li_next != NULL;
2130 li = li->li_next)
2131 {
2132 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
2133 == 0)
2134 ptrs[i++].item = li;
2135 if (info->item_compare_func_err)
2136 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002137 emsg(_(e_uniq_compare_function_failed));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002138 break;
2139 }
2140 }
2141
2142 if (!info->item_compare_func_err)
2143 {
2144 while (--i >= 0)
2145 {
2146 li = ptrs[i].item->li_next;
2147 ptrs[i].item->li_next = li->li_next;
2148 if (li->li_next != NULL)
2149 li->li_next->li_prev = ptrs[i].item;
2150 else
2151 l->lv_u.mat.lv_last = ptrs[i].item;
2152 list_fix_watch(l, li);
2153 listitem_free(l, li);
2154 l->lv_len--;
2155 }
2156 }
2157
2158 vim_free(ptrs);
2159}
2160
2161/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002162 * Parse the optional arguments supplied to the sort() or uniq() function and
2163 * return the values in "info".
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002164 */
2165 static int
2166parse_sort_uniq_args(typval_T *argvars, sortinfo_T *info)
2167{
2168 info->item_compare_ic = FALSE;
2169 info->item_compare_lc = FALSE;
2170 info->item_compare_numeric = FALSE;
2171 info->item_compare_numbers = FALSE;
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002172 info->item_compare_float = FALSE;
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002173 info->item_compare_func = NULL;
2174 info->item_compare_partial = NULL;
2175 info->item_compare_selfdict = NULL;
2176
2177 if (argvars[1].v_type == VAR_UNKNOWN)
2178 return OK;
2179
2180 // optional second argument: {func}
2181 if (argvars[1].v_type == VAR_FUNC)
2182 info->item_compare_func = argvars[1].vval.v_string;
2183 else if (argvars[1].v_type == VAR_PARTIAL)
2184 info->item_compare_partial = argvars[1].vval.v_partial;
2185 else
2186 {
2187 int error = FALSE;
2188 int nr = 0;
2189
2190 if (argvars[1].v_type == VAR_NUMBER)
2191 {
2192 nr = tv_get_number_chk(&argvars[1], &error);
2193 if (error)
2194 return FAIL;
2195 if (nr == 1)
2196 info->item_compare_ic = TRUE;
2197 }
2198 if (nr != 1)
2199 {
2200 if (argvars[1].v_type != VAR_NUMBER)
2201 info->item_compare_func = tv_get_string(&argvars[1]);
2202 else if (nr != 0)
2203 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002204 emsg(_(e_invalid_argument));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002205 return FAIL;
2206 }
2207 }
2208 if (info->item_compare_func != NULL)
2209 {
2210 if (*info->item_compare_func == NUL)
2211 {
2212 // empty string means default sort
2213 info->item_compare_func = NULL;
2214 }
2215 else if (STRCMP(info->item_compare_func, "n") == 0)
2216 {
2217 info->item_compare_func = NULL;
2218 info->item_compare_numeric = TRUE;
2219 }
2220 else if (STRCMP(info->item_compare_func, "N") == 0)
2221 {
2222 info->item_compare_func = NULL;
2223 info->item_compare_numbers = TRUE;
2224 }
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002225 else if (STRCMP(info->item_compare_func, "f") == 0)
2226 {
2227 info->item_compare_func = NULL;
2228 info->item_compare_float = TRUE;
2229 }
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002230 else if (STRCMP(info->item_compare_func, "i") == 0)
2231 {
2232 info->item_compare_func = NULL;
2233 info->item_compare_ic = TRUE;
2234 }
2235 else if (STRCMP(info->item_compare_func, "l") == 0)
2236 {
2237 info->item_compare_func = NULL;
2238 info->item_compare_lc = TRUE;
2239 }
2240 }
2241 }
2242
2243 if (argvars[2].v_type != VAR_UNKNOWN)
2244 {
2245 // optional third argument: {dict}
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002246 if (check_for_dict_arg(argvars, 2) == FAIL)
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002247 return FAIL;
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002248 info->item_compare_selfdict = argvars[2].vval.v_dict;
2249 }
2250
2251 return OK;
2252}
2253
2254/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002255 * "sort()" or "uniq()" function
2256 */
2257 static void
2258do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
2259{
2260 list_T *l;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002261 sortinfo_T *old_sortinfo;
2262 sortinfo_T info;
2263 long len;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002264
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002265 if (in_vim9script()
2266 && (check_for_list_arg(argvars, 0) == FAIL
2267 || (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar2007dd42022-02-23 13:17:47 +00002268 && (check_for_string_or_func_arg(argvars, 1) == FAIL
2269 || check_for_opt_dict_arg(argvars, 2) == FAIL))))
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002270 return;
2271
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002272 if (argvars[0].v_type != VAR_LIST)
2273 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002274 semsg(_(e_argument_of_str_must_be_list), sort ? "sort()" : "uniq()");
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002275 return;
2276 }
2277
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002278 // Pointer to current info struct used in compare function. Save and
2279 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002280 old_sortinfo = sortinfo;
2281 sortinfo = &info;
2282
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002283 l = argvars[0].vval.v_list;
2284 if (l != NULL && value_check_lock(l->lv_lock,
2285 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
2286 TRUE))
2287 goto theend;
2288 rettv_list_set(rettv, l);
2289 if (l == NULL)
2290 goto theend;
2291 CHECK_LIST_MATERIALIZE(l);
2292
2293 len = list_len(l);
2294 if (len <= 1)
2295 goto theend; // short list sorts pretty quickly
2296
2297 if (parse_sort_uniq_args(argvars, &info) == FAIL)
2298 goto theend;
2299
2300 if (sort)
2301 do_sort(l, &info);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002302 else
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002303 do_uniq(l, &info);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002304
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002305theend:
2306 sortinfo = old_sortinfo;
2307}
2308
2309/*
2310 * "sort({list})" function
2311 */
2312 void
2313f_sort(typval_T *argvars, typval_T *rettv)
2314{
2315 do_sort_uniq(argvars, rettv, TRUE);
2316}
2317
2318/*
2319 * "uniq({list})" function
2320 */
2321 void
2322f_uniq(typval_T *argvars, typval_T *rettv)
2323{
2324 do_sort_uniq(argvars, rettv, FALSE);
2325}
2326
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002327/*
2328 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01002329 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002330 */
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002331 int
Bram Moolenaarea696852020-11-09 18:31:39 +01002332filter_map_one(
2333 typval_T *tv, // original value
2334 typval_T *expr, // callback
2335 filtermap_T filtermap,
Bram Moolenaar82418262022-09-28 16:16:15 +01002336 funccall_T *fc, // from eval_expr_get_funccal()
Bram Moolenaarea696852020-11-09 18:31:39 +01002337 typval_T *newtv, // for map() and mapnew(): new value
2338 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002339{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002340 typval_T argv[3];
2341 int retval = FAIL;
2342
2343 copy_tv(tv, get_vim_var_tv(VV_VAL));
2344 argv[0] = *get_vim_var_tv(VV_KEY);
2345 argv[1] = *get_vim_var_tv(VV_VAL);
zeertzjqad0c4422023-08-17 22:15:47 +02002346 if (eval_expr_typval(expr, FALSE, argv, 2, fc, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002347 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002348 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002349 {
2350 int error = FALSE;
2351
2352 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02002353 if (in_vim9script())
Bram Moolenaar18024052021-12-25 21:43:28 +00002354 *remp = !tv_get_bool_chk(newtv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002355 else
Bram Moolenaarea696852020-11-09 18:31:39 +01002356 *remp = (tv_get_number_chk(newtv, &error) == 0);
2357 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002358 // On type error, nothing has been removed; return FAIL to stop the
2359 // loop. The error message was given by tv_get_number_chk().
2360 if (error)
2361 goto theend;
2362 }
2363 retval = OK;
2364theend:
2365 clear_tv(get_vim_var_tv(VV_VAL));
2366 return retval;
2367}
2368
2369/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002370 * Implementation of map() and filter() for a List. Apply "expr" to every item
2371 * in List "l" and return the result in "rettv".
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002372 */
2373 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002374list_filter_map(
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002375 list_T *l,
2376 filtermap_T filtermap,
2377 type_T *argtype,
2378 char *func_name,
2379 char_u *arg_errmsg,
2380 typval_T *expr,
2381 typval_T *rettv)
2382{
2383 int prev_lock;
2384 list_T *l_ret = NULL;
2385 int idx = 0;
2386 int rem;
2387 listitem_T *li, *nli;
Bram Moolenaar82418262022-09-28 16:16:15 +01002388 typval_T newtv;
2389 funccall_T *fc;
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002390
2391 if (filtermap == FILTERMAP_MAPNEW)
2392 {
2393 rettv->v_type = VAR_LIST;
2394 rettv->vval.v_list = NULL;
2395 }
Bram Moolenaar52df40e2022-09-28 13:22:59 +01002396 if (l == NULL || (filtermap == FILTERMAP_FILTER
2397 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002398 return;
2399
2400 prev_lock = l->lv_lock;
2401
2402 if (filtermap == FILTERMAP_MAPNEW)
2403 {
2404 if (rettv_list_alloc(rettv) == FAIL)
2405 return;
2406 l_ret = rettv->vval.v_list;
2407 }
2408 // set_vim_var_nr() doesn't set the type
2409 set_vim_var_type(VV_KEY, VAR_NUMBER);
2410
Ernie Raele6d40dc2023-03-19 21:23:38 +00002411 if (l->lv_lock == 0)
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002412 l->lv_lock = VAR_LOCKED;
2413
zeertzjqe7d49462023-04-16 20:53:55 +01002414 // Create one funccall_T for all eval_expr_typval() calls.
Bram Moolenaar82418262022-09-28 16:16:15 +01002415 fc = eval_expr_get_funccal(expr, &newtv);
2416
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002417 if (l->lv_first == &range_list_item)
2418 {
2419 varnumber_T val = l->lv_u.nonmat.lv_start;
2420 int len = l->lv_len;
2421 int stride = l->lv_u.nonmat.lv_stride;
2422
2423 // List from range(): loop over the numbers
2424 if (filtermap != FILTERMAP_MAPNEW)
2425 {
2426 l->lv_first = NULL;
2427 l->lv_u.mat.lv_last = NULL;
2428 l->lv_len = 0;
2429 l->lv_u.mat.lv_idx_item = NULL;
2430 }
2431
2432 for (idx = 0; idx < len; ++idx)
2433 {
2434 typval_T tv;
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002435
2436 tv.v_type = VAR_NUMBER;
2437 tv.v_lock = 0;
2438 tv.vval.v_number = val;
2439 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaar82418262022-09-28 16:16:15 +01002440 if (filter_map_one(&tv, expr, filtermap, fc, &newtv, &rem) == FAIL)
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002441 break;
2442 if (did_emsg)
2443 {
2444 clear_tv(&newtv);
2445 break;
2446 }
2447 if (filtermap != FILTERMAP_FILTER)
2448 {
2449 if (filtermap == FILTERMAP_MAP && argtype != NULL
2450 && check_typval_arg_type(
2451 argtype->tt_member, &newtv,
2452 func_name, 0) == FAIL)
2453 {
2454 clear_tv(&newtv);
2455 break;
2456 }
2457 // map(), mapnew(): always append the new value to the
2458 // list
2459 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2460 ? l : l_ret, &newtv) == FAIL)
2461 break;
2462 }
2463 else if (!rem)
2464 {
2465 // filter(): append the list item value when not rem
2466 if (list_append_tv_move(l, &tv) == FAIL)
2467 break;
2468 }
2469
2470 val += stride;
2471 }
2472 }
2473 else
2474 {
2475 // Materialized list: loop over the items
2476 for (li = l->lv_first; li != NULL; li = nli)
2477 {
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002478 if (filtermap == FILTERMAP_MAP && value_check_lock(
2479 li->li_tv.v_lock, arg_errmsg, TRUE))
2480 break;
2481 nli = li->li_next;
2482 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaar82418262022-09-28 16:16:15 +01002483 if (filter_map_one(&li->li_tv, expr, filtermap, fc,
2484 &newtv, &rem) == FAIL)
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002485 break;
2486 if (did_emsg)
2487 {
2488 clear_tv(&newtv);
2489 break;
2490 }
2491 if (filtermap == FILTERMAP_MAP)
2492 {
2493 if (argtype != NULL && check_typval_arg_type(
2494 argtype->tt_member, &newtv, func_name, 0) == FAIL)
2495 {
2496 clear_tv(&newtv);
2497 break;
2498 }
2499 // map(): replace the list item value
2500 clear_tv(&li->li_tv);
2501 newtv.v_lock = 0;
2502 li->li_tv = newtv;
2503 }
2504 else if (filtermap == FILTERMAP_MAPNEW)
2505 {
2506 // mapnew(): append the list item value
2507 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2508 break;
2509 }
2510 else if (filtermap == FILTERMAP_FILTER && rem)
2511 listitem_remove(l, li);
2512 ++idx;
2513 }
2514 }
2515
2516 l->lv_lock = prev_lock;
Bram Moolenaar82418262022-09-28 16:16:15 +01002517 if (fc != NULL)
2518 remove_funccal();
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002519}
2520
2521/*
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002522 * Implementation of map() and filter().
2523 */
2524 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01002525filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002526{
2527 typval_T *expr;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002528 char *func_name = filtermap == FILTERMAP_MAP ? "map()"
Bram Moolenaarea696852020-11-09 18:31:39 +01002529 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002530 : "filter()";
Bram Moolenaarea696852020-11-09 18:31:39 +01002531 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2532 ? N_("map() argument")
2533 : filtermap == FILTERMAP_MAPNEW
2534 ? N_("mapnew() argument")
2535 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002536 int save_did_emsg;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002537 type_T *type = NULL;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002538
Bram Moolenaarea696852020-11-09 18:31:39 +01002539 // map() and filter() return the first argument, also on failure.
Bram Moolenaar2d877592021-12-16 08:21:09 +00002540 if (filtermap != FILTERMAP_MAPNEW && argvars[0].v_type != VAR_STRING)
Bram Moolenaarea696852020-11-09 18:31:39 +01002541 copy_tv(&argvars[0], rettv);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002542
2543 if (in_vim9script()
Bram Moolenaar2d877592021-12-16 08:21:09 +00002544 && (check_for_list_or_dict_or_blob_or_string_arg(argvars, 0)
2545 == FAIL))
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002546 return;
2547
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002548 if (filtermap == FILTERMAP_MAP && in_vim9script())
2549 {
Bram Moolenaar35c807d2022-01-27 16:36:29 +00002550 // Check that map() does not change the declared type of the list or
2551 // dict.
2552 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
2553 type = argvars[0].vval.v_dict->dv_type;
2554 else if (argvars[0].v_type == VAR_LIST
2555 && argvars[0].vval.v_list != NULL)
2556 type = argvars[0].vval.v_list->lv_type;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002557 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002558
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002559 if (argvars[0].v_type != VAR_BLOB
2560 && argvars[0].v_type != VAR_LIST
2561 && argvars[0].v_type != VAR_DICT
2562 && argvars[0].v_type != VAR_STRING)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002563 {
rbtnnc479ce02021-12-15 19:14:54 +00002564 semsg(_(e_argument_of_str_must_be_list_string_dictionary_or_blob),
2565 func_name);
Bram Moolenaar98cd3032022-01-27 17:37:41 +00002566 return;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002567 }
2568
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002569 // On type errors, the preceding call has already displayed an error
2570 // message. Avoid a misleading error message for an empty string that
2571 // was not passed as argument.
Bram Moolenaar35c807d2022-01-27 16:36:29 +00002572 expr = &argvars[1];
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002573 if (expr->v_type == VAR_UNKNOWN)
2574 return;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002575
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002576 typval_T save_val;
2577 typval_T save_key;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002578
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002579 prepare_vimvar(VV_VAL, &save_val);
2580 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002581
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002582 // We reset "did_emsg" to be able to detect whether an error
2583 // occurred during evaluation of the expression.
2584 save_did_emsg = did_emsg;
2585 did_emsg = FALSE;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002586
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002587 if (argvars[0].v_type == VAR_DICT)
2588 dict_filter_map(argvars[0].vval.v_dict, filtermap, type, func_name,
Ernie Raele6d40dc2023-03-19 21:23:38 +00002589 arg_errmsg, expr, rettv);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002590 else if (argvars[0].v_type == VAR_BLOB)
Ernie Raele6d40dc2023-03-19 21:23:38 +00002591 blob_filter_map(argvars[0].vval.v_blob, filtermap, expr,
2592 arg_errmsg, rettv);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002593 else if (argvars[0].v_type == VAR_STRING)
Ernie Raele6d40dc2023-03-19 21:23:38 +00002594 string_filter_map(tv_get_string(&argvars[0]), filtermap, expr, rettv);
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002595 else // argvars[0].v_type == VAR_LIST
2596 list_filter_map(argvars[0].vval.v_list, filtermap, type, func_name,
Ernie Raele6d40dc2023-03-19 21:23:38 +00002597 arg_errmsg, expr, rettv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002598
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00002599 restore_vimvar(VV_KEY, &save_key);
2600 restore_vimvar(VV_VAL, &save_val);
2601
2602 did_emsg |= save_did_emsg;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002603}
2604
2605/*
2606 * "filter()" function
2607 */
2608 void
2609f_filter(typval_T *argvars, typval_T *rettv)
2610{
Bram Moolenaarea696852020-11-09 18:31:39 +01002611 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002612}
2613
2614/*
2615 * "map()" function
2616 */
2617 void
2618f_map(typval_T *argvars, typval_T *rettv)
2619{
Bram Moolenaarea696852020-11-09 18:31:39 +01002620 filter_map(argvars, rettv, FILTERMAP_MAP);
2621}
2622
2623/*
2624 * "mapnew()" function
2625 */
2626 void
2627f_mapnew(typval_T *argvars, typval_T *rettv)
2628{
2629 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002630}
2631
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002632/*
2633 * "add(list, item)" function
2634 */
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002635 static void
2636list_add(typval_T *argvars, typval_T *rettv)
2637{
2638 list_T *l = argvars[0].vval.v_list;
2639
2640 if (l == NULL)
2641 {
2642 if (in_vim9script())
2643 emsg(_(e_cannot_add_to_null_list));
2644 }
2645 else if (!value_check_lock(l->lv_lock,
2646 (char_u *)N_("add() argument"), TRUE)
2647 && list_append_tv(l, &argvars[1]) == OK)
2648 {
2649 copy_tv(&argvars[0], rettv);
2650 }
2651}
2652
2653/*
2654 * "add(object, item)" function
2655 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002656 void
2657f_add(typval_T *argvars, typval_T *rettv)
2658{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002659 rettv->vval.v_number = 1; // Default: Failed
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002660
2661 if (in_vim9script()
2662 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2663 || (argvars[0].v_type == VAR_BLOB
2664 && check_for_number_arg(argvars, 1) == FAIL)))
2665 return;
2666
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002667 if (argvars[0].v_type == VAR_LIST)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002668 list_add(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002669 else if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002670 blob_add(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002671 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002672 emsg(_(e_list_or_blob_required));
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002673}
2674
2675/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002676 * Count the number of times item "needle" occurs in List "l" starting at index
2677 * "idx". Case is ignored if "ic" is TRUE.
2678 */
2679 static long
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002680list_count(list_T *l, typval_T *needle, long idx, int ic)
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002681{
2682 long n = 0;
2683 listitem_T *li;
2684
2685 if (l == NULL)
2686 return 0;
2687
2688 CHECK_LIST_MATERIALIZE(l);
2689
2690 if (list_len(l) == 0)
2691 return 0;
2692
2693 li = list_find(l, idx);
2694 if (li == NULL)
2695 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002696 semsg(_(e_list_index_out_of_range_nr), idx);
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002697 return 0;
2698 }
2699
2700 for ( ; li != NULL; li = li->li_next)
2701 if (tv_equal(&li->li_tv, needle, ic, FALSE))
2702 ++n;
2703
2704 return n;
2705}
2706
2707/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002708 * "count()" function
2709 */
2710 void
2711f_count(typval_T *argvars, typval_T *rettv)
2712{
2713 long n = 0;
2714 int ic = FALSE;
2715 int error = FALSE;
2716
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002717 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002718 && (check_for_string_or_list_or_dict_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002719 || check_for_opt_bool_arg(argvars, 2) == FAIL
2720 || (argvars[2].v_type != VAR_UNKNOWN
2721 && check_for_opt_number_arg(argvars, 3) == FAIL)))
2722 return;
2723
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002724 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002725 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002726
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002727 if (!error && argvars[0].v_type == VAR_STRING)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002728 n = string_count(argvars[0].vval.v_string,
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002729 tv_get_string_chk(&argvars[1]), ic);
2730 else if (!error && argvars[0].v_type == VAR_LIST)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002731 {
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002732 long idx = 0;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002733
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002734 if (argvars[2].v_type != VAR_UNKNOWN
2735 && argvars[3].v_type != VAR_UNKNOWN)
2736 idx = (long)tv_get_number_chk(&argvars[3], &error);
2737 if (!error)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002738 n = list_count(argvars[0].vval.v_list, &argvars[1], idx, ic);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002739 }
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002740 else if (!error && argvars[0].v_type == VAR_DICT)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002741 {
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002742 if (argvars[2].v_type != VAR_UNKNOWN
2743 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002744 emsg(_(e_invalid_argument));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002745 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002746 n = dict_count(argvars[0].vval.v_dict, &argvars[1], ic);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002747 }
zeertzjq4f389e72023-08-17 22:10:40 +02002748 else if (!error)
2749 semsg(_(e_argument_of_str_must_be_list_string_or_dictionary),
2750 "count()");
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002751 rettv->vval.v_number = n;
2752}
2753
2754/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002755 * extend() a List. Append List argvars[1] to List argvars[0] before index
2756 * argvars[3] and return the resulting list in "rettv". "is_new" is TRUE for
2757 * extendnew().
2758 */
2759 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002760list_extend_func(
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002761 typval_T *argvars,
2762 type_T *type,
2763 char *func_name,
2764 char_u *arg_errmsg,
2765 int is_new,
2766 typval_T *rettv)
2767{
2768 list_T *l1, *l2;
2769 listitem_T *item;
2770 long before;
2771 int error = FALSE;
2772
2773 l1 = argvars[0].vval.v_list;
2774 if (l1 == NULL)
2775 {
2776 emsg(_(e_cannot_extend_null_list));
2777 return;
2778 }
2779 l2 = argvars[1].vval.v_list;
2780 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar381692b2022-02-02 20:01:27 +00002781 && l2 != NULL)
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002782 {
2783 if (is_new)
2784 {
Bram Moolenaar381692b2022-02-02 20:01:27 +00002785 l1 = list_copy(l1, FALSE, TRUE, get_copyID());
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002786 if (l1 == NULL)
2787 return;
2788 }
2789
2790 if (argvars[2].v_type != VAR_UNKNOWN)
2791 {
2792 before = (long)tv_get_number_chk(&argvars[2], &error);
2793 if (error)
2794 return; // type error; errmsg already given
2795
2796 if (before == l1->lv_len)
2797 item = NULL;
2798 else
2799 {
2800 item = list_find(l1, before);
2801 if (item == NULL)
2802 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002803 semsg(_(e_list_index_out_of_range_nr), before);
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002804 return;
2805 }
2806 }
2807 }
2808 else
2809 item = NULL;
2810 if (type != NULL && check_typval_arg_type(
2811 type, &argvars[1], func_name, 2) == FAIL)
2812 return;
2813 list_extend(l1, l2, item);
2814
2815 if (is_new)
2816 {
2817 rettv->v_type = VAR_LIST;
2818 rettv->vval.v_list = l1;
2819 rettv->v_lock = FALSE;
2820 }
2821 else
2822 copy_tv(&argvars[0], rettv);
2823 }
2824}
2825
2826/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002827 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002828 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002829 static void
2830extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002831{
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002832 type_T *type = NULL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002833 char *func_name = is_new ? "extendnew()" : "extend()";
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002834
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002835 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002836 {
2837 // Check that extend() does not change the type of the list if it was
2838 // declared.
2839 if (!is_new && in_vim9script() && argvars[0].vval.v_list != NULL)
2840 type = argvars[0].vval.v_list->lv_type;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002841 list_extend_func(argvars, type, func_name, arg_errmsg, is_new, rettv);
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002842 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002843 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002844 {
Bram Moolenaarbe19d782023-03-09 22:06:49 +00002845 // Check that extend() does not change the type of the dict if it was
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002846 // declared.
2847 if (!is_new && in_vim9script() && argvars[0].vval.v_dict != NULL)
2848 type = argvars[0].vval.v_dict->dv_type;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002849 dict_extend_func(argvars, type, func_name, arg_errmsg, is_new, rettv);
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002850 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002851 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002852 semsg(_(e_argument_of_str_must_be_list_or_dictionary), func_name);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002853}
2854
2855/*
2856 * "extend(list, list [, idx])" function
2857 * "extend(dict, dict [, action])" function
2858 */
2859 void
2860f_extend(typval_T *argvars, typval_T *rettv)
2861{
2862 char_u *errmsg = (char_u *)N_("extend() argument");
2863
2864 extend(argvars, rettv, errmsg, FALSE);
2865}
2866
2867/*
2868 * "extendnew(list, list [, idx])" function
2869 * "extendnew(dict, dict [, action])" function
2870 */
2871 void
2872f_extendnew(typval_T *argvars, typval_T *rettv)
2873{
2874 char_u *errmsg = (char_u *)N_("extendnew() argument");
2875
2876 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002877}
2878
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002879 static void
2880list_insert_func(typval_T *argvars, typval_T *rettv)
2881{
2882 list_T *l = argvars[0].vval.v_list;
2883 long before = 0;
2884 listitem_T *item;
2885 int error = FALSE;
2886
2887 if (l == NULL)
2888 {
2889 if (in_vim9script())
2890 emsg(_(e_cannot_add_to_null_list));
2891 return;
2892 }
2893
2894 if (value_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
2895 return;
2896
2897 if (argvars[2].v_type != VAR_UNKNOWN)
2898 before = (long)tv_get_number_chk(&argvars[2], &error);
2899 if (error)
2900 return; // type error; errmsg already given
2901
2902 if (before == l->lv_len)
2903 item = NULL;
2904 else
2905 {
2906 item = list_find(l, before);
2907 if (item == NULL)
2908 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002909 semsg(_(e_list_index_out_of_range_nr), before);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002910 l = NULL;
2911 }
2912 }
2913 if (l != NULL)
2914 {
2915 (void)list_insert_tv(l, &argvars[1], item);
2916 copy_tv(&argvars[0], rettv);
2917 }
2918}
2919
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002920/*
2921 * "insert()" function
2922 */
2923 void
2924f_insert(typval_T *argvars, typval_T *rettv)
2925{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002926 if (in_vim9script()
2927 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2928 || (argvars[0].v_type == VAR_BLOB
2929 && check_for_number_arg(argvars, 1) == FAIL)
2930 || check_for_opt_number_arg(argvars, 2) == FAIL))
2931 return;
2932
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002933 if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002934 blob_insert_func(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002935 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002936 semsg(_(e_argument_of_str_must_be_list_or_blob), "insert()");
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002937 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002938 list_insert_func(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002939}
2940
2941/*
2942 * "remove()" function
2943 */
2944 void
2945f_remove(typval_T *argvars, typval_T *rettv)
2946{
2947 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2948
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002949 if (in_vim9script()
2950 && (check_for_list_or_dict_or_blob_arg(argvars, 0) == FAIL
2951 || ((argvars[0].v_type == VAR_LIST
2952 || argvars[0].v_type == VAR_BLOB)
2953 && (check_for_number_arg(argvars, 1) == FAIL
2954 || check_for_opt_number_arg(argvars, 2) == FAIL))
2955 || (argvars[0].v_type == VAR_DICT
2956 && check_for_string_or_number_arg(argvars, 1) == FAIL)))
2957 return;
2958
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002959 if (argvars[0].v_type == VAR_DICT)
2960 dict_remove(argvars, rettv, arg_errmsg);
2961 else if (argvars[0].v_type == VAR_BLOB)
Sean Dewar80d73952021-08-04 19:25:54 +02002962 blob_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002963 else if (argvars[0].v_type == VAR_LIST)
2964 list_remove(argvars, rettv, arg_errmsg);
2965 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002966 semsg(_(e_argument_of_str_must_be_list_dictionary_or_blob), "remove()");
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002967}
2968
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002969 static void
2970list_reverse(list_T *l, typval_T *rettv)
2971{
2972 listitem_T *li, *ni;
2973
2974 rettv_list_set(rettv, l);
2975 if (l != NULL
2976 && !value_check_lock(l->lv_lock,
2977 (char_u *)N_("reverse() argument"), TRUE))
2978 {
2979 if (l->lv_first == &range_list_item)
2980 {
2981 varnumber_T new_start = l->lv_u.nonmat.lv_start
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00002982 + ((varnumber_T)l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002983 l->lv_u.nonmat.lv_end = new_start
2984 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2985 l->lv_u.nonmat.lv_start = new_start;
2986 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
2987 return;
2988 }
2989 li = l->lv_u.mat.lv_last;
2990 l->lv_first = l->lv_u.mat.lv_last = NULL;
2991 l->lv_len = 0;
2992 while (li != NULL)
2993 {
2994 ni = li->li_prev;
2995 list_append(l, li);
2996 li = ni;
2997 }
2998 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
2999 }
3000}
3001
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003002/*
3003 * "reverse({list})" function
3004 */
3005 void
3006f_reverse(typval_T *argvars, typval_T *rettv)
3007{
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +01003008 if (check_for_string_or_list_or_blob_arg(argvars, 0) == FAIL)
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02003009 return;
3010
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003011 if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003012 blob_reverse(argvars[0].vval.v_blob, rettv);
Yegappan Lakshmanan03ff1c22023-05-06 14:08:21 +01003013 else if (argvars[0].v_type == VAR_STRING)
zeertzjq4dd266c2023-08-19 11:35:03 +02003014 {
3015 rettv->v_type = VAR_STRING;
3016 if (argvars[0].vval.v_string != NULL)
3017 rettv->vval.v_string = reverse_text(argvars[0].vval.v_string);
3018 else
3019 rettv->vval.v_string = NULL;
3020 }
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +01003021 else if (argvars[0].v_type == VAR_LIST)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003022 list_reverse(argvars[0].vval.v_list, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003023}
3024
Bram Moolenaar85629982020-06-01 18:39:20 +02003025/*
Bram Moolenaarf1c60d42022-09-22 17:07:00 +01003026 * Implementation of reduce() for list "argvars[0]", using the function "expr"
3027 * starting with the optional initial value argvars[2] and return the result in
3028 * "rettv".
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003029 */
3030 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003031list_reduce(
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003032 typval_T *argvars,
Bram Moolenaarf1c60d42022-09-22 17:07:00 +01003033 typval_T *expr,
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003034 typval_T *rettv)
3035{
3036 list_T *l = argvars[0].vval.v_list;
3037 listitem_T *li = NULL;
Bram Moolenaar52df40e2022-09-28 13:22:59 +01003038 int range_list;
3039 int range_idx = 0;
3040 varnumber_T range_val = 0;
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003041 typval_T initial;
3042 typval_T argv[3];
3043 int r;
3044 int called_emsg_start = called_emsg;
3045 int prev_locked;
Bram Moolenaar82418262022-09-28 16:16:15 +01003046 funccall_T *fc;
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003047
Bram Moolenaar52df40e2022-09-28 13:22:59 +01003048 // Using reduce on a range() uses "range_idx" and "range_val".
3049 range_list = l != NULL && l->lv_first == &range_list_item;
3050 if (range_list)
3051 range_val = l->lv_u.nonmat.lv_start;
3052
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003053 if (argvars[2].v_type == VAR_UNKNOWN)
3054 {
Bram Moolenaar52df40e2022-09-28 13:22:59 +01003055 if (l == NULL || l->lv_len == 0)
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003056 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003057 semsg(_(e_reduce_of_an_empty_str_with_no_initial_value), "List");
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003058 return;
3059 }
Bram Moolenaar52df40e2022-09-28 13:22:59 +01003060 if (range_list)
3061 {
3062 initial.v_type = VAR_NUMBER;
3063 initial.vval.v_number = range_val;
3064 range_val += l->lv_u.nonmat.lv_stride;
3065 range_idx = 1;
3066 }
3067 else
3068 {
3069 initial = l->lv_first->li_tv;
3070 li = l->lv_first->li_next;
3071 }
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003072 }
3073 else
3074 {
3075 initial = argvars[2];
Bram Moolenaar52df40e2022-09-28 13:22:59 +01003076 if (l != NULL && !range_list)
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003077 li = l->lv_first;
3078 }
3079 copy_tv(&initial, rettv);
3080
3081 if (l == NULL)
3082 return;
3083
zeertzjqe7d49462023-04-16 20:53:55 +01003084 // Create one funccall_T for all eval_expr_typval() calls.
Bram Moolenaar82418262022-09-28 16:16:15 +01003085 fc = eval_expr_get_funccal(expr, rettv);
3086
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003087 prev_locked = l->lv_lock;
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003088 l->lv_lock = VAR_FIXED; // disallow the list changing here
Bram Moolenaar52df40e2022-09-28 13:22:59 +01003089
3090 while (range_list ? range_idx < l->lv_len : li != NULL)
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003091 {
3092 argv[0] = *rettv;
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003093 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarf1c60d42022-09-22 17:07:00 +01003094
Bram Moolenaar52df40e2022-09-28 13:22:59 +01003095 if (range_list)
3096 {
3097 argv[1].v_type = VAR_NUMBER;
3098 argv[1].vval.v_number = range_val;
3099 }
3100 else
3101 argv[1] = li->li_tv;
3102
zeertzjqad0c4422023-08-17 22:15:47 +02003103 r = eval_expr_typval(expr, TRUE, argv, 2, fc, rettv);
Bram Moolenaarf1c60d42022-09-22 17:07:00 +01003104
Bram Moolenaar1936c762022-09-28 15:19:10 +01003105 if (argv[0].v_type != VAR_NUMBER && argv[0].v_type != VAR_UNKNOWN)
3106 clear_tv(&argv[0]);
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003107 if (r == FAIL || called_emsg != called_emsg_start)
3108 break;
Bram Moolenaar52df40e2022-09-28 13:22:59 +01003109
Bram Moolenaar1936c762022-09-28 15:19:10 +01003110 // advance to the next item
Bram Moolenaar52df40e2022-09-28 13:22:59 +01003111 if (range_list)
3112 {
3113 range_val += l->lv_u.nonmat.lv_stride;
3114 ++range_idx;
3115 }
3116 else
3117 li = li->li_next;
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003118 }
Bram Moolenaar52df40e2022-09-28 13:22:59 +01003119
Bram Moolenaar82418262022-09-28 16:16:15 +01003120 if (fc != NULL)
3121 remove_funccal();
3122
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003123 l->lv_lock = prev_locked;
3124}
3125
3126/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003127 * "reduce(list, { accumulator, element -> value } [, initial])" function
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00003128 * "reduce(blob, { accumulator, element -> value } [, initial])"
3129 * "reduce(string, { accumulator, element -> value } [, initial])"
Bram Moolenaar85629982020-06-01 18:39:20 +02003130 */
3131 void
3132f_reduce(typval_T *argvars, typval_T *rettv)
3133{
Bram Moolenaar85629982020-06-01 18:39:20 +02003134 char_u *func_name;
Bram Moolenaar85629982020-06-01 18:39:20 +02003135
rbtnn0ccb5842021-12-18 18:33:46 +00003136 if (in_vim9script()
3137 && check_for_string_or_list_or_blob_arg(argvars, 0) == FAIL)
Bram Moolenaar85629982020-06-01 18:39:20 +02003138 return;
rbtnn0ccb5842021-12-18 18:33:46 +00003139
3140 if (argvars[0].v_type != VAR_STRING
3141 && argvars[0].v_type != VAR_LIST
3142 && argvars[0].v_type != VAR_BLOB)
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003143 {
zeertzjqad0c4422023-08-17 22:15:47 +02003144 emsg(_(e_string_list_or_blob_required));
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003145 return;
3146 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003147
3148 if (argvars[1].v_type == VAR_FUNC)
3149 func_name = argvars[1].vval.v_string;
3150 else if (argvars[1].v_type == VAR_PARTIAL)
Bram Moolenaarf1c60d42022-09-22 17:07:00 +01003151 func_name = partial_name(argvars[1].vval.v_partial);
Bram Moolenaar85629982020-06-01 18:39:20 +02003152 else
3153 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01003154 if (func_name == NULL || *func_name == NUL)
3155 {
3156 emsg(_(e_missing_function_argument));
3157 return;
3158 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003159
Bram Moolenaar85629982020-06-01 18:39:20 +02003160 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaarf1c60d42022-09-22 17:07:00 +01003161 list_reduce(argvars, &argvars[1], rettv);
rbtnn0ccb5842021-12-18 18:33:46 +00003162 else if (argvars[0].v_type == VAR_STRING)
Bram Moolenaarf1c60d42022-09-22 17:07:00 +01003163 string_reduce(argvars, &argvars[1], rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003164 else
Bram Moolenaarf1c60d42022-09-22 17:07:00 +01003165 blob_reduce(argvars, &argvars[1], rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003166}
3167
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02003168#endif // defined(FEAT_EVAL)