blob: 3a6ea97668b36c9b53f1a5f3916ebdde042930ad [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.
112 * Next list_set_item() must be called for each item.
113 */
114 list_T *
115list_alloc_with_items(int count)
116{
117 list_T *l;
118
119 l = (list_T *)alloc_clear(sizeof(list_T) + count * sizeof(listitem_T));
120 if (l != NULL)
121 {
122 list_init(l);
123
124 if (count > 0)
125 {
126 listitem_T *li = (listitem_T *)(l + 1);
127 int i;
128
129 l->lv_len = count;
130 l->lv_with_items = count;
131 l->lv_first = li;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100132 l->lv_u.mat.lv_last = li + count - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 for (i = 0; i < count; ++i)
134 {
135 if (i == 0)
136 li->li_prev = NULL;
137 else
138 li->li_prev = li - 1;
139 if (i == count - 1)
140 li->li_next = NULL;
141 else
142 li->li_next = li + 1;
143 ++li;
144 }
145 }
146 }
147 return l;
148}
149
150/*
151 * Set item "idx" for a list previously allocated with list_alloc_with_items().
152 * The contents of "tv" is moved into the list item.
153 * Each item must be set exactly once.
154 */
155 void
156list_set_item(list_T *l, int idx, typval_T *tv)
157{
158 listitem_T *li = (listitem_T *)(l + 1) + idx;
159
160 li->li_tv = *tv;
161}
162
163/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200164 * Allocate an empty list for a return value, with reference count set.
165 * Returns OK or FAIL.
166 */
167 int
168rettv_list_alloc(typval_T *rettv)
169{
170 list_T *l = list_alloc();
171
172 if (l == NULL)
173 return FAIL;
174
Bram Moolenaarda861d62016-07-17 15:46:27 +0200175 rettv->v_lock = 0;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200176 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200177 return OK;
178}
179
180/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100181 * Same as rettv_list_alloc() but uses an allocation id for testing.
182 */
183 int
184rettv_list_alloc_id(typval_T *rettv, alloc_id_T id UNUSED)
185{
186#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200187 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaar162b7142018-12-21 15:17:36 +0100188 return FAIL;
189#endif
190 return rettv_list_alloc(rettv);
191}
192
193
194/*
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200195 * Set a list as the return value. Increments the reference count.
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200196 */
197 void
198rettv_list_set(typval_T *rettv, list_T *l)
199{
200 rettv->v_type = VAR_LIST;
201 rettv->vval.v_list = l;
202 if (l != NULL)
203 ++l->lv_refcount;
204}
205
206/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200207 * Unreference a list: decrement the reference count and free it when it
208 * becomes zero.
209 */
210 void
211list_unref(list_T *l)
212{
213 if (l != NULL && --l->lv_refcount <= 0)
214 list_free(l);
215}
216
217/*
218 * Free a list, including all non-container items it points to.
219 * Ignores the reference count.
220 */
221 static void
222list_free_contents(list_T *l)
223{
224 listitem_T *item;
225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 if (l->lv_first != &range_list_item)
227 for (item = l->lv_first; item != NULL; item = l->lv_first)
228 {
229 // Remove the item before deleting it.
230 l->lv_first = item->li_next;
231 clear_tv(&item->li_tv);
232 list_free_item(l, item);
233 }
Bram Moolenaarda861d62016-07-17 15:46:27 +0200234}
235
236/*
237 * Go through the list of lists and free items without the copyID.
238 * But don't free a list that has a watcher (used in a for loop), these
239 * are not referenced anywhere.
240 */
241 int
242list_free_nonref(int copyID)
243{
244 list_T *ll;
245 int did_free = FALSE;
246
247 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
248 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
249 && ll->lv_watch == NULL)
250 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100251 // Free the List and ordinary items it contains, but don't recurse
252 // into Lists and Dictionaries, they will be in the list of dicts
253 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200254 list_free_contents(ll);
255 did_free = TRUE;
256 }
257 return did_free;
258}
259
260 static void
261list_free_list(list_T *l)
262{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100263 // Remove the list from the list of lists for garbage collection.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200264 if (l->lv_used_prev == NULL)
265 first_list = l->lv_used_next;
266 else
267 l->lv_used_prev->lv_used_next = l->lv_used_next;
268 if (l->lv_used_next != NULL)
269 l->lv_used_next->lv_used_prev = l->lv_used_prev;
270
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100271 free_type(l->lv_type);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200272 vim_free(l);
273}
274
275 void
276list_free_items(int copyID)
277{
278 list_T *ll, *ll_next;
279
280 for (ll = first_list; ll != NULL; ll = ll_next)
281 {
282 ll_next = ll->lv_used_next;
283 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
284 && ll->lv_watch == NULL)
285 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100286 // Free the List and ordinary items it contains, but don't recurse
287 // into Lists and Dictionaries, they will be in the list of dicts
288 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200289 list_free_list(ll);
290 }
291 }
292}
293
294 void
295list_free(list_T *l)
296{
297 if (!in_free_unref_items)
298 {
299 list_free_contents(l);
300 list_free_list(l);
301 }
302}
303
304/*
305 * Allocate a list item.
306 * It is not initialized, don't forget to set v_lock.
307 */
308 listitem_T *
309listitem_alloc(void)
310{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200311 return ALLOC_ONE(listitem_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200312}
313
314/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100315 * Free a list item, unless it was allocated together with the list itself.
316 * Does not clear the value. Does not notify watchers.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200317 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200318 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319list_free_item(list_T *l, listitem_T *item)
320{
321 if (l->lv_with_items == 0 || item < (listitem_T *)l
322 || item >= (listitem_T *)(l + 1) + l->lv_with_items)
323 vim_free(item);
324}
325
326/*
327 * Free a list item, unless it was allocated together with the list itself.
328 * Also clears the value. Does not notify watchers.
329 */
330 void
331listitem_free(list_T *l, listitem_T *item)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200332{
333 clear_tv(&item->li_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 list_free_item(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200335}
336
337/*
338 * Remove a list item from a List and free it. Also clears the value.
339 */
340 void
341listitem_remove(list_T *l, listitem_T *item)
342{
343 vimlist_remove(l, item, item);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344 listitem_free(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200345}
346
347/*
348 * Get the number of items in a list.
349 */
350 long
351list_len(list_T *l)
352{
353 if (l == NULL)
354 return 0L;
355 return l->lv_len;
356}
357
358/*
359 * Return TRUE when two lists have exactly the same values.
360 */
361 int
362list_equal(
363 list_T *l1,
364 list_T *l2,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100365 int ic, // ignore case for strings
366 int recursive) // TRUE when used recursively
Bram Moolenaarda861d62016-07-17 15:46:27 +0200367{
368 listitem_T *item1, *item2;
369
Bram Moolenaarda861d62016-07-17 15:46:27 +0200370 if (l1 == l2)
371 return TRUE;
372 if (list_len(l1) != list_len(l2))
373 return FALSE;
Bram Moolenaar7b293c72020-04-09 21:33:22 +0200374 if (list_len(l1) == 0)
375 // empty and NULL list are considered equal
376 return TRUE;
377 if (l1 == NULL || l2 == NULL)
378 return FALSE;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200379
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200380 CHECK_LIST_MATERIALIZE(l1);
381 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100382
Bram Moolenaarda861d62016-07-17 15:46:27 +0200383 for (item1 = l1->lv_first, item2 = l2->lv_first;
384 item1 != NULL && item2 != NULL;
385 item1 = item1->li_next, item2 = item2->li_next)
386 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
387 return FALSE;
388 return item1 == NULL && item2 == NULL;
389}
390
391/*
392 * Locate item with index "n" in list "l" and return it.
393 * A negative index is counted from the end; -1 is the last item.
394 * Returns NULL when "n" is out of range.
395 */
396 listitem_T *
397list_find(list_T *l, long n)
398{
399 listitem_T *item;
400 long idx;
401
402 if (l == NULL)
403 return NULL;
404
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100405 // Negative index is relative to the end.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200406 if (n < 0)
407 n = l->lv_len + n;
408
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100409 // Check for index out of range.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200410 if (n < 0 || n >= l->lv_len)
411 return NULL;
412
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200413 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100415 // When there is a cached index may start search from there.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100416 if (l->lv_u.mat.lv_idx_item != NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200417 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100418 if (n < l->lv_u.mat.lv_idx / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200419 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100420 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200421 item = l->lv_first;
422 idx = 0;
423 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100424 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200425 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100426 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100427 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200428 idx = l->lv_len - 1;
429 }
430 else
431 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100432 // closest to the cached index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100433 item = l->lv_u.mat.lv_idx_item;
434 idx = l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200435 }
436 }
437 else
438 {
439 if (n < l->lv_len / 2)
440 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100441 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200442 item = l->lv_first;
443 idx = 0;
444 }
445 else
446 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100447 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100448 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200449 idx = l->lv_len - 1;
450 }
451 }
452
453 while (n > idx)
454 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100455 // search forward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200456 item = item->li_next;
457 ++idx;
458 }
459 while (n < idx)
460 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100461 // search backward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200462 item = item->li_prev;
463 --idx;
464 }
465
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100466 // cache the used index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100467 l->lv_u.mat.lv_idx = idx;
468 l->lv_u.mat.lv_idx_item = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200469
470 return item;
471}
472
473/*
474 * Get list item "l[idx]" as a number.
475 */
476 long
477list_find_nr(
478 list_T *l,
479 long idx,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100480 int *errorp) // set to TRUE when something wrong
Bram Moolenaarda861d62016-07-17 15:46:27 +0200481{
482 listitem_T *li;
483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484 if (l != NULL && l->lv_first == &range_list_item)
485 {
486 long n = idx;
487
488 // not materialized range() list: compute the value.
489 // Negative index is relative to the end.
490 if (n < 0)
491 n = l->lv_len + n;
492
493 // Check for index out of range.
494 if (n < 0 || n >= l->lv_len)
495 {
496 if (errorp != NULL)
497 *errorp = TRUE;
498 return -1L;
499 }
500
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100501 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502 }
503
Bram Moolenaarda861d62016-07-17 15:46:27 +0200504 li = list_find(l, idx);
505 if (li == NULL)
506 {
507 if (errorp != NULL)
508 *errorp = TRUE;
509 return -1L;
510 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100511 return (long)tv_get_number_chk(&li->li_tv, errorp);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200512}
513
514/*
515 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
516 */
517 char_u *
518list_find_str(list_T *l, long idx)
519{
520 listitem_T *li;
521
522 li = list_find(l, idx - 1);
523 if (li == NULL)
524 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000525 semsg(_(e_list_index_out_of_range_nr), idx);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200526 return NULL;
527 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100528 return tv_get_string(&li->li_tv);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200529}
530
531/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100532 * Like list_find() but when a negative index is used that is not found use
533 * zero and set "idx" to zero. Used for first index of a range.
534 */
535 listitem_T *
536list_find_index(list_T *l, long *idx)
537{
538 listitem_T *li = list_find(l, *idx);
539
540 if (li == NULL)
541 {
542 if (*idx < 0)
543 {
544 *idx = 0;
545 li = list_find(l, *idx);
546 }
547 }
548 return li;
549}
550
551/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200552 * Locate "item" list "l" and return its index.
553 * Returns -1 when "item" is not in the list.
554 */
555 long
556list_idx_of_item(list_T *l, listitem_T *item)
557{
558 long idx = 0;
559 listitem_T *li;
560
561 if (l == NULL)
562 return -1;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200563 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200564 idx = 0;
565 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
566 ++idx;
567 if (li == NULL)
568 return -1;
569 return idx;
570}
571
572/*
573 * Append item "item" to the end of list "l".
574 */
575 void
576list_append(list_T *l, listitem_T *item)
577{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200578 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100579 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200580 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100581 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200582 l->lv_first = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200583 item->li_prev = NULL;
584 }
585 else
586 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100587 l->lv_u.mat.lv_last->li_next = item;
588 item->li_prev = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200589 }
Bram Moolenaar35c807d2022-01-27 16:36:29 +0000590 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200591 ++l->lv_len;
592 item->li_next = NULL;
593}
594
595/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200597 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200598 */
599 int
600list_append_tv(list_T *l, typval_T *tv)
601{
Bram Moolenaar90fba562021-07-09 19:17:55 +0200602 listitem_T *li;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200603
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200604 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200605 && check_typval_arg_type(l->lv_type->tt_member, tv,
606 NULL, 0) == FAIL)
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200607 return FAIL;
Bram Moolenaar90fba562021-07-09 19:17:55 +0200608 li = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200609 if (li == NULL)
610 return FAIL;
611 copy_tv(tv, &li->li_tv);
612 list_append(l, li);
613 return OK;
614}
615
616/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 * As list_append_tv() but move the value instead of copying it.
618 * Return FAIL when out of memory.
619 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200620 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621list_append_tv_move(list_T *l, typval_T *tv)
622{
623 listitem_T *li = listitem_alloc();
624
625 if (li == NULL)
626 return FAIL;
627 li->li_tv = *tv;
628 list_append(l, li);
629 return OK;
630}
631
632/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200633 * Add a dictionary to a list. Used by getqflist().
634 * Return FAIL when out of memory.
635 */
636 int
637list_append_dict(list_T *list, dict_T *dict)
638{
639 listitem_T *li = listitem_alloc();
640
641 if (li == NULL)
642 return FAIL;
643 li->li_tv.v_type = VAR_DICT;
644 li->li_tv.v_lock = 0;
645 li->li_tv.vval.v_dict = dict;
646 list_append(list, li);
647 ++dict->dv_refcount;
648 return OK;
649}
650
651/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100652 * Append list2 to list1.
653 * Return FAIL when out of memory.
654 */
655 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200656list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100657{
658 listitem_T *li = listitem_alloc();
659
660 if (li == NULL)
661 return FAIL;
662 li->li_tv.v_type = VAR_LIST;
663 li->li_tv.v_lock = 0;
664 li->li_tv.vval.v_list = list2;
665 list_append(list1, li);
666 ++list2->lv_refcount;
667 return OK;
668}
669
670/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200671 * Make a copy of "str" and append it as an item to list "l".
672 * When "len" >= 0 use "str[len]".
673 * Returns FAIL when out of memory.
674 */
675 int
676list_append_string(list_T *l, char_u *str, int len)
677{
678 listitem_T *li = listitem_alloc();
679
680 if (li == NULL)
681 return FAIL;
682 list_append(l, li);
683 li->li_tv.v_type = VAR_STRING;
684 li->li_tv.v_lock = 0;
685 if (str == NULL)
686 li->li_tv.vval.v_string = NULL;
687 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
688 : vim_strsave(str))) == NULL)
689 return FAIL;
690 return OK;
691}
692
693/*
694 * Append "n" to list "l".
695 * Returns FAIL when out of memory.
696 */
697 int
698list_append_number(list_T *l, varnumber_T n)
699{
700 listitem_T *li;
701
702 li = listitem_alloc();
703 if (li == NULL)
704 return FAIL;
705 li->li_tv.v_type = VAR_NUMBER;
706 li->li_tv.v_lock = 0;
707 li->li_tv.vval.v_number = n;
708 list_append(l, li);
709 return OK;
710}
711
712/*
713 * Insert typval_T "tv" in list "l" before "item".
714 * If "item" is NULL append at the end.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100715 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200716 */
717 int
718list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
719{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100720 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200721
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100722 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200723 && check_typval_arg_type(l->lv_type->tt_member, tv,
724 NULL, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100725 return FAIL;
726 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200727 if (ni == NULL)
728 return FAIL;
729 copy_tv(tv, &ni->li_tv);
730 list_insert(l, ni, item);
731 return OK;
732}
733
734 void
735list_insert(list_T *l, listitem_T *ni, listitem_T *item)
736{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200737 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200738 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100739 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200740 list_append(l, ni);
741 else
742 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100743 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200744 ni->li_prev = item->li_prev;
745 ni->li_next = item;
746 if (item->li_prev == NULL)
747 {
748 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100749 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200750 }
751 else
752 {
753 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100754 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200755 }
756 item->li_prev = ni;
757 ++l->lv_len;
758 }
759}
760
761/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200762 * Get the list item in "l" with index "n1". "n1" is adjusted if needed.
763 * In Vim9, it is at the end of the list, add an item.
764 * Return NULL if there is no such item.
765 */
766 listitem_T *
767check_range_index_one(list_T *l, long *n1, int quiet)
768{
769 listitem_T *li = list_find_index(l, n1);
770
771 if (li == NULL)
772 {
773 // Vim9: Allow for adding an item at the end.
774 if (in_vim9script() && *n1 == l->lv_len && l->lv_lock == 0)
775 {
776 list_append_number(l, 0);
777 li = list_find_index(l, n1);
778 }
779 if (li == NULL)
780 {
781 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000782 semsg(_(e_list_index_out_of_range_nr), *n1);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200783 return NULL;
784 }
785 }
786 return li;
787}
788
789/*
790 * Check that "n2" can be used as the second index in a range of list "l".
791 * If "n1" or "n2" is negative it is changed to the positive index.
792 * "li1" is the item for item "n1".
793 * Return OK or FAIL.
794 */
795 int
796check_range_index_two(
797 list_T *l,
798 long *n1,
799 listitem_T *li1,
800 long *n2,
801 int quiet)
802{
803 if (*n2 < 0)
804 {
805 listitem_T *ni = list_find(l, *n2);
806
807 if (ni == NULL)
808 {
809 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000810 semsg(_(e_list_index_out_of_range_nr), *n2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200811 return FAIL;
812 }
813 *n2 = list_idx_of_item(l, ni);
814 }
815
816 // Check that n2 isn't before n1.
817 if (*n1 < 0)
818 *n1 = list_idx_of_item(l, li1);
819 if (*n2 < *n1)
820 {
821 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000822 semsg(_(e_list_index_out_of_range_nr), *n2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200823 return FAIL;
824 }
825 return OK;
826}
827
828/*
829 * Assign values from list "src" into a range of "dest".
830 * "idx1_arg" is the index of the first item in "dest" to be replaced.
831 * "idx2" is the index of last item to be replaced, but when "empty_idx2" is
832 * TRUE then replace all items after "idx1".
833 * "op" is the operator, normally "=" but can be "+=" and the like.
834 * "varname" is used for error messages.
835 * Returns OK or FAIL.
836 */
837 int
838list_assign_range(
839 list_T *dest,
840 list_T *src,
841 long idx1_arg,
842 long idx2,
843 int empty_idx2,
844 char_u *op,
845 char_u *varname)
846{
847 listitem_T *src_li;
848 listitem_T *dest_li;
849 long idx1 = idx1_arg;
850 listitem_T *first_li = list_find_index(dest, &idx1);
851 long idx;
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200852 type_T *member_type = NULL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200853
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000854 // Check whether any of the list items is locked before making any changes.
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200855 idx = idx1;
856 dest_li = first_li;
857 for (src_li = src->lv_first; src_li != NULL && dest_li != NULL; )
858 {
859 if (value_check_lock(dest_li->li_tv.v_lock, varname, FALSE))
860 return FAIL;
861 src_li = src_li->li_next;
862 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
863 break;
864 dest_li = dest_li->li_next;
865 ++idx;
866 }
867
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200868 if (in_vim9script() && dest->lv_type != NULL
869 && dest->lv_type->tt_member != NULL)
870 member_type = dest->lv_type->tt_member;
871
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000872 // Assign the List values to the list items.
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200873 idx = idx1;
874 dest_li = first_li;
875 for (src_li = src->lv_first; src_li != NULL; )
876 {
877 if (op != NULL && *op != '=')
878 tv_op(&dest_li->li_tv, &src_li->li_tv, op);
879 else
880 {
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200881 if (member_type != NULL
882 && check_typval_arg_type(member_type, &src_li->li_tv,
883 NULL, 0) == FAIL)
884 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200885 clear_tv(&dest_li->li_tv);
886 copy_tv(&src_li->li_tv, &dest_li->li_tv);
887 }
888 src_li = src_li->li_next;
889 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
890 break;
891 if (dest_li->li_next == NULL)
892 {
893 // Need to add an empty item.
894 if (list_append_number(dest, 0) == FAIL)
895 {
896 src_li = NULL;
897 break;
898 }
899 }
900 dest_li = dest_li->li_next;
901 ++idx;
902 }
903 if (src_li != NULL)
904 {
905 emsg(_(e_list_value_has_more_items_than_targets));
906 return FAIL;
907 }
908 if (empty_idx2
909 ? (dest_li != NULL && dest_li->li_next != NULL)
910 : idx != idx2)
911 {
912 emsg(_(e_list_value_does_not_have_enough_items));
913 return FAIL;
914 }
915 return OK;
916}
917
918/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200919 * Flatten "list" to depth "maxdepth".
920 * It does nothing if "maxdepth" is 0.
921 * Returns FAIL when out of memory.
922 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100923 static void
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200924list_flatten(list_T *list, long maxdepth)
925{
926 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200927 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200928 int n;
929
930 if (maxdepth == 0)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100931 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200932 CHECK_LIST_MATERIALIZE(list);
933
934 n = 0;
935 item = list->lv_first;
936 while (item != NULL)
937 {
938 fast_breakcheck();
939 if (got_int)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100940 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200941
942 if (item->li_tv.v_type == VAR_LIST)
943 {
944 listitem_T *next = item->li_next;
945
946 vimlist_remove(list, item, item);
947 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +0200948 {
949 list_free_item(list, item);
Bram Moolenaar3b690062021-02-01 20:14:51 +0100950 return;
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +0200951 }
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200952 clear_tv(&item->li_tv);
953 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200954
955 if (item->li_prev == NULL)
956 item = list->lv_first;
957 else
958 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200959 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200960
961 if (++n >= maxdepth)
962 {
963 n = 0;
964 item = next;
965 }
966 }
967 else
968 {
969 n = 0;
970 item = item->li_next;
971 }
972 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200973}
974
975/*
Bram Moolenaar3b690062021-02-01 20:14:51 +0100976 * "flatten()" and "flattennew()" functions
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200977 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100978 static void
979flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200980{
981 list_T *l;
982 long maxdepth;
983 int error = FALSE;
984
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200985 if (in_vim9script()
986 && (check_for_list_arg(argvars, 0) == FAIL
987 || check_for_opt_number_arg(argvars, 1) == FAIL))
988 return;
989
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200990 if (argvars[0].v_type != VAR_LIST)
991 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +0000992 semsg(_(e_argument_of_str_must_be_list), "flatten()");
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200993 return;
994 }
995
996 if (argvars[1].v_type == VAR_UNKNOWN)
997 maxdepth = 999999;
998 else
999 {
1000 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
1001 if (error)
1002 return;
1003 if (maxdepth < 0)
1004 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001005 emsg(_(e_maxdepth_must_be_non_negative_number));
Bram Moolenaar077a1e62020-06-08 20:50:43 +02001006 return;
1007 }
1008 }
1009
1010 l = argvars[0].vval.v_list;
Bram Moolenaar3b690062021-02-01 20:14:51 +01001011 rettv->v_type = VAR_LIST;
1012 rettv->vval.v_list = l;
1013 if (l == NULL)
1014 return;
1015
1016 if (make_copy)
1017 {
Bram Moolenaar381692b2022-02-02 20:01:27 +00001018 l = list_copy(l, TRUE, TRUE, get_copyID());
Bram Moolenaar3b690062021-02-01 20:14:51 +01001019 rettv->vval.v_list = l;
1020 if (l == NULL)
1021 return;
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +02001022 // The type will change.
1023 free_type(l->lv_type);
1024 l->lv_type = NULL;
Bram Moolenaar3b690062021-02-01 20:14:51 +01001025 }
1026 else
1027 {
1028 if (value_check_lock(l->lv_lock,
1029 (char_u *)N_("flatten() argument"), TRUE))
1030 return;
1031 ++l->lv_refcount;
1032 }
1033
1034 list_flatten(l, maxdepth);
1035}
1036
1037/*
1038 * "flatten(list[, {maxdepth}])" function
1039 */
1040 void
1041f_flatten(typval_T *argvars, typval_T *rettv)
1042{
1043 if (in_vim9script())
1044 emsg(_(e_cannot_use_flatten_in_vim9_script));
1045 else
1046 flatten_common(argvars, rettv, FALSE);
1047}
1048
1049/*
1050 * "flattennew(list[, {maxdepth}])" function
1051 */
1052 void
1053f_flattennew(typval_T *argvars, typval_T *rettv)
1054{
1055 flatten_common(argvars, rettv, TRUE);
Bram Moolenaar077a1e62020-06-08 20:50:43 +02001056}
1057
1058/*
Bram Moolenaar1a739232020-10-10 15:37:58 +02001059 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001060 * If "bef" is NULL append at the end, otherwise insert before this item.
1061 * Returns FAIL when out of memory.
1062 */
1063 int
1064list_extend(list_T *l1, list_T *l2, listitem_T *bef)
1065{
1066 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001067 int todo;
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001068 listitem_T *bef_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001069
Bram Moolenaar1a739232020-10-10 15:37:58 +02001070 // NULL list is equivalent to an empty list: nothing to do.
1071 if (l2 == NULL || l2->lv_len == 0)
1072 return OK;
1073
1074 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001075 CHECK_LIST_MATERIALIZE(l1);
1076 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001078 // When exending a list with itself, at some point we run into the item
1079 // that was before "bef" and need to skip over the already inserted items
1080 // to "bef".
1081 bef_prev = bef == NULL ? NULL : bef->li_prev;
1082
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001083 // We also quit the loop when we have inserted the original item count of
1084 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001085 for (item = l2->lv_first; item != NULL && --todo >= 0;
1086 item = item == bef_prev ? bef : item->li_next)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001087 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
1088 return FAIL;
1089 return OK;
1090}
1091
1092/*
1093 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
1094 * Return FAIL when out of memory.
1095 */
1096 int
1097list_concat(list_T *l1, list_T *l2, typval_T *tv)
1098{
1099 list_T *l;
1100
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001101 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +02001102 if (l1 == NULL)
1103 l = list_alloc();
1104 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00001105 l = list_copy(l1, FALSE, TRUE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001106 if (l == NULL)
1107 return FAIL;
1108 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +01001109 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001110 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001111 if (l1 == NULL)
1112 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001113
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001114 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +02001115 return list_extend(l, l2, NULL);
1116}
1117
Bram Moolenaar9af78762020-06-16 11:34:42 +02001118 list_T *
1119list_slice(list_T *ol, long n1, long n2)
1120{
1121 listitem_T *item;
1122 list_T *l = list_alloc();
1123
1124 if (l == NULL)
1125 return NULL;
1126 for (item = list_find(ol, n1); n1 <= n2; ++n1)
1127 {
1128 if (list_append_tv(l, &item->li_tv) == FAIL)
1129 {
1130 list_free(l);
1131 return NULL;
1132 }
1133 item = item->li_next;
1134 }
1135 return l;
1136}
1137
Bram Moolenaared591872020-08-15 22:14:53 +02001138 int
1139list_slice_or_index(
1140 list_T *list,
1141 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01001142 varnumber_T n1_arg,
1143 varnumber_T n2_arg,
1144 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +02001145 typval_T *rettv,
1146 int verbose)
1147{
1148 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001149 varnumber_T n1 = n1_arg;
1150 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +02001151 typval_T var1;
1152
1153 if (n1 < 0)
1154 n1 = len + n1;
1155 if (n1 < 0 || n1 >= len)
1156 {
Bram Moolenaar92f05f22021-08-12 21:12:56 +02001157 // For a range we allow invalid values and for legacy script return an
1158 // empty list, for Vim9 script start at the first item.
1159 // A list index out of range is an error.
Bram Moolenaared591872020-08-15 22:14:53 +02001160 if (!range)
1161 {
1162 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001163 semsg(_(e_list_index_out_of_range_nr), (long)n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +02001164 return FAIL;
1165 }
Bram Moolenaar92f05f22021-08-12 21:12:56 +02001166 if (in_vim9script())
1167 n1 = n1 < 0 ? 0 : len;
1168 else
1169 n1 = len;
Bram Moolenaared591872020-08-15 22:14:53 +02001170 }
1171 if (range)
1172 {
1173 list_T *l;
1174
1175 if (n2 < 0)
1176 n2 = len + n2;
1177 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01001178 n2 = len - (exclusive ? 0 : 1);
1179 if (exclusive)
1180 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +02001181 if (n2 < 0 || n2 + 1 < n1)
1182 n2 = -1;
1183 l = list_slice(list, n1, n2);
1184 if (l == NULL)
1185 return FAIL;
1186 clear_tv(rettv);
1187 rettv_list_set(rettv, l);
1188 }
1189 else
1190 {
1191 // copy the item to "var1" to avoid that freeing the list makes it
1192 // invalid.
1193 copy_tv(&list_find(list, n1)->li_tv, &var1);
1194 clear_tv(rettv);
1195 *rettv = var1;
1196 }
1197 return OK;
1198}
1199
Bram Moolenaarda861d62016-07-17 15:46:27 +02001200/*
1201 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1202 * The refcount of the new list is set to 1.
Bram Moolenaar381692b2022-02-02 20:01:27 +00001203 * See item_copy() for "top" and "copyID".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001204 * Returns NULL when out of memory.
1205 */
1206 list_T *
Bram Moolenaar381692b2022-02-02 20:01:27 +00001207list_copy(list_T *orig, int deep, int top, int copyID)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001208{
1209 list_T *copy;
1210 listitem_T *item;
1211 listitem_T *ni;
1212
1213 if (orig == NULL)
1214 return NULL;
1215
1216 copy = list_alloc();
1217 if (copy != NULL)
1218 {
Bram Moolenaar7676c152022-02-03 21:47:34 +00001219 if (orig->lv_type == NULL)
1220 copy->lv_type = NULL;
1221 else
1222 copy->lv_type = alloc_type(top || deep
1223 ? &t_list_any: orig->lv_type);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001224 if (copyID != 0)
1225 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001226 // Do this before adding the items, because one of the items may
1227 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001228 orig->lv_copyID = copyID;
1229 orig->lv_copylist = copy;
1230 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001231 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001232 for (item = orig->lv_first; item != NULL && !got_int;
1233 item = item->li_next)
1234 {
1235 ni = listitem_alloc();
1236 if (ni == NULL)
1237 break;
1238 if (deep)
1239 {
Bram Moolenaar381692b2022-02-02 20:01:27 +00001240 if (item_copy(&item->li_tv, &ni->li_tv,
1241 deep, FALSE, copyID) == FAIL)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001242 {
1243 vim_free(ni);
1244 break;
1245 }
1246 }
1247 else
1248 copy_tv(&item->li_tv, &ni->li_tv);
1249 list_append(copy, ni);
1250 }
1251 ++copy->lv_refcount;
1252 if (item != NULL)
1253 {
1254 list_unref(copy);
1255 copy = NULL;
1256 }
1257 }
1258
1259 return copy;
1260}
1261
1262/*
1263 * Remove items "item" to "item2" from list "l".
1264 * Does not free the listitem or the value!
1265 * This used to be called list_remove, but that conflicts with a Sun header
1266 * file.
1267 */
1268 void
1269vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1270{
1271 listitem_T *ip;
1272
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001273 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001275 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001276 for (ip = item; ip != NULL; ip = ip->li_next)
1277 {
1278 --l->lv_len;
1279 list_fix_watch(l, ip);
1280 if (ip == item2)
1281 break;
1282 }
1283
1284 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001285 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001286 else
1287 item2->li_next->li_prev = item->li_prev;
1288 if (item->li_prev == NULL)
1289 l->lv_first = item2->li_next;
1290 else
1291 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001292 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001293}
1294
1295/*
1296 * Return an allocated string with the string representation of a list.
1297 * May return NULL.
1298 */
1299 char_u *
1300list2string(typval_T *tv, int copyID, int restore_copyID)
1301{
1302 garray_T ga;
1303
1304 if (tv->vval.v_list == NULL)
1305 return NULL;
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001306 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001307 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001308 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001309 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1310 FALSE, restore_copyID, copyID) == FAIL)
1311 {
1312 vim_free(ga.ga_data);
1313 return NULL;
1314 }
1315 ga_append(&ga, ']');
1316 ga_append(&ga, NUL);
1317 return (char_u *)ga.ga_data;
1318}
1319
1320typedef struct join_S {
1321 char_u *s;
1322 char_u *tofree;
1323} join_T;
1324
1325 static int
1326list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001327 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001328 list_T *l,
1329 char_u *sep,
1330 int echo_style,
1331 int restore_copyID,
1332 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001333 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001334{
1335 int i;
1336 join_T *p;
1337 int len;
1338 int sumlen = 0;
1339 int first = TRUE;
1340 char_u *tofree;
1341 char_u numbuf[NUMBUFLEN];
1342 listitem_T *item;
1343 char_u *s;
1344
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001345 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001346 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001347 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1348 {
1349 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001350 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001351 if (s == NULL)
1352 return FAIL;
1353
1354 len = (int)STRLEN(s);
1355 sumlen += len;
1356
1357 (void)ga_grow(join_gap, 1);
1358 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1359 if (tofree != NULL || s != numbuf)
1360 {
1361 p->s = s;
1362 p->tofree = tofree;
1363 }
1364 else
1365 {
1366 p->s = vim_strnsave(s, len);
1367 p->tofree = p->s;
1368 }
1369
1370 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001371 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001372 break;
1373 }
1374
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001375 // Allocate result buffer with its total size, avoid re-allocation and
1376 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001377 if (join_gap->ga_len >= 2)
1378 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1379 if (ga_grow(gap, sumlen + 2) == FAIL)
1380 return FAIL;
1381
1382 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1383 {
1384 if (first)
1385 first = FALSE;
1386 else
1387 ga_concat(gap, sep);
1388 p = ((join_T *)join_gap->ga_data) + i;
1389
1390 if (p->s != NULL)
1391 ga_concat(gap, p->s);
1392 line_breakcheck();
1393 }
1394
1395 return OK;
1396}
1397
1398/*
1399 * Join list "l" into a string in "*gap", using separator "sep".
1400 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1401 * Return FAIL or OK.
1402 */
1403 int
1404list_join(
1405 garray_T *gap,
1406 list_T *l,
1407 char_u *sep,
1408 int echo_style,
1409 int restore_copyID,
1410 int copyID)
1411{
1412 garray_T join_ga;
1413 int retval;
1414 join_T *p;
1415 int i;
1416
1417 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001418 return OK; // nothing to do
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001419 ga_init2(&join_ga, sizeof(join_T), l->lv_len);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001420 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1421 copyID, &join_ga);
1422
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001423 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001424 if (join_ga.ga_data != NULL)
1425 {
1426 p = (join_T *)join_ga.ga_data;
1427 for (i = 0; i < join_ga.ga_len; ++i)
1428 {
1429 vim_free(p->tofree);
1430 ++p;
1431 }
1432 ga_clear(&join_ga);
1433 }
1434
1435 return retval;
1436}
1437
1438/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001439 * "join()" function
1440 */
1441 void
1442f_join(typval_T *argvars, typval_T *rettv)
1443{
1444 garray_T ga;
1445 char_u *sep;
1446
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001447 if (in_vim9script()
1448 && (check_for_list_arg(argvars, 0) == FAIL
1449 || check_for_opt_string_arg(argvars, 1) == FAIL))
1450 return;
1451
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001452 if (argvars[0].v_type != VAR_LIST)
1453 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001454 emsg(_(e_list_required));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001455 return;
1456 }
Bram Moolenaaref982572021-08-12 19:27:57 +02001457 rettv->v_type = VAR_STRING;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001458 if (argvars[0].vval.v_list == NULL)
1459 return;
Bram Moolenaaref982572021-08-12 19:27:57 +02001460
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001461 if (argvars[1].v_type == VAR_UNKNOWN)
1462 sep = (char_u *)" ";
1463 else
1464 sep = tv_get_string_chk(&argvars[1]);
1465
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001466 if (sep != NULL)
1467 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001468 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001469 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1470 ga_append(&ga, NUL);
1471 rettv->vval.v_string = (char_u *)ga.ga_data;
1472 }
1473 else
1474 rettv->vval.v_string = NULL;
1475}
1476
1477/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001478 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001479 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001480 * Return OK or FAIL.
1481 */
1482 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001483eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001484{
Bram Moolenaar71478202020-06-26 22:46:27 +02001485 int evaluate = evalarg == NULL ? FALSE
1486 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001487 list_T *l = NULL;
1488 typval_T tv;
1489 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001490 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001491 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001492
1493 if (evaluate)
1494 {
1495 l = list_alloc();
1496 if (l == NULL)
1497 return FAIL;
1498 }
1499
Bram Moolenaar962d7212020-07-04 14:15:00 +02001500 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001501 while (**arg != ']' && **arg != NUL)
1502 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001503 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001504 goto failret;
1505 if (evaluate)
1506 {
1507 item = listitem_alloc();
1508 if (item != NULL)
1509 {
1510 item->li_tv = tv;
1511 item->li_tv.v_lock = 0;
1512 list_append(l, item);
1513 }
1514 else
1515 clear_tv(&tv);
1516 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001517 // Legacy Vim script allowed a space before the comma.
1518 if (!vim9script)
1519 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001520
Bram Moolenaare6e03172020-06-27 16:36:05 +02001521 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001522 had_comma = **arg == ',';
1523 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001524 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001525 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001526 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001527 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001528 goto failret;
1529 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001530 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001531 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001532
Bram Moolenaare6b53242020-07-01 17:28:33 +02001533 // The "]" can be on the next line. But a double quoted string may
1534 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001535 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001536 if (**arg == ']')
1537 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001538
1539 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001540 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001542 {
1543 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001544 semsg(_(e_no_white_space_allowed_before_str_str),
1545 ",", *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001546 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001547 semsg(_(e_missing_comma_in_list_str), *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001548 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001549 goto failret;
1550 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001551 }
1552
1553 if (**arg != ']')
1554 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 if (do_error)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001556 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001557failret:
1558 if (evaluate)
1559 list_free(l);
1560 return FAIL;
1561 }
1562
Bram Moolenaar9d489562020-07-30 20:08:50 +02001563 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001564 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001565 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001566
1567 return OK;
1568}
1569
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001570/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001571 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001572 */
1573 int
1574write_list(FILE *fd, list_T *list, int binary)
1575{
1576 listitem_T *li;
1577 int c;
1578 int ret = OK;
1579 char_u *s;
1580
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001581 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001582 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001584 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001585 {
1586 if (*s == '\n')
1587 c = putc(NUL, fd);
1588 else
1589 c = putc(*s, fd);
1590 if (c == EOF)
1591 {
1592 ret = FAIL;
1593 break;
1594 }
1595 }
1596 if (!binary || li->li_next != NULL)
1597 if (putc('\n', fd) == EOF)
1598 {
1599 ret = FAIL;
1600 break;
1601 }
1602 if (ret == FAIL)
1603 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001604 emsg(_(e_error_while_writing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605 break;
1606 }
1607 }
1608 return ret;
1609}
1610
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001611/*
1612 * Initialize a static list with 10 items.
1613 */
1614 void
1615init_static_list(staticList10_T *sl)
1616{
1617 list_T *l = &sl->sl_list;
1618 int i;
1619
1620 memset(sl, 0, sizeof(staticList10_T));
1621 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001622 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001623 l->lv_refcount = DO_NOT_FREE_CNT;
1624 l->lv_lock = VAR_FIXED;
1625 sl->sl_list.lv_len = 10;
1626
1627 for (i = 0; i < 10; ++i)
1628 {
1629 listitem_T *li = &sl->sl_items[i];
1630
1631 if (i == 0)
1632 li->li_prev = NULL;
1633 else
1634 li->li_prev = li - 1;
1635 if (i == 9)
1636 li->li_next = NULL;
1637 else
1638 li->li_next = li + 1;
1639 }
1640}
1641
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001642/*
1643 * "list2str()" function
1644 */
1645 void
1646f_list2str(typval_T *argvars, typval_T *rettv)
1647{
1648 list_T *l;
1649 listitem_T *li;
1650 garray_T ga;
1651 int utf8 = FALSE;
1652
1653 rettv->v_type = VAR_STRING;
1654 rettv->vval.v_string = NULL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001655
1656 if (in_vim9script()
1657 && (check_for_list_arg(argvars, 0) == FAIL
1658 || check_for_opt_bool_arg(argvars, 1) == FAIL))
1659 return;
1660
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001661 if (argvars[0].v_type != VAR_LIST)
1662 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001663 emsg(_(e_invalid_argument));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001664 return;
1665 }
1666
1667 l = argvars[0].vval.v_list;
1668 if (l == NULL)
1669 return; // empty list results in empty string
1670
1671 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001672 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001673
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001674 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001675 ga_init2(&ga, 1, 80);
1676 if (has_mbyte || utf8)
1677 {
1678 char_u buf[MB_MAXBYTES + 1];
1679 int (*char2bytes)(int, char_u *);
1680
1681 if (utf8 || enc_utf8)
1682 char2bytes = utf_char2bytes;
1683 else
1684 char2bytes = mb_char2bytes;
1685
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001686 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001687 {
1688 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1689 ga_concat(&ga, buf);
1690 }
1691 ga_append(&ga, NUL);
1692 }
1693 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1694 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001695 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001696 ga_append(&ga, tv_get_number(&li->li_tv));
1697 ga_append(&ga, NUL);
1698 }
1699
1700 rettv->v_type = VAR_STRING;
1701 rettv->vval.v_string = ga.ga_data;
1702}
1703
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001704/*
1705 * Remove item argvars[1] from List argvars[0]. If argvars[2] is supplied, then
1706 * remove the range of items from argvars[1] to argvars[2] (inclusive).
1707 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001708 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001709list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1710{
1711 list_T *l;
1712 listitem_T *item, *item2;
1713 listitem_T *li;
1714 int error = FALSE;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001715 long idx;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001716 long end;
1717 int cnt = 0;
1718 list_T *rl;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001719
1720 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001721 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001722 return;
1723
1724 idx = (long)tv_get_number_chk(&argvars[1], &error);
1725 if (error)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001726 return; // type error: do nothing, errmsg already given
1727
1728 if ((item = list_find(l, idx)) == NULL)
1729 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001730 semsg(_(e_list_index_out_of_range_nr), idx);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001731 return;
1732 }
1733
1734 if (argvars[2].v_type == VAR_UNKNOWN)
1735 {
1736 // Remove one item, return its value.
1737 vimlist_remove(l, item, item);
1738 *rettv = item->li_tv;
1739 list_free_item(l, item);
1740 return;
1741 }
1742
1743 // Remove range of items, return list with values.
1744 end = (long)tv_get_number_chk(&argvars[2], &error);
1745 if (error)
1746 return; // type error: do nothing
1747
1748 if ((item2 = list_find(l, end)) == NULL)
1749 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001750 semsg(_(e_list_index_out_of_range_nr), end);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001751 return;
1752 }
1753
1754 for (li = item; li != NULL; li = li->li_next)
1755 {
1756 ++cnt;
1757 if (li == item2)
1758 break;
1759 }
1760 if (li == NULL) // didn't find "item2" after "item"
1761 {
1762 emsg(_(e_invalid_range));
1763 return;
1764 }
1765
1766 vimlist_remove(l, item, item2);
1767 if (rettv_list_alloc(rettv) != OK)
1768 return;
1769
1770 rl = rettv->vval.v_list;
1771
1772 if (l->lv_with_items > 0)
1773 {
1774 // need to copy the list items and move the value
1775 while (item != NULL)
1776 {
1777 li = listitem_alloc();
1778 if (li == NULL)
1779 return;
1780 li->li_tv = item->li_tv;
1781 init_tv(&item->li_tv);
1782 list_append(rl, li);
1783 if (item == item2)
1784 break;
1785 item = item->li_next;
1786 }
1787 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001788 else
1789 {
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001790 rl->lv_first = item;
1791 rl->lv_u.mat.lv_last = item2;
1792 item->li_prev = NULL;
1793 item2->li_next = NULL;
1794 rl->lv_len = cnt;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001795 }
1796}
1797
1798static int item_compare(const void *s1, const void *s2);
1799static int item_compare2(const void *s1, const void *s2);
1800
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001801// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001802typedef struct
1803{
1804 listitem_T *item;
1805 int idx;
1806} sortItem_T;
1807
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001808// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001809typedef struct
1810{
1811 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001812 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001813 int item_compare_numeric;
1814 int item_compare_numbers;
1815#ifdef FEAT_FLOAT
1816 int item_compare_float;
1817#endif
1818 char_u *item_compare_func;
1819 partial_T *item_compare_partial;
1820 dict_T *item_compare_selfdict;
1821 int item_compare_func_err;
1822 int item_compare_keep_zero;
1823} sortinfo_T;
1824static sortinfo_T *sortinfo = NULL;
1825#define ITEM_COMPARE_FAIL 999
1826
1827/*
1828 * Compare functions for f_sort() and f_uniq() below.
1829 */
1830 static int
1831item_compare(const void *s1, const void *s2)
1832{
1833 sortItem_T *si1, *si2;
1834 typval_T *tv1, *tv2;
1835 char_u *p1, *p2;
1836 char_u *tofree1 = NULL, *tofree2 = NULL;
1837 int res;
1838 char_u numbuf1[NUMBUFLEN];
1839 char_u numbuf2[NUMBUFLEN];
1840
1841 si1 = (sortItem_T *)s1;
1842 si2 = (sortItem_T *)s2;
1843 tv1 = &si1->item->li_tv;
1844 tv2 = &si2->item->li_tv;
1845
1846 if (sortinfo->item_compare_numbers)
1847 {
1848 varnumber_T v1 = tv_get_number(tv1);
1849 varnumber_T v2 = tv_get_number(tv2);
1850
1851 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1852 }
1853
1854#ifdef FEAT_FLOAT
1855 if (sortinfo->item_compare_float)
1856 {
1857 float_T v1 = tv_get_float(tv1);
1858 float_T v2 = tv_get_float(tv2);
1859
1860 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1861 }
1862#endif
1863
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001864 // tv2string() puts quotes around a string and allocates memory. Don't do
1865 // that for string variables. Use a single quote when comparing with a
1866 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001867 if (tv1->v_type == VAR_STRING)
1868 {
1869 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1870 p1 = (char_u *)"'";
1871 else
1872 p1 = tv1->vval.v_string;
1873 }
1874 else
1875 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1876 if (tv2->v_type == VAR_STRING)
1877 {
1878 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1879 p2 = (char_u *)"'";
1880 else
1881 p2 = tv2->vval.v_string;
1882 }
1883 else
1884 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1885 if (p1 == NULL)
1886 p1 = (char_u *)"";
1887 if (p2 == NULL)
1888 p2 = (char_u *)"";
1889 if (!sortinfo->item_compare_numeric)
1890 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001891 if (sortinfo->item_compare_lc)
1892 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001893 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001894 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001895 }
1896 else
1897 {
1898 double n1, n2;
1899 n1 = strtod((char *)p1, (char **)&p1);
1900 n2 = strtod((char *)p2, (char **)&p2);
1901 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1902 }
1903
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001904 // When the result would be zero, compare the item indexes. Makes the
1905 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001906 if (res == 0 && !sortinfo->item_compare_keep_zero)
1907 res = si1->idx > si2->idx ? 1 : -1;
1908
1909 vim_free(tofree1);
1910 vim_free(tofree2);
1911 return res;
1912}
1913
1914 static int
1915item_compare2(const void *s1, const void *s2)
1916{
1917 sortItem_T *si1, *si2;
1918 int res;
1919 typval_T rettv;
1920 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001921 char_u *func_name;
1922 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001923 funcexe_T funcexe;
Bram Moolenaar23018f22021-12-27 11:54:37 +00001924 int did_emsg_before = did_emsg;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001925
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001926 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001927 if (sortinfo->item_compare_func_err)
1928 return 0;
1929
1930 si1 = (sortItem_T *)s1;
1931 si2 = (sortItem_T *)s2;
1932
1933 if (partial == NULL)
1934 func_name = sortinfo->item_compare_func;
1935 else
1936 func_name = partial_name(partial);
1937
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001938 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1939 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001940 copy_tv(&si1->item->li_tv, &argv[0]);
1941 copy_tv(&si2->item->li_tv, &argv[1]);
1942
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001943 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001944 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001945 funcexe.fe_evaluate = TRUE;
1946 funcexe.fe_partial = partial;
1947 funcexe.fe_selfdict = sortinfo->item_compare_selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001948 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001949 clear_tv(&argv[0]);
1950 clear_tv(&argv[1]);
1951
Bram Moolenaar23018f22021-12-27 11:54:37 +00001952 if (res == FAIL || did_emsg > did_emsg_before)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001953 res = ITEM_COMPARE_FAIL;
1954 else
Yasuhiro Matsumotoc04f6232021-09-19 17:01:39 +02001955 {
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001956 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Yasuhiro Matsumotoc04f6232021-09-19 17:01:39 +02001957 if (res > 0)
1958 res = 1;
1959 else if (res < 0)
1960 res = -1;
1961 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001962 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001963 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001964 clear_tv(&rettv);
1965
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001966 // When the result would be zero, compare the pointers themselves. Makes
1967 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001968 if (res == 0 && !sortinfo->item_compare_keep_zero)
1969 res = si1->idx > si2->idx ? 1 : -1;
1970
1971 return res;
1972}
1973
1974/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00001975 * sort() List "l"
1976 */
1977 static void
1978do_sort(list_T *l, sortinfo_T *info)
1979{
1980 long len;
1981 sortItem_T *ptrs;
1982 long i = 0;
1983 listitem_T *li;
1984
1985 len = list_len(l);
1986
1987 // Make an array with each entry pointing to an item in the List.
1988 ptrs = ALLOC_MULT(sortItem_T, len);
1989 if (ptrs == NULL)
1990 return;
1991
1992 // sort(): ptrs will be the list to sort
1993 FOR_ALL_LIST_ITEMS(l, li)
1994 {
1995 ptrs[i].item = li;
1996 ptrs[i].idx = i;
1997 ++i;
1998 }
1999
2000 info->item_compare_func_err = FALSE;
2001 info->item_compare_keep_zero = FALSE;
2002 // test the compare function
2003 if ((info->item_compare_func != NULL
2004 || info->item_compare_partial != NULL)
2005 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
2006 == ITEM_COMPARE_FAIL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002007 emsg(_(e_sort_compare_function_failed));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002008 else
2009 {
2010 // Sort the array with item pointers.
2011 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
2012 info->item_compare_func == NULL
2013 && info->item_compare_partial == NULL
2014 ? item_compare : item_compare2);
2015
2016 if (!info->item_compare_func_err)
2017 {
2018 // Clear the List and append the items in sorted order.
2019 l->lv_first = l->lv_u.mat.lv_last
2020 = l->lv_u.mat.lv_idx_item = NULL;
2021 l->lv_len = 0;
2022 for (i = 0; i < len; ++i)
2023 list_append(l, ptrs[i].item);
2024 }
2025 }
2026
2027 vim_free(ptrs);
2028}
2029
2030/*
2031 * uniq() List "l"
2032 */
2033 static void
2034do_uniq(list_T *l, sortinfo_T *info)
2035{
2036 long len;
2037 sortItem_T *ptrs;
2038 long i = 0;
2039 listitem_T *li;
2040 int (*item_compare_func_ptr)(const void *, const void *);
2041
2042 len = list_len(l);
2043
2044 // Make an array with each entry pointing to an item in the List.
2045 ptrs = ALLOC_MULT(sortItem_T, len);
2046 if (ptrs == NULL)
2047 return;
2048
2049 // f_uniq(): ptrs will be a stack of items to remove
2050 info->item_compare_func_err = FALSE;
2051 info->item_compare_keep_zero = TRUE;
2052 item_compare_func_ptr = info->item_compare_func != NULL
2053 || info->item_compare_partial != NULL
2054 ? item_compare2 : item_compare;
2055
2056 for (li = l->lv_first; li != NULL && li->li_next != NULL;
2057 li = li->li_next)
2058 {
2059 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
2060 == 0)
2061 ptrs[i++].item = li;
2062 if (info->item_compare_func_err)
2063 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002064 emsg(_(e_uniq_compare_function_failed));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002065 break;
2066 }
2067 }
2068
2069 if (!info->item_compare_func_err)
2070 {
2071 while (--i >= 0)
2072 {
2073 li = ptrs[i].item->li_next;
2074 ptrs[i].item->li_next = li->li_next;
2075 if (li->li_next != NULL)
2076 li->li_next->li_prev = ptrs[i].item;
2077 else
2078 l->lv_u.mat.lv_last = ptrs[i].item;
2079 list_fix_watch(l, li);
2080 listitem_free(l, li);
2081 l->lv_len--;
2082 }
2083 }
2084
2085 vim_free(ptrs);
2086}
2087
2088/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002089 * Parse the optional arguments supplied to the sort() or uniq() function and
2090 * return the values in "info".
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002091 */
2092 static int
2093parse_sort_uniq_args(typval_T *argvars, sortinfo_T *info)
2094{
2095 info->item_compare_ic = FALSE;
2096 info->item_compare_lc = FALSE;
2097 info->item_compare_numeric = FALSE;
2098 info->item_compare_numbers = FALSE;
2099#ifdef FEAT_FLOAT
2100 info->item_compare_float = FALSE;
2101#endif
2102 info->item_compare_func = NULL;
2103 info->item_compare_partial = NULL;
2104 info->item_compare_selfdict = NULL;
2105
2106 if (argvars[1].v_type == VAR_UNKNOWN)
2107 return OK;
2108
2109 // optional second argument: {func}
2110 if (argvars[1].v_type == VAR_FUNC)
2111 info->item_compare_func = argvars[1].vval.v_string;
2112 else if (argvars[1].v_type == VAR_PARTIAL)
2113 info->item_compare_partial = argvars[1].vval.v_partial;
2114 else
2115 {
2116 int error = FALSE;
2117 int nr = 0;
2118
2119 if (argvars[1].v_type == VAR_NUMBER)
2120 {
2121 nr = tv_get_number_chk(&argvars[1], &error);
2122 if (error)
2123 return FAIL;
2124 if (nr == 1)
2125 info->item_compare_ic = TRUE;
2126 }
2127 if (nr != 1)
2128 {
2129 if (argvars[1].v_type != VAR_NUMBER)
2130 info->item_compare_func = tv_get_string(&argvars[1]);
2131 else if (nr != 0)
2132 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002133 emsg(_(e_invalid_argument));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002134 return FAIL;
2135 }
2136 }
2137 if (info->item_compare_func != NULL)
2138 {
2139 if (*info->item_compare_func == NUL)
2140 {
2141 // empty string means default sort
2142 info->item_compare_func = NULL;
2143 }
2144 else if (STRCMP(info->item_compare_func, "n") == 0)
2145 {
2146 info->item_compare_func = NULL;
2147 info->item_compare_numeric = TRUE;
2148 }
2149 else if (STRCMP(info->item_compare_func, "N") == 0)
2150 {
2151 info->item_compare_func = NULL;
2152 info->item_compare_numbers = TRUE;
2153 }
2154#ifdef FEAT_FLOAT
2155 else if (STRCMP(info->item_compare_func, "f") == 0)
2156 {
2157 info->item_compare_func = NULL;
2158 info->item_compare_float = TRUE;
2159 }
2160#endif
2161 else if (STRCMP(info->item_compare_func, "i") == 0)
2162 {
2163 info->item_compare_func = NULL;
2164 info->item_compare_ic = TRUE;
2165 }
2166 else if (STRCMP(info->item_compare_func, "l") == 0)
2167 {
2168 info->item_compare_func = NULL;
2169 info->item_compare_lc = TRUE;
2170 }
2171 }
2172 }
2173
2174 if (argvars[2].v_type != VAR_UNKNOWN)
2175 {
2176 // optional third argument: {dict}
2177 if (argvars[2].v_type != VAR_DICT)
2178 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002179 emsg(_(e_dictionary_required));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002180 return FAIL;
2181 }
2182 info->item_compare_selfdict = argvars[2].vval.v_dict;
2183 }
2184
2185 return OK;
2186}
2187
2188/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002189 * "sort()" or "uniq()" function
2190 */
2191 static void
2192do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
2193{
2194 list_T *l;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002195 sortinfo_T *old_sortinfo;
2196 sortinfo_T info;
2197 long len;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002198
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002199 if (in_vim9script()
2200 && (check_for_list_arg(argvars, 0) == FAIL
2201 || (argvars[1].v_type != VAR_UNKNOWN
2202 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
2203 return;
2204
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002205 if (argvars[0].v_type != VAR_LIST)
2206 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002207 semsg(_(e_argument_of_str_must_be_list), sort ? "sort()" : "uniq()");
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002208 return;
2209 }
2210
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002211 // Pointer to current info struct used in compare function. Save and
2212 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002213 old_sortinfo = sortinfo;
2214 sortinfo = &info;
2215
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002216 l = argvars[0].vval.v_list;
2217 if (l != NULL && value_check_lock(l->lv_lock,
2218 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
2219 TRUE))
2220 goto theend;
2221 rettv_list_set(rettv, l);
2222 if (l == NULL)
2223 goto theend;
2224 CHECK_LIST_MATERIALIZE(l);
2225
2226 len = list_len(l);
2227 if (len <= 1)
2228 goto theend; // short list sorts pretty quickly
2229
2230 if (parse_sort_uniq_args(argvars, &info) == FAIL)
2231 goto theend;
2232
2233 if (sort)
2234 do_sort(l, &info);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002235 else
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002236 do_uniq(l, &info);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002237
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002238theend:
2239 sortinfo = old_sortinfo;
2240}
2241
2242/*
2243 * "sort({list})" function
2244 */
2245 void
2246f_sort(typval_T *argvars, typval_T *rettv)
2247{
2248 do_sort_uniq(argvars, rettv, TRUE);
2249}
2250
2251/*
2252 * "uniq({list})" function
2253 */
2254 void
2255f_uniq(typval_T *argvars, typval_T *rettv)
2256{
2257 do_sort_uniq(argvars, rettv, FALSE);
2258}
2259
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002260/*
2261 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01002262 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002263 */
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002264 int
Bram Moolenaarea696852020-11-09 18:31:39 +01002265filter_map_one(
2266 typval_T *tv, // original value
2267 typval_T *expr, // callback
2268 filtermap_T filtermap,
2269 typval_T *newtv, // for map() and mapnew(): new value
2270 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002271{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002272 typval_T argv[3];
2273 int retval = FAIL;
2274
2275 copy_tv(tv, get_vim_var_tv(VV_VAL));
2276 argv[0] = *get_vim_var_tv(VV_KEY);
2277 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01002278 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002279 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002280 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002281 {
2282 int error = FALSE;
2283
2284 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02002285 if (in_vim9script())
Bram Moolenaar18024052021-12-25 21:43:28 +00002286 *remp = !tv_get_bool_chk(newtv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002287 else
Bram Moolenaarea696852020-11-09 18:31:39 +01002288 *remp = (tv_get_number_chk(newtv, &error) == 0);
2289 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002290 // On type error, nothing has been removed; return FAIL to stop the
2291 // loop. The error message was given by tv_get_number_chk().
2292 if (error)
2293 goto theend;
2294 }
2295 retval = OK;
2296theend:
2297 clear_tv(get_vim_var_tv(VV_VAL));
2298 return retval;
2299}
2300
2301/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002302 * Implementation of map() and filter() for a List. Apply "expr" to every item
2303 * in List "l" and return the result in "rettv".
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002304 */
2305 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002306list_filter_map(
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002307 list_T *l,
2308 filtermap_T filtermap,
2309 type_T *argtype,
2310 char *func_name,
2311 char_u *arg_errmsg,
2312 typval_T *expr,
2313 typval_T *rettv)
2314{
2315 int prev_lock;
2316 list_T *l_ret = NULL;
2317 int idx = 0;
2318 int rem;
2319 listitem_T *li, *nli;
2320
2321 if (filtermap == FILTERMAP_MAPNEW)
2322 {
2323 rettv->v_type = VAR_LIST;
2324 rettv->vval.v_list = NULL;
2325 }
2326 if (l == NULL
2327 || (filtermap == FILTERMAP_FILTER
2328 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
2329 return;
2330
2331 prev_lock = l->lv_lock;
2332
2333 if (filtermap == FILTERMAP_MAPNEW)
2334 {
2335 if (rettv_list_alloc(rettv) == FAIL)
2336 return;
2337 l_ret = rettv->vval.v_list;
2338 }
2339 // set_vim_var_nr() doesn't set the type
2340 set_vim_var_type(VV_KEY, VAR_NUMBER);
2341
2342 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
2343 l->lv_lock = VAR_LOCKED;
2344
2345 if (l->lv_first == &range_list_item)
2346 {
2347 varnumber_T val = l->lv_u.nonmat.lv_start;
2348 int len = l->lv_len;
2349 int stride = l->lv_u.nonmat.lv_stride;
2350
2351 // List from range(): loop over the numbers
2352 if (filtermap != FILTERMAP_MAPNEW)
2353 {
2354 l->lv_first = NULL;
2355 l->lv_u.mat.lv_last = NULL;
2356 l->lv_len = 0;
2357 l->lv_u.mat.lv_idx_item = NULL;
2358 }
2359
2360 for (idx = 0; idx < len; ++idx)
2361 {
2362 typval_T tv;
2363 typval_T newtv;
2364
2365 tv.v_type = VAR_NUMBER;
2366 tv.v_lock = 0;
2367 tv.vval.v_number = val;
2368 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaar35c807d2022-01-27 16:36:29 +00002369 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL)
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002370 break;
2371 if (did_emsg)
2372 {
2373 clear_tv(&newtv);
2374 break;
2375 }
2376 if (filtermap != FILTERMAP_FILTER)
2377 {
2378 if (filtermap == FILTERMAP_MAP && argtype != NULL
2379 && check_typval_arg_type(
2380 argtype->tt_member, &newtv,
2381 func_name, 0) == FAIL)
2382 {
2383 clear_tv(&newtv);
2384 break;
2385 }
2386 // map(), mapnew(): always append the new value to the
2387 // list
2388 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2389 ? l : l_ret, &newtv) == FAIL)
2390 break;
2391 }
2392 else if (!rem)
2393 {
2394 // filter(): append the list item value when not rem
2395 if (list_append_tv_move(l, &tv) == FAIL)
2396 break;
2397 }
2398
2399 val += stride;
2400 }
2401 }
2402 else
2403 {
2404 // Materialized list: loop over the items
2405 for (li = l->lv_first; li != NULL; li = nli)
2406 {
2407 typval_T newtv;
2408
2409 if (filtermap == FILTERMAP_MAP && value_check_lock(
2410 li->li_tv.v_lock, arg_errmsg, TRUE))
2411 break;
2412 nli = li->li_next;
2413 set_vim_var_nr(VV_KEY, idx);
2414 if (filter_map_one(&li->li_tv, expr, filtermap,
2415 &newtv, &rem) == FAIL)
2416 break;
2417 if (did_emsg)
2418 {
2419 clear_tv(&newtv);
2420 break;
2421 }
2422 if (filtermap == FILTERMAP_MAP)
2423 {
2424 if (argtype != NULL && check_typval_arg_type(
2425 argtype->tt_member, &newtv, func_name, 0) == FAIL)
2426 {
2427 clear_tv(&newtv);
2428 break;
2429 }
2430 // map(): replace the list item value
2431 clear_tv(&li->li_tv);
2432 newtv.v_lock = 0;
2433 li->li_tv = newtv;
2434 }
2435 else if (filtermap == FILTERMAP_MAPNEW)
2436 {
2437 // mapnew(): append the list item value
2438 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2439 break;
2440 }
2441 else if (filtermap == FILTERMAP_FILTER && rem)
2442 listitem_remove(l, li);
2443 ++idx;
2444 }
2445 }
2446
2447 l->lv_lock = prev_lock;
2448}
2449
2450/*
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002451 * Implementation of map() and filter().
2452 */
2453 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01002454filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002455{
2456 typval_T *expr;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002457 char *func_name = filtermap == FILTERMAP_MAP ? "map()"
Bram Moolenaarea696852020-11-09 18:31:39 +01002458 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002459 : "filter()";
Bram Moolenaarea696852020-11-09 18:31:39 +01002460 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2461 ? N_("map() argument")
2462 : filtermap == FILTERMAP_MAPNEW
2463 ? N_("mapnew() argument")
2464 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002465 int save_did_emsg;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002466 type_T *type = NULL;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002467
Bram Moolenaarea696852020-11-09 18:31:39 +01002468 // map() and filter() return the first argument, also on failure.
Bram Moolenaar2d877592021-12-16 08:21:09 +00002469 if (filtermap != FILTERMAP_MAPNEW && argvars[0].v_type != VAR_STRING)
Bram Moolenaarea696852020-11-09 18:31:39 +01002470 copy_tv(&argvars[0], rettv);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002471
2472 if (in_vim9script()
Bram Moolenaar2d877592021-12-16 08:21:09 +00002473 && (check_for_list_or_dict_or_blob_or_string_arg(argvars, 0)
2474 == FAIL))
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002475 return;
2476
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002477 if (filtermap == FILTERMAP_MAP && in_vim9script())
2478 {
Bram Moolenaar35c807d2022-01-27 16:36:29 +00002479 // Check that map() does not change the declared type of the list or
2480 // dict.
2481 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
2482 type = argvars[0].vval.v_dict->dv_type;
2483 else if (argvars[0].v_type == VAR_LIST
2484 && argvars[0].vval.v_list != NULL)
2485 type = argvars[0].vval.v_list->lv_type;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002486 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002487
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002488 if (argvars[0].v_type != VAR_BLOB
2489 && argvars[0].v_type != VAR_LIST
2490 && argvars[0].v_type != VAR_DICT
2491 && argvars[0].v_type != VAR_STRING)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002492 {
rbtnnc479ce02021-12-15 19:14:54 +00002493 semsg(_(e_argument_of_str_must_be_list_string_dictionary_or_blob),
2494 func_name);
Bram Moolenaar98cd3032022-01-27 17:37:41 +00002495 return;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002496 }
2497
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002498 // On type errors, the preceding call has already displayed an error
2499 // message. Avoid a misleading error message for an empty string that
2500 // was not passed as argument.
Bram Moolenaar35c807d2022-01-27 16:36:29 +00002501 expr = &argvars[1];
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002502 if (expr->v_type != VAR_UNKNOWN)
2503 {
2504 typval_T save_val;
2505 typval_T save_key;
2506
2507 prepare_vimvar(VV_VAL, &save_val);
2508 prepare_vimvar(VV_KEY, &save_key);
2509
2510 // We reset "did_emsg" to be able to detect whether an error
2511 // occurred during evaluation of the expression.
2512 save_did_emsg = did_emsg;
2513 did_emsg = FALSE;
2514
2515 if (argvars[0].v_type == VAR_DICT)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002516 dict_filter_map(argvars[0].vval.v_dict, filtermap, type, func_name,
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002517 arg_errmsg, expr, rettv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002518 else if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002519 blob_filter_map(argvars[0].vval.v_blob, filtermap, expr, rettv);
rbtnnc479ce02021-12-15 19:14:54 +00002520 else if (argvars[0].v_type == VAR_STRING)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002521 string_filter_map(tv_get_string(&argvars[0]), filtermap, expr,
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002522 rettv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002523 else // argvars[0].v_type == VAR_LIST
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002524 list_filter_map(argvars[0].vval.v_list, filtermap, type, func_name,
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002525 arg_errmsg, expr, rettv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002526
2527 restore_vimvar(VV_KEY, &save_key);
2528 restore_vimvar(VV_VAL, &save_val);
2529
2530 did_emsg |= save_did_emsg;
2531 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002532}
2533
2534/*
2535 * "filter()" function
2536 */
2537 void
2538f_filter(typval_T *argvars, typval_T *rettv)
2539{
Bram Moolenaarea696852020-11-09 18:31:39 +01002540 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002541}
2542
2543/*
2544 * "map()" function
2545 */
2546 void
2547f_map(typval_T *argvars, typval_T *rettv)
2548{
Bram Moolenaarea696852020-11-09 18:31:39 +01002549 filter_map(argvars, rettv, FILTERMAP_MAP);
2550}
2551
2552/*
2553 * "mapnew()" function
2554 */
2555 void
2556f_mapnew(typval_T *argvars, typval_T *rettv)
2557{
2558 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002559}
2560
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002561/*
2562 * "add(list, item)" function
2563 */
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002564 static void
2565list_add(typval_T *argvars, typval_T *rettv)
2566{
2567 list_T *l = argvars[0].vval.v_list;
2568
2569 if (l == NULL)
2570 {
2571 if (in_vim9script())
2572 emsg(_(e_cannot_add_to_null_list));
2573 }
2574 else if (!value_check_lock(l->lv_lock,
2575 (char_u *)N_("add() argument"), TRUE)
2576 && list_append_tv(l, &argvars[1]) == OK)
2577 {
2578 copy_tv(&argvars[0], rettv);
2579 }
2580}
2581
2582/*
2583 * "add(object, item)" function
2584 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002585 void
2586f_add(typval_T *argvars, typval_T *rettv)
2587{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002588 rettv->vval.v_number = 1; // Default: Failed
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002589
2590 if (in_vim9script()
2591 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2592 || (argvars[0].v_type == VAR_BLOB
2593 && check_for_number_arg(argvars, 1) == FAIL)))
2594 return;
2595
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002596 if (argvars[0].v_type == VAR_LIST)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002597 list_add(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002598 else if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002599 blob_add(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002600 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002601 emsg(_(e_list_or_blob_required));
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002602}
2603
2604/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002605 * Count the number of times item "needle" occurs in List "l" starting at index
2606 * "idx". Case is ignored if "ic" is TRUE.
2607 */
2608 static long
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002609list_count(list_T *l, typval_T *needle, long idx, int ic)
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002610{
2611 long n = 0;
2612 listitem_T *li;
2613
2614 if (l == NULL)
2615 return 0;
2616
2617 CHECK_LIST_MATERIALIZE(l);
2618
2619 if (list_len(l) == 0)
2620 return 0;
2621
2622 li = list_find(l, idx);
2623 if (li == NULL)
2624 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002625 semsg(_(e_list_index_out_of_range_nr), idx);
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002626 return 0;
2627 }
2628
2629 for ( ; li != NULL; li = li->li_next)
2630 if (tv_equal(&li->li_tv, needle, ic, FALSE))
2631 ++n;
2632
2633 return n;
2634}
2635
2636/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002637 * "count()" function
2638 */
2639 void
2640f_count(typval_T *argvars, typval_T *rettv)
2641{
2642 long n = 0;
2643 int ic = FALSE;
2644 int error = FALSE;
2645
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002646 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002647 && (check_for_string_or_list_or_dict_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002648 || check_for_opt_bool_arg(argvars, 2) == FAIL
2649 || (argvars[2].v_type != VAR_UNKNOWN
2650 && check_for_opt_number_arg(argvars, 3) == FAIL)))
2651 return;
2652
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002653 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002654 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002655
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002656 if (!error && argvars[0].v_type == VAR_STRING)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002657 n = string_count(argvars[0].vval.v_string,
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002658 tv_get_string_chk(&argvars[1]), ic);
2659 else if (!error && argvars[0].v_type == VAR_LIST)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002660 {
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002661 long idx = 0;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002662
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002663 if (argvars[2].v_type != VAR_UNKNOWN
2664 && argvars[3].v_type != VAR_UNKNOWN)
2665 idx = (long)tv_get_number_chk(&argvars[3], &error);
2666 if (!error)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002667 n = list_count(argvars[0].vval.v_list, &argvars[1], idx, ic);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002668 }
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002669 else if (!error && argvars[0].v_type == VAR_DICT)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002670 {
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002671 if (argvars[2].v_type != VAR_UNKNOWN
2672 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002673 emsg(_(e_invalid_argument));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002674 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002675 n = dict_count(argvars[0].vval.v_dict, &argvars[1], ic);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002676 }
2677 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002678 semsg(_(e_argument_of_str_must_be_list_or_dictionary), "count()");
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002679 rettv->vval.v_number = n;
2680}
2681
2682/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002683 * extend() a List. Append List argvars[1] to List argvars[0] before index
2684 * argvars[3] and return the resulting list in "rettv". "is_new" is TRUE for
2685 * extendnew().
2686 */
2687 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002688list_extend_func(
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002689 typval_T *argvars,
2690 type_T *type,
2691 char *func_name,
2692 char_u *arg_errmsg,
2693 int is_new,
2694 typval_T *rettv)
2695{
2696 list_T *l1, *l2;
2697 listitem_T *item;
2698 long before;
2699 int error = FALSE;
2700
2701 l1 = argvars[0].vval.v_list;
2702 if (l1 == NULL)
2703 {
2704 emsg(_(e_cannot_extend_null_list));
2705 return;
2706 }
2707 l2 = argvars[1].vval.v_list;
2708 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar381692b2022-02-02 20:01:27 +00002709 && l2 != NULL)
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002710 {
2711 if (is_new)
2712 {
Bram Moolenaar381692b2022-02-02 20:01:27 +00002713 l1 = list_copy(l1, FALSE, TRUE, get_copyID());
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002714 if (l1 == NULL)
2715 return;
2716 }
2717
2718 if (argvars[2].v_type != VAR_UNKNOWN)
2719 {
2720 before = (long)tv_get_number_chk(&argvars[2], &error);
2721 if (error)
2722 return; // type error; errmsg already given
2723
2724 if (before == l1->lv_len)
2725 item = NULL;
2726 else
2727 {
2728 item = list_find(l1, before);
2729 if (item == NULL)
2730 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002731 semsg(_(e_list_index_out_of_range_nr), before);
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002732 return;
2733 }
2734 }
2735 }
2736 else
2737 item = NULL;
2738 if (type != NULL && check_typval_arg_type(
2739 type, &argvars[1], func_name, 2) == FAIL)
2740 return;
2741 list_extend(l1, l2, item);
2742
2743 if (is_new)
2744 {
2745 rettv->v_type = VAR_LIST;
2746 rettv->vval.v_list = l1;
2747 rettv->v_lock = FALSE;
2748 }
2749 else
2750 copy_tv(&argvars[0], rettv);
2751 }
2752}
2753
2754/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002755 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002756 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002757 static void
2758extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002759{
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002760 type_T *type = NULL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002761 char *func_name = is_new ? "extendnew()" : "extend()";
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002762
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002763 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002764 {
2765 // Check that extend() does not change the type of the list if it was
2766 // declared.
2767 if (!is_new && in_vim9script() && argvars[0].vval.v_list != NULL)
2768 type = argvars[0].vval.v_list->lv_type;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002769 list_extend_func(argvars, type, func_name, arg_errmsg, is_new, rettv);
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002770 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002771 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002772 {
2773 // Check that extend() does not change the type of the list if it was
2774 // declared.
2775 if (!is_new && in_vim9script() && argvars[0].vval.v_dict != NULL)
2776 type = argvars[0].vval.v_dict->dv_type;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002777 dict_extend_func(argvars, type, func_name, arg_errmsg, is_new, rettv);
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002778 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002779 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002780 semsg(_(e_argument_of_str_must_be_list_or_dictionary), func_name);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002781}
2782
2783/*
2784 * "extend(list, list [, idx])" function
2785 * "extend(dict, dict [, action])" function
2786 */
2787 void
2788f_extend(typval_T *argvars, typval_T *rettv)
2789{
2790 char_u *errmsg = (char_u *)N_("extend() argument");
2791
2792 extend(argvars, rettv, errmsg, FALSE);
2793}
2794
2795/*
2796 * "extendnew(list, list [, idx])" function
2797 * "extendnew(dict, dict [, action])" function
2798 */
2799 void
2800f_extendnew(typval_T *argvars, typval_T *rettv)
2801{
2802 char_u *errmsg = (char_u *)N_("extendnew() argument");
2803
2804 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002805}
2806
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002807 static void
2808list_insert_func(typval_T *argvars, typval_T *rettv)
2809{
2810 list_T *l = argvars[0].vval.v_list;
2811 long before = 0;
2812 listitem_T *item;
2813 int error = FALSE;
2814
2815 if (l == NULL)
2816 {
2817 if (in_vim9script())
2818 emsg(_(e_cannot_add_to_null_list));
2819 return;
2820 }
2821
2822 if (value_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
2823 return;
2824
2825 if (argvars[2].v_type != VAR_UNKNOWN)
2826 before = (long)tv_get_number_chk(&argvars[2], &error);
2827 if (error)
2828 return; // type error; errmsg already given
2829
2830 if (before == l->lv_len)
2831 item = NULL;
2832 else
2833 {
2834 item = list_find(l, before);
2835 if (item == NULL)
2836 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002837 semsg(_(e_list_index_out_of_range_nr), before);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002838 l = NULL;
2839 }
2840 }
2841 if (l != NULL)
2842 {
2843 (void)list_insert_tv(l, &argvars[1], item);
2844 copy_tv(&argvars[0], rettv);
2845 }
2846}
2847
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002848/*
2849 * "insert()" function
2850 */
2851 void
2852f_insert(typval_T *argvars, typval_T *rettv)
2853{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002854 if (in_vim9script()
2855 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2856 || (argvars[0].v_type == VAR_BLOB
2857 && check_for_number_arg(argvars, 1) == FAIL)
2858 || check_for_opt_number_arg(argvars, 2) == FAIL))
2859 return;
2860
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002861 if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002862 blob_insert_func(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002863 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002864 semsg(_(e_argument_of_str_must_be_list_or_blob), "insert()");
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002865 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002866 list_insert_func(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002867}
2868
2869/*
2870 * "remove()" function
2871 */
2872 void
2873f_remove(typval_T *argvars, typval_T *rettv)
2874{
2875 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2876
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002877 if (in_vim9script()
2878 && (check_for_list_or_dict_or_blob_arg(argvars, 0) == FAIL
2879 || ((argvars[0].v_type == VAR_LIST
2880 || argvars[0].v_type == VAR_BLOB)
2881 && (check_for_number_arg(argvars, 1) == FAIL
2882 || check_for_opt_number_arg(argvars, 2) == FAIL))
2883 || (argvars[0].v_type == VAR_DICT
2884 && check_for_string_or_number_arg(argvars, 1) == FAIL)))
2885 return;
2886
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002887 if (argvars[0].v_type == VAR_DICT)
2888 dict_remove(argvars, rettv, arg_errmsg);
2889 else if (argvars[0].v_type == VAR_BLOB)
Sean Dewar80d73952021-08-04 19:25:54 +02002890 blob_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002891 else if (argvars[0].v_type == VAR_LIST)
2892 list_remove(argvars, rettv, arg_errmsg);
2893 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002894 semsg(_(e_argument_of_str_must_be_list_dictionary_or_blob), "remove()");
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002895}
2896
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002897 static void
2898list_reverse(list_T *l, typval_T *rettv)
2899{
2900 listitem_T *li, *ni;
2901
2902 rettv_list_set(rettv, l);
2903 if (l != NULL
2904 && !value_check_lock(l->lv_lock,
2905 (char_u *)N_("reverse() argument"), TRUE))
2906 {
2907 if (l->lv_first == &range_list_item)
2908 {
2909 varnumber_T new_start = l->lv_u.nonmat.lv_start
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00002910 + ((varnumber_T)l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002911 l->lv_u.nonmat.lv_end = new_start
2912 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2913 l->lv_u.nonmat.lv_start = new_start;
2914 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
2915 return;
2916 }
2917 li = l->lv_u.mat.lv_last;
2918 l->lv_first = l->lv_u.mat.lv_last = NULL;
2919 l->lv_len = 0;
2920 while (li != NULL)
2921 {
2922 ni = li->li_prev;
2923 list_append(l, li);
2924 li = ni;
2925 }
2926 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
2927 }
2928}
2929
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002930/*
2931 * "reverse({list})" function
2932 */
2933 void
2934f_reverse(typval_T *argvars, typval_T *rettv)
2935{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002936 if (in_vim9script() && check_for_list_or_blob_arg(argvars, 0) == FAIL)
2937 return;
2938
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002939 if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002940 blob_reverse(argvars[0].vval.v_blob, rettv);
2941 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002942 semsg(_(e_argument_of_str_must_be_list_or_blob), "reverse()");
Bram Moolenaaref982572021-08-12 19:27:57 +02002943 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002944 list_reverse(argvars[0].vval.v_list, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002945}
2946
Bram Moolenaar85629982020-06-01 18:39:20 +02002947/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002948 * reduce() List argvars[0] using the function 'funcname' with arguments in
2949 * 'funcexe' starting with the initial value argvars[2] and return the result
2950 * in 'rettv'.
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002951 */
2952 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002953list_reduce(
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002954 typval_T *argvars,
2955 char_u *func_name,
2956 funcexe_T *funcexe,
2957 typval_T *rettv)
2958{
2959 list_T *l = argvars[0].vval.v_list;
2960 listitem_T *li = NULL;
2961 typval_T initial;
2962 typval_T argv[3];
2963 int r;
2964 int called_emsg_start = called_emsg;
2965 int prev_locked;
2966
2967 if (l != NULL)
2968 CHECK_LIST_MATERIALIZE(l);
2969 if (argvars[2].v_type == VAR_UNKNOWN)
2970 {
2971 if (l == NULL || l->lv_first == NULL)
2972 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002973 semsg(_(e_reduce_of_an_empty_str_with_no_initial_value), "List");
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002974 return;
2975 }
2976 initial = l->lv_first->li_tv;
2977 li = l->lv_first->li_next;
2978 }
2979 else
2980 {
2981 initial = argvars[2];
2982 if (l != NULL)
2983 li = l->lv_first;
2984 }
2985 copy_tv(&initial, rettv);
2986
2987 if (l == NULL)
2988 return;
2989
2990 prev_locked = l->lv_lock;
2991
2992 l->lv_lock = VAR_FIXED; // disallow the list changing here
2993 for ( ; li != NULL; li = li->li_next)
2994 {
2995 argv[0] = *rettv;
2996 argv[1] = li->li_tv;
2997 rettv->v_type = VAR_UNKNOWN;
2998 r = call_func(func_name, -1, rettv, 2, argv, funcexe);
2999 clear_tv(&argv[0]);
3000 if (r == FAIL || called_emsg != called_emsg_start)
3001 break;
3002 }
3003 l->lv_lock = prev_locked;
3004}
3005
3006/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003007 * "reduce(list, { accumulator, element -> value } [, initial])" function
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00003008 * "reduce(blob, { accumulator, element -> value } [, initial])"
3009 * "reduce(string, { accumulator, element -> value } [, initial])"
Bram Moolenaar85629982020-06-01 18:39:20 +02003010 */
3011 void
3012f_reduce(typval_T *argvars, typval_T *rettv)
3013{
Bram Moolenaar85629982020-06-01 18:39:20 +02003014 char_u *func_name;
3015 partial_T *partial = NULL;
3016 funcexe_T funcexe;
Bram Moolenaar85629982020-06-01 18:39:20 +02003017
rbtnn0ccb5842021-12-18 18:33:46 +00003018 if (in_vim9script()
3019 && check_for_string_or_list_or_blob_arg(argvars, 0) == FAIL)
Bram Moolenaar85629982020-06-01 18:39:20 +02003020 return;
rbtnn0ccb5842021-12-18 18:33:46 +00003021
3022 if (argvars[0].v_type != VAR_STRING
3023 && argvars[0].v_type != VAR_LIST
3024 && argvars[0].v_type != VAR_BLOB)
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003025 {
rbtnn0ccb5842021-12-18 18:33:46 +00003026 semsg(_(e_string_list_or_blob_required), "reduce()");
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003027 return;
3028 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003029
3030 if (argvars[1].v_type == VAR_FUNC)
3031 func_name = argvars[1].vval.v_string;
3032 else if (argvars[1].v_type == VAR_PARTIAL)
3033 {
3034 partial = argvars[1].vval.v_partial;
3035 func_name = partial_name(partial);
3036 }
3037 else
3038 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01003039 if (func_name == NULL || *func_name == NUL)
3040 {
3041 emsg(_(e_missing_function_argument));
3042 return;
3043 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003044
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003045 CLEAR_FIELD(funcexe);
3046 funcexe.fe_evaluate = TRUE;
3047 funcexe.fe_partial = partial;
Bram Moolenaar85629982020-06-01 18:39:20 +02003048
3049 if (argvars[0].v_type == VAR_LIST)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003050 list_reduce(argvars, func_name, &funcexe, rettv);
rbtnn0ccb5842021-12-18 18:33:46 +00003051 else if (argvars[0].v_type == VAR_STRING)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003052 string_reduce(argvars, func_name, &funcexe, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003053 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003054 blob_reduce(argvars, func_name, &funcexe, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003055}
3056
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02003057#endif // defined(FEAT_EVAL)