blob: 8658701b45029122c3812d2e402e39232d5e8345 [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 Moolenaar0ff6aad2020-01-29 21:27:21 +0100583 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200584 item->li_prev = NULL;
585 }
586 else
587 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100588 l->lv_u.mat.lv_last->li_next = item;
589 item->li_prev = l->lv_u.mat.lv_last;
590 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200591 }
592 ++l->lv_len;
593 item->li_next = NULL;
594}
595
596/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200598 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200599 */
600 int
601list_append_tv(list_T *l, typval_T *tv)
602{
Bram Moolenaar90fba562021-07-09 19:17:55 +0200603 listitem_T *li;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200604
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200605 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200606 && check_typval_arg_type(l->lv_type->tt_member, tv,
607 NULL, 0) == FAIL)
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200608 return FAIL;
Bram Moolenaar90fba562021-07-09 19:17:55 +0200609 li = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200610 if (li == NULL)
611 return FAIL;
612 copy_tv(tv, &li->li_tv);
613 list_append(l, li);
614 return OK;
615}
616
617/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 * As list_append_tv() but move the value instead of copying it.
619 * Return FAIL when out of memory.
620 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200621 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622list_append_tv_move(list_T *l, typval_T *tv)
623{
624 listitem_T *li = listitem_alloc();
625
626 if (li == NULL)
627 return FAIL;
628 li->li_tv = *tv;
629 list_append(l, li);
630 return OK;
631}
632
633/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200634 * Add a dictionary to a list. Used by getqflist().
635 * Return FAIL when out of memory.
636 */
637 int
638list_append_dict(list_T *list, dict_T *dict)
639{
640 listitem_T *li = listitem_alloc();
641
642 if (li == NULL)
643 return FAIL;
644 li->li_tv.v_type = VAR_DICT;
645 li->li_tv.v_lock = 0;
646 li->li_tv.vval.v_dict = dict;
647 list_append(list, li);
648 ++dict->dv_refcount;
649 return OK;
650}
651
652/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100653 * Append list2 to list1.
654 * Return FAIL when out of memory.
655 */
656 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200657list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100658{
659 listitem_T *li = listitem_alloc();
660
661 if (li == NULL)
662 return FAIL;
663 li->li_tv.v_type = VAR_LIST;
664 li->li_tv.v_lock = 0;
665 li->li_tv.vval.v_list = list2;
666 list_append(list1, li);
667 ++list2->lv_refcount;
668 return OK;
669}
670
671/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200672 * Make a copy of "str" and append it as an item to list "l".
673 * When "len" >= 0 use "str[len]".
674 * Returns FAIL when out of memory.
675 */
676 int
677list_append_string(list_T *l, char_u *str, int len)
678{
679 listitem_T *li = listitem_alloc();
680
681 if (li == NULL)
682 return FAIL;
683 list_append(l, li);
684 li->li_tv.v_type = VAR_STRING;
685 li->li_tv.v_lock = 0;
686 if (str == NULL)
687 li->li_tv.vval.v_string = NULL;
688 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
689 : vim_strsave(str))) == NULL)
690 return FAIL;
691 return OK;
692}
693
694/*
695 * Append "n" to list "l".
696 * Returns FAIL when out of memory.
697 */
698 int
699list_append_number(list_T *l, varnumber_T n)
700{
701 listitem_T *li;
702
703 li = listitem_alloc();
704 if (li == NULL)
705 return FAIL;
706 li->li_tv.v_type = VAR_NUMBER;
707 li->li_tv.v_lock = 0;
708 li->li_tv.vval.v_number = n;
709 list_append(l, li);
710 return OK;
711}
712
713/*
714 * Insert typval_T "tv" in list "l" before "item".
715 * If "item" is NULL append at the end.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100716 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200717 */
718 int
719list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
720{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100721 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200722
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100723 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200724 && check_typval_arg_type(l->lv_type->tt_member, tv,
725 NULL, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100726 return FAIL;
727 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200728 if (ni == NULL)
729 return FAIL;
730 copy_tv(tv, &ni->li_tv);
731 list_insert(l, ni, item);
732 return OK;
733}
734
735 void
736list_insert(list_T *l, listitem_T *ni, listitem_T *item)
737{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200738 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200739 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100740 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200741 list_append(l, ni);
742 else
743 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100744 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200745 ni->li_prev = item->li_prev;
746 ni->li_next = item;
747 if (item->li_prev == NULL)
748 {
749 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100750 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200751 }
752 else
753 {
754 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100755 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200756 }
757 item->li_prev = ni;
758 ++l->lv_len;
759 }
760}
761
762/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200763 * Get the list item in "l" with index "n1". "n1" is adjusted if needed.
764 * In Vim9, it is at the end of the list, add an item.
765 * Return NULL if there is no such item.
766 */
767 listitem_T *
768check_range_index_one(list_T *l, long *n1, int quiet)
769{
770 listitem_T *li = list_find_index(l, n1);
771
772 if (li == NULL)
773 {
774 // Vim9: Allow for adding an item at the end.
775 if (in_vim9script() && *n1 == l->lv_len && l->lv_lock == 0)
776 {
777 list_append_number(l, 0);
778 li = list_find_index(l, n1);
779 }
780 if (li == NULL)
781 {
782 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000783 semsg(_(e_list_index_out_of_range_nr), *n1);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200784 return NULL;
785 }
786 }
787 return li;
788}
789
790/*
791 * Check that "n2" can be used as the second index in a range of list "l".
792 * If "n1" or "n2" is negative it is changed to the positive index.
793 * "li1" is the item for item "n1".
794 * Return OK or FAIL.
795 */
796 int
797check_range_index_two(
798 list_T *l,
799 long *n1,
800 listitem_T *li1,
801 long *n2,
802 int quiet)
803{
804 if (*n2 < 0)
805 {
806 listitem_T *ni = list_find(l, *n2);
807
808 if (ni == NULL)
809 {
810 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000811 semsg(_(e_list_index_out_of_range_nr), *n2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200812 return FAIL;
813 }
814 *n2 = list_idx_of_item(l, ni);
815 }
816
817 // Check that n2 isn't before n1.
818 if (*n1 < 0)
819 *n1 = list_idx_of_item(l, li1);
820 if (*n2 < *n1)
821 {
822 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000823 semsg(_(e_list_index_out_of_range_nr), *n2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200824 return FAIL;
825 }
826 return OK;
827}
828
829/*
830 * Assign values from list "src" into a range of "dest".
831 * "idx1_arg" is the index of the first item in "dest" to be replaced.
832 * "idx2" is the index of last item to be replaced, but when "empty_idx2" is
833 * TRUE then replace all items after "idx1".
834 * "op" is the operator, normally "=" but can be "+=" and the like.
835 * "varname" is used for error messages.
836 * Returns OK or FAIL.
837 */
838 int
839list_assign_range(
840 list_T *dest,
841 list_T *src,
842 long idx1_arg,
843 long idx2,
844 int empty_idx2,
845 char_u *op,
846 char_u *varname)
847{
848 listitem_T *src_li;
849 listitem_T *dest_li;
850 long idx1 = idx1_arg;
851 listitem_T *first_li = list_find_index(dest, &idx1);
852 long idx;
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200853 type_T *member_type = NULL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200854
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000855 // Check whether any of the list items is locked before making any changes.
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200856 idx = idx1;
857 dest_li = first_li;
858 for (src_li = src->lv_first; src_li != NULL && dest_li != NULL; )
859 {
860 if (value_check_lock(dest_li->li_tv.v_lock, varname, FALSE))
861 return FAIL;
862 src_li = src_li->li_next;
863 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
864 break;
865 dest_li = dest_li->li_next;
866 ++idx;
867 }
868
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200869 if (in_vim9script() && dest->lv_type != NULL
870 && dest->lv_type->tt_member != NULL)
871 member_type = dest->lv_type->tt_member;
872
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000873 // Assign the List values to the list items.
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200874 idx = idx1;
875 dest_li = first_li;
876 for (src_li = src->lv_first; src_li != NULL; )
877 {
878 if (op != NULL && *op != '=')
879 tv_op(&dest_li->li_tv, &src_li->li_tv, op);
880 else
881 {
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200882 if (member_type != NULL
883 && check_typval_arg_type(member_type, &src_li->li_tv,
884 NULL, 0) == FAIL)
885 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200886 clear_tv(&dest_li->li_tv);
887 copy_tv(&src_li->li_tv, &dest_li->li_tv);
888 }
889 src_li = src_li->li_next;
890 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
891 break;
892 if (dest_li->li_next == NULL)
893 {
894 // Need to add an empty item.
895 if (list_append_number(dest, 0) == FAIL)
896 {
897 src_li = NULL;
898 break;
899 }
900 }
901 dest_li = dest_li->li_next;
902 ++idx;
903 }
904 if (src_li != NULL)
905 {
906 emsg(_(e_list_value_has_more_items_than_targets));
907 return FAIL;
908 }
909 if (empty_idx2
910 ? (dest_li != NULL && dest_li->li_next != NULL)
911 : idx != idx2)
912 {
913 emsg(_(e_list_value_does_not_have_enough_items));
914 return FAIL;
915 }
916 return OK;
917}
918
919/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200920 * Flatten "list" to depth "maxdepth".
921 * It does nothing if "maxdepth" is 0.
922 * Returns FAIL when out of memory.
923 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100924 static void
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200925list_flatten(list_T *list, long maxdepth)
926{
927 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200928 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200929 int n;
930
931 if (maxdepth == 0)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100932 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200933 CHECK_LIST_MATERIALIZE(list);
934
935 n = 0;
936 item = list->lv_first;
937 while (item != NULL)
938 {
939 fast_breakcheck();
940 if (got_int)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100941 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200942
943 if (item->li_tv.v_type == VAR_LIST)
944 {
945 listitem_T *next = item->li_next;
946
947 vimlist_remove(list, item, item);
948 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +0200949 {
950 list_free_item(list, item);
Bram Moolenaar3b690062021-02-01 20:14:51 +0100951 return;
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +0200952 }
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200953 clear_tv(&item->li_tv);
954 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200955
956 if (item->li_prev == NULL)
957 item = list->lv_first;
958 else
959 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200960 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200961
962 if (++n >= maxdepth)
963 {
964 n = 0;
965 item = next;
966 }
967 }
968 else
969 {
970 n = 0;
971 item = item->li_next;
972 }
973 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200974}
975
976/*
Bram Moolenaar3b690062021-02-01 20:14:51 +0100977 * "flatten()" and "flattennew()" functions
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200978 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100979 static void
980flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200981{
982 list_T *l;
983 long maxdepth;
984 int error = FALSE;
985
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200986 if (in_vim9script()
987 && (check_for_list_arg(argvars, 0) == FAIL
988 || check_for_opt_number_arg(argvars, 1) == FAIL))
989 return;
990
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200991 if (argvars[0].v_type != VAR_LIST)
992 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +0000993 semsg(_(e_argument_of_str_must_be_list), "flatten()");
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200994 return;
995 }
996
997 if (argvars[1].v_type == VAR_UNKNOWN)
998 maxdepth = 999999;
999 else
1000 {
1001 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
1002 if (error)
1003 return;
1004 if (maxdepth < 0)
1005 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001006 emsg(_(e_maxdepth_must_be_non_negative_number));
Bram Moolenaar077a1e62020-06-08 20:50:43 +02001007 return;
1008 }
1009 }
1010
1011 l = argvars[0].vval.v_list;
Bram Moolenaar3b690062021-02-01 20:14:51 +01001012 rettv->v_type = VAR_LIST;
1013 rettv->vval.v_list = l;
1014 if (l == NULL)
1015 return;
1016
1017 if (make_copy)
1018 {
1019 l = list_copy(l, TRUE, get_copyID());
1020 rettv->vval.v_list = l;
1021 if (l == NULL)
1022 return;
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +02001023 // The type will change.
1024 free_type(l->lv_type);
1025 l->lv_type = NULL;
Bram Moolenaar3b690062021-02-01 20:14:51 +01001026 }
1027 else
1028 {
1029 if (value_check_lock(l->lv_lock,
1030 (char_u *)N_("flatten() argument"), TRUE))
1031 return;
1032 ++l->lv_refcount;
1033 }
1034
1035 list_flatten(l, maxdepth);
1036}
1037
1038/*
1039 * "flatten(list[, {maxdepth}])" function
1040 */
1041 void
1042f_flatten(typval_T *argvars, typval_T *rettv)
1043{
1044 if (in_vim9script())
1045 emsg(_(e_cannot_use_flatten_in_vim9_script));
1046 else
1047 flatten_common(argvars, rettv, FALSE);
1048}
1049
1050/*
1051 * "flattennew(list[, {maxdepth}])" function
1052 */
1053 void
1054f_flattennew(typval_T *argvars, typval_T *rettv)
1055{
1056 flatten_common(argvars, rettv, TRUE);
Bram Moolenaar077a1e62020-06-08 20:50:43 +02001057}
1058
1059/*
Bram Moolenaar1a739232020-10-10 15:37:58 +02001060 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001061 * If "bef" is NULL append at the end, otherwise insert before this item.
1062 * Returns FAIL when out of memory.
1063 */
1064 int
1065list_extend(list_T *l1, list_T *l2, listitem_T *bef)
1066{
1067 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001068 int todo;
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001069 listitem_T *bef_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001070
Bram Moolenaar1a739232020-10-10 15:37:58 +02001071 // NULL list is equivalent to an empty list: nothing to do.
1072 if (l2 == NULL || l2->lv_len == 0)
1073 return OK;
1074
1075 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001076 CHECK_LIST_MATERIALIZE(l1);
1077 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001079 // When exending a list with itself, at some point we run into the item
1080 // that was before "bef" and need to skip over the already inserted items
1081 // to "bef".
1082 bef_prev = bef == NULL ? NULL : bef->li_prev;
1083
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001084 // We also quit the loop when we have inserted the original item count of
1085 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001086 for (item = l2->lv_first; item != NULL && --todo >= 0;
1087 item = item == bef_prev ? bef : item->li_next)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001088 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
1089 return FAIL;
1090 return OK;
1091}
1092
1093/*
1094 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
1095 * Return FAIL when out of memory.
1096 */
1097 int
1098list_concat(list_T *l1, list_T *l2, typval_T *tv)
1099{
1100 list_T *l;
1101
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001102 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +02001103 if (l1 == NULL)
1104 l = list_alloc();
1105 else
1106 l = list_copy(l1, FALSE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001107 if (l == NULL)
1108 return FAIL;
1109 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +01001110 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001111 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001112 if (l1 == NULL)
1113 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001114
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001115 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +02001116 return list_extend(l, l2, NULL);
1117}
1118
Bram Moolenaar9af78762020-06-16 11:34:42 +02001119 list_T *
1120list_slice(list_T *ol, long n1, long n2)
1121{
1122 listitem_T *item;
1123 list_T *l = list_alloc();
1124
1125 if (l == NULL)
1126 return NULL;
1127 for (item = list_find(ol, n1); n1 <= n2; ++n1)
1128 {
1129 if (list_append_tv(l, &item->li_tv) == FAIL)
1130 {
1131 list_free(l);
1132 return NULL;
1133 }
1134 item = item->li_next;
1135 }
1136 return l;
1137}
1138
Bram Moolenaared591872020-08-15 22:14:53 +02001139 int
1140list_slice_or_index(
1141 list_T *list,
1142 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01001143 varnumber_T n1_arg,
1144 varnumber_T n2_arg,
1145 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +02001146 typval_T *rettv,
1147 int verbose)
1148{
1149 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001150 varnumber_T n1 = n1_arg;
1151 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +02001152 typval_T var1;
1153
1154 if (n1 < 0)
1155 n1 = len + n1;
1156 if (n1 < 0 || n1 >= len)
1157 {
Bram Moolenaar92f05f22021-08-12 21:12:56 +02001158 // For a range we allow invalid values and for legacy script return an
1159 // empty list, for Vim9 script start at the first item.
1160 // A list index out of range is an error.
Bram Moolenaared591872020-08-15 22:14:53 +02001161 if (!range)
1162 {
1163 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001164 semsg(_(e_list_index_out_of_range_nr), (long)n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +02001165 return FAIL;
1166 }
Bram Moolenaar92f05f22021-08-12 21:12:56 +02001167 if (in_vim9script())
1168 n1 = n1 < 0 ? 0 : len;
1169 else
1170 n1 = len;
Bram Moolenaared591872020-08-15 22:14:53 +02001171 }
1172 if (range)
1173 {
1174 list_T *l;
1175
1176 if (n2 < 0)
1177 n2 = len + n2;
1178 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01001179 n2 = len - (exclusive ? 0 : 1);
1180 if (exclusive)
1181 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +02001182 if (n2 < 0 || n2 + 1 < n1)
1183 n2 = -1;
1184 l = list_slice(list, n1, n2);
1185 if (l == NULL)
1186 return FAIL;
1187 clear_tv(rettv);
1188 rettv_list_set(rettv, l);
1189 }
1190 else
1191 {
1192 // copy the item to "var1" to avoid that freeing the list makes it
1193 // invalid.
1194 copy_tv(&list_find(list, n1)->li_tv, &var1);
1195 clear_tv(rettv);
1196 *rettv = var1;
1197 }
1198 return OK;
1199}
1200
Bram Moolenaarda861d62016-07-17 15:46:27 +02001201/*
1202 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1203 * The refcount of the new list is set to 1.
1204 * See item_copy() for "copyID".
1205 * Returns NULL when out of memory.
1206 */
1207 list_T *
1208list_copy(list_T *orig, int deep, int copyID)
1209{
1210 list_T *copy;
1211 listitem_T *item;
1212 listitem_T *ni;
1213
1214 if (orig == NULL)
1215 return NULL;
1216
1217 copy = list_alloc();
1218 if (copy != NULL)
1219 {
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +02001220 copy->lv_type = alloc_type(orig->lv_type);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001221 if (copyID != 0)
1222 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001223 // Do this before adding the items, because one of the items may
1224 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001225 orig->lv_copyID = copyID;
1226 orig->lv_copylist = copy;
1227 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001228 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001229 for (item = orig->lv_first; item != NULL && !got_int;
1230 item = item->li_next)
1231 {
1232 ni = listitem_alloc();
1233 if (ni == NULL)
1234 break;
1235 if (deep)
1236 {
1237 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
1238 {
1239 vim_free(ni);
1240 break;
1241 }
1242 }
1243 else
1244 copy_tv(&item->li_tv, &ni->li_tv);
1245 list_append(copy, ni);
1246 }
1247 ++copy->lv_refcount;
1248 if (item != NULL)
1249 {
1250 list_unref(copy);
1251 copy = NULL;
1252 }
1253 }
1254
1255 return copy;
1256}
1257
1258/*
1259 * Remove items "item" to "item2" from list "l".
1260 * Does not free the listitem or the value!
1261 * This used to be called list_remove, but that conflicts with a Sun header
1262 * file.
1263 */
1264 void
1265vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1266{
1267 listitem_T *ip;
1268
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001269 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001271 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001272 for (ip = item; ip != NULL; ip = ip->li_next)
1273 {
1274 --l->lv_len;
1275 list_fix_watch(l, ip);
1276 if (ip == item2)
1277 break;
1278 }
1279
1280 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001281 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001282 else
1283 item2->li_next->li_prev = item->li_prev;
1284 if (item->li_prev == NULL)
1285 l->lv_first = item2->li_next;
1286 else
1287 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001288 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001289}
1290
1291/*
1292 * Return an allocated string with the string representation of a list.
1293 * May return NULL.
1294 */
1295 char_u *
1296list2string(typval_T *tv, int copyID, int restore_copyID)
1297{
1298 garray_T ga;
1299
1300 if (tv->vval.v_list == NULL)
1301 return NULL;
1302 ga_init2(&ga, (int)sizeof(char), 80);
1303 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001304 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001305 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1306 FALSE, restore_copyID, copyID) == FAIL)
1307 {
1308 vim_free(ga.ga_data);
1309 return NULL;
1310 }
1311 ga_append(&ga, ']');
1312 ga_append(&ga, NUL);
1313 return (char_u *)ga.ga_data;
1314}
1315
1316typedef struct join_S {
1317 char_u *s;
1318 char_u *tofree;
1319} join_T;
1320
1321 static int
1322list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001323 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001324 list_T *l,
1325 char_u *sep,
1326 int echo_style,
1327 int restore_copyID,
1328 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001329 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001330{
1331 int i;
1332 join_T *p;
1333 int len;
1334 int sumlen = 0;
1335 int first = TRUE;
1336 char_u *tofree;
1337 char_u numbuf[NUMBUFLEN];
1338 listitem_T *item;
1339 char_u *s;
1340
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001341 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001342 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001343 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1344 {
1345 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001346 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001347 if (s == NULL)
1348 return FAIL;
1349
1350 len = (int)STRLEN(s);
1351 sumlen += len;
1352
1353 (void)ga_grow(join_gap, 1);
1354 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1355 if (tofree != NULL || s != numbuf)
1356 {
1357 p->s = s;
1358 p->tofree = tofree;
1359 }
1360 else
1361 {
1362 p->s = vim_strnsave(s, len);
1363 p->tofree = p->s;
1364 }
1365
1366 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001367 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001368 break;
1369 }
1370
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001371 // Allocate result buffer with its total size, avoid re-allocation and
1372 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001373 if (join_gap->ga_len >= 2)
1374 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1375 if (ga_grow(gap, sumlen + 2) == FAIL)
1376 return FAIL;
1377
1378 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1379 {
1380 if (first)
1381 first = FALSE;
1382 else
1383 ga_concat(gap, sep);
1384 p = ((join_T *)join_gap->ga_data) + i;
1385
1386 if (p->s != NULL)
1387 ga_concat(gap, p->s);
1388 line_breakcheck();
1389 }
1390
1391 return OK;
1392}
1393
1394/*
1395 * Join list "l" into a string in "*gap", using separator "sep".
1396 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1397 * Return FAIL or OK.
1398 */
1399 int
1400list_join(
1401 garray_T *gap,
1402 list_T *l,
1403 char_u *sep,
1404 int echo_style,
1405 int restore_copyID,
1406 int copyID)
1407{
1408 garray_T join_ga;
1409 int retval;
1410 join_T *p;
1411 int i;
1412
1413 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001414 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001415 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1416 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1417 copyID, &join_ga);
1418
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001419 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001420 if (join_ga.ga_data != NULL)
1421 {
1422 p = (join_T *)join_ga.ga_data;
1423 for (i = 0; i < join_ga.ga_len; ++i)
1424 {
1425 vim_free(p->tofree);
1426 ++p;
1427 }
1428 ga_clear(&join_ga);
1429 }
1430
1431 return retval;
1432}
1433
1434/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001435 * "join()" function
1436 */
1437 void
1438f_join(typval_T *argvars, typval_T *rettv)
1439{
1440 garray_T ga;
1441 char_u *sep;
1442
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001443 if (in_vim9script()
1444 && (check_for_list_arg(argvars, 0) == FAIL
1445 || check_for_opt_string_arg(argvars, 1) == FAIL))
1446 return;
1447
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001448 if (argvars[0].v_type != VAR_LIST)
1449 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001450 emsg(_(e_list_required));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001451 return;
1452 }
Bram Moolenaaref982572021-08-12 19:27:57 +02001453 rettv->v_type = VAR_STRING;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001454 if (argvars[0].vval.v_list == NULL)
1455 return;
Bram Moolenaaref982572021-08-12 19:27:57 +02001456
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001457 if (argvars[1].v_type == VAR_UNKNOWN)
1458 sep = (char_u *)" ";
1459 else
1460 sep = tv_get_string_chk(&argvars[1]);
1461
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001462 if (sep != NULL)
1463 {
1464 ga_init2(&ga, (int)sizeof(char), 80);
1465 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1466 ga_append(&ga, NUL);
1467 rettv->vval.v_string = (char_u *)ga.ga_data;
1468 }
1469 else
1470 rettv->vval.v_string = NULL;
1471}
1472
1473/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001474 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001475 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001476 * Return OK or FAIL.
1477 */
1478 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001479eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001480{
Bram Moolenaar71478202020-06-26 22:46:27 +02001481 int evaluate = evalarg == NULL ? FALSE
1482 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001483 list_T *l = NULL;
1484 typval_T tv;
1485 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001486 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001487 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001488
1489 if (evaluate)
1490 {
1491 l = list_alloc();
1492 if (l == NULL)
1493 return FAIL;
1494 }
1495
Bram Moolenaar962d7212020-07-04 14:15:00 +02001496 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001497 while (**arg != ']' && **arg != NUL)
1498 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001499 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001500 goto failret;
1501 if (evaluate)
1502 {
1503 item = listitem_alloc();
1504 if (item != NULL)
1505 {
1506 item->li_tv = tv;
1507 item->li_tv.v_lock = 0;
1508 list_append(l, item);
1509 }
1510 else
1511 clear_tv(&tv);
1512 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001513 // Legacy Vim script allowed a space before the comma.
1514 if (!vim9script)
1515 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001516
Bram Moolenaare6e03172020-06-27 16:36:05 +02001517 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001518 had_comma = **arg == ',';
1519 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001520 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001521 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001522 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001523 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001524 goto failret;
1525 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001526 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001527 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001528
Bram Moolenaare6b53242020-07-01 17:28:33 +02001529 // The "]" can be on the next line. But a double quoted string may
1530 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001531 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001532 if (**arg == ']')
1533 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001534
1535 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001536 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001538 {
1539 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001540 semsg(_(e_no_white_space_allowed_before_str_str),
1541 ",", *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001542 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001543 semsg(_(e_missing_comma_in_list_str), *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001544 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001545 goto failret;
1546 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001547 }
1548
1549 if (**arg != ']')
1550 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 if (do_error)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001552 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001553failret:
1554 if (evaluate)
1555 list_free(l);
1556 return FAIL;
1557 }
1558
Bram Moolenaar9d489562020-07-30 20:08:50 +02001559 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001560 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001561 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001562
1563 return OK;
1564}
1565
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001566/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001567 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001568 */
1569 int
1570write_list(FILE *fd, list_T *list, int binary)
1571{
1572 listitem_T *li;
1573 int c;
1574 int ret = OK;
1575 char_u *s;
1576
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001577 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001578 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001579 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001580 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581 {
1582 if (*s == '\n')
1583 c = putc(NUL, fd);
1584 else
1585 c = putc(*s, fd);
1586 if (c == EOF)
1587 {
1588 ret = FAIL;
1589 break;
1590 }
1591 }
1592 if (!binary || li->li_next != NULL)
1593 if (putc('\n', fd) == EOF)
1594 {
1595 ret = FAIL;
1596 break;
1597 }
1598 if (ret == FAIL)
1599 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001600 emsg(_(e_error_while_writing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001601 break;
1602 }
1603 }
1604 return ret;
1605}
1606
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001607/*
1608 * Initialize a static list with 10 items.
1609 */
1610 void
1611init_static_list(staticList10_T *sl)
1612{
1613 list_T *l = &sl->sl_list;
1614 int i;
1615
1616 memset(sl, 0, sizeof(staticList10_T));
1617 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001618 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001619 l->lv_refcount = DO_NOT_FREE_CNT;
1620 l->lv_lock = VAR_FIXED;
1621 sl->sl_list.lv_len = 10;
1622
1623 for (i = 0; i < 10; ++i)
1624 {
1625 listitem_T *li = &sl->sl_items[i];
1626
1627 if (i == 0)
1628 li->li_prev = NULL;
1629 else
1630 li->li_prev = li - 1;
1631 if (i == 9)
1632 li->li_next = NULL;
1633 else
1634 li->li_next = li + 1;
1635 }
1636}
1637
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001638/*
1639 * "list2str()" function
1640 */
1641 void
1642f_list2str(typval_T *argvars, typval_T *rettv)
1643{
1644 list_T *l;
1645 listitem_T *li;
1646 garray_T ga;
1647 int utf8 = FALSE;
1648
1649 rettv->v_type = VAR_STRING;
1650 rettv->vval.v_string = NULL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001651
1652 if (in_vim9script()
1653 && (check_for_list_arg(argvars, 0) == FAIL
1654 || check_for_opt_bool_arg(argvars, 1) == FAIL))
1655 return;
1656
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001657 if (argvars[0].v_type != VAR_LIST)
1658 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001659 emsg(_(e_invalid_argument));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001660 return;
1661 }
1662
1663 l = argvars[0].vval.v_list;
1664 if (l == NULL)
1665 return; // empty list results in empty string
1666
1667 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001668 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001669
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001670 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001671 ga_init2(&ga, 1, 80);
1672 if (has_mbyte || utf8)
1673 {
1674 char_u buf[MB_MAXBYTES + 1];
1675 int (*char2bytes)(int, char_u *);
1676
1677 if (utf8 || enc_utf8)
1678 char2bytes = utf_char2bytes;
1679 else
1680 char2bytes = mb_char2bytes;
1681
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001682 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001683 {
1684 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1685 ga_concat(&ga, buf);
1686 }
1687 ga_append(&ga, NUL);
1688 }
1689 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1690 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001691 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001692 ga_append(&ga, tv_get_number(&li->li_tv));
1693 ga_append(&ga, NUL);
1694 }
1695
1696 rettv->v_type = VAR_STRING;
1697 rettv->vval.v_string = ga.ga_data;
1698}
1699
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001700/*
1701 * Remove item argvars[1] from List argvars[0]. If argvars[2] is supplied, then
1702 * remove the range of items from argvars[1] to argvars[2] (inclusive).
1703 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001704 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001705list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1706{
1707 list_T *l;
1708 listitem_T *item, *item2;
1709 listitem_T *li;
1710 int error = FALSE;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001711 long idx;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001712 long end;
1713 int cnt = 0;
1714 list_T *rl;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001715
1716 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001717 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001718 return;
1719
1720 idx = (long)tv_get_number_chk(&argvars[1], &error);
1721 if (error)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001722 return; // type error: do nothing, errmsg already given
1723
1724 if ((item = list_find(l, idx)) == NULL)
1725 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001726 semsg(_(e_list_index_out_of_range_nr), idx);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001727 return;
1728 }
1729
1730 if (argvars[2].v_type == VAR_UNKNOWN)
1731 {
1732 // Remove one item, return its value.
1733 vimlist_remove(l, item, item);
1734 *rettv = item->li_tv;
1735 list_free_item(l, item);
1736 return;
1737 }
1738
1739 // Remove range of items, return list with values.
1740 end = (long)tv_get_number_chk(&argvars[2], &error);
1741 if (error)
1742 return; // type error: do nothing
1743
1744 if ((item2 = list_find(l, end)) == NULL)
1745 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001746 semsg(_(e_list_index_out_of_range_nr), end);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001747 return;
1748 }
1749
1750 for (li = item; li != NULL; li = li->li_next)
1751 {
1752 ++cnt;
1753 if (li == item2)
1754 break;
1755 }
1756 if (li == NULL) // didn't find "item2" after "item"
1757 {
1758 emsg(_(e_invalid_range));
1759 return;
1760 }
1761
1762 vimlist_remove(l, item, item2);
1763 if (rettv_list_alloc(rettv) != OK)
1764 return;
1765
1766 rl = rettv->vval.v_list;
1767
1768 if (l->lv_with_items > 0)
1769 {
1770 // need to copy the list items and move the value
1771 while (item != NULL)
1772 {
1773 li = listitem_alloc();
1774 if (li == NULL)
1775 return;
1776 li->li_tv = item->li_tv;
1777 init_tv(&item->li_tv);
1778 list_append(rl, li);
1779 if (item == item2)
1780 break;
1781 item = item->li_next;
1782 }
1783 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001784 else
1785 {
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001786 rl->lv_first = item;
1787 rl->lv_u.mat.lv_last = item2;
1788 item->li_prev = NULL;
1789 item2->li_next = NULL;
1790 rl->lv_len = cnt;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001791 }
1792}
1793
1794static int item_compare(const void *s1, const void *s2);
1795static int item_compare2(const void *s1, const void *s2);
1796
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001797// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001798typedef struct
1799{
1800 listitem_T *item;
1801 int idx;
1802} sortItem_T;
1803
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001804// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001805typedef struct
1806{
1807 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001808 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001809 int item_compare_numeric;
1810 int item_compare_numbers;
1811#ifdef FEAT_FLOAT
1812 int item_compare_float;
1813#endif
1814 char_u *item_compare_func;
1815 partial_T *item_compare_partial;
1816 dict_T *item_compare_selfdict;
1817 int item_compare_func_err;
1818 int item_compare_keep_zero;
1819} sortinfo_T;
1820static sortinfo_T *sortinfo = NULL;
1821#define ITEM_COMPARE_FAIL 999
1822
1823/*
1824 * Compare functions for f_sort() and f_uniq() below.
1825 */
1826 static int
1827item_compare(const void *s1, const void *s2)
1828{
1829 sortItem_T *si1, *si2;
1830 typval_T *tv1, *tv2;
1831 char_u *p1, *p2;
1832 char_u *tofree1 = NULL, *tofree2 = NULL;
1833 int res;
1834 char_u numbuf1[NUMBUFLEN];
1835 char_u numbuf2[NUMBUFLEN];
1836
1837 si1 = (sortItem_T *)s1;
1838 si2 = (sortItem_T *)s2;
1839 tv1 = &si1->item->li_tv;
1840 tv2 = &si2->item->li_tv;
1841
1842 if (sortinfo->item_compare_numbers)
1843 {
1844 varnumber_T v1 = tv_get_number(tv1);
1845 varnumber_T v2 = tv_get_number(tv2);
1846
1847 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1848 }
1849
1850#ifdef FEAT_FLOAT
1851 if (sortinfo->item_compare_float)
1852 {
1853 float_T v1 = tv_get_float(tv1);
1854 float_T v2 = tv_get_float(tv2);
1855
1856 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1857 }
1858#endif
1859
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001860 // tv2string() puts quotes around a string and allocates memory. Don't do
1861 // that for string variables. Use a single quote when comparing with a
1862 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001863 if (tv1->v_type == VAR_STRING)
1864 {
1865 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1866 p1 = (char_u *)"'";
1867 else
1868 p1 = tv1->vval.v_string;
1869 }
1870 else
1871 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1872 if (tv2->v_type == VAR_STRING)
1873 {
1874 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1875 p2 = (char_u *)"'";
1876 else
1877 p2 = tv2->vval.v_string;
1878 }
1879 else
1880 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1881 if (p1 == NULL)
1882 p1 = (char_u *)"";
1883 if (p2 == NULL)
1884 p2 = (char_u *)"";
1885 if (!sortinfo->item_compare_numeric)
1886 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001887 if (sortinfo->item_compare_lc)
1888 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001889 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001890 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001891 }
1892 else
1893 {
1894 double n1, n2;
1895 n1 = strtod((char *)p1, (char **)&p1);
1896 n2 = strtod((char *)p2, (char **)&p2);
1897 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1898 }
1899
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001900 // When the result would be zero, compare the item indexes. Makes the
1901 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001902 if (res == 0 && !sortinfo->item_compare_keep_zero)
1903 res = si1->idx > si2->idx ? 1 : -1;
1904
1905 vim_free(tofree1);
1906 vim_free(tofree2);
1907 return res;
1908}
1909
1910 static int
1911item_compare2(const void *s1, const void *s2)
1912{
1913 sortItem_T *si1, *si2;
1914 int res;
1915 typval_T rettv;
1916 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001917 char_u *func_name;
1918 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001919 funcexe_T funcexe;
Bram Moolenaar23018f22021-12-27 11:54:37 +00001920 int did_emsg_before = did_emsg;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001921
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001922 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001923 if (sortinfo->item_compare_func_err)
1924 return 0;
1925
1926 si1 = (sortItem_T *)s1;
1927 si2 = (sortItem_T *)s2;
1928
1929 if (partial == NULL)
1930 func_name = sortinfo->item_compare_func;
1931 else
1932 func_name = partial_name(partial);
1933
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001934 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1935 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001936 copy_tv(&si1->item->li_tv, &argv[0]);
1937 copy_tv(&si2->item->li_tv, &argv[1]);
1938
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001939 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001940 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001941 funcexe.fe_evaluate = TRUE;
1942 funcexe.fe_partial = partial;
1943 funcexe.fe_selfdict = sortinfo->item_compare_selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001944 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001945 clear_tv(&argv[0]);
1946 clear_tv(&argv[1]);
1947
Bram Moolenaar23018f22021-12-27 11:54:37 +00001948 if (res == FAIL || did_emsg > did_emsg_before)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001949 res = ITEM_COMPARE_FAIL;
1950 else
Yasuhiro Matsumotoc04f6232021-09-19 17:01:39 +02001951 {
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001952 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Yasuhiro Matsumotoc04f6232021-09-19 17:01:39 +02001953 if (res > 0)
1954 res = 1;
1955 else if (res < 0)
1956 res = -1;
1957 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001958 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001959 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001960 clear_tv(&rettv);
1961
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001962 // When the result would be zero, compare the pointers themselves. Makes
1963 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001964 if (res == 0 && !sortinfo->item_compare_keep_zero)
1965 res = si1->idx > si2->idx ? 1 : -1;
1966
1967 return res;
1968}
1969
1970/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00001971 * sort() List "l"
1972 */
1973 static void
1974do_sort(list_T *l, sortinfo_T *info)
1975{
1976 long len;
1977 sortItem_T *ptrs;
1978 long i = 0;
1979 listitem_T *li;
1980
1981 len = list_len(l);
1982
1983 // Make an array with each entry pointing to an item in the List.
1984 ptrs = ALLOC_MULT(sortItem_T, len);
1985 if (ptrs == NULL)
1986 return;
1987
1988 // sort(): ptrs will be the list to sort
1989 FOR_ALL_LIST_ITEMS(l, li)
1990 {
1991 ptrs[i].item = li;
1992 ptrs[i].idx = i;
1993 ++i;
1994 }
1995
1996 info->item_compare_func_err = FALSE;
1997 info->item_compare_keep_zero = FALSE;
1998 // test the compare function
1999 if ((info->item_compare_func != NULL
2000 || info->item_compare_partial != NULL)
2001 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
2002 == ITEM_COMPARE_FAIL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002003 emsg(_(e_sort_compare_function_failed));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002004 else
2005 {
2006 // Sort the array with item pointers.
2007 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
2008 info->item_compare_func == NULL
2009 && info->item_compare_partial == NULL
2010 ? item_compare : item_compare2);
2011
2012 if (!info->item_compare_func_err)
2013 {
2014 // Clear the List and append the items in sorted order.
2015 l->lv_first = l->lv_u.mat.lv_last
2016 = l->lv_u.mat.lv_idx_item = NULL;
2017 l->lv_len = 0;
2018 for (i = 0; i < len; ++i)
2019 list_append(l, ptrs[i].item);
2020 }
2021 }
2022
2023 vim_free(ptrs);
2024}
2025
2026/*
2027 * uniq() List "l"
2028 */
2029 static void
2030do_uniq(list_T *l, sortinfo_T *info)
2031{
2032 long len;
2033 sortItem_T *ptrs;
2034 long i = 0;
2035 listitem_T *li;
2036 int (*item_compare_func_ptr)(const void *, const void *);
2037
2038 len = list_len(l);
2039
2040 // Make an array with each entry pointing to an item in the List.
2041 ptrs = ALLOC_MULT(sortItem_T, len);
2042 if (ptrs == NULL)
2043 return;
2044
2045 // f_uniq(): ptrs will be a stack of items to remove
2046 info->item_compare_func_err = FALSE;
2047 info->item_compare_keep_zero = TRUE;
2048 item_compare_func_ptr = info->item_compare_func != NULL
2049 || info->item_compare_partial != NULL
2050 ? item_compare2 : item_compare;
2051
2052 for (li = l->lv_first; li != NULL && li->li_next != NULL;
2053 li = li->li_next)
2054 {
2055 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
2056 == 0)
2057 ptrs[i++].item = li;
2058 if (info->item_compare_func_err)
2059 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002060 emsg(_(e_uniq_compare_function_failed));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002061 break;
2062 }
2063 }
2064
2065 if (!info->item_compare_func_err)
2066 {
2067 while (--i >= 0)
2068 {
2069 li = ptrs[i].item->li_next;
2070 ptrs[i].item->li_next = li->li_next;
2071 if (li->li_next != NULL)
2072 li->li_next->li_prev = ptrs[i].item;
2073 else
2074 l->lv_u.mat.lv_last = ptrs[i].item;
2075 list_fix_watch(l, li);
2076 listitem_free(l, li);
2077 l->lv_len--;
2078 }
2079 }
2080
2081 vim_free(ptrs);
2082}
2083
2084/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002085 * Parse the optional arguments supplied to the sort() or uniq() function and
2086 * return the values in "info".
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002087 */
2088 static int
2089parse_sort_uniq_args(typval_T *argvars, sortinfo_T *info)
2090{
2091 info->item_compare_ic = FALSE;
2092 info->item_compare_lc = FALSE;
2093 info->item_compare_numeric = FALSE;
2094 info->item_compare_numbers = FALSE;
2095#ifdef FEAT_FLOAT
2096 info->item_compare_float = FALSE;
2097#endif
2098 info->item_compare_func = NULL;
2099 info->item_compare_partial = NULL;
2100 info->item_compare_selfdict = NULL;
2101
2102 if (argvars[1].v_type == VAR_UNKNOWN)
2103 return OK;
2104
2105 // optional second argument: {func}
2106 if (argvars[1].v_type == VAR_FUNC)
2107 info->item_compare_func = argvars[1].vval.v_string;
2108 else if (argvars[1].v_type == VAR_PARTIAL)
2109 info->item_compare_partial = argvars[1].vval.v_partial;
2110 else
2111 {
2112 int error = FALSE;
2113 int nr = 0;
2114
2115 if (argvars[1].v_type == VAR_NUMBER)
2116 {
2117 nr = tv_get_number_chk(&argvars[1], &error);
2118 if (error)
2119 return FAIL;
2120 if (nr == 1)
2121 info->item_compare_ic = TRUE;
2122 }
2123 if (nr != 1)
2124 {
2125 if (argvars[1].v_type != VAR_NUMBER)
2126 info->item_compare_func = tv_get_string(&argvars[1]);
2127 else if (nr != 0)
2128 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002129 emsg(_(e_invalid_argument));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002130 return FAIL;
2131 }
2132 }
2133 if (info->item_compare_func != NULL)
2134 {
2135 if (*info->item_compare_func == NUL)
2136 {
2137 // empty string means default sort
2138 info->item_compare_func = NULL;
2139 }
2140 else if (STRCMP(info->item_compare_func, "n") == 0)
2141 {
2142 info->item_compare_func = NULL;
2143 info->item_compare_numeric = TRUE;
2144 }
2145 else if (STRCMP(info->item_compare_func, "N") == 0)
2146 {
2147 info->item_compare_func = NULL;
2148 info->item_compare_numbers = TRUE;
2149 }
2150#ifdef FEAT_FLOAT
2151 else if (STRCMP(info->item_compare_func, "f") == 0)
2152 {
2153 info->item_compare_func = NULL;
2154 info->item_compare_float = TRUE;
2155 }
2156#endif
2157 else if (STRCMP(info->item_compare_func, "i") == 0)
2158 {
2159 info->item_compare_func = NULL;
2160 info->item_compare_ic = TRUE;
2161 }
2162 else if (STRCMP(info->item_compare_func, "l") == 0)
2163 {
2164 info->item_compare_func = NULL;
2165 info->item_compare_lc = TRUE;
2166 }
2167 }
2168 }
2169
2170 if (argvars[2].v_type != VAR_UNKNOWN)
2171 {
2172 // optional third argument: {dict}
2173 if (argvars[2].v_type != VAR_DICT)
2174 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002175 emsg(_(e_dictionary_required));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002176 return FAIL;
2177 }
2178 info->item_compare_selfdict = argvars[2].vval.v_dict;
2179 }
2180
2181 return OK;
2182}
2183
2184/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002185 * "sort()" or "uniq()" function
2186 */
2187 static void
2188do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
2189{
2190 list_T *l;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002191 sortinfo_T *old_sortinfo;
2192 sortinfo_T info;
2193 long len;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002194
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002195 if (in_vim9script()
2196 && (check_for_list_arg(argvars, 0) == FAIL
2197 || (argvars[1].v_type != VAR_UNKNOWN
2198 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
2199 return;
2200
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002201 if (argvars[0].v_type != VAR_LIST)
2202 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002203 semsg(_(e_argument_of_str_must_be_list), sort ? "sort()" : "uniq()");
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002204 return;
2205 }
2206
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002207 // Pointer to current info struct used in compare function. Save and
2208 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002209 old_sortinfo = sortinfo;
2210 sortinfo = &info;
2211
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002212 l = argvars[0].vval.v_list;
2213 if (l != NULL && value_check_lock(l->lv_lock,
2214 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
2215 TRUE))
2216 goto theend;
2217 rettv_list_set(rettv, l);
2218 if (l == NULL)
2219 goto theend;
2220 CHECK_LIST_MATERIALIZE(l);
2221
2222 len = list_len(l);
2223 if (len <= 1)
2224 goto theend; // short list sorts pretty quickly
2225
2226 if (parse_sort_uniq_args(argvars, &info) == FAIL)
2227 goto theend;
2228
2229 if (sort)
2230 do_sort(l, &info);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002231 else
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002232 do_uniq(l, &info);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002233
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002234theend:
2235 sortinfo = old_sortinfo;
2236}
2237
2238/*
2239 * "sort({list})" function
2240 */
2241 void
2242f_sort(typval_T *argvars, typval_T *rettv)
2243{
2244 do_sort_uniq(argvars, rettv, TRUE);
2245}
2246
2247/*
2248 * "uniq({list})" function
2249 */
2250 void
2251f_uniq(typval_T *argvars, typval_T *rettv)
2252{
2253 do_sort_uniq(argvars, rettv, FALSE);
2254}
2255
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002256/*
2257 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01002258 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002259 */
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002260 int
Bram Moolenaarea696852020-11-09 18:31:39 +01002261filter_map_one(
2262 typval_T *tv, // original value
2263 typval_T *expr, // callback
2264 filtermap_T filtermap,
2265 typval_T *newtv, // for map() and mapnew(): new value
2266 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002267{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002268 typval_T argv[3];
2269 int retval = FAIL;
2270
2271 copy_tv(tv, get_vim_var_tv(VV_VAL));
2272 argv[0] = *get_vim_var_tv(VV_KEY);
2273 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01002274 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002275 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002276 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002277 {
2278 int error = FALSE;
2279
2280 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02002281 if (in_vim9script())
Bram Moolenaar18024052021-12-25 21:43:28 +00002282 *remp = !tv_get_bool_chk(newtv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002283 else
Bram Moolenaarea696852020-11-09 18:31:39 +01002284 *remp = (tv_get_number_chk(newtv, &error) == 0);
2285 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002286 // On type error, nothing has been removed; return FAIL to stop the
2287 // loop. The error message was given by tv_get_number_chk().
2288 if (error)
2289 goto theend;
2290 }
2291 retval = OK;
2292theend:
2293 clear_tv(get_vim_var_tv(VV_VAL));
2294 return retval;
2295}
2296
2297/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002298 * Implementation of map() and filter() for a List. Apply "expr" to every item
2299 * in List "l" and return the result in "rettv".
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002300 */
2301 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002302list_filter_map(
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002303 list_T *l,
2304 filtermap_T filtermap,
2305 type_T *argtype,
2306 char *func_name,
2307 char_u *arg_errmsg,
2308 typval_T *expr,
2309 typval_T *rettv)
2310{
2311 int prev_lock;
2312 list_T *l_ret = NULL;
2313 int idx = 0;
2314 int rem;
2315 listitem_T *li, *nli;
2316
2317 if (filtermap == FILTERMAP_MAPNEW)
2318 {
2319 rettv->v_type = VAR_LIST;
2320 rettv->vval.v_list = NULL;
2321 }
2322 if (l == NULL
2323 || (filtermap == FILTERMAP_FILTER
2324 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
2325 return;
2326
2327 prev_lock = l->lv_lock;
2328
2329 if (filtermap == FILTERMAP_MAPNEW)
2330 {
2331 if (rettv_list_alloc(rettv) == FAIL)
2332 return;
2333 l_ret = rettv->vval.v_list;
2334 }
2335 // set_vim_var_nr() doesn't set the type
2336 set_vim_var_type(VV_KEY, VAR_NUMBER);
2337
2338 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
2339 l->lv_lock = VAR_LOCKED;
2340
2341 if (l->lv_first == &range_list_item)
2342 {
2343 varnumber_T val = l->lv_u.nonmat.lv_start;
2344 int len = l->lv_len;
2345 int stride = l->lv_u.nonmat.lv_stride;
2346
2347 // List from range(): loop over the numbers
2348 if (filtermap != FILTERMAP_MAPNEW)
2349 {
2350 l->lv_first = NULL;
2351 l->lv_u.mat.lv_last = NULL;
2352 l->lv_len = 0;
2353 l->lv_u.mat.lv_idx_item = NULL;
2354 }
2355
2356 for (idx = 0; idx < len; ++idx)
2357 {
2358 typval_T tv;
2359 typval_T newtv;
2360
2361 tv.v_type = VAR_NUMBER;
2362 tv.v_lock = 0;
2363 tv.vval.v_number = val;
2364 set_vim_var_nr(VV_KEY, idx);
2365 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2366 == FAIL)
2367 break;
2368 if (did_emsg)
2369 {
2370 clear_tv(&newtv);
2371 break;
2372 }
2373 if (filtermap != FILTERMAP_FILTER)
2374 {
2375 if (filtermap == FILTERMAP_MAP && argtype != NULL
2376 && check_typval_arg_type(
2377 argtype->tt_member, &newtv,
2378 func_name, 0) == FAIL)
2379 {
2380 clear_tv(&newtv);
2381 break;
2382 }
2383 // map(), mapnew(): always append the new value to the
2384 // list
2385 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2386 ? l : l_ret, &newtv) == FAIL)
2387 break;
2388 }
2389 else if (!rem)
2390 {
2391 // filter(): append the list item value when not rem
2392 if (list_append_tv_move(l, &tv) == FAIL)
2393 break;
2394 }
2395
2396 val += stride;
2397 }
2398 }
2399 else
2400 {
2401 // Materialized list: loop over the items
2402 for (li = l->lv_first; li != NULL; li = nli)
2403 {
2404 typval_T newtv;
2405
2406 if (filtermap == FILTERMAP_MAP && value_check_lock(
2407 li->li_tv.v_lock, arg_errmsg, TRUE))
2408 break;
2409 nli = li->li_next;
2410 set_vim_var_nr(VV_KEY, idx);
2411 if (filter_map_one(&li->li_tv, expr, filtermap,
2412 &newtv, &rem) == FAIL)
2413 break;
2414 if (did_emsg)
2415 {
2416 clear_tv(&newtv);
2417 break;
2418 }
2419 if (filtermap == FILTERMAP_MAP)
2420 {
2421 if (argtype != NULL && check_typval_arg_type(
2422 argtype->tt_member, &newtv, func_name, 0) == FAIL)
2423 {
2424 clear_tv(&newtv);
2425 break;
2426 }
2427 // map(): replace the list item value
2428 clear_tv(&li->li_tv);
2429 newtv.v_lock = 0;
2430 li->li_tv = newtv;
2431 }
2432 else if (filtermap == FILTERMAP_MAPNEW)
2433 {
2434 // mapnew(): append the list item value
2435 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2436 break;
2437 }
2438 else if (filtermap == FILTERMAP_FILTER && rem)
2439 listitem_remove(l, li);
2440 ++idx;
2441 }
2442 }
2443
2444 l->lv_lock = prev_lock;
2445}
2446
2447/*
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002448 * Implementation of map() and filter().
2449 */
2450 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01002451filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002452{
2453 typval_T *expr;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002454 char *func_name = filtermap == FILTERMAP_MAP ? "map()"
Bram Moolenaarea696852020-11-09 18:31:39 +01002455 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002456 : "filter()";
Bram Moolenaarea696852020-11-09 18:31:39 +01002457 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2458 ? N_("map() argument")
2459 : filtermap == FILTERMAP_MAPNEW
2460 ? N_("mapnew() argument")
2461 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002462 int save_did_emsg;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002463 type_T *type = NULL;
2464 garray_T type_list;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002465
Bram Moolenaarea696852020-11-09 18:31:39 +01002466 // map() and filter() return the first argument, also on failure.
Bram Moolenaar2d877592021-12-16 08:21:09 +00002467 if (filtermap != FILTERMAP_MAPNEW && argvars[0].v_type != VAR_STRING)
Bram Moolenaarea696852020-11-09 18:31:39 +01002468 copy_tv(&argvars[0], rettv);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002469
2470 if (in_vim9script()
Bram Moolenaar2d877592021-12-16 08:21:09 +00002471 && (check_for_list_or_dict_or_blob_or_string_arg(argvars, 0)
2472 == FAIL))
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002473 return;
2474
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002475 if (filtermap == FILTERMAP_MAP && in_vim9script())
2476 {
2477 // Check that map() does not change the type of the dict.
2478 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaar114dbda2022-01-03 12:28:03 +00002479 type = typval2type(argvars, get_copyID(), &type_list, TVTT_DO_MEMBER);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002480 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002481
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002482 if (argvars[0].v_type != VAR_BLOB
2483 && argvars[0].v_type != VAR_LIST
2484 && argvars[0].v_type != VAR_DICT
2485 && argvars[0].v_type != VAR_STRING)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002486 {
rbtnnc479ce02021-12-15 19:14:54 +00002487 semsg(_(e_argument_of_str_must_be_list_string_dictionary_or_blob),
2488 func_name);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002489 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002490 }
2491
2492 expr = &argvars[1];
2493 // On type errors, the preceding call has already displayed an error
2494 // message. Avoid a misleading error message for an empty string that
2495 // was not passed as argument.
2496 if (expr->v_type != VAR_UNKNOWN)
2497 {
2498 typval_T save_val;
2499 typval_T save_key;
2500
2501 prepare_vimvar(VV_VAL, &save_val);
2502 prepare_vimvar(VV_KEY, &save_key);
2503
2504 // We reset "did_emsg" to be able to detect whether an error
2505 // occurred during evaluation of the expression.
2506 save_did_emsg = did_emsg;
2507 did_emsg = FALSE;
2508
2509 if (argvars[0].v_type == VAR_DICT)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002510 dict_filter_map(argvars[0].vval.v_dict, filtermap, type, func_name,
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002511 arg_errmsg, expr, rettv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002512 else if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002513 blob_filter_map(argvars[0].vval.v_blob, filtermap, expr, rettv);
rbtnnc479ce02021-12-15 19:14:54 +00002514 else if (argvars[0].v_type == VAR_STRING)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002515 string_filter_map(tv_get_string(&argvars[0]), filtermap, expr,
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002516 rettv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002517 else // argvars[0].v_type == VAR_LIST
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002518 list_filter_map(argvars[0].vval.v_list, filtermap, type, func_name,
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002519 arg_errmsg, expr, rettv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002520
2521 restore_vimvar(VV_KEY, &save_key);
2522 restore_vimvar(VV_VAL, &save_val);
2523
2524 did_emsg |= save_did_emsg;
2525 }
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002526
2527theend:
2528 if (type != NULL)
2529 clear_type_list(&type_list);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002530}
2531
2532/*
2533 * "filter()" function
2534 */
2535 void
2536f_filter(typval_T *argvars, typval_T *rettv)
2537{
Bram Moolenaarea696852020-11-09 18:31:39 +01002538 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002539}
2540
2541/*
2542 * "map()" function
2543 */
2544 void
2545f_map(typval_T *argvars, typval_T *rettv)
2546{
Bram Moolenaarea696852020-11-09 18:31:39 +01002547 filter_map(argvars, rettv, FILTERMAP_MAP);
2548}
2549
2550/*
2551 * "mapnew()" function
2552 */
2553 void
2554f_mapnew(typval_T *argvars, typval_T *rettv)
2555{
2556 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002557}
2558
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002559/*
2560 * "add(list, item)" function
2561 */
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002562 static void
2563list_add(typval_T *argvars, typval_T *rettv)
2564{
2565 list_T *l = argvars[0].vval.v_list;
2566
2567 if (l == NULL)
2568 {
2569 if (in_vim9script())
2570 emsg(_(e_cannot_add_to_null_list));
2571 }
2572 else if (!value_check_lock(l->lv_lock,
2573 (char_u *)N_("add() argument"), TRUE)
2574 && list_append_tv(l, &argvars[1]) == OK)
2575 {
2576 copy_tv(&argvars[0], rettv);
2577 }
2578}
2579
2580/*
2581 * "add(object, item)" function
2582 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002583 void
2584f_add(typval_T *argvars, typval_T *rettv)
2585{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002586 rettv->vval.v_number = 1; // Default: Failed
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002587
2588 if (in_vim9script()
2589 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2590 || (argvars[0].v_type == VAR_BLOB
2591 && check_for_number_arg(argvars, 1) == FAIL)))
2592 return;
2593
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002594 if (argvars[0].v_type == VAR_LIST)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002595 list_add(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002596 else if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002597 blob_add(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002598 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002599 emsg(_(e_list_or_blob_required));
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002600}
2601
2602/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002603 * Count the number of times item "needle" occurs in List "l" starting at index
2604 * "idx". Case is ignored if "ic" is TRUE.
2605 */
2606 static long
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002607list_count(list_T *l, typval_T *needle, long idx, int ic)
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002608{
2609 long n = 0;
2610 listitem_T *li;
2611
2612 if (l == NULL)
2613 return 0;
2614
2615 CHECK_LIST_MATERIALIZE(l);
2616
2617 if (list_len(l) == 0)
2618 return 0;
2619
2620 li = list_find(l, idx);
2621 if (li == NULL)
2622 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002623 semsg(_(e_list_index_out_of_range_nr), idx);
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002624 return 0;
2625 }
2626
2627 for ( ; li != NULL; li = li->li_next)
2628 if (tv_equal(&li->li_tv, needle, ic, FALSE))
2629 ++n;
2630
2631 return n;
2632}
2633
2634/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002635 * "count()" function
2636 */
2637 void
2638f_count(typval_T *argvars, typval_T *rettv)
2639{
2640 long n = 0;
2641 int ic = FALSE;
2642 int error = FALSE;
2643
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002644 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002645 && (check_for_string_or_list_or_dict_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002646 || check_for_opt_bool_arg(argvars, 2) == FAIL
2647 || (argvars[2].v_type != VAR_UNKNOWN
2648 && check_for_opt_number_arg(argvars, 3) == FAIL)))
2649 return;
2650
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002651 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002652 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002653
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002654 if (!error && argvars[0].v_type == VAR_STRING)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002655 n = string_count(argvars[0].vval.v_string,
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002656 tv_get_string_chk(&argvars[1]), ic);
2657 else if (!error && argvars[0].v_type == VAR_LIST)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002658 {
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002659 long idx = 0;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002660
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002661 if (argvars[2].v_type != VAR_UNKNOWN
2662 && argvars[3].v_type != VAR_UNKNOWN)
2663 idx = (long)tv_get_number_chk(&argvars[3], &error);
2664 if (!error)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002665 n = list_count(argvars[0].vval.v_list, &argvars[1], idx, ic);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002666 }
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002667 else if (!error && argvars[0].v_type == VAR_DICT)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002668 {
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002669 if (argvars[2].v_type != VAR_UNKNOWN
2670 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002671 emsg(_(e_invalid_argument));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002672 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002673 n = dict_count(argvars[0].vval.v_dict, &argvars[1], ic);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002674 }
2675 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002676 semsg(_(e_argument_of_str_must_be_list_or_dictionary), "count()");
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002677 rettv->vval.v_number = n;
2678}
2679
2680/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002681 * extend() a List. Append List argvars[1] to List argvars[0] before index
2682 * argvars[3] and return the resulting list in "rettv". "is_new" is TRUE for
2683 * extendnew().
2684 */
2685 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002686list_extend_func(
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002687 typval_T *argvars,
2688 type_T *type,
2689 char *func_name,
2690 char_u *arg_errmsg,
2691 int is_new,
2692 typval_T *rettv)
2693{
2694 list_T *l1, *l2;
2695 listitem_T *item;
2696 long before;
2697 int error = FALSE;
2698
2699 l1 = argvars[0].vval.v_list;
2700 if (l1 == NULL)
2701 {
2702 emsg(_(e_cannot_extend_null_list));
2703 return;
2704 }
2705 l2 = argvars[1].vval.v_list;
2706 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
2707 && l2 != NULL)
2708 {
2709 if (is_new)
2710 {
2711 l1 = list_copy(l1, FALSE, get_copyID());
2712 if (l1 == NULL)
2713 return;
2714 }
2715
2716 if (argvars[2].v_type != VAR_UNKNOWN)
2717 {
2718 before = (long)tv_get_number_chk(&argvars[2], &error);
2719 if (error)
2720 return; // type error; errmsg already given
2721
2722 if (before == l1->lv_len)
2723 item = NULL;
2724 else
2725 {
2726 item = list_find(l1, before);
2727 if (item == NULL)
2728 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002729 semsg(_(e_list_index_out_of_range_nr), before);
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002730 return;
2731 }
2732 }
2733 }
2734 else
2735 item = NULL;
2736 if (type != NULL && check_typval_arg_type(
2737 type, &argvars[1], func_name, 2) == FAIL)
2738 return;
2739 list_extend(l1, l2, item);
2740
2741 if (is_new)
2742 {
2743 rettv->v_type = VAR_LIST;
2744 rettv->vval.v_list = l1;
2745 rettv->v_lock = FALSE;
2746 }
2747 else
2748 copy_tv(&argvars[0], rettv);
2749 }
2750}
2751
2752/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002753 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002754 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002755 static void
2756extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002757{
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002758 type_T *type = NULL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002759 char *func_name = is_new ? "extendnew()" : "extend()";
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002760
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002761 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002762 {
2763 // Check that extend() does not change the type of the list if it was
2764 // declared.
2765 if (!is_new && in_vim9script() && argvars[0].vval.v_list != NULL)
2766 type = argvars[0].vval.v_list->lv_type;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002767 list_extend_func(argvars, type, func_name, arg_errmsg, is_new, rettv);
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002768 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002769 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002770 {
2771 // Check that extend() does not change the type of the list if it was
2772 // declared.
2773 if (!is_new && in_vim9script() && argvars[0].vval.v_dict != NULL)
2774 type = argvars[0].vval.v_dict->dv_type;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002775 dict_extend_func(argvars, type, func_name, arg_errmsg, is_new, rettv);
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002776 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002777 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002778 semsg(_(e_argument_of_str_must_be_list_or_dictionary), func_name);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002779}
2780
2781/*
2782 * "extend(list, list [, idx])" function
2783 * "extend(dict, dict [, action])" function
2784 */
2785 void
2786f_extend(typval_T *argvars, typval_T *rettv)
2787{
2788 char_u *errmsg = (char_u *)N_("extend() argument");
2789
2790 extend(argvars, rettv, errmsg, FALSE);
2791}
2792
2793/*
2794 * "extendnew(list, list [, idx])" function
2795 * "extendnew(dict, dict [, action])" function
2796 */
2797 void
2798f_extendnew(typval_T *argvars, typval_T *rettv)
2799{
2800 char_u *errmsg = (char_u *)N_("extendnew() argument");
2801
2802 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002803}
2804
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002805 static void
2806list_insert_func(typval_T *argvars, typval_T *rettv)
2807{
2808 list_T *l = argvars[0].vval.v_list;
2809 long before = 0;
2810 listitem_T *item;
2811 int error = FALSE;
2812
2813 if (l == NULL)
2814 {
2815 if (in_vim9script())
2816 emsg(_(e_cannot_add_to_null_list));
2817 return;
2818 }
2819
2820 if (value_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
2821 return;
2822
2823 if (argvars[2].v_type != VAR_UNKNOWN)
2824 before = (long)tv_get_number_chk(&argvars[2], &error);
2825 if (error)
2826 return; // type error; errmsg already given
2827
2828 if (before == l->lv_len)
2829 item = NULL;
2830 else
2831 {
2832 item = list_find(l, before);
2833 if (item == NULL)
2834 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002835 semsg(_(e_list_index_out_of_range_nr), before);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002836 l = NULL;
2837 }
2838 }
2839 if (l != NULL)
2840 {
2841 (void)list_insert_tv(l, &argvars[1], item);
2842 copy_tv(&argvars[0], rettv);
2843 }
2844}
2845
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002846/*
2847 * "insert()" function
2848 */
2849 void
2850f_insert(typval_T *argvars, typval_T *rettv)
2851{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002852 if (in_vim9script()
2853 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2854 || (argvars[0].v_type == VAR_BLOB
2855 && check_for_number_arg(argvars, 1) == FAIL)
2856 || check_for_opt_number_arg(argvars, 2) == FAIL))
2857 return;
2858
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002859 if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002860 blob_insert_func(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002861 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002862 semsg(_(e_argument_of_str_must_be_list_or_blob), "insert()");
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002863 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002864 list_insert_func(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002865}
2866
2867/*
2868 * "remove()" function
2869 */
2870 void
2871f_remove(typval_T *argvars, typval_T *rettv)
2872{
2873 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2874
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002875 if (in_vim9script()
2876 && (check_for_list_or_dict_or_blob_arg(argvars, 0) == FAIL
2877 || ((argvars[0].v_type == VAR_LIST
2878 || argvars[0].v_type == VAR_BLOB)
2879 && (check_for_number_arg(argvars, 1) == FAIL
2880 || check_for_opt_number_arg(argvars, 2) == FAIL))
2881 || (argvars[0].v_type == VAR_DICT
2882 && check_for_string_or_number_arg(argvars, 1) == FAIL)))
2883 return;
2884
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002885 if (argvars[0].v_type == VAR_DICT)
2886 dict_remove(argvars, rettv, arg_errmsg);
2887 else if (argvars[0].v_type == VAR_BLOB)
Sean Dewar80d73952021-08-04 19:25:54 +02002888 blob_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002889 else if (argvars[0].v_type == VAR_LIST)
2890 list_remove(argvars, rettv, arg_errmsg);
2891 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002892 semsg(_(e_argument_of_str_must_be_list_dictionary_or_blob), "remove()");
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002893}
2894
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002895 static void
2896list_reverse(list_T *l, typval_T *rettv)
2897{
2898 listitem_T *li, *ni;
2899
2900 rettv_list_set(rettv, l);
2901 if (l != NULL
2902 && !value_check_lock(l->lv_lock,
2903 (char_u *)N_("reverse() argument"), TRUE))
2904 {
2905 if (l->lv_first == &range_list_item)
2906 {
2907 varnumber_T new_start = l->lv_u.nonmat.lv_start
2908 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2909 l->lv_u.nonmat.lv_end = new_start
2910 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2911 l->lv_u.nonmat.lv_start = new_start;
2912 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
2913 return;
2914 }
2915 li = l->lv_u.mat.lv_last;
2916 l->lv_first = l->lv_u.mat.lv_last = NULL;
2917 l->lv_len = 0;
2918 while (li != NULL)
2919 {
2920 ni = li->li_prev;
2921 list_append(l, li);
2922 li = ni;
2923 }
2924 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
2925 }
2926}
2927
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002928/*
2929 * "reverse({list})" function
2930 */
2931 void
2932f_reverse(typval_T *argvars, typval_T *rettv)
2933{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002934 if (in_vim9script() && check_for_list_or_blob_arg(argvars, 0) == FAIL)
2935 return;
2936
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002937 if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002938 blob_reverse(argvars[0].vval.v_blob, rettv);
2939 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002940 semsg(_(e_argument_of_str_must_be_list_or_blob), "reverse()");
Bram Moolenaaref982572021-08-12 19:27:57 +02002941 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002942 list_reverse(argvars[0].vval.v_list, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002943}
2944
Bram Moolenaar85629982020-06-01 18:39:20 +02002945/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002946 * reduce() List argvars[0] using the function 'funcname' with arguments in
2947 * 'funcexe' starting with the initial value argvars[2] and return the result
2948 * in 'rettv'.
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002949 */
2950 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002951list_reduce(
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002952 typval_T *argvars,
2953 char_u *func_name,
2954 funcexe_T *funcexe,
2955 typval_T *rettv)
2956{
2957 list_T *l = argvars[0].vval.v_list;
2958 listitem_T *li = NULL;
2959 typval_T initial;
2960 typval_T argv[3];
2961 int r;
2962 int called_emsg_start = called_emsg;
2963 int prev_locked;
2964
2965 if (l != NULL)
2966 CHECK_LIST_MATERIALIZE(l);
2967 if (argvars[2].v_type == VAR_UNKNOWN)
2968 {
2969 if (l == NULL || l->lv_first == NULL)
2970 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002971 semsg(_(e_reduce_of_an_empty_str_with_no_initial_value), "List");
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002972 return;
2973 }
2974 initial = l->lv_first->li_tv;
2975 li = l->lv_first->li_next;
2976 }
2977 else
2978 {
2979 initial = argvars[2];
2980 if (l != NULL)
2981 li = l->lv_first;
2982 }
2983 copy_tv(&initial, rettv);
2984
2985 if (l == NULL)
2986 return;
2987
2988 prev_locked = l->lv_lock;
2989
2990 l->lv_lock = VAR_FIXED; // disallow the list changing here
2991 for ( ; li != NULL; li = li->li_next)
2992 {
2993 argv[0] = *rettv;
2994 argv[1] = li->li_tv;
2995 rettv->v_type = VAR_UNKNOWN;
2996 r = call_func(func_name, -1, rettv, 2, argv, funcexe);
2997 clear_tv(&argv[0]);
2998 if (r == FAIL || called_emsg != called_emsg_start)
2999 break;
3000 }
3001 l->lv_lock = prev_locked;
3002}
3003
3004/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003005 * "reduce(list, { accumulator, element -> value } [, initial])" function
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00003006 * "reduce(blob, { accumulator, element -> value } [, initial])"
3007 * "reduce(string, { accumulator, element -> value } [, initial])"
Bram Moolenaar85629982020-06-01 18:39:20 +02003008 */
3009 void
3010f_reduce(typval_T *argvars, typval_T *rettv)
3011{
Bram Moolenaar85629982020-06-01 18:39:20 +02003012 char_u *func_name;
3013 partial_T *partial = NULL;
3014 funcexe_T funcexe;
Bram Moolenaar85629982020-06-01 18:39:20 +02003015
rbtnn0ccb5842021-12-18 18:33:46 +00003016 if (in_vim9script()
3017 && check_for_string_or_list_or_blob_arg(argvars, 0) == FAIL)
Bram Moolenaar85629982020-06-01 18:39:20 +02003018 return;
rbtnn0ccb5842021-12-18 18:33:46 +00003019
3020 if (argvars[0].v_type != VAR_STRING
3021 && argvars[0].v_type != VAR_LIST
3022 && argvars[0].v_type != VAR_BLOB)
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003023 {
rbtnn0ccb5842021-12-18 18:33:46 +00003024 semsg(_(e_string_list_or_blob_required), "reduce()");
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003025 return;
3026 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003027
3028 if (argvars[1].v_type == VAR_FUNC)
3029 func_name = argvars[1].vval.v_string;
3030 else if (argvars[1].v_type == VAR_PARTIAL)
3031 {
3032 partial = argvars[1].vval.v_partial;
3033 func_name = partial_name(partial);
3034 }
3035 else
3036 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01003037 if (func_name == NULL || *func_name == NUL)
3038 {
3039 emsg(_(e_missing_function_argument));
3040 return;
3041 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003042
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003043 CLEAR_FIELD(funcexe);
3044 funcexe.fe_evaluate = TRUE;
3045 funcexe.fe_partial = partial;
Bram Moolenaar85629982020-06-01 18:39:20 +02003046
3047 if (argvars[0].v_type == VAR_LIST)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003048 list_reduce(argvars, func_name, &funcexe, rettv);
rbtnn0ccb5842021-12-18 18:33:46 +00003049 else if (argvars[0].v_type == VAR_STRING)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003050 string_reduce(argvars, func_name, &funcexe, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003051 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003052 blob_reduce(argvars, func_name, &funcexe, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003053}
3054
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02003055#endif // defined(FEAT_EVAL)