blob: 5e70f2111b524eaa59c4e06493fde2b69a2e06d2 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarda861d62016-07-17 15:46:27 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +020011 * list.c: List support and container (List, Dict, Blob) functions.
Bram Moolenaarda861d62016-07-17 15:46:27 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010018// List heads for garbage collection.
19static list_T *first_list = NULL; // list of all lists
Bram Moolenaarda861d62016-07-17 15:46:27 +020020
Bram Moolenaar00d253e2020-04-06 22:13:01 +020021#define FOR_ALL_WATCHERS(l, lw) \
22 for ((lw) = (l)->lv_watch; (lw) != NULL; (lw) = (lw)->lw_next)
23
Bram Moolenaarbdff0122020-04-05 18:56:05 +020024static void list_free_item(list_T *l, listitem_T *item);
25
Bram Moolenaarda861d62016-07-17 15:46:27 +020026/*
27 * Add a watcher to a list.
28 */
29 void
30list_add_watch(list_T *l, listwatch_T *lw)
31{
32 lw->lw_next = l->lv_watch;
33 l->lv_watch = lw;
34}
35
36/*
37 * Remove a watcher from a list.
38 * No warning when it isn't found...
39 */
40 void
41list_rem_watch(list_T *l, listwatch_T *lwrem)
42{
43 listwatch_T *lw, **lwp;
44
45 lwp = &l->lv_watch;
Bram Moolenaar00d253e2020-04-06 22:13:01 +020046 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020047 {
48 if (lw == lwrem)
49 {
50 *lwp = lw->lw_next;
51 break;
52 }
53 lwp = &lw->lw_next;
54 }
55}
56
57/*
58 * Just before removing an item from a list: advance watchers to the next
59 * item.
60 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020061 static void
Bram Moolenaarda861d62016-07-17 15:46:27 +020062list_fix_watch(list_T *l, listitem_T *item)
63{
64 listwatch_T *lw;
65
Bram Moolenaar00d253e2020-04-06 22:13:01 +020066 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020067 if (lw->lw_item == item)
68 lw->lw_item = item->li_next;
69}
70
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010071 static void
72list_init(list_T *l)
73{
74 // Prepend the list to the list of lists for garbage collection.
75 if (first_list != NULL)
76 first_list->lv_used_prev = l;
77 l->lv_used_prev = NULL;
78 l->lv_used_next = first_list;
79 first_list = l;
80}
81
Bram Moolenaarda861d62016-07-17 15:46:27 +020082/*
83 * Allocate an empty header for a list.
84 * Caller should take care of the reference count.
85 */
86 list_T *
87list_alloc(void)
88{
89 list_T *l;
90
Bram Moolenaarc799fe22019-05-28 23:08:19 +020091 l = ALLOC_CLEAR_ONE(list_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +020092 if (l != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010093 list_init(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +020094 return l;
95}
96
97/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010098 * list_alloc() with an ID for alloc_fail().
99 */
100 list_T *
101list_alloc_id(alloc_id_T id UNUSED)
102{
103#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200104 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100105 return NULL;
106#endif
107 return (list_alloc());
108}
109
110/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100111 * Allocate space for a list, plus "count" items.
112 * Next list_set_item() must be called for each item.
113 */
114 list_T *
115list_alloc_with_items(int count)
116{
117 list_T *l;
118
119 l = (list_T *)alloc_clear(sizeof(list_T) + count * sizeof(listitem_T));
120 if (l != NULL)
121 {
122 list_init(l);
123
124 if (count > 0)
125 {
126 listitem_T *li = (listitem_T *)(l + 1);
127 int i;
128
129 l->lv_len = count;
130 l->lv_with_items = count;
131 l->lv_first = li;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100132 l->lv_u.mat.lv_last = li + count - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 for (i = 0; i < count; ++i)
134 {
135 if (i == 0)
136 li->li_prev = NULL;
137 else
138 li->li_prev = li - 1;
139 if (i == count - 1)
140 li->li_next = NULL;
141 else
142 li->li_next = li + 1;
143 ++li;
144 }
145 }
146 }
147 return l;
148}
149
150/*
151 * Set item "idx" for a list previously allocated with list_alloc_with_items().
152 * The contents of "tv" is moved into the list item.
153 * Each item must be set exactly once.
154 */
155 void
156list_set_item(list_T *l, int idx, typval_T *tv)
157{
158 listitem_T *li = (listitem_T *)(l + 1) + idx;
159
160 li->li_tv = *tv;
161}
162
163/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200164 * Allocate an empty list for a return value, with reference count set.
165 * Returns OK or FAIL.
166 */
167 int
168rettv_list_alloc(typval_T *rettv)
169{
170 list_T *l = list_alloc();
171
172 if (l == NULL)
173 return FAIL;
174
Bram Moolenaarda861d62016-07-17 15:46:27 +0200175 rettv->v_lock = 0;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200176 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200177 return OK;
178}
179
180/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100181 * Same as rettv_list_alloc() but uses an allocation id for testing.
182 */
183 int
184rettv_list_alloc_id(typval_T *rettv, alloc_id_T id UNUSED)
185{
186#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200187 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaar162b7142018-12-21 15:17:36 +0100188 return FAIL;
189#endif
190 return rettv_list_alloc(rettv);
191}
192
193
194/*
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200195 * Set a list as the return value. Increments the reference count.
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200196 */
197 void
198rettv_list_set(typval_T *rettv, list_T *l)
199{
200 rettv->v_type = VAR_LIST;
201 rettv->vval.v_list = l;
202 if (l != NULL)
203 ++l->lv_refcount;
204}
205
206/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200207 * Unreference a list: decrement the reference count and free it when it
208 * becomes zero.
209 */
210 void
211list_unref(list_T *l)
212{
213 if (l != NULL && --l->lv_refcount <= 0)
214 list_free(l);
215}
216
217/*
218 * Free a list, including all non-container items it points to.
219 * Ignores the reference count.
220 */
221 static void
222list_free_contents(list_T *l)
223{
224 listitem_T *item;
225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 if (l->lv_first != &range_list_item)
227 for (item = l->lv_first; item != NULL; item = l->lv_first)
228 {
229 // Remove the item before deleting it.
230 l->lv_first = item->li_next;
231 clear_tv(&item->li_tv);
232 list_free_item(l, item);
233 }
Bram Moolenaarda861d62016-07-17 15:46:27 +0200234}
235
236/*
237 * Go through the list of lists and free items without the copyID.
238 * But don't free a list that has a watcher (used in a for loop), these
239 * are not referenced anywhere.
240 */
241 int
242list_free_nonref(int copyID)
243{
244 list_T *ll;
245 int did_free = FALSE;
246
247 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
248 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
249 && ll->lv_watch == NULL)
250 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100251 // Free the List and ordinary items it contains, but don't recurse
252 // into Lists and Dictionaries, they will be in the list of dicts
253 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200254 list_free_contents(ll);
255 did_free = TRUE;
256 }
257 return did_free;
258}
259
260 static void
261list_free_list(list_T *l)
262{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100263 // Remove the list from the list of lists for garbage collection.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200264 if (l->lv_used_prev == NULL)
265 first_list = l->lv_used_next;
266 else
267 l->lv_used_prev->lv_used_next = l->lv_used_next;
268 if (l->lv_used_next != NULL)
269 l->lv_used_next->lv_used_prev = l->lv_used_prev;
270
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100271 free_type(l->lv_type);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200272 vim_free(l);
273}
274
275 void
276list_free_items(int copyID)
277{
278 list_T *ll, *ll_next;
279
280 for (ll = first_list; ll != NULL; ll = ll_next)
281 {
282 ll_next = ll->lv_used_next;
283 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
284 && ll->lv_watch == NULL)
285 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100286 // Free the List and ordinary items it contains, but don't recurse
287 // into Lists and Dictionaries, they will be in the list of dicts
288 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200289 list_free_list(ll);
290 }
291 }
292}
293
294 void
295list_free(list_T *l)
296{
297 if (!in_free_unref_items)
298 {
299 list_free_contents(l);
300 list_free_list(l);
301 }
302}
303
304/*
305 * Allocate a list item.
306 * It is not initialized, don't forget to set v_lock.
307 */
308 listitem_T *
309listitem_alloc(void)
310{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200311 return ALLOC_ONE(listitem_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200312}
313
314/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100315 * Free a list item, unless it was allocated together with the list itself.
316 * Does not clear the value. Does not notify watchers.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200317 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200318 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319list_free_item(list_T *l, listitem_T *item)
320{
321 if (l->lv_with_items == 0 || item < (listitem_T *)l
322 || item >= (listitem_T *)(l + 1) + l->lv_with_items)
323 vim_free(item);
324}
325
326/*
327 * Free a list item, unless it was allocated together with the list itself.
328 * Also clears the value. Does not notify watchers.
329 */
330 void
331listitem_free(list_T *l, listitem_T *item)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200332{
333 clear_tv(&item->li_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 list_free_item(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200335}
336
337/*
338 * Remove a list item from a List and free it. Also clears the value.
339 */
340 void
341listitem_remove(list_T *l, listitem_T *item)
342{
343 vimlist_remove(l, item, item);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100344 listitem_free(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200345}
346
347/*
348 * Get the number of items in a list.
349 */
350 long
351list_len(list_T *l)
352{
353 if (l == NULL)
354 return 0L;
355 return l->lv_len;
356}
357
358/*
359 * Return TRUE when two lists have exactly the same values.
360 */
361 int
362list_equal(
363 list_T *l1,
364 list_T *l2,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100365 int ic, // ignore case for strings
366 int recursive) // TRUE when used recursively
Bram Moolenaarda861d62016-07-17 15:46:27 +0200367{
368 listitem_T *item1, *item2;
369
Bram Moolenaarda861d62016-07-17 15:46:27 +0200370 if (l1 == l2)
371 return TRUE;
372 if (list_len(l1) != list_len(l2))
373 return FALSE;
Bram Moolenaar7b293c72020-04-09 21:33:22 +0200374 if (list_len(l1) == 0)
375 // empty and NULL list are considered equal
376 return TRUE;
377 if (l1 == NULL || l2 == NULL)
378 return FALSE;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200379
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200380 CHECK_LIST_MATERIALIZE(l1);
381 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100382
Bram Moolenaarda861d62016-07-17 15:46:27 +0200383 for (item1 = l1->lv_first, item2 = l2->lv_first;
384 item1 != NULL && item2 != NULL;
385 item1 = item1->li_next, item2 = item2->li_next)
386 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
387 return FALSE;
388 return item1 == NULL && item2 == NULL;
389}
390
391/*
392 * Locate item with index "n" in list "l" and return it.
393 * A negative index is counted from the end; -1 is the last item.
394 * Returns NULL when "n" is out of range.
395 */
396 listitem_T *
397list_find(list_T *l, long n)
398{
399 listitem_T *item;
400 long idx;
401
402 if (l == NULL)
403 return NULL;
404
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100405 // Negative index is relative to the end.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200406 if (n < 0)
407 n = l->lv_len + n;
408
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100409 // Check for index out of range.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200410 if (n < 0 || n >= l->lv_len)
411 return NULL;
412
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200413 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100415 // When there is a cached index may start search from there.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100416 if (l->lv_u.mat.lv_idx_item != NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200417 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100418 if (n < l->lv_u.mat.lv_idx / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200419 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100420 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200421 item = l->lv_first;
422 idx = 0;
423 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100424 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200425 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100426 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100427 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200428 idx = l->lv_len - 1;
429 }
430 else
431 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100432 // closest to the cached index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100433 item = l->lv_u.mat.lv_idx_item;
434 idx = l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200435 }
436 }
437 else
438 {
439 if (n < l->lv_len / 2)
440 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100441 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200442 item = l->lv_first;
443 idx = 0;
444 }
445 else
446 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100447 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100448 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200449 idx = l->lv_len - 1;
450 }
451 }
452
453 while (n > idx)
454 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100455 // search forward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200456 item = item->li_next;
457 ++idx;
458 }
459 while (n < idx)
460 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100461 // search backward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200462 item = item->li_prev;
463 --idx;
464 }
465
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100466 // cache the used index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100467 l->lv_u.mat.lv_idx = idx;
468 l->lv_u.mat.lv_idx_item = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200469
470 return item;
471}
472
473/*
474 * Get list item "l[idx]" as a number.
475 */
476 long
477list_find_nr(
478 list_T *l,
479 long idx,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100480 int *errorp) // set to TRUE when something wrong
Bram Moolenaarda861d62016-07-17 15:46:27 +0200481{
482 listitem_T *li;
483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484 if (l != NULL && l->lv_first == &range_list_item)
485 {
486 long n = idx;
487
488 // not materialized range() list: compute the value.
489 // Negative index is relative to the end.
490 if (n < 0)
491 n = l->lv_len + n;
492
493 // Check for index out of range.
494 if (n < 0 || n >= l->lv_len)
495 {
496 if (errorp != NULL)
497 *errorp = TRUE;
498 return -1L;
499 }
500
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100501 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502 }
503
Bram Moolenaarda861d62016-07-17 15:46:27 +0200504 li = list_find(l, idx);
505 if (li == NULL)
506 {
507 if (errorp != NULL)
508 *errorp = TRUE;
509 return -1L;
510 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100511 return (long)tv_get_number_chk(&li->li_tv, errorp);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200512}
513
514/*
515 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
516 */
517 char_u *
518list_find_str(list_T *l, long idx)
519{
520 listitem_T *li;
521
522 li = list_find(l, idx - 1);
523 if (li == NULL)
524 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000525 semsg(_(e_list_index_out_of_range_nr), idx);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200526 return NULL;
527 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100528 return tv_get_string(&li->li_tv);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200529}
530
531/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100532 * Like list_find() but when a negative index is used that is not found use
533 * zero and set "idx" to zero. Used for first index of a range.
534 */
535 listitem_T *
536list_find_index(list_T *l, long *idx)
537{
538 listitem_T *li = list_find(l, *idx);
539
540 if (li == NULL)
541 {
542 if (*idx < 0)
543 {
544 *idx = 0;
545 li = list_find(l, *idx);
546 }
547 }
548 return li;
549}
550
551/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200552 * Locate "item" list "l" and return its index.
553 * Returns -1 when "item" is not in the list.
554 */
555 long
556list_idx_of_item(list_T *l, listitem_T *item)
557{
558 long idx = 0;
559 listitem_T *li;
560
561 if (l == NULL)
562 return -1;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200563 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200564 idx = 0;
565 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
566 ++idx;
567 if (li == NULL)
568 return -1;
569 return idx;
570}
571
572/*
573 * Append item "item" to the end of list "l".
574 */
575 void
576list_append(list_T *l, listitem_T *item)
577{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200578 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100579 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200580 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100581 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200582 l->lv_first = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200583 item->li_prev = NULL;
584 }
585 else
586 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100587 l->lv_u.mat.lv_last->li_next = item;
588 item->li_prev = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200589 }
Bram Moolenaar35c807d2022-01-27 16:36:29 +0000590 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200591 ++l->lv_len;
592 item->li_next = NULL;
593}
594
595/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200597 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200598 */
599 int
600list_append_tv(list_T *l, typval_T *tv)
601{
Bram Moolenaar90fba562021-07-09 19:17:55 +0200602 listitem_T *li;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200603
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200604 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200605 && check_typval_arg_type(l->lv_type->tt_member, tv,
606 NULL, 0) == FAIL)
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200607 return FAIL;
Bram Moolenaar90fba562021-07-09 19:17:55 +0200608 li = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200609 if (li == NULL)
610 return FAIL;
611 copy_tv(tv, &li->li_tv);
612 list_append(l, li);
613 return OK;
614}
615
616/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 * As list_append_tv() but move the value instead of copying it.
618 * Return FAIL when out of memory.
619 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200620 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621list_append_tv_move(list_T *l, typval_T *tv)
622{
623 listitem_T *li = listitem_alloc();
624
625 if (li == NULL)
626 return FAIL;
627 li->li_tv = *tv;
628 list_append(l, li);
629 return OK;
630}
631
632/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200633 * Add a dictionary to a list. Used by getqflist().
634 * Return FAIL when out of memory.
635 */
636 int
637list_append_dict(list_T *list, dict_T *dict)
638{
639 listitem_T *li = listitem_alloc();
640
641 if (li == NULL)
642 return FAIL;
643 li->li_tv.v_type = VAR_DICT;
644 li->li_tv.v_lock = 0;
645 li->li_tv.vval.v_dict = dict;
646 list_append(list, li);
647 ++dict->dv_refcount;
648 return OK;
649}
650
651/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100652 * Append list2 to list1.
653 * Return FAIL when out of memory.
654 */
655 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200656list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100657{
658 listitem_T *li = listitem_alloc();
659
660 if (li == NULL)
661 return FAIL;
662 li->li_tv.v_type = VAR_LIST;
663 li->li_tv.v_lock = 0;
664 li->li_tv.vval.v_list = list2;
665 list_append(list1, li);
666 ++list2->lv_refcount;
667 return OK;
668}
669
670/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200671 * Make a copy of "str" and append it as an item to list "l".
672 * When "len" >= 0 use "str[len]".
673 * Returns FAIL when out of memory.
674 */
675 int
676list_append_string(list_T *l, char_u *str, int len)
677{
678 listitem_T *li = listitem_alloc();
679
680 if (li == NULL)
681 return FAIL;
682 list_append(l, li);
683 li->li_tv.v_type = VAR_STRING;
684 li->li_tv.v_lock = 0;
685 if (str == NULL)
686 li->li_tv.vval.v_string = NULL;
687 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
688 : vim_strsave(str))) == NULL)
689 return FAIL;
690 return OK;
691}
692
693/*
694 * Append "n" to list "l".
695 * Returns FAIL when out of memory.
696 */
697 int
698list_append_number(list_T *l, varnumber_T n)
699{
700 listitem_T *li;
701
702 li = listitem_alloc();
703 if (li == NULL)
704 return FAIL;
705 li->li_tv.v_type = VAR_NUMBER;
706 li->li_tv.v_lock = 0;
707 li->li_tv.vval.v_number = n;
708 list_append(l, li);
709 return OK;
710}
711
712/*
713 * Insert typval_T "tv" in list "l" before "item".
714 * If "item" is NULL append at the end.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100715 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200716 */
717 int
718list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
719{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100720 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200721
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100722 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200723 && check_typval_arg_type(l->lv_type->tt_member, tv,
724 NULL, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100725 return FAIL;
726 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200727 if (ni == NULL)
728 return FAIL;
729 copy_tv(tv, &ni->li_tv);
730 list_insert(l, ni, item);
731 return OK;
732}
733
734 void
735list_insert(list_T *l, listitem_T *ni, listitem_T *item)
736{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200737 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200738 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100739 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200740 list_append(l, ni);
741 else
742 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100743 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200744 ni->li_prev = item->li_prev;
745 ni->li_next = item;
746 if (item->li_prev == NULL)
747 {
748 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100749 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200750 }
751 else
752 {
753 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100754 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200755 }
756 item->li_prev = ni;
757 ++l->lv_len;
758 }
759}
760
761/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200762 * Get the list item in "l" with index "n1". "n1" is adjusted if needed.
Bram Moolenaar22ebd172022-04-01 15:26:58 +0100763 * In Vim9, it is at the end of the list, add an item if "can_append" is TRUE.
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200764 * Return NULL if there is no such item.
765 */
766 listitem_T *
Bram Moolenaar22ebd172022-04-01 15:26:58 +0100767check_range_index_one(list_T *l, long *n1, int can_append, int quiet)
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200768{
Bram Moolenaar22ebd172022-04-01 15:26:58 +0100769 long orig_n1 = *n1;
770 listitem_T *li = list_find_index(l, n1);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200771
772 if (li == NULL)
773 {
774 // Vim9: Allow for adding an item at the end.
Bram Moolenaar22ebd172022-04-01 15:26:58 +0100775 if (can_append && in_vim9script()
776 && *n1 == l->lv_len && l->lv_lock == 0)
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200777 {
778 list_append_number(l, 0);
779 li = list_find_index(l, n1);
780 }
781 if (li == NULL)
782 {
783 if (!quiet)
Bram Moolenaar22ebd172022-04-01 15:26:58 +0100784 semsg(_(e_list_index_out_of_range_nr), orig_n1);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200785 return NULL;
786 }
787 }
788 return li;
789}
790
791/*
792 * Check that "n2" can be used as the second index in a range of list "l".
793 * If "n1" or "n2" is negative it is changed to the positive index.
794 * "li1" is the item for item "n1".
795 * Return OK or FAIL.
796 */
797 int
798check_range_index_two(
799 list_T *l,
800 long *n1,
801 listitem_T *li1,
802 long *n2,
803 int quiet)
804{
805 if (*n2 < 0)
806 {
807 listitem_T *ni = list_find(l, *n2);
808
809 if (ni == NULL)
810 {
811 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000812 semsg(_(e_list_index_out_of_range_nr), *n2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200813 return FAIL;
814 }
815 *n2 = list_idx_of_item(l, ni);
816 }
817
818 // Check that n2 isn't before n1.
819 if (*n1 < 0)
820 *n1 = list_idx_of_item(l, li1);
821 if (*n2 < *n1)
822 {
823 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000824 semsg(_(e_list_index_out_of_range_nr), *n2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200825 return FAIL;
826 }
827 return OK;
828}
829
830/*
831 * Assign values from list "src" into a range of "dest".
832 * "idx1_arg" is the index of the first item in "dest" to be replaced.
833 * "idx2" is the index of last item to be replaced, but when "empty_idx2" is
834 * TRUE then replace all items after "idx1".
835 * "op" is the operator, normally "=" but can be "+=" and the like.
836 * "varname" is used for error messages.
837 * Returns OK or FAIL.
838 */
839 int
840list_assign_range(
841 list_T *dest,
842 list_T *src,
843 long idx1_arg,
844 long idx2,
845 int empty_idx2,
846 char_u *op,
847 char_u *varname)
848{
849 listitem_T *src_li;
850 listitem_T *dest_li;
851 long idx1 = idx1_arg;
852 listitem_T *first_li = list_find_index(dest, &idx1);
853 long idx;
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200854 type_T *member_type = NULL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200855
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000856 // Check whether any of the list items is locked before making any changes.
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200857 idx = idx1;
858 dest_li = first_li;
859 for (src_li = src->lv_first; src_li != NULL && dest_li != NULL; )
860 {
861 if (value_check_lock(dest_li->li_tv.v_lock, varname, FALSE))
862 return FAIL;
863 src_li = src_li->li_next;
864 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
865 break;
866 dest_li = dest_li->li_next;
867 ++idx;
868 }
869
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200870 if (in_vim9script() && dest->lv_type != NULL
871 && dest->lv_type->tt_member != NULL)
872 member_type = dest->lv_type->tt_member;
873
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000874 // Assign the List values to the list items.
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200875 idx = idx1;
876 dest_li = first_li;
877 for (src_li = src->lv_first; src_li != NULL; )
878 {
879 if (op != NULL && *op != '=')
880 tv_op(&dest_li->li_tv, &src_li->li_tv, op);
881 else
882 {
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200883 if (member_type != NULL
884 && check_typval_arg_type(member_type, &src_li->li_tv,
885 NULL, 0) == FAIL)
886 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200887 clear_tv(&dest_li->li_tv);
888 copy_tv(&src_li->li_tv, &dest_li->li_tv);
889 }
890 src_li = src_li->li_next;
891 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
892 break;
893 if (dest_li->li_next == NULL)
894 {
895 // Need to add an empty item.
896 if (list_append_number(dest, 0) == FAIL)
897 {
898 src_li = NULL;
899 break;
900 }
901 }
902 dest_li = dest_li->li_next;
903 ++idx;
904 }
905 if (src_li != NULL)
906 {
907 emsg(_(e_list_value_has_more_items_than_targets));
908 return FAIL;
909 }
910 if (empty_idx2
911 ? (dest_li != NULL && dest_li->li_next != NULL)
912 : idx != idx2)
913 {
914 emsg(_(e_list_value_does_not_have_enough_items));
915 return FAIL;
916 }
917 return OK;
918}
919
920/*
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000921 * Flatten up to "maxitems" in "list", starting at "first" to depth "maxdepth".
922 * When "first" is NULL use the first item.
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200923 * It does nothing if "maxdepth" is 0.
924 * Returns FAIL when out of memory.
925 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100926 static void
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000927list_flatten(list_T *list, listitem_T *first, long maxitems, long maxdepth)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200928{
929 listitem_T *item;
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000930 int done = 0;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200931
932 if (maxdepth == 0)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100933 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200934 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000935 if (first == NULL)
936 item = list->lv_first;
937 else
938 item = first;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200939
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000940 while (item != NULL && done < maxitems)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200941 {
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000942 listitem_T *next = item->li_next;
943
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200944 fast_breakcheck();
945 if (got_int)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100946 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200947
948 if (item->li_tv.v_type == VAR_LIST)
949 {
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000950 list_T *itemlist = item->li_tv.vval.v_list;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200951
952 vimlist_remove(list, item, item);
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000953 if (list_extend(list, itemlist, next) == FAIL)
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +0200954 {
955 list_free_item(list, item);
Bram Moolenaar3b690062021-02-01 20:14:51 +0100956 return;
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +0200957 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200958
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000959 if (maxdepth > 0)
960 list_flatten(list, item->li_prev == NULL
961 ? list->lv_first : item->li_prev->li_next,
962 itemlist->lv_len, maxdepth - 1);
Bram Moolenaarf3980dc2022-03-26 16:42:23 +0000963 clear_tv(&item->li_tv);
Bram Moolenaarc6c1ec42022-03-26 10:50:11 +0000964 list_free_item(list, item);
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000965 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200966
Bram Moolenaaracf7d732022-03-25 19:50:57 +0000967 ++done;
968 item = next;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200969 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200970}
971
972/*
Bram Moolenaar3b690062021-02-01 20:14:51 +0100973 * "flatten()" and "flattennew()" functions
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200974 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100975 static void
976flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200977{
978 list_T *l;
979 long maxdepth;
980 int error = FALSE;
981
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200982 if (in_vim9script()
983 && (check_for_list_arg(argvars, 0) == FAIL
984 || check_for_opt_number_arg(argvars, 1) == FAIL))
985 return;
986
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200987 if (argvars[0].v_type != VAR_LIST)
988 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +0000989 semsg(_(e_argument_of_str_must_be_list), "flatten()");
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200990 return;
991 }
992
993 if (argvars[1].v_type == VAR_UNKNOWN)
994 maxdepth = 999999;
995 else
996 {
997 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
998 if (error)
999 return;
1000 if (maxdepth < 0)
1001 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001002 emsg(_(e_maxdepth_must_be_non_negative_number));
Bram Moolenaar077a1e62020-06-08 20:50:43 +02001003 return;
1004 }
1005 }
1006
1007 l = argvars[0].vval.v_list;
Bram Moolenaar3b690062021-02-01 20:14:51 +01001008 rettv->v_type = VAR_LIST;
1009 rettv->vval.v_list = l;
1010 if (l == NULL)
1011 return;
1012
1013 if (make_copy)
1014 {
Bram Moolenaarc6c1ec42022-03-26 10:50:11 +00001015 l = list_copy(l, FALSE, TRUE, get_copyID());
Bram Moolenaar3b690062021-02-01 20:14:51 +01001016 rettv->vval.v_list = l;
1017 if (l == NULL)
1018 return;
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +02001019 // The type will change.
1020 free_type(l->lv_type);
1021 l->lv_type = NULL;
Bram Moolenaar3b690062021-02-01 20:14:51 +01001022 }
1023 else
1024 {
1025 if (value_check_lock(l->lv_lock,
1026 (char_u *)N_("flatten() argument"), TRUE))
1027 return;
1028 ++l->lv_refcount;
1029 }
1030
Bram Moolenaaracf7d732022-03-25 19:50:57 +00001031 list_flatten(l, NULL, l->lv_len, maxdepth);
Bram Moolenaar3b690062021-02-01 20:14:51 +01001032}
1033
1034/*
1035 * "flatten(list[, {maxdepth}])" function
1036 */
1037 void
1038f_flatten(typval_T *argvars, typval_T *rettv)
1039{
1040 if (in_vim9script())
1041 emsg(_(e_cannot_use_flatten_in_vim9_script));
1042 else
1043 flatten_common(argvars, rettv, FALSE);
1044}
1045
1046/*
1047 * "flattennew(list[, {maxdepth}])" function
1048 */
1049 void
1050f_flattennew(typval_T *argvars, typval_T *rettv)
1051{
1052 flatten_common(argvars, rettv, TRUE);
Bram Moolenaar077a1e62020-06-08 20:50:43 +02001053}
1054
1055/*
Bram Moolenaar976f8592022-08-30 14:34:52 +01001056 * "items(list)" function
1057 * Caller must have already checked that argvars[0] is a List.
1058 */
1059 void
1060list2items(typval_T *argvars, typval_T *rettv)
1061{
1062 list_T *l = argvars[0].vval.v_list;
1063 listitem_T *li;
1064 varnumber_T idx;
1065
1066 if (rettv_list_alloc(rettv) == FAIL)
1067 return;
Bram Moolenaar976f8592022-08-30 14:34:52 +01001068 if (l == NULL)
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001069 return; // null list behaves like an empty list
Bram Moolenaar976f8592022-08-30 14:34:52 +01001070
1071 // TODO: would be more efficient to not materialize the argument
1072 CHECK_LIST_MATERIALIZE(l);
1073 for (idx = 0, li = l->lv_first; li != NULL; li = li->li_next, ++idx)
1074 {
1075 list_T *l2 = list_alloc();
1076
1077 if (l2 == NULL)
1078 break;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01001079 if (list_append_list(rettv->vval.v_list, l2) == FAIL)
1080 {
1081 vim_free(l2);
1082 break;
1083 }
1084 if (list_append_number(l2, idx) == FAIL
Bram Moolenaar976f8592022-08-30 14:34:52 +01001085 || list_append_tv(l2, &li->li_tv) == FAIL)
1086 break;
1087 }
1088}
1089
1090/*
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001091 * "items(string)" function
1092 * Caller must have already checked that argvars[0] is a String.
1093 */
1094 void
1095string2items(typval_T *argvars, typval_T *rettv)
1096{
1097 char_u *p = argvars[0].vval.v_string;
1098 varnumber_T idx;
1099
1100 if (rettv_list_alloc(rettv) == FAIL)
1101 return;
1102 if (p == NULL)
1103 return; // null string behaves like an empty string
1104
1105 for (idx = 0; *p != NUL; ++idx)
1106 {
1107 int len = mb_ptr2len(p);
1108 list_T *l2;
1109
1110 if (len == 0)
1111 break;
1112 l2 = list_alloc();
1113 if (l2 == NULL)
1114 break;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01001115 if (list_append_list(rettv->vval.v_list, l2) == FAIL)
1116 {
1117 vim_free(l2);
1118 break;
1119 }
1120 if (list_append_number(l2, idx) == FAIL
Bram Moolenaar3e518a82022-08-30 17:45:33 +01001121 || list_append_string(l2, p, len) == FAIL)
1122 break;
1123 p += len;
1124 }
1125}
1126
1127/*
Bram Moolenaar1a739232020-10-10 15:37:58 +02001128 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001129 * If "bef" is NULL append at the end, otherwise insert before this item.
1130 * Returns FAIL when out of memory.
1131 */
1132 int
1133list_extend(list_T *l1, list_T *l2, listitem_T *bef)
1134{
1135 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001136 int todo;
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001137 listitem_T *bef_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001138
Bram Moolenaar1a739232020-10-10 15:37:58 +02001139 // NULL list is equivalent to an empty list: nothing to do.
1140 if (l2 == NULL || l2->lv_len == 0)
1141 return OK;
1142
1143 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001144 CHECK_LIST_MATERIALIZE(l1);
1145 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001147 // When exending a list with itself, at some point we run into the item
1148 // that was before "bef" and need to skip over the already inserted items
1149 // to "bef".
1150 bef_prev = bef == NULL ? NULL : bef->li_prev;
1151
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001152 // We also quit the loop when we have inserted the original item count of
1153 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001154 for (item = l2->lv_first; item != NULL && --todo >= 0;
1155 item = item == bef_prev ? bef : item->li_next)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001156 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
1157 return FAIL;
1158 return OK;
1159}
1160
1161/*
1162 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
1163 * Return FAIL when out of memory.
1164 */
1165 int
1166list_concat(list_T *l1, list_T *l2, typval_T *tv)
1167{
1168 list_T *l;
1169
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001170 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +02001171 if (l1 == NULL)
1172 l = list_alloc();
1173 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00001174 l = list_copy(l1, FALSE, TRUE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001175 if (l == NULL)
1176 return FAIL;
1177 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +01001178 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001179 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001180 if (l1 == NULL)
1181 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001182
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001183 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +02001184 return list_extend(l, l2, NULL);
1185}
1186
Bram Moolenaar9af78762020-06-16 11:34:42 +02001187 list_T *
1188list_slice(list_T *ol, long n1, long n2)
1189{
1190 listitem_T *item;
1191 list_T *l = list_alloc();
1192
1193 if (l == NULL)
1194 return NULL;
1195 for (item = list_find(ol, n1); n1 <= n2; ++n1)
1196 {
1197 if (list_append_tv(l, &item->li_tv) == FAIL)
1198 {
1199 list_free(l);
1200 return NULL;
1201 }
1202 item = item->li_next;
1203 }
1204 return l;
1205}
1206
Bram Moolenaared591872020-08-15 22:14:53 +02001207 int
1208list_slice_or_index(
1209 list_T *list,
1210 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01001211 varnumber_T n1_arg,
1212 varnumber_T n2_arg,
1213 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +02001214 typval_T *rettv,
1215 int verbose)
1216{
1217 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001218 varnumber_T n1 = n1_arg;
1219 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +02001220 typval_T var1;
1221
1222 if (n1 < 0)
1223 n1 = len + n1;
1224 if (n1 < 0 || n1 >= len)
1225 {
Bram Moolenaar92f05f22021-08-12 21:12:56 +02001226 // For a range we allow invalid values and for legacy script return an
1227 // empty list, for Vim9 script start at the first item.
1228 // A list index out of range is an error.
Bram Moolenaared591872020-08-15 22:14:53 +02001229 if (!range)
1230 {
1231 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001232 semsg(_(e_list_index_out_of_range_nr), (long)n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +02001233 return FAIL;
1234 }
Bram Moolenaar92f05f22021-08-12 21:12:56 +02001235 if (in_vim9script())
1236 n1 = n1 < 0 ? 0 : len;
1237 else
1238 n1 = len;
Bram Moolenaared591872020-08-15 22:14:53 +02001239 }
1240 if (range)
1241 {
1242 list_T *l;
1243
1244 if (n2 < 0)
1245 n2 = len + n2;
1246 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01001247 n2 = len - (exclusive ? 0 : 1);
1248 if (exclusive)
1249 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +02001250 if (n2 < 0 || n2 + 1 < n1)
1251 n2 = -1;
1252 l = list_slice(list, n1, n2);
1253 if (l == NULL)
1254 return FAIL;
1255 clear_tv(rettv);
1256 rettv_list_set(rettv, l);
1257 }
1258 else
1259 {
1260 // copy the item to "var1" to avoid that freeing the list makes it
1261 // invalid.
1262 copy_tv(&list_find(list, n1)->li_tv, &var1);
1263 clear_tv(rettv);
1264 *rettv = var1;
1265 }
1266 return OK;
1267}
1268
Bram Moolenaarda861d62016-07-17 15:46:27 +02001269/*
1270 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1271 * The refcount of the new list is set to 1.
Bram Moolenaar381692b2022-02-02 20:01:27 +00001272 * See item_copy() for "top" and "copyID".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001273 * Returns NULL when out of memory.
1274 */
1275 list_T *
Bram Moolenaar381692b2022-02-02 20:01:27 +00001276list_copy(list_T *orig, int deep, int top, int copyID)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001277{
1278 list_T *copy;
1279 listitem_T *item;
1280 listitem_T *ni;
1281
1282 if (orig == NULL)
1283 return NULL;
1284
1285 copy = list_alloc();
1286 if (copy != NULL)
1287 {
Bram Moolenaar46950b22022-02-04 11:36:51 +00001288 if (orig->lv_type == NULL || top || deep)
Bram Moolenaar7676c152022-02-03 21:47:34 +00001289 copy->lv_type = NULL;
1290 else
Bram Moolenaar46950b22022-02-04 11:36:51 +00001291 copy->lv_type = alloc_type(orig->lv_type);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001292 if (copyID != 0)
1293 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001294 // Do this before adding the items, because one of the items may
1295 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001296 orig->lv_copyID = copyID;
1297 orig->lv_copylist = copy;
1298 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001299 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001300 for (item = orig->lv_first; item != NULL && !got_int;
1301 item = item->li_next)
1302 {
1303 ni = listitem_alloc();
1304 if (ni == NULL)
1305 break;
1306 if (deep)
1307 {
Bram Moolenaar381692b2022-02-02 20:01:27 +00001308 if (item_copy(&item->li_tv, &ni->li_tv,
1309 deep, FALSE, copyID) == FAIL)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001310 {
1311 vim_free(ni);
1312 break;
1313 }
1314 }
1315 else
1316 copy_tv(&item->li_tv, &ni->li_tv);
1317 list_append(copy, ni);
1318 }
1319 ++copy->lv_refcount;
1320 if (item != NULL)
1321 {
1322 list_unref(copy);
1323 copy = NULL;
1324 }
1325 }
1326
1327 return copy;
1328}
1329
1330/*
1331 * Remove items "item" to "item2" from list "l".
1332 * Does not free the listitem or the value!
1333 * This used to be called list_remove, but that conflicts with a Sun header
1334 * file.
1335 */
1336 void
1337vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1338{
1339 listitem_T *ip;
1340
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001341 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001343 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001344 for (ip = item; ip != NULL; ip = ip->li_next)
1345 {
1346 --l->lv_len;
1347 list_fix_watch(l, ip);
1348 if (ip == item2)
1349 break;
1350 }
1351
1352 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001353 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001354 else
1355 item2->li_next->li_prev = item->li_prev;
1356 if (item->li_prev == NULL)
1357 l->lv_first = item2->li_next;
1358 else
1359 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001360 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001361}
1362
1363/*
1364 * Return an allocated string with the string representation of a list.
1365 * May return NULL.
1366 */
1367 char_u *
1368list2string(typval_T *tv, int copyID, int restore_copyID)
1369{
1370 garray_T ga;
1371
1372 if (tv->vval.v_list == NULL)
1373 return NULL;
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001374 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001375 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001376 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001377 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1378 FALSE, restore_copyID, copyID) == FAIL)
1379 {
1380 vim_free(ga.ga_data);
1381 return NULL;
1382 }
1383 ga_append(&ga, ']');
1384 ga_append(&ga, NUL);
1385 return (char_u *)ga.ga_data;
1386}
1387
1388typedef struct join_S {
1389 char_u *s;
1390 char_u *tofree;
1391} join_T;
1392
1393 static int
1394list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001395 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001396 list_T *l,
1397 char_u *sep,
1398 int echo_style,
1399 int restore_copyID,
1400 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001401 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001402{
1403 int i;
1404 join_T *p;
1405 int len;
1406 int sumlen = 0;
1407 int first = TRUE;
1408 char_u *tofree;
1409 char_u numbuf[NUMBUFLEN];
1410 listitem_T *item;
1411 char_u *s;
1412
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001413 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001414 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001415 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1416 {
1417 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001418 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001419 if (s == NULL)
1420 return FAIL;
1421
1422 len = (int)STRLEN(s);
1423 sumlen += len;
1424
1425 (void)ga_grow(join_gap, 1);
1426 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1427 if (tofree != NULL || s != numbuf)
1428 {
1429 p->s = s;
1430 p->tofree = tofree;
1431 }
1432 else
1433 {
1434 p->s = vim_strnsave(s, len);
1435 p->tofree = p->s;
1436 }
1437
1438 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001439 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001440 break;
1441 }
1442
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001443 // Allocate result buffer with its total size, avoid re-allocation and
1444 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001445 if (join_gap->ga_len >= 2)
1446 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1447 if (ga_grow(gap, sumlen + 2) == FAIL)
1448 return FAIL;
1449
1450 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1451 {
1452 if (first)
1453 first = FALSE;
1454 else
1455 ga_concat(gap, sep);
1456 p = ((join_T *)join_gap->ga_data) + i;
1457
1458 if (p->s != NULL)
1459 ga_concat(gap, p->s);
1460 line_breakcheck();
1461 }
1462
1463 return OK;
1464}
1465
1466/*
1467 * Join list "l" into a string in "*gap", using separator "sep".
1468 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1469 * Return FAIL or OK.
1470 */
1471 int
1472list_join(
1473 garray_T *gap,
1474 list_T *l,
1475 char_u *sep,
1476 int echo_style,
1477 int restore_copyID,
1478 int copyID)
1479{
1480 garray_T join_ga;
1481 int retval;
1482 join_T *p;
1483 int i;
1484
1485 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001486 return OK; // nothing to do
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001487 ga_init2(&join_ga, sizeof(join_T), l->lv_len);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001488 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1489 copyID, &join_ga);
1490
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001491 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001492 if (join_ga.ga_data != NULL)
1493 {
1494 p = (join_T *)join_ga.ga_data;
1495 for (i = 0; i < join_ga.ga_len; ++i)
1496 {
1497 vim_free(p->tofree);
1498 ++p;
1499 }
1500 ga_clear(&join_ga);
1501 }
1502
1503 return retval;
1504}
1505
1506/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001507 * "join()" function
1508 */
1509 void
1510f_join(typval_T *argvars, typval_T *rettv)
1511{
1512 garray_T ga;
1513 char_u *sep;
1514
Yegappan Lakshmananca195cc2022-06-14 13:42:26 +01001515 rettv->v_type = VAR_STRING;
1516
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001517 if (in_vim9script()
1518 && (check_for_list_arg(argvars, 0) == FAIL
1519 || check_for_opt_string_arg(argvars, 1) == FAIL))
1520 return;
1521
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001522 if (argvars[0].v_type != VAR_LIST)
1523 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001524 emsg(_(e_list_required));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001525 return;
1526 }
Yegappan Lakshmananca195cc2022-06-14 13:42:26 +01001527
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001528 if (argvars[0].vval.v_list == NULL)
1529 return;
Bram Moolenaaref982572021-08-12 19:27:57 +02001530
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001531 if (argvars[1].v_type == VAR_UNKNOWN)
1532 sep = (char_u *)" ";
1533 else
1534 sep = tv_get_string_chk(&argvars[1]);
1535
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001536 if (sep != NULL)
1537 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001538 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001539 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1540 ga_append(&ga, NUL);
1541 rettv->vval.v_string = (char_u *)ga.ga_data;
1542 }
1543 else
1544 rettv->vval.v_string = NULL;
1545}
1546
1547/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001548 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001549 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001550 * Return OK or FAIL.
1551 */
1552 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001553eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001554{
Bram Moolenaar71478202020-06-26 22:46:27 +02001555 int evaluate = evalarg == NULL ? FALSE
1556 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001557 list_T *l = NULL;
1558 typval_T tv;
1559 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001560 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001561 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001562
1563 if (evaluate)
1564 {
1565 l = list_alloc();
1566 if (l == NULL)
1567 return FAIL;
1568 }
1569
Bram Moolenaar962d7212020-07-04 14:15:00 +02001570 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001571 while (**arg != ']' && **arg != NUL)
1572 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001573 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001574 goto failret;
1575 if (evaluate)
1576 {
1577 item = listitem_alloc();
1578 if (item != NULL)
1579 {
1580 item->li_tv = tv;
1581 item->li_tv.v_lock = 0;
1582 list_append(l, item);
1583 }
1584 else
1585 clear_tv(&tv);
1586 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001587 // Legacy Vim script allowed a space before the comma.
1588 if (!vim9script)
1589 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001590
Bram Moolenaare6e03172020-06-27 16:36:05 +02001591 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001592 had_comma = **arg == ',';
1593 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001594 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001595 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001596 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001597 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001598 goto failret;
1599 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001600 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001601 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001602
Bram Moolenaare6b53242020-07-01 17:28:33 +02001603 // The "]" can be on the next line. But a double quoted string may
1604 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001605 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001606 if (**arg == ']')
1607 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001608
1609 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001610 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001612 {
1613 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001614 semsg(_(e_no_white_space_allowed_before_str_str),
1615 ",", *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001616 else
Bram Moolenaara6f79292022-01-04 21:30:47 +00001617 semsg(_(e_missing_comma_in_list_str), *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001618 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001619 goto failret;
1620 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001621 }
1622
1623 if (**arg != ']')
1624 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001625 if (do_error)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001626 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001627failret:
1628 if (evaluate)
1629 list_free(l);
1630 return FAIL;
1631 }
1632
Bram Moolenaar9d489562020-07-30 20:08:50 +02001633 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001634 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001635 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001636
1637 return OK;
1638}
1639
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001640/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001641 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001642 */
1643 int
1644write_list(FILE *fd, list_T *list, int binary)
1645{
1646 listitem_T *li;
1647 int c;
1648 int ret = OK;
1649 char_u *s;
1650
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001651 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001652 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001653 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001654 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001655 {
1656 if (*s == '\n')
1657 c = putc(NUL, fd);
1658 else
1659 c = putc(*s, fd);
1660 if (c == EOF)
1661 {
1662 ret = FAIL;
1663 break;
1664 }
1665 }
1666 if (!binary || li->li_next != NULL)
1667 if (putc('\n', fd) == EOF)
1668 {
1669 ret = FAIL;
1670 break;
1671 }
1672 if (ret == FAIL)
1673 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001674 emsg(_(e_error_while_writing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001675 break;
1676 }
1677 }
1678 return ret;
1679}
1680
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001681/*
1682 * Initialize a static list with 10 items.
1683 */
1684 void
1685init_static_list(staticList10_T *sl)
1686{
1687 list_T *l = &sl->sl_list;
1688 int i;
1689
1690 memset(sl, 0, sizeof(staticList10_T));
1691 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001692 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001693 l->lv_refcount = DO_NOT_FREE_CNT;
1694 l->lv_lock = VAR_FIXED;
1695 sl->sl_list.lv_len = 10;
1696
1697 for (i = 0; i < 10; ++i)
1698 {
1699 listitem_T *li = &sl->sl_items[i];
1700
1701 if (i == 0)
1702 li->li_prev = NULL;
1703 else
1704 li->li_prev = li - 1;
1705 if (i == 9)
1706 li->li_next = NULL;
1707 else
1708 li->li_next = li + 1;
1709 }
1710}
1711
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001712/*
1713 * "list2str()" function
1714 */
1715 void
1716f_list2str(typval_T *argvars, typval_T *rettv)
1717{
1718 list_T *l;
1719 listitem_T *li;
1720 garray_T ga;
1721 int utf8 = FALSE;
1722
1723 rettv->v_type = VAR_STRING;
1724 rettv->vval.v_string = NULL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001725
1726 if (in_vim9script()
1727 && (check_for_list_arg(argvars, 0) == FAIL
1728 || check_for_opt_bool_arg(argvars, 1) == FAIL))
1729 return;
1730
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001731 if (argvars[0].v_type != VAR_LIST)
1732 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001733 emsg(_(e_invalid_argument));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001734 return;
1735 }
1736
1737 l = argvars[0].vval.v_list;
1738 if (l == NULL)
1739 return; // empty list results in empty string
1740
1741 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001742 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001743
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001744 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001745 ga_init2(&ga, 1, 80);
1746 if (has_mbyte || utf8)
1747 {
1748 char_u buf[MB_MAXBYTES + 1];
1749 int (*char2bytes)(int, char_u *);
1750
1751 if (utf8 || enc_utf8)
1752 char2bytes = utf_char2bytes;
1753 else
1754 char2bytes = mb_char2bytes;
1755
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001756 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001757 {
1758 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1759 ga_concat(&ga, buf);
1760 }
1761 ga_append(&ga, NUL);
1762 }
1763 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1764 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001765 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001766 ga_append(&ga, tv_get_number(&li->li_tv));
1767 ga_append(&ga, NUL);
1768 }
1769
1770 rettv->v_type = VAR_STRING;
1771 rettv->vval.v_string = ga.ga_data;
1772}
1773
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001774/*
1775 * Remove item argvars[1] from List argvars[0]. If argvars[2] is supplied, then
1776 * remove the range of items from argvars[1] to argvars[2] (inclusive).
1777 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001778 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001779list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1780{
1781 list_T *l;
1782 listitem_T *item, *item2;
1783 listitem_T *li;
1784 int error = FALSE;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001785 long idx;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001786 long end;
1787 int cnt = 0;
1788 list_T *rl;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001789
1790 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001791 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001792 return;
1793
1794 idx = (long)tv_get_number_chk(&argvars[1], &error);
1795 if (error)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001796 return; // type error: do nothing, errmsg already given
1797
1798 if ((item = list_find(l, idx)) == NULL)
1799 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001800 semsg(_(e_list_index_out_of_range_nr), idx);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001801 return;
1802 }
1803
1804 if (argvars[2].v_type == VAR_UNKNOWN)
1805 {
1806 // Remove one item, return its value.
1807 vimlist_remove(l, item, item);
1808 *rettv = item->li_tv;
1809 list_free_item(l, item);
1810 return;
1811 }
1812
1813 // Remove range of items, return list with values.
1814 end = (long)tv_get_number_chk(&argvars[2], &error);
1815 if (error)
1816 return; // type error: do nothing
1817
1818 if ((item2 = list_find(l, end)) == NULL)
1819 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001820 semsg(_(e_list_index_out_of_range_nr), end);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001821 return;
1822 }
1823
1824 for (li = item; li != NULL; li = li->li_next)
1825 {
1826 ++cnt;
1827 if (li == item2)
1828 break;
1829 }
1830 if (li == NULL) // didn't find "item2" after "item"
1831 {
1832 emsg(_(e_invalid_range));
1833 return;
1834 }
1835
1836 vimlist_remove(l, item, item2);
Bram Moolenaar93a10962022-06-16 11:42:09 +01001837 if (rettv_list_alloc(rettv) == FAIL)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001838 return;
1839
1840 rl = rettv->vval.v_list;
1841
1842 if (l->lv_with_items > 0)
1843 {
1844 // need to copy the list items and move the value
1845 while (item != NULL)
1846 {
1847 li = listitem_alloc();
1848 if (li == NULL)
1849 return;
1850 li->li_tv = item->li_tv;
1851 init_tv(&item->li_tv);
1852 list_append(rl, li);
1853 if (item == item2)
1854 break;
1855 item = item->li_next;
1856 }
1857 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001858 else
1859 {
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001860 rl->lv_first = item;
1861 rl->lv_u.mat.lv_last = item2;
1862 item->li_prev = NULL;
1863 item2->li_next = NULL;
1864 rl->lv_len = cnt;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001865 }
1866}
1867
1868static int item_compare(const void *s1, const void *s2);
1869static int item_compare2(const void *s1, const void *s2);
1870
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001871// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001872typedef struct
1873{
1874 listitem_T *item;
1875 int idx;
1876} sortItem_T;
1877
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001878// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001879typedef struct
1880{
1881 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001882 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001883 int item_compare_numeric;
1884 int item_compare_numbers;
1885#ifdef FEAT_FLOAT
1886 int item_compare_float;
1887#endif
1888 char_u *item_compare_func;
1889 partial_T *item_compare_partial;
1890 dict_T *item_compare_selfdict;
1891 int item_compare_func_err;
1892 int item_compare_keep_zero;
1893} sortinfo_T;
1894static sortinfo_T *sortinfo = NULL;
1895#define ITEM_COMPARE_FAIL 999
1896
1897/*
1898 * Compare functions for f_sort() and f_uniq() below.
1899 */
1900 static int
1901item_compare(const void *s1, const void *s2)
1902{
1903 sortItem_T *si1, *si2;
1904 typval_T *tv1, *tv2;
1905 char_u *p1, *p2;
1906 char_u *tofree1 = NULL, *tofree2 = NULL;
1907 int res;
1908 char_u numbuf1[NUMBUFLEN];
1909 char_u numbuf2[NUMBUFLEN];
1910
1911 si1 = (sortItem_T *)s1;
1912 si2 = (sortItem_T *)s2;
1913 tv1 = &si1->item->li_tv;
1914 tv2 = &si2->item->li_tv;
1915
1916 if (sortinfo->item_compare_numbers)
1917 {
1918 varnumber_T v1 = tv_get_number(tv1);
1919 varnumber_T v2 = tv_get_number(tv2);
1920
1921 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1922 }
1923
1924#ifdef FEAT_FLOAT
1925 if (sortinfo->item_compare_float)
1926 {
1927 float_T v1 = tv_get_float(tv1);
1928 float_T v2 = tv_get_float(tv2);
1929
1930 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1931 }
1932#endif
1933
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001934 // tv2string() puts quotes around a string and allocates memory. Don't do
1935 // that for string variables. Use a single quote when comparing with a
1936 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001937 if (tv1->v_type == VAR_STRING)
1938 {
1939 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1940 p1 = (char_u *)"'";
1941 else
1942 p1 = tv1->vval.v_string;
1943 }
1944 else
1945 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1946 if (tv2->v_type == VAR_STRING)
1947 {
1948 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1949 p2 = (char_u *)"'";
1950 else
1951 p2 = tv2->vval.v_string;
1952 }
1953 else
1954 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1955 if (p1 == NULL)
1956 p1 = (char_u *)"";
1957 if (p2 == NULL)
1958 p2 = (char_u *)"";
1959 if (!sortinfo->item_compare_numeric)
1960 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001961 if (sortinfo->item_compare_lc)
1962 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001963 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001964 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001965 }
1966 else
1967 {
1968 double n1, n2;
1969 n1 = strtod((char *)p1, (char **)&p1);
1970 n2 = strtod((char *)p2, (char **)&p2);
1971 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1972 }
1973
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001974 // When the result would be zero, compare the item indexes. Makes the
1975 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001976 if (res == 0 && !sortinfo->item_compare_keep_zero)
1977 res = si1->idx > si2->idx ? 1 : -1;
1978
1979 vim_free(tofree1);
1980 vim_free(tofree2);
1981 return res;
1982}
1983
1984 static int
1985item_compare2(const void *s1, const void *s2)
1986{
1987 sortItem_T *si1, *si2;
1988 int res;
1989 typval_T rettv;
1990 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001991 char_u *func_name;
1992 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001993 funcexe_T funcexe;
Bram Moolenaar23018f22021-12-27 11:54:37 +00001994 int did_emsg_before = did_emsg;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001995
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001996 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001997 if (sortinfo->item_compare_func_err)
1998 return 0;
1999
2000 si1 = (sortItem_T *)s1;
2001 si2 = (sortItem_T *)s2;
2002
2003 if (partial == NULL)
2004 func_name = sortinfo->item_compare_func;
2005 else
2006 func_name = partial_name(partial);
2007
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002008 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
2009 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002010 copy_tv(&si1->item->li_tv, &argv[0]);
2011 copy_tv(&si2->item->li_tv, &argv[1]);
2012
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002013 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02002014 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002015 funcexe.fe_evaluate = TRUE;
2016 funcexe.fe_partial = partial;
2017 funcexe.fe_selfdict = sortinfo->item_compare_selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002018 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002019 clear_tv(&argv[0]);
2020 clear_tv(&argv[1]);
2021
Bram Moolenaar23018f22021-12-27 11:54:37 +00002022 if (res == FAIL || did_emsg > did_emsg_before)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002023 res = ITEM_COMPARE_FAIL;
2024 else
Yasuhiro Matsumotoc04f6232021-09-19 17:01:39 +02002025 {
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002026 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Yasuhiro Matsumotoc04f6232021-09-19 17:01:39 +02002027 if (res > 0)
2028 res = 1;
2029 else if (res < 0)
2030 res = -1;
2031 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002032 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002033 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002034 clear_tv(&rettv);
2035
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002036 // When the result would be zero, compare the pointers themselves. Makes
2037 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002038 if (res == 0 && !sortinfo->item_compare_keep_zero)
2039 res = si1->idx > si2->idx ? 1 : -1;
2040
2041 return res;
2042}
2043
2044/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002045 * sort() List "l"
2046 */
2047 static void
2048do_sort(list_T *l, sortinfo_T *info)
2049{
2050 long len;
2051 sortItem_T *ptrs;
2052 long i = 0;
2053 listitem_T *li;
2054
2055 len = list_len(l);
2056
2057 // Make an array with each entry pointing to an item in the List.
2058 ptrs = ALLOC_MULT(sortItem_T, len);
2059 if (ptrs == NULL)
2060 return;
2061
2062 // sort(): ptrs will be the list to sort
2063 FOR_ALL_LIST_ITEMS(l, li)
2064 {
2065 ptrs[i].item = li;
2066 ptrs[i].idx = i;
2067 ++i;
2068 }
2069
2070 info->item_compare_func_err = FALSE;
2071 info->item_compare_keep_zero = FALSE;
2072 // test the compare function
2073 if ((info->item_compare_func != NULL
2074 || info->item_compare_partial != NULL)
2075 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
2076 == ITEM_COMPARE_FAIL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002077 emsg(_(e_sort_compare_function_failed));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002078 else
2079 {
2080 // Sort the array with item pointers.
2081 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
2082 info->item_compare_func == NULL
2083 && info->item_compare_partial == NULL
2084 ? item_compare : item_compare2);
2085
2086 if (!info->item_compare_func_err)
2087 {
2088 // Clear the List and append the items in sorted order.
2089 l->lv_first = l->lv_u.mat.lv_last
2090 = l->lv_u.mat.lv_idx_item = NULL;
2091 l->lv_len = 0;
2092 for (i = 0; i < len; ++i)
2093 list_append(l, ptrs[i].item);
2094 }
2095 }
2096
2097 vim_free(ptrs);
2098}
2099
2100/*
2101 * uniq() List "l"
2102 */
2103 static void
2104do_uniq(list_T *l, sortinfo_T *info)
2105{
2106 long len;
2107 sortItem_T *ptrs;
2108 long i = 0;
2109 listitem_T *li;
2110 int (*item_compare_func_ptr)(const void *, const void *);
2111
2112 len = list_len(l);
2113
2114 // Make an array with each entry pointing to an item in the List.
2115 ptrs = ALLOC_MULT(sortItem_T, len);
2116 if (ptrs == NULL)
2117 return;
2118
2119 // f_uniq(): ptrs will be a stack of items to remove
2120 info->item_compare_func_err = FALSE;
2121 info->item_compare_keep_zero = TRUE;
2122 item_compare_func_ptr = info->item_compare_func != NULL
2123 || info->item_compare_partial != NULL
2124 ? item_compare2 : item_compare;
2125
2126 for (li = l->lv_first; li != NULL && li->li_next != NULL;
2127 li = li->li_next)
2128 {
2129 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
2130 == 0)
2131 ptrs[i++].item = li;
2132 if (info->item_compare_func_err)
2133 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002134 emsg(_(e_uniq_compare_function_failed));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002135 break;
2136 }
2137 }
2138
2139 if (!info->item_compare_func_err)
2140 {
2141 while (--i >= 0)
2142 {
2143 li = ptrs[i].item->li_next;
2144 ptrs[i].item->li_next = li->li_next;
2145 if (li->li_next != NULL)
2146 li->li_next->li_prev = ptrs[i].item;
2147 else
2148 l->lv_u.mat.lv_last = ptrs[i].item;
2149 list_fix_watch(l, li);
2150 listitem_free(l, li);
2151 l->lv_len--;
2152 }
2153 }
2154
2155 vim_free(ptrs);
2156}
2157
2158/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002159 * Parse the optional arguments supplied to the sort() or uniq() function and
2160 * return the values in "info".
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002161 */
2162 static int
2163parse_sort_uniq_args(typval_T *argvars, sortinfo_T *info)
2164{
2165 info->item_compare_ic = FALSE;
2166 info->item_compare_lc = FALSE;
2167 info->item_compare_numeric = FALSE;
2168 info->item_compare_numbers = FALSE;
2169#ifdef FEAT_FLOAT
2170 info->item_compare_float = FALSE;
2171#endif
2172 info->item_compare_func = NULL;
2173 info->item_compare_partial = NULL;
2174 info->item_compare_selfdict = NULL;
2175
2176 if (argvars[1].v_type == VAR_UNKNOWN)
2177 return OK;
2178
2179 // optional second argument: {func}
2180 if (argvars[1].v_type == VAR_FUNC)
2181 info->item_compare_func = argvars[1].vval.v_string;
2182 else if (argvars[1].v_type == VAR_PARTIAL)
2183 info->item_compare_partial = argvars[1].vval.v_partial;
2184 else
2185 {
2186 int error = FALSE;
2187 int nr = 0;
2188
2189 if (argvars[1].v_type == VAR_NUMBER)
2190 {
2191 nr = tv_get_number_chk(&argvars[1], &error);
2192 if (error)
2193 return FAIL;
2194 if (nr == 1)
2195 info->item_compare_ic = TRUE;
2196 }
2197 if (nr != 1)
2198 {
2199 if (argvars[1].v_type != VAR_NUMBER)
2200 info->item_compare_func = tv_get_string(&argvars[1]);
2201 else if (nr != 0)
2202 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002203 emsg(_(e_invalid_argument));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002204 return FAIL;
2205 }
2206 }
2207 if (info->item_compare_func != NULL)
2208 {
2209 if (*info->item_compare_func == NUL)
2210 {
2211 // empty string means default sort
2212 info->item_compare_func = NULL;
2213 }
2214 else if (STRCMP(info->item_compare_func, "n") == 0)
2215 {
2216 info->item_compare_func = NULL;
2217 info->item_compare_numeric = TRUE;
2218 }
2219 else if (STRCMP(info->item_compare_func, "N") == 0)
2220 {
2221 info->item_compare_func = NULL;
2222 info->item_compare_numbers = TRUE;
2223 }
2224#ifdef FEAT_FLOAT
2225 else if (STRCMP(info->item_compare_func, "f") == 0)
2226 {
2227 info->item_compare_func = NULL;
2228 info->item_compare_float = TRUE;
2229 }
2230#endif
2231 else if (STRCMP(info->item_compare_func, "i") == 0)
2232 {
2233 info->item_compare_func = NULL;
2234 info->item_compare_ic = TRUE;
2235 }
2236 else if (STRCMP(info->item_compare_func, "l") == 0)
2237 {
2238 info->item_compare_func = NULL;
2239 info->item_compare_lc = TRUE;
2240 }
2241 }
2242 }
2243
2244 if (argvars[2].v_type != VAR_UNKNOWN)
2245 {
2246 // optional third argument: {dict}
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002247 if (check_for_dict_arg(argvars, 2) == FAIL)
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002248 return FAIL;
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002249 info->item_compare_selfdict = argvars[2].vval.v_dict;
2250 }
2251
2252 return OK;
2253}
2254
2255/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002256 * "sort()" or "uniq()" function
2257 */
2258 static void
2259do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
2260{
2261 list_T *l;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002262 sortinfo_T *old_sortinfo;
2263 sortinfo_T info;
2264 long len;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002265
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002266 if (in_vim9script()
2267 && (check_for_list_arg(argvars, 0) == FAIL
2268 || (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar2007dd42022-02-23 13:17:47 +00002269 && (check_for_string_or_func_arg(argvars, 1) == FAIL
2270 || check_for_opt_dict_arg(argvars, 2) == FAIL))))
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002271 return;
2272
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002273 if (argvars[0].v_type != VAR_LIST)
2274 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002275 semsg(_(e_argument_of_str_must_be_list), sort ? "sort()" : "uniq()");
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002276 return;
2277 }
2278
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002279 // Pointer to current info struct used in compare function. Save and
2280 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002281 old_sortinfo = sortinfo;
2282 sortinfo = &info;
2283
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002284 l = argvars[0].vval.v_list;
2285 if (l != NULL && value_check_lock(l->lv_lock,
2286 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
2287 TRUE))
2288 goto theend;
2289 rettv_list_set(rettv, l);
2290 if (l == NULL)
2291 goto theend;
2292 CHECK_LIST_MATERIALIZE(l);
2293
2294 len = list_len(l);
2295 if (len <= 1)
2296 goto theend; // short list sorts pretty quickly
2297
2298 if (parse_sort_uniq_args(argvars, &info) == FAIL)
2299 goto theend;
2300
2301 if (sort)
2302 do_sort(l, &info);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002303 else
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002304 do_uniq(l, &info);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002305
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002306theend:
2307 sortinfo = old_sortinfo;
2308}
2309
2310/*
2311 * "sort({list})" function
2312 */
2313 void
2314f_sort(typval_T *argvars, typval_T *rettv)
2315{
2316 do_sort_uniq(argvars, rettv, TRUE);
2317}
2318
2319/*
2320 * "uniq({list})" function
2321 */
2322 void
2323f_uniq(typval_T *argvars, typval_T *rettv)
2324{
2325 do_sort_uniq(argvars, rettv, FALSE);
2326}
2327
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002328/*
2329 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01002330 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002331 */
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002332 int
Bram Moolenaarea696852020-11-09 18:31:39 +01002333filter_map_one(
2334 typval_T *tv, // original value
2335 typval_T *expr, // callback
2336 filtermap_T filtermap,
2337 typval_T *newtv, // for map() and mapnew(): new value
2338 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002339{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002340 typval_T argv[3];
2341 int retval = FAIL;
2342
2343 copy_tv(tv, get_vim_var_tv(VV_VAL));
2344 argv[0] = *get_vim_var_tv(VV_KEY);
2345 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01002346 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002347 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002348 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002349 {
2350 int error = FALSE;
2351
2352 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02002353 if (in_vim9script())
Bram Moolenaar18024052021-12-25 21:43:28 +00002354 *remp = !tv_get_bool_chk(newtv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002355 else
Bram Moolenaarea696852020-11-09 18:31:39 +01002356 *remp = (tv_get_number_chk(newtv, &error) == 0);
2357 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002358 // On type error, nothing has been removed; return FAIL to stop the
2359 // loop. The error message was given by tv_get_number_chk().
2360 if (error)
2361 goto theend;
2362 }
2363 retval = OK;
2364theend:
2365 clear_tv(get_vim_var_tv(VV_VAL));
2366 return retval;
2367}
2368
2369/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002370 * Implementation of map() and filter() for a List. Apply "expr" to every item
2371 * in List "l" and return the result in "rettv".
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002372 */
2373 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002374list_filter_map(
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002375 list_T *l,
2376 filtermap_T filtermap,
2377 type_T *argtype,
2378 char *func_name,
2379 char_u *arg_errmsg,
2380 typval_T *expr,
2381 typval_T *rettv)
2382{
2383 int prev_lock;
2384 list_T *l_ret = NULL;
2385 int idx = 0;
2386 int rem;
2387 listitem_T *li, *nli;
2388
2389 if (filtermap == FILTERMAP_MAPNEW)
2390 {
2391 rettv->v_type = VAR_LIST;
2392 rettv->vval.v_list = NULL;
2393 }
2394 if (l == NULL
2395 || (filtermap == FILTERMAP_FILTER
2396 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
2397 return;
2398
2399 prev_lock = l->lv_lock;
2400
2401 if (filtermap == FILTERMAP_MAPNEW)
2402 {
2403 if (rettv_list_alloc(rettv) == FAIL)
2404 return;
2405 l_ret = rettv->vval.v_list;
2406 }
2407 // set_vim_var_nr() doesn't set the type
2408 set_vim_var_type(VV_KEY, VAR_NUMBER);
2409
2410 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
2411 l->lv_lock = VAR_LOCKED;
2412
2413 if (l->lv_first == &range_list_item)
2414 {
2415 varnumber_T val = l->lv_u.nonmat.lv_start;
2416 int len = l->lv_len;
2417 int stride = l->lv_u.nonmat.lv_stride;
2418
2419 // List from range(): loop over the numbers
2420 if (filtermap != FILTERMAP_MAPNEW)
2421 {
2422 l->lv_first = NULL;
2423 l->lv_u.mat.lv_last = NULL;
2424 l->lv_len = 0;
2425 l->lv_u.mat.lv_idx_item = NULL;
2426 }
2427
2428 for (idx = 0; idx < len; ++idx)
2429 {
2430 typval_T tv;
2431 typval_T newtv;
2432
2433 tv.v_type = VAR_NUMBER;
2434 tv.v_lock = 0;
2435 tv.vval.v_number = val;
2436 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaar35c807d2022-01-27 16:36:29 +00002437 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL)
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002438 break;
2439 if (did_emsg)
2440 {
2441 clear_tv(&newtv);
2442 break;
2443 }
2444 if (filtermap != FILTERMAP_FILTER)
2445 {
2446 if (filtermap == FILTERMAP_MAP && argtype != NULL
2447 && check_typval_arg_type(
2448 argtype->tt_member, &newtv,
2449 func_name, 0) == FAIL)
2450 {
2451 clear_tv(&newtv);
2452 break;
2453 }
2454 // map(), mapnew(): always append the new value to the
2455 // list
2456 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2457 ? l : l_ret, &newtv) == FAIL)
2458 break;
2459 }
2460 else if (!rem)
2461 {
2462 // filter(): append the list item value when not rem
2463 if (list_append_tv_move(l, &tv) == FAIL)
2464 break;
2465 }
2466
2467 val += stride;
2468 }
2469 }
2470 else
2471 {
2472 // Materialized list: loop over the items
2473 for (li = l->lv_first; li != NULL; li = nli)
2474 {
2475 typval_T newtv;
2476
2477 if (filtermap == FILTERMAP_MAP && value_check_lock(
2478 li->li_tv.v_lock, arg_errmsg, TRUE))
2479 break;
2480 nli = li->li_next;
2481 set_vim_var_nr(VV_KEY, idx);
2482 if (filter_map_one(&li->li_tv, expr, filtermap,
2483 &newtv, &rem) == FAIL)
2484 break;
2485 if (did_emsg)
2486 {
2487 clear_tv(&newtv);
2488 break;
2489 }
2490 if (filtermap == FILTERMAP_MAP)
2491 {
2492 if (argtype != NULL && check_typval_arg_type(
2493 argtype->tt_member, &newtv, func_name, 0) == FAIL)
2494 {
2495 clear_tv(&newtv);
2496 break;
2497 }
2498 // map(): replace the list item value
2499 clear_tv(&li->li_tv);
2500 newtv.v_lock = 0;
2501 li->li_tv = newtv;
2502 }
2503 else if (filtermap == FILTERMAP_MAPNEW)
2504 {
2505 // mapnew(): append the list item value
2506 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2507 break;
2508 }
2509 else if (filtermap == FILTERMAP_FILTER && rem)
2510 listitem_remove(l, li);
2511 ++idx;
2512 }
2513 }
2514
2515 l->lv_lock = prev_lock;
2516}
2517
2518/*
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002519 * Implementation of map() and filter().
2520 */
2521 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01002522filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002523{
2524 typval_T *expr;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002525 char *func_name = filtermap == FILTERMAP_MAP ? "map()"
Bram Moolenaarea696852020-11-09 18:31:39 +01002526 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002527 : "filter()";
Bram Moolenaarea696852020-11-09 18:31:39 +01002528 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2529 ? N_("map() argument")
2530 : filtermap == FILTERMAP_MAPNEW
2531 ? N_("mapnew() argument")
2532 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002533 int save_did_emsg;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002534 type_T *type = NULL;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002535
Bram Moolenaarea696852020-11-09 18:31:39 +01002536 // map() and filter() return the first argument, also on failure.
Bram Moolenaar2d877592021-12-16 08:21:09 +00002537 if (filtermap != FILTERMAP_MAPNEW && argvars[0].v_type != VAR_STRING)
Bram Moolenaarea696852020-11-09 18:31:39 +01002538 copy_tv(&argvars[0], rettv);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002539
2540 if (in_vim9script()
Bram Moolenaar2d877592021-12-16 08:21:09 +00002541 && (check_for_list_or_dict_or_blob_or_string_arg(argvars, 0)
2542 == FAIL))
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002543 return;
2544
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002545 if (filtermap == FILTERMAP_MAP && in_vim9script())
2546 {
Bram Moolenaar35c807d2022-01-27 16:36:29 +00002547 // Check that map() does not change the declared type of the list or
2548 // dict.
2549 if (argvars[0].v_type == VAR_DICT && argvars[0].vval.v_dict != NULL)
2550 type = argvars[0].vval.v_dict->dv_type;
2551 else if (argvars[0].v_type == VAR_LIST
2552 && argvars[0].vval.v_list != NULL)
2553 type = argvars[0].vval.v_list->lv_type;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002554 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002555
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002556 if (argvars[0].v_type != VAR_BLOB
2557 && argvars[0].v_type != VAR_LIST
2558 && argvars[0].v_type != VAR_DICT
2559 && argvars[0].v_type != VAR_STRING)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002560 {
rbtnnc479ce02021-12-15 19:14:54 +00002561 semsg(_(e_argument_of_str_must_be_list_string_dictionary_or_blob),
2562 func_name);
Bram Moolenaar98cd3032022-01-27 17:37:41 +00002563 return;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002564 }
2565
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002566 // On type errors, the preceding call has already displayed an error
2567 // message. Avoid a misleading error message for an empty string that
2568 // was not passed as argument.
Bram Moolenaar35c807d2022-01-27 16:36:29 +00002569 expr = &argvars[1];
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002570 if (expr->v_type != VAR_UNKNOWN)
2571 {
2572 typval_T save_val;
2573 typval_T save_key;
2574
2575 prepare_vimvar(VV_VAL, &save_val);
2576 prepare_vimvar(VV_KEY, &save_key);
2577
2578 // We reset "did_emsg" to be able to detect whether an error
2579 // occurred during evaluation of the expression.
2580 save_did_emsg = did_emsg;
2581 did_emsg = FALSE;
2582
2583 if (argvars[0].v_type == VAR_DICT)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002584 dict_filter_map(argvars[0].vval.v_dict, filtermap, type, func_name,
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002585 arg_errmsg, expr, rettv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002586 else if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002587 blob_filter_map(argvars[0].vval.v_blob, filtermap, expr, rettv);
rbtnnc479ce02021-12-15 19:14:54 +00002588 else if (argvars[0].v_type == VAR_STRING)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002589 string_filter_map(tv_get_string(&argvars[0]), filtermap, expr,
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002590 rettv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002591 else // argvars[0].v_type == VAR_LIST
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002592 list_filter_map(argvars[0].vval.v_list, filtermap, type, func_name,
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002593 arg_errmsg, expr, rettv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002594
2595 restore_vimvar(VV_KEY, &save_key);
2596 restore_vimvar(VV_VAL, &save_val);
2597
2598 did_emsg |= save_did_emsg;
2599 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002600}
2601
2602/*
2603 * "filter()" function
2604 */
2605 void
2606f_filter(typval_T *argvars, typval_T *rettv)
2607{
Bram Moolenaarea696852020-11-09 18:31:39 +01002608 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002609}
2610
2611/*
2612 * "map()" function
2613 */
2614 void
2615f_map(typval_T *argvars, typval_T *rettv)
2616{
Bram Moolenaarea696852020-11-09 18:31:39 +01002617 filter_map(argvars, rettv, FILTERMAP_MAP);
2618}
2619
2620/*
2621 * "mapnew()" function
2622 */
2623 void
2624f_mapnew(typval_T *argvars, typval_T *rettv)
2625{
2626 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002627}
2628
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002629/*
2630 * "add(list, item)" function
2631 */
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002632 static void
2633list_add(typval_T *argvars, typval_T *rettv)
2634{
2635 list_T *l = argvars[0].vval.v_list;
2636
2637 if (l == NULL)
2638 {
2639 if (in_vim9script())
2640 emsg(_(e_cannot_add_to_null_list));
2641 }
2642 else if (!value_check_lock(l->lv_lock,
2643 (char_u *)N_("add() argument"), TRUE)
2644 && list_append_tv(l, &argvars[1]) == OK)
2645 {
2646 copy_tv(&argvars[0], rettv);
2647 }
2648}
2649
2650/*
2651 * "add(object, item)" function
2652 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002653 void
2654f_add(typval_T *argvars, typval_T *rettv)
2655{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002656 rettv->vval.v_number = 1; // Default: Failed
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002657
2658 if (in_vim9script()
2659 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2660 || (argvars[0].v_type == VAR_BLOB
2661 && check_for_number_arg(argvars, 1) == FAIL)))
2662 return;
2663
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002664 if (argvars[0].v_type == VAR_LIST)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002665 list_add(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002666 else if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002667 blob_add(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002668 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002669 emsg(_(e_list_or_blob_required));
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002670}
2671
2672/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002673 * Count the number of times item "needle" occurs in List "l" starting at index
2674 * "idx". Case is ignored if "ic" is TRUE.
2675 */
2676 static long
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002677list_count(list_T *l, typval_T *needle, long idx, int ic)
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002678{
2679 long n = 0;
2680 listitem_T *li;
2681
2682 if (l == NULL)
2683 return 0;
2684
2685 CHECK_LIST_MATERIALIZE(l);
2686
2687 if (list_len(l) == 0)
2688 return 0;
2689
2690 li = list_find(l, idx);
2691 if (li == NULL)
2692 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002693 semsg(_(e_list_index_out_of_range_nr), idx);
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002694 return 0;
2695 }
2696
2697 for ( ; li != NULL; li = li->li_next)
2698 if (tv_equal(&li->li_tv, needle, ic, FALSE))
2699 ++n;
2700
2701 return n;
2702}
2703
2704/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002705 * "count()" function
2706 */
2707 void
2708f_count(typval_T *argvars, typval_T *rettv)
2709{
2710 long n = 0;
2711 int ic = FALSE;
2712 int error = FALSE;
2713
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002714 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002715 && (check_for_string_or_list_or_dict_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002716 || check_for_opt_bool_arg(argvars, 2) == FAIL
2717 || (argvars[2].v_type != VAR_UNKNOWN
2718 && check_for_opt_number_arg(argvars, 3) == FAIL)))
2719 return;
2720
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002721 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002722 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002723
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002724 if (!error && argvars[0].v_type == VAR_STRING)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002725 n = string_count(argvars[0].vval.v_string,
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002726 tv_get_string_chk(&argvars[1]), ic);
2727 else if (!error && argvars[0].v_type == VAR_LIST)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002728 {
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002729 long idx = 0;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002730
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002731 if (argvars[2].v_type != VAR_UNKNOWN
2732 && argvars[3].v_type != VAR_UNKNOWN)
2733 idx = (long)tv_get_number_chk(&argvars[3], &error);
2734 if (!error)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002735 n = list_count(argvars[0].vval.v_list, &argvars[1], idx, ic);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002736 }
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002737 else if (!error && argvars[0].v_type == VAR_DICT)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002738 {
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002739 if (argvars[2].v_type != VAR_UNKNOWN
2740 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002741 emsg(_(e_invalid_argument));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002742 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002743 n = dict_count(argvars[0].vval.v_dict, &argvars[1], ic);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002744 }
2745 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002746 semsg(_(e_argument_of_str_must_be_list_or_dictionary), "count()");
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002747 rettv->vval.v_number = n;
2748}
2749
2750/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002751 * extend() a List. Append List argvars[1] to List argvars[0] before index
2752 * argvars[3] and return the resulting list in "rettv". "is_new" is TRUE for
2753 * extendnew().
2754 */
2755 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002756list_extend_func(
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002757 typval_T *argvars,
2758 type_T *type,
2759 char *func_name,
2760 char_u *arg_errmsg,
2761 int is_new,
2762 typval_T *rettv)
2763{
2764 list_T *l1, *l2;
2765 listitem_T *item;
2766 long before;
2767 int error = FALSE;
2768
2769 l1 = argvars[0].vval.v_list;
2770 if (l1 == NULL)
2771 {
2772 emsg(_(e_cannot_extend_null_list));
2773 return;
2774 }
2775 l2 = argvars[1].vval.v_list;
2776 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar381692b2022-02-02 20:01:27 +00002777 && l2 != NULL)
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002778 {
2779 if (is_new)
2780 {
Bram Moolenaar381692b2022-02-02 20:01:27 +00002781 l1 = list_copy(l1, FALSE, TRUE, get_copyID());
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002782 if (l1 == NULL)
2783 return;
2784 }
2785
2786 if (argvars[2].v_type != VAR_UNKNOWN)
2787 {
2788 before = (long)tv_get_number_chk(&argvars[2], &error);
2789 if (error)
2790 return; // type error; errmsg already given
2791
2792 if (before == l1->lv_len)
2793 item = NULL;
2794 else
2795 {
2796 item = list_find(l1, before);
2797 if (item == NULL)
2798 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002799 semsg(_(e_list_index_out_of_range_nr), before);
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002800 return;
2801 }
2802 }
2803 }
2804 else
2805 item = NULL;
2806 if (type != NULL && check_typval_arg_type(
2807 type, &argvars[1], func_name, 2) == FAIL)
2808 return;
2809 list_extend(l1, l2, item);
2810
2811 if (is_new)
2812 {
2813 rettv->v_type = VAR_LIST;
2814 rettv->vval.v_list = l1;
2815 rettv->v_lock = FALSE;
2816 }
2817 else
2818 copy_tv(&argvars[0], rettv);
2819 }
2820}
2821
2822/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002823 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002824 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002825 static void
2826extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002827{
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002828 type_T *type = NULL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002829 char *func_name = is_new ? "extendnew()" : "extend()";
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002830
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002831 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002832 {
2833 // Check that extend() does not change the type of the list if it was
2834 // declared.
2835 if (!is_new && in_vim9script() && argvars[0].vval.v_list != NULL)
2836 type = argvars[0].vval.v_list->lv_type;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002837 list_extend_func(argvars, type, func_name, arg_errmsg, is_new, rettv);
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002838 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002839 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002840 {
2841 // Check that extend() does not change the type of the list if it was
2842 // declared.
2843 if (!is_new && in_vim9script() && argvars[0].vval.v_dict != NULL)
2844 type = argvars[0].vval.v_dict->dv_type;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002845 dict_extend_func(argvars, type, func_name, arg_errmsg, is_new, rettv);
Bram Moolenaarad8f2482022-01-03 16:52:28 +00002846 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002847 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002848 semsg(_(e_argument_of_str_must_be_list_or_dictionary), func_name);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002849}
2850
2851/*
2852 * "extend(list, list [, idx])" function
2853 * "extend(dict, dict [, action])" function
2854 */
2855 void
2856f_extend(typval_T *argvars, typval_T *rettv)
2857{
2858 char_u *errmsg = (char_u *)N_("extend() argument");
2859
2860 extend(argvars, rettv, errmsg, FALSE);
2861}
2862
2863/*
2864 * "extendnew(list, list [, idx])" function
2865 * "extendnew(dict, dict [, action])" function
2866 */
2867 void
2868f_extendnew(typval_T *argvars, typval_T *rettv)
2869{
2870 char_u *errmsg = (char_u *)N_("extendnew() argument");
2871
2872 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002873}
2874
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002875 static void
2876list_insert_func(typval_T *argvars, typval_T *rettv)
2877{
2878 list_T *l = argvars[0].vval.v_list;
2879 long before = 0;
2880 listitem_T *item;
2881 int error = FALSE;
2882
2883 if (l == NULL)
2884 {
2885 if (in_vim9script())
2886 emsg(_(e_cannot_add_to_null_list));
2887 return;
2888 }
2889
2890 if (value_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
2891 return;
2892
2893 if (argvars[2].v_type != VAR_UNKNOWN)
2894 before = (long)tv_get_number_chk(&argvars[2], &error);
2895 if (error)
2896 return; // type error; errmsg already given
2897
2898 if (before == l->lv_len)
2899 item = NULL;
2900 else
2901 {
2902 item = list_find(l, before);
2903 if (item == NULL)
2904 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002905 semsg(_(e_list_index_out_of_range_nr), before);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002906 l = NULL;
2907 }
2908 }
2909 if (l != NULL)
2910 {
2911 (void)list_insert_tv(l, &argvars[1], item);
2912 copy_tv(&argvars[0], rettv);
2913 }
2914}
2915
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002916/*
2917 * "insert()" function
2918 */
2919 void
2920f_insert(typval_T *argvars, typval_T *rettv)
2921{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002922 if (in_vim9script()
2923 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2924 || (argvars[0].v_type == VAR_BLOB
2925 && check_for_number_arg(argvars, 1) == FAIL)
2926 || check_for_opt_number_arg(argvars, 2) == FAIL))
2927 return;
2928
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002929 if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002930 blob_insert_func(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002931 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00002932 semsg(_(e_argument_of_str_must_be_list_or_blob), "insert()");
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002933 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002934 list_insert_func(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002935}
2936
2937/*
2938 * "remove()" function
2939 */
2940 void
2941f_remove(typval_T *argvars, typval_T *rettv)
2942{
2943 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2944
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002945 if (in_vim9script()
2946 && (check_for_list_or_dict_or_blob_arg(argvars, 0) == FAIL
2947 || ((argvars[0].v_type == VAR_LIST
2948 || argvars[0].v_type == VAR_BLOB)
2949 && (check_for_number_arg(argvars, 1) == FAIL
2950 || check_for_opt_number_arg(argvars, 2) == FAIL))
2951 || (argvars[0].v_type == VAR_DICT
2952 && check_for_string_or_number_arg(argvars, 1) == FAIL)))
2953 return;
2954
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002955 if (argvars[0].v_type == VAR_DICT)
2956 dict_remove(argvars, rettv, arg_errmsg);
2957 else if (argvars[0].v_type == VAR_BLOB)
Sean Dewar80d73952021-08-04 19:25:54 +02002958 blob_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002959 else if (argvars[0].v_type == VAR_LIST)
2960 list_remove(argvars, rettv, arg_errmsg);
2961 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002962 semsg(_(e_argument_of_str_must_be_list_dictionary_or_blob), "remove()");
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002963}
2964
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002965 static void
2966list_reverse(list_T *l, typval_T *rettv)
2967{
2968 listitem_T *li, *ni;
2969
2970 rettv_list_set(rettv, l);
2971 if (l != NULL
2972 && !value_check_lock(l->lv_lock,
2973 (char_u *)N_("reverse() argument"), TRUE))
2974 {
2975 if (l->lv_first == &range_list_item)
2976 {
2977 varnumber_T new_start = l->lv_u.nonmat.lv_start
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00002978 + ((varnumber_T)l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002979 l->lv_u.nonmat.lv_end = new_start
2980 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2981 l->lv_u.nonmat.lv_start = new_start;
2982 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
2983 return;
2984 }
2985 li = l->lv_u.mat.lv_last;
2986 l->lv_first = l->lv_u.mat.lv_last = NULL;
2987 l->lv_len = 0;
2988 while (li != NULL)
2989 {
2990 ni = li->li_prev;
2991 list_append(l, li);
2992 li = ni;
2993 }
2994 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
2995 }
2996}
2997
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002998/*
2999 * "reverse({list})" function
3000 */
3001 void
3002f_reverse(typval_T *argvars, typval_T *rettv)
3003{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02003004 if (in_vim9script() && check_for_list_or_blob_arg(argvars, 0) == FAIL)
3005 return;
3006
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003007 if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003008 blob_reverse(argvars[0].vval.v_blob, rettv);
3009 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003010 semsg(_(e_argument_of_str_must_be_list_or_blob), "reverse()");
Bram Moolenaaref982572021-08-12 19:27:57 +02003011 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003012 list_reverse(argvars[0].vval.v_list, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003013}
3014
Bram Moolenaar85629982020-06-01 18:39:20 +02003015/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00003016 * reduce() List argvars[0] using the function 'funcname' with arguments in
3017 * 'funcexe' starting with the initial value argvars[2] and return the result
3018 * in 'rettv'.
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003019 */
3020 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003021list_reduce(
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003022 typval_T *argvars,
3023 char_u *func_name,
3024 funcexe_T *funcexe,
3025 typval_T *rettv)
3026{
3027 list_T *l = argvars[0].vval.v_list;
3028 listitem_T *li = NULL;
3029 typval_T initial;
3030 typval_T argv[3];
3031 int r;
3032 int called_emsg_start = called_emsg;
3033 int prev_locked;
3034
3035 if (l != NULL)
3036 CHECK_LIST_MATERIALIZE(l);
3037 if (argvars[2].v_type == VAR_UNKNOWN)
3038 {
3039 if (l == NULL || l->lv_first == NULL)
3040 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003041 semsg(_(e_reduce_of_an_empty_str_with_no_initial_value), "List");
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003042 return;
3043 }
3044 initial = l->lv_first->li_tv;
3045 li = l->lv_first->li_next;
3046 }
3047 else
3048 {
3049 initial = argvars[2];
3050 if (l != NULL)
3051 li = l->lv_first;
3052 }
3053 copy_tv(&initial, rettv);
3054
3055 if (l == NULL)
3056 return;
3057
3058 prev_locked = l->lv_lock;
3059
3060 l->lv_lock = VAR_FIXED; // disallow the list changing here
3061 for ( ; li != NULL; li = li->li_next)
3062 {
3063 argv[0] = *rettv;
3064 argv[1] = li->li_tv;
3065 rettv->v_type = VAR_UNKNOWN;
3066 r = call_func(func_name, -1, rettv, 2, argv, funcexe);
3067 clear_tv(&argv[0]);
3068 if (r == FAIL || called_emsg != called_emsg_start)
3069 break;
3070 }
3071 l->lv_lock = prev_locked;
3072}
3073
3074/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003075 * "reduce(list, { accumulator, element -> value } [, initial])" function
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00003076 * "reduce(blob, { accumulator, element -> value } [, initial])"
3077 * "reduce(string, { accumulator, element -> value } [, initial])"
Bram Moolenaar85629982020-06-01 18:39:20 +02003078 */
3079 void
3080f_reduce(typval_T *argvars, typval_T *rettv)
3081{
Bram Moolenaar85629982020-06-01 18:39:20 +02003082 char_u *func_name;
3083 partial_T *partial = NULL;
3084 funcexe_T funcexe;
Bram Moolenaar85629982020-06-01 18:39:20 +02003085
rbtnn0ccb5842021-12-18 18:33:46 +00003086 if (in_vim9script()
3087 && check_for_string_or_list_or_blob_arg(argvars, 0) == FAIL)
Bram Moolenaar85629982020-06-01 18:39:20 +02003088 return;
rbtnn0ccb5842021-12-18 18:33:46 +00003089
3090 if (argvars[0].v_type != VAR_STRING
3091 && argvars[0].v_type != VAR_LIST
3092 && argvars[0].v_type != VAR_BLOB)
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003093 {
rbtnn0ccb5842021-12-18 18:33:46 +00003094 semsg(_(e_string_list_or_blob_required), "reduce()");
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003095 return;
3096 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003097
3098 if (argvars[1].v_type == VAR_FUNC)
3099 func_name = argvars[1].vval.v_string;
3100 else if (argvars[1].v_type == VAR_PARTIAL)
3101 {
3102 partial = argvars[1].vval.v_partial;
3103 func_name = partial_name(partial);
3104 }
3105 else
3106 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01003107 if (func_name == NULL || *func_name == NUL)
3108 {
3109 emsg(_(e_missing_function_argument));
3110 return;
3111 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003112
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003113 CLEAR_FIELD(funcexe);
3114 funcexe.fe_evaluate = TRUE;
3115 funcexe.fe_partial = partial;
Bram Moolenaar85629982020-06-01 18:39:20 +02003116
3117 if (argvars[0].v_type == VAR_LIST)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003118 list_reduce(argvars, func_name, &funcexe, rettv);
rbtnn0ccb5842021-12-18 18:33:46 +00003119 else if (argvars[0].v_type == VAR_STRING)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003120 string_reduce(argvars, func_name, &funcexe, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003121 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003122 blob_reduce(argvars, func_name, &funcexe, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003123}
3124
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02003125#endif // defined(FEAT_EVAL)