blob: c364fa72fda590d0dee21501d7a2c817e93faf63 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarda861d62016-07-17 15:46:27 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +020011 * list.c: List support and container (List, Dict, Blob) functions.
Bram Moolenaarda861d62016-07-17 15:46:27 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar08c308a2019-09-04 17:48:15 +020018static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
19
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010020// List heads for garbage collection.
21static list_T *first_list = NULL; // list of all lists
Bram Moolenaarda861d62016-07-17 15:46:27 +020022
Bram Moolenaar00d253e2020-04-06 22:13:01 +020023#define FOR_ALL_WATCHERS(l, lw) \
24 for ((lw) = (l)->lv_watch; (lw) != NULL; (lw) = (lw)->lw_next)
25
Bram Moolenaarbdff0122020-04-05 18:56:05 +020026static void list_free_item(list_T *l, listitem_T *item);
27
Bram Moolenaarda861d62016-07-17 15:46:27 +020028/*
29 * Add a watcher to a list.
30 */
31 void
32list_add_watch(list_T *l, listwatch_T *lw)
33{
34 lw->lw_next = l->lv_watch;
35 l->lv_watch = lw;
36}
37
38/*
39 * Remove a watcher from a list.
40 * No warning when it isn't found...
41 */
42 void
43list_rem_watch(list_T *l, listwatch_T *lwrem)
44{
45 listwatch_T *lw, **lwp;
46
47 lwp = &l->lv_watch;
Bram Moolenaar00d253e2020-04-06 22:13:01 +020048 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020049 {
50 if (lw == lwrem)
51 {
52 *lwp = lw->lw_next;
53 break;
54 }
55 lwp = &lw->lw_next;
56 }
57}
58
59/*
60 * Just before removing an item from a list: advance watchers to the next
61 * item.
62 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020063 static void
Bram Moolenaarda861d62016-07-17 15:46:27 +020064list_fix_watch(list_T *l, listitem_T *item)
65{
66 listwatch_T *lw;
67
Bram Moolenaar00d253e2020-04-06 22:13:01 +020068 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020069 if (lw->lw_item == item)
70 lw->lw_item = item->li_next;
71}
72
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073 static void
74list_init(list_T *l)
75{
76 // Prepend the list to the list of lists for garbage collection.
77 if (first_list != NULL)
78 first_list->lv_used_prev = l;
79 l->lv_used_prev = NULL;
80 l->lv_used_next = first_list;
81 first_list = l;
82}
83
Bram Moolenaarda861d62016-07-17 15:46:27 +020084/*
85 * Allocate an empty header for a list.
86 * Caller should take care of the reference count.
87 */
88 list_T *
89list_alloc(void)
90{
91 list_T *l;
92
Bram Moolenaarc799fe22019-05-28 23:08:19 +020093 l = ALLOC_CLEAR_ONE(list_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +020094 if (l != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095 list_init(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +020096 return l;
97}
98
99/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100100 * list_alloc() with an ID for alloc_fail().
101 */
102 list_T *
103list_alloc_id(alloc_id_T id UNUSED)
104{
105#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200106 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100107 return NULL;
108#endif
109 return (list_alloc());
110}
111
112/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 * Allocate space for a list, plus "count" items.
114 * Next list_set_item() must be called for each item.
115 */
116 list_T *
117list_alloc_with_items(int count)
118{
119 list_T *l;
120
121 l = (list_T *)alloc_clear(sizeof(list_T) + count * sizeof(listitem_T));
122 if (l != NULL)
123 {
124 list_init(l);
125
126 if (count > 0)
127 {
128 listitem_T *li = (listitem_T *)(l + 1);
129 int i;
130
131 l->lv_len = count;
132 l->lv_with_items = count;
133 l->lv_first = li;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100134 l->lv_u.mat.lv_last = li + count - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 for (i = 0; i < count; ++i)
136 {
137 if (i == 0)
138 li->li_prev = NULL;
139 else
140 li->li_prev = li - 1;
141 if (i == count - 1)
142 li->li_next = NULL;
143 else
144 li->li_next = li + 1;
145 ++li;
146 }
147 }
148 }
149 return l;
150}
151
152/*
153 * Set item "idx" for a list previously allocated with list_alloc_with_items().
154 * The contents of "tv" is moved into the list item.
155 * Each item must be set exactly once.
156 */
157 void
158list_set_item(list_T *l, int idx, typval_T *tv)
159{
160 listitem_T *li = (listitem_T *)(l + 1) + idx;
161
162 li->li_tv = *tv;
163}
164
165/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200166 * Allocate an empty list for a return value, with reference count set.
167 * Returns OK or FAIL.
168 */
169 int
170rettv_list_alloc(typval_T *rettv)
171{
172 list_T *l = list_alloc();
173
174 if (l == NULL)
175 return FAIL;
176
Bram Moolenaarda861d62016-07-17 15:46:27 +0200177 rettv->v_lock = 0;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200178 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200179 return OK;
180}
181
182/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100183 * Same as rettv_list_alloc() but uses an allocation id for testing.
184 */
185 int
186rettv_list_alloc_id(typval_T *rettv, alloc_id_T id UNUSED)
187{
188#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200189 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaar162b7142018-12-21 15:17:36 +0100190 return FAIL;
191#endif
192 return rettv_list_alloc(rettv);
193}
194
195
196/*
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200197 * Set a list as the return value. Increments the reference count.
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200198 */
199 void
200rettv_list_set(typval_T *rettv, list_T *l)
201{
202 rettv->v_type = VAR_LIST;
203 rettv->vval.v_list = l;
204 if (l != NULL)
205 ++l->lv_refcount;
206}
207
208/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200209 * Unreference a list: decrement the reference count and free it when it
210 * becomes zero.
211 */
212 void
213list_unref(list_T *l)
214{
215 if (l != NULL && --l->lv_refcount <= 0)
216 list_free(l);
217}
218
219/*
220 * Free a list, including all non-container items it points to.
221 * Ignores the reference count.
222 */
223 static void
224list_free_contents(list_T *l)
225{
226 listitem_T *item;
227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 if (l->lv_first != &range_list_item)
229 for (item = l->lv_first; item != NULL; item = l->lv_first)
230 {
231 // Remove the item before deleting it.
232 l->lv_first = item->li_next;
233 clear_tv(&item->li_tv);
234 list_free_item(l, item);
235 }
Bram Moolenaarda861d62016-07-17 15:46:27 +0200236}
237
238/*
239 * Go through the list of lists and free items without the copyID.
240 * But don't free a list that has a watcher (used in a for loop), these
241 * are not referenced anywhere.
242 */
243 int
244list_free_nonref(int copyID)
245{
246 list_T *ll;
247 int did_free = FALSE;
248
249 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
250 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
251 && ll->lv_watch == NULL)
252 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100253 // Free the List and ordinary items it contains, but don't recurse
254 // into Lists and Dictionaries, they will be in the list of dicts
255 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200256 list_free_contents(ll);
257 did_free = TRUE;
258 }
259 return did_free;
260}
261
262 static void
263list_free_list(list_T *l)
264{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100265 // Remove the list from the list of lists for garbage collection.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200266 if (l->lv_used_prev == NULL)
267 first_list = l->lv_used_next;
268 else
269 l->lv_used_prev->lv_used_next = l->lv_used_next;
270 if (l->lv_used_next != NULL)
271 l->lv_used_next->lv_used_prev = l->lv_used_prev;
272
273 vim_free(l);
274}
275
276 void
277list_free_items(int copyID)
278{
279 list_T *ll, *ll_next;
280
281 for (ll = first_list; ll != NULL; ll = ll_next)
282 {
283 ll_next = ll->lv_used_next;
284 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
285 && ll->lv_watch == NULL)
286 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100287 // Free the List and ordinary items it contains, but don't recurse
288 // into Lists and Dictionaries, they will be in the list of dicts
289 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200290 list_free_list(ll);
291 }
292 }
293}
294
295 void
296list_free(list_T *l)
297{
298 if (!in_free_unref_items)
299 {
300 list_free_contents(l);
301 list_free_list(l);
302 }
303}
304
305/*
306 * Allocate a list item.
307 * It is not initialized, don't forget to set v_lock.
308 */
309 listitem_T *
310listitem_alloc(void)
311{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200312 return ALLOC_ONE(listitem_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200313}
314
315/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100316 * Free a list item, unless it was allocated together with the list itself.
317 * Does not clear the value. Does not notify watchers.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200318 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200319 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100320list_free_item(list_T *l, listitem_T *item)
321{
322 if (l->lv_with_items == 0 || item < (listitem_T *)l
323 || item >= (listitem_T *)(l + 1) + l->lv_with_items)
324 vim_free(item);
325}
326
327/*
328 * Free a list item, unless it was allocated together with the list itself.
329 * Also clears the value. Does not notify watchers.
330 */
331 void
332listitem_free(list_T *l, listitem_T *item)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200333{
334 clear_tv(&item->li_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335 list_free_item(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200336}
337
338/*
339 * Remove a list item from a List and free it. Also clears the value.
340 */
341 void
342listitem_remove(list_T *l, listitem_T *item)
343{
344 vimlist_remove(l, item, item);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100345 listitem_free(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200346}
347
348/*
349 * Get the number of items in a list.
350 */
351 long
352list_len(list_T *l)
353{
354 if (l == NULL)
355 return 0L;
356 return l->lv_len;
357}
358
359/*
360 * Return TRUE when two lists have exactly the same values.
361 */
362 int
363list_equal(
364 list_T *l1,
365 list_T *l2,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100366 int ic, // ignore case for strings
367 int recursive) // TRUE when used recursively
Bram Moolenaarda861d62016-07-17 15:46:27 +0200368{
369 listitem_T *item1, *item2;
370
Bram Moolenaarda861d62016-07-17 15:46:27 +0200371 if (l1 == l2)
372 return TRUE;
373 if (list_len(l1) != list_len(l2))
374 return FALSE;
Bram Moolenaar7b293c72020-04-09 21:33:22 +0200375 if (list_len(l1) == 0)
376 // empty and NULL list are considered equal
377 return TRUE;
378 if (l1 == NULL || l2 == NULL)
379 return FALSE;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200380
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200381 CHECK_LIST_MATERIALIZE(l1);
382 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383
Bram Moolenaarda861d62016-07-17 15:46:27 +0200384 for (item1 = l1->lv_first, item2 = l2->lv_first;
385 item1 != NULL && item2 != NULL;
386 item1 = item1->li_next, item2 = item2->li_next)
387 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
388 return FALSE;
389 return item1 == NULL && item2 == NULL;
390}
391
392/*
393 * Locate item with index "n" in list "l" and return it.
394 * A negative index is counted from the end; -1 is the last item.
395 * Returns NULL when "n" is out of range.
396 */
397 listitem_T *
398list_find(list_T *l, long n)
399{
400 listitem_T *item;
401 long idx;
402
403 if (l == NULL)
404 return NULL;
405
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100406 // Negative index is relative to the end.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200407 if (n < 0)
408 n = l->lv_len + n;
409
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100410 // Check for index out of range.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200411 if (n < 0 || n >= l->lv_len)
412 return NULL;
413
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200414 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100415
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100416 // When there is a cached index may start search from there.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100417 if (l->lv_u.mat.lv_idx_item != NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200418 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100419 if (n < l->lv_u.mat.lv_idx / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200420 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100421 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200422 item = l->lv_first;
423 idx = 0;
424 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100425 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200426 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100427 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100428 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200429 idx = l->lv_len - 1;
430 }
431 else
432 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100433 // closest to the cached index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100434 item = l->lv_u.mat.lv_idx_item;
435 idx = l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200436 }
437 }
438 else
439 {
440 if (n < l->lv_len / 2)
441 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100442 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200443 item = l->lv_first;
444 idx = 0;
445 }
446 else
447 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100448 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100449 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200450 idx = l->lv_len - 1;
451 }
452 }
453
454 while (n > idx)
455 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100456 // search forward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200457 item = item->li_next;
458 ++idx;
459 }
460 while (n < idx)
461 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100462 // search backward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200463 item = item->li_prev;
464 --idx;
465 }
466
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100467 // cache the used index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100468 l->lv_u.mat.lv_idx = idx;
469 l->lv_u.mat.lv_idx_item = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200470
471 return item;
472}
473
474/*
475 * Get list item "l[idx]" as a number.
476 */
477 long
478list_find_nr(
479 list_T *l,
480 long idx,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100481 int *errorp) // set to TRUE when something wrong
Bram Moolenaarda861d62016-07-17 15:46:27 +0200482{
483 listitem_T *li;
484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100485 if (l != NULL && l->lv_first == &range_list_item)
486 {
487 long n = idx;
488
489 // not materialized range() list: compute the value.
490 // Negative index is relative to the end.
491 if (n < 0)
492 n = l->lv_len + n;
493
494 // Check for index out of range.
495 if (n < 0 || n >= l->lv_len)
496 {
497 if (errorp != NULL)
498 *errorp = TRUE;
499 return -1L;
500 }
501
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100502 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503 }
504
Bram Moolenaarda861d62016-07-17 15:46:27 +0200505 li = list_find(l, idx);
506 if (li == NULL)
507 {
508 if (errorp != NULL)
509 *errorp = TRUE;
510 return -1L;
511 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100512 return (long)tv_get_number_chk(&li->li_tv, errorp);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200513}
514
515/*
516 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
517 */
518 char_u *
519list_find_str(list_T *l, long idx)
520{
521 listitem_T *li;
522
523 li = list_find(l, idx - 1);
524 if (li == NULL)
525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100526 semsg(_(e_listidx), idx);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200527 return NULL;
528 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100529 return tv_get_string(&li->li_tv);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200530}
531
532/*
533 * Locate "item" list "l" and return its index.
534 * Returns -1 when "item" is not in the list.
535 */
536 long
537list_idx_of_item(list_T *l, listitem_T *item)
538{
539 long idx = 0;
540 listitem_T *li;
541
542 if (l == NULL)
543 return -1;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200544 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200545 idx = 0;
546 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
547 ++idx;
548 if (li == NULL)
549 return -1;
550 return idx;
551}
552
553/*
554 * Append item "item" to the end of list "l".
555 */
556 void
557list_append(list_T *l, listitem_T *item)
558{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200559 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100560 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200561 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100562 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200563 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100564 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200565 item->li_prev = NULL;
566 }
567 else
568 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100569 l->lv_u.mat.lv_last->li_next = item;
570 item->li_prev = l->lv_u.mat.lv_last;
571 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200572 }
573 ++l->lv_len;
574 item->li_next = NULL;
575}
576
577/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200579 * Return FAIL when out of memory.
580 */
581 int
582list_append_tv(list_T *l, typval_T *tv)
583{
584 listitem_T *li = listitem_alloc();
585
586 if (li == NULL)
587 return FAIL;
588 copy_tv(tv, &li->li_tv);
589 list_append(l, li);
590 return OK;
591}
592
593/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594 * As list_append_tv() but move the value instead of copying it.
595 * Return FAIL when out of memory.
596 */
597 int
598list_append_tv_move(list_T *l, typval_T *tv)
599{
600 listitem_T *li = listitem_alloc();
601
602 if (li == NULL)
603 return FAIL;
604 li->li_tv = *tv;
605 list_append(l, li);
606 return OK;
607}
608
609/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200610 * Add a dictionary to a list. Used by getqflist().
611 * Return FAIL when out of memory.
612 */
613 int
614list_append_dict(list_T *list, dict_T *dict)
615{
616 listitem_T *li = listitem_alloc();
617
618 if (li == NULL)
619 return FAIL;
620 li->li_tv.v_type = VAR_DICT;
621 li->li_tv.v_lock = 0;
622 li->li_tv.vval.v_dict = dict;
623 list_append(list, li);
624 ++dict->dv_refcount;
625 return OK;
626}
627
628/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100629 * Append list2 to list1.
630 * Return FAIL when out of memory.
631 */
632 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200633list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100634{
635 listitem_T *li = listitem_alloc();
636
637 if (li == NULL)
638 return FAIL;
639 li->li_tv.v_type = VAR_LIST;
640 li->li_tv.v_lock = 0;
641 li->li_tv.vval.v_list = list2;
642 list_append(list1, li);
643 ++list2->lv_refcount;
644 return OK;
645}
646
647/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200648 * Make a copy of "str" and append it as an item to list "l".
649 * When "len" >= 0 use "str[len]".
650 * Returns FAIL when out of memory.
651 */
652 int
653list_append_string(list_T *l, char_u *str, int len)
654{
655 listitem_T *li = listitem_alloc();
656
657 if (li == NULL)
658 return FAIL;
659 list_append(l, li);
660 li->li_tv.v_type = VAR_STRING;
661 li->li_tv.v_lock = 0;
662 if (str == NULL)
663 li->li_tv.vval.v_string = NULL;
664 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
665 : vim_strsave(str))) == NULL)
666 return FAIL;
667 return OK;
668}
669
670/*
671 * Append "n" to list "l".
672 * Returns FAIL when out of memory.
673 */
674 int
675list_append_number(list_T *l, varnumber_T n)
676{
677 listitem_T *li;
678
679 li = listitem_alloc();
680 if (li == NULL)
681 return FAIL;
682 li->li_tv.v_type = VAR_NUMBER;
683 li->li_tv.v_lock = 0;
684 li->li_tv.vval.v_number = n;
685 list_append(l, li);
686 return OK;
687}
688
689/*
690 * Insert typval_T "tv" in list "l" before "item".
691 * If "item" is NULL append at the end.
692 * Return FAIL when out of memory.
693 */
694 int
695list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
696{
697 listitem_T *ni = listitem_alloc();
698
699 if (ni == NULL)
700 return FAIL;
701 copy_tv(tv, &ni->li_tv);
702 list_insert(l, ni, item);
703 return OK;
704}
705
706 void
707list_insert(list_T *l, listitem_T *ni, listitem_T *item)
708{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200709 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200710 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100711 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200712 list_append(l, ni);
713 else
714 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100715 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200716 ni->li_prev = item->li_prev;
717 ni->li_next = item;
718 if (item->li_prev == NULL)
719 {
720 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100721 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200722 }
723 else
724 {
725 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100726 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200727 }
728 item->li_prev = ni;
729 ++l->lv_len;
730 }
731}
732
733/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200734 * Flatten "list" to depth "maxdepth".
735 * It does nothing if "maxdepth" is 0.
736 * Returns FAIL when out of memory.
737 */
738 static int
739list_flatten(list_T *list, long maxdepth)
740{
741 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200742 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200743 int n;
744
745 if (maxdepth == 0)
746 return OK;
747 CHECK_LIST_MATERIALIZE(list);
748
749 n = 0;
750 item = list->lv_first;
751 while (item != NULL)
752 {
753 fast_breakcheck();
754 if (got_int)
755 return FAIL;
756
757 if (item->li_tv.v_type == VAR_LIST)
758 {
759 listitem_T *next = item->li_next;
760
761 vimlist_remove(list, item, item);
762 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
763 return FAIL;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200764 clear_tv(&item->li_tv);
765 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200766
767 if (item->li_prev == NULL)
768 item = list->lv_first;
769 else
770 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200771 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200772
773 if (++n >= maxdepth)
774 {
775 n = 0;
776 item = next;
777 }
778 }
779 else
780 {
781 n = 0;
782 item = item->li_next;
783 }
784 }
785
786 return OK;
787}
788
789/*
790 * "flatten(list[, {maxdepth}])" function
791 */
792 void
793f_flatten(typval_T *argvars, typval_T *rettv)
794{
795 list_T *l;
796 long maxdepth;
797 int error = FALSE;
798
799 if (argvars[0].v_type != VAR_LIST)
800 {
801 semsg(_(e_listarg), "flatten()");
802 return;
803 }
804
805 if (argvars[1].v_type == VAR_UNKNOWN)
806 maxdepth = 999999;
807 else
808 {
809 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
810 if (error)
811 return;
812 if (maxdepth < 0)
813 {
814 emsg(_("E900: maxdepth must be non-negative number"));
815 return;
816 }
817 }
818
819 l = argvars[0].vval.v_list;
Bram Moolenaara187c432020-09-16 21:08:28 +0200820 if (l != NULL && !value_check_lock(l->lv_lock,
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200821 (char_u *)N_("flatten() argument"), TRUE)
822 && list_flatten(l, maxdepth) == OK)
823 copy_tv(&argvars[0], rettv);
824}
825
826/*
Bram Moolenaar1a739232020-10-10 15:37:58 +0200827 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200828 * If "bef" is NULL append at the end, otherwise insert before this item.
829 * Returns FAIL when out of memory.
830 */
831 int
832list_extend(list_T *l1, list_T *l2, listitem_T *bef)
833{
834 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200835 int todo;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200836
Bram Moolenaar1a739232020-10-10 15:37:58 +0200837 // NULL list is equivalent to an empty list: nothing to do.
838 if (l2 == NULL || l2->lv_len == 0)
839 return OK;
840
841 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200842 CHECK_LIST_MATERIALIZE(l1);
843 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100844
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100845 // We also quit the loop when we have inserted the original item count of
846 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200847 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
848 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
849 return FAIL;
850 return OK;
851}
852
853/*
854 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
855 * Return FAIL when out of memory.
856 */
857 int
858list_concat(list_T *l1, list_T *l2, typval_T *tv)
859{
860 list_T *l;
861
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100862 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +0200863 if (l1 == NULL)
864 l = list_alloc();
865 else
866 l = list_copy(l1, FALSE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200867 if (l == NULL)
868 return FAIL;
869 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +0100870 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200871 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200872 if (l1 == NULL)
873 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200874
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100875 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200876 return list_extend(l, l2, NULL);
877}
878
Bram Moolenaar9af78762020-06-16 11:34:42 +0200879 list_T *
880list_slice(list_T *ol, long n1, long n2)
881{
882 listitem_T *item;
883 list_T *l = list_alloc();
884
885 if (l == NULL)
886 return NULL;
887 for (item = list_find(ol, n1); n1 <= n2; ++n1)
888 {
889 if (list_append_tv(l, &item->li_tv) == FAIL)
890 {
891 list_free(l);
892 return NULL;
893 }
894 item = item->li_next;
895 }
896 return l;
897}
898
Bram Moolenaared591872020-08-15 22:14:53 +0200899 int
900list_slice_or_index(
901 list_T *list,
902 int range,
903 long n1_arg,
904 long n2_arg,
905 typval_T *rettv,
906 int verbose)
907{
908 long len = list_len(list);
909 long n1 = n1_arg;
910 long n2 = n2_arg;
911 typval_T var1;
912
913 if (n1 < 0)
914 n1 = len + n1;
915 if (n1 < 0 || n1 >= len)
916 {
917 // For a range we allow invalid values and return an empty
918 // list. A list index out of range is an error.
919 if (!range)
920 {
921 if (verbose)
922 semsg(_(e_listidx), n1);
923 return FAIL;
924 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200925 n1 = n1 < 0 ? 0 : len;
Bram Moolenaared591872020-08-15 22:14:53 +0200926 }
927 if (range)
928 {
929 list_T *l;
930
931 if (n2 < 0)
932 n2 = len + n2;
933 else if (n2 >= len)
934 n2 = len - 1;
935 if (n2 < 0 || n2 + 1 < n1)
936 n2 = -1;
937 l = list_slice(list, n1, n2);
938 if (l == NULL)
939 return FAIL;
940 clear_tv(rettv);
941 rettv_list_set(rettv, l);
942 }
943 else
944 {
945 // copy the item to "var1" to avoid that freeing the list makes it
946 // invalid.
947 copy_tv(&list_find(list, n1)->li_tv, &var1);
948 clear_tv(rettv);
949 *rettv = var1;
950 }
951 return OK;
952}
953
Bram Moolenaarda861d62016-07-17 15:46:27 +0200954/*
955 * Make a copy of list "orig". Shallow if "deep" is FALSE.
956 * The refcount of the new list is set to 1.
957 * See item_copy() for "copyID".
958 * Returns NULL when out of memory.
959 */
960 list_T *
961list_copy(list_T *orig, int deep, int copyID)
962{
963 list_T *copy;
964 listitem_T *item;
965 listitem_T *ni;
966
967 if (orig == NULL)
968 return NULL;
969
970 copy = list_alloc();
971 if (copy != NULL)
972 {
973 if (copyID != 0)
974 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100975 // Do this before adding the items, because one of the items may
976 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200977 orig->lv_copyID = copyID;
978 orig->lv_copylist = copy;
979 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200980 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200981 for (item = orig->lv_first; item != NULL && !got_int;
982 item = item->li_next)
983 {
984 ni = listitem_alloc();
985 if (ni == NULL)
986 break;
987 if (deep)
988 {
989 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
990 {
991 vim_free(ni);
992 break;
993 }
994 }
995 else
996 copy_tv(&item->li_tv, &ni->li_tv);
997 list_append(copy, ni);
998 }
999 ++copy->lv_refcount;
1000 if (item != NULL)
1001 {
1002 list_unref(copy);
1003 copy = NULL;
1004 }
1005 }
1006
1007 return copy;
1008}
1009
1010/*
1011 * Remove items "item" to "item2" from list "l".
1012 * Does not free the listitem or the value!
1013 * This used to be called list_remove, but that conflicts with a Sun header
1014 * file.
1015 */
1016 void
1017vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1018{
1019 listitem_T *ip;
1020
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001021 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001023 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001024 for (ip = item; ip != NULL; ip = ip->li_next)
1025 {
1026 --l->lv_len;
1027 list_fix_watch(l, ip);
1028 if (ip == item2)
1029 break;
1030 }
1031
1032 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001033 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001034 else
1035 item2->li_next->li_prev = item->li_prev;
1036 if (item->li_prev == NULL)
1037 l->lv_first = item2->li_next;
1038 else
1039 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001040 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001041}
1042
1043/*
1044 * Return an allocated string with the string representation of a list.
1045 * May return NULL.
1046 */
1047 char_u *
1048list2string(typval_T *tv, int copyID, int restore_copyID)
1049{
1050 garray_T ga;
1051
1052 if (tv->vval.v_list == NULL)
1053 return NULL;
1054 ga_init2(&ga, (int)sizeof(char), 80);
1055 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001056 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001057 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1058 FALSE, restore_copyID, copyID) == FAIL)
1059 {
1060 vim_free(ga.ga_data);
1061 return NULL;
1062 }
1063 ga_append(&ga, ']');
1064 ga_append(&ga, NUL);
1065 return (char_u *)ga.ga_data;
1066}
1067
1068typedef struct join_S {
1069 char_u *s;
1070 char_u *tofree;
1071} join_T;
1072
1073 static int
1074list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001075 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001076 list_T *l,
1077 char_u *sep,
1078 int echo_style,
1079 int restore_copyID,
1080 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001081 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001082{
1083 int i;
1084 join_T *p;
1085 int len;
1086 int sumlen = 0;
1087 int first = TRUE;
1088 char_u *tofree;
1089 char_u numbuf[NUMBUFLEN];
1090 listitem_T *item;
1091 char_u *s;
1092
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001093 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001094 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001095 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1096 {
1097 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001098 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001099 if (s == NULL)
1100 return FAIL;
1101
1102 len = (int)STRLEN(s);
1103 sumlen += len;
1104
1105 (void)ga_grow(join_gap, 1);
1106 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1107 if (tofree != NULL || s != numbuf)
1108 {
1109 p->s = s;
1110 p->tofree = tofree;
1111 }
1112 else
1113 {
1114 p->s = vim_strnsave(s, len);
1115 p->tofree = p->s;
1116 }
1117
1118 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001119 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001120 break;
1121 }
1122
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001123 // Allocate result buffer with its total size, avoid re-allocation and
1124 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001125 if (join_gap->ga_len >= 2)
1126 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1127 if (ga_grow(gap, sumlen + 2) == FAIL)
1128 return FAIL;
1129
1130 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1131 {
1132 if (first)
1133 first = FALSE;
1134 else
1135 ga_concat(gap, sep);
1136 p = ((join_T *)join_gap->ga_data) + i;
1137
1138 if (p->s != NULL)
1139 ga_concat(gap, p->s);
1140 line_breakcheck();
1141 }
1142
1143 return OK;
1144}
1145
1146/*
1147 * Join list "l" into a string in "*gap", using separator "sep".
1148 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1149 * Return FAIL or OK.
1150 */
1151 int
1152list_join(
1153 garray_T *gap,
1154 list_T *l,
1155 char_u *sep,
1156 int echo_style,
1157 int restore_copyID,
1158 int copyID)
1159{
1160 garray_T join_ga;
1161 int retval;
1162 join_T *p;
1163 int i;
1164
1165 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001166 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001167 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1168 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1169 copyID, &join_ga);
1170
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001171 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001172 if (join_ga.ga_data != NULL)
1173 {
1174 p = (join_T *)join_ga.ga_data;
1175 for (i = 0; i < join_ga.ga_len; ++i)
1176 {
1177 vim_free(p->tofree);
1178 ++p;
1179 }
1180 ga_clear(&join_ga);
1181 }
1182
1183 return retval;
1184}
1185
1186/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001187 * "join()" function
1188 */
1189 void
1190f_join(typval_T *argvars, typval_T *rettv)
1191{
1192 garray_T ga;
1193 char_u *sep;
1194
1195 if (argvars[0].v_type != VAR_LIST)
1196 {
1197 emsg(_(e_listreq));
1198 return;
1199 }
1200 if (argvars[0].vval.v_list == NULL)
1201 return;
1202 if (argvars[1].v_type == VAR_UNKNOWN)
1203 sep = (char_u *)" ";
1204 else
1205 sep = tv_get_string_chk(&argvars[1]);
1206
1207 rettv->v_type = VAR_STRING;
1208
1209 if (sep != NULL)
1210 {
1211 ga_init2(&ga, (int)sizeof(char), 80);
1212 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1213 ga_append(&ga, NUL);
1214 rettv->vval.v_string = (char_u *)ga.ga_data;
1215 }
1216 else
1217 rettv->vval.v_string = NULL;
1218}
1219
1220/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001221 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001222 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001223 * Return OK or FAIL.
1224 */
1225 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001226eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001227{
Bram Moolenaar71478202020-06-26 22:46:27 +02001228 int evaluate = evalarg == NULL ? FALSE
1229 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001230 list_T *l = NULL;
1231 typval_T tv;
1232 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001233 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001234 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001235
1236 if (evaluate)
1237 {
1238 l = list_alloc();
1239 if (l == NULL)
1240 return FAIL;
1241 }
1242
Bram Moolenaar962d7212020-07-04 14:15:00 +02001243 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001244 while (**arg != ']' && **arg != NUL)
1245 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001246 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001247 goto failret;
1248 if (evaluate)
1249 {
1250 item = listitem_alloc();
1251 if (item != NULL)
1252 {
1253 item->li_tv = tv;
1254 item->li_tv.v_lock = 0;
1255 list_append(l, item);
1256 }
1257 else
1258 clear_tv(&tv);
1259 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001260 // Legacy Vim script allowed a space before the comma.
1261 if (!vim9script)
1262 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001263
Bram Moolenaare6e03172020-06-27 16:36:05 +02001264 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001265 had_comma = **arg == ',';
1266 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001267 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001268 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001269 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001270 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaare6e03172020-06-27 16:36:05 +02001271 goto failret;
1272 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001273 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001274 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001275
Bram Moolenaare6b53242020-07-01 17:28:33 +02001276 // The "]" can be on the next line. But a double quoted string may
1277 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001278 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001279 if (**arg == ']')
1280 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001281
1282 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001283 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001285 {
1286 if (**arg == ',')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001287 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02001288 else
1289 semsg(_("E696: Missing comma in List: %s"), *arg);
1290 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001291 goto failret;
1292 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001293 }
1294
1295 if (**arg != ']')
1296 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001298 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001299failret:
1300 if (evaluate)
1301 list_free(l);
1302 return FAIL;
1303 }
1304
Bram Moolenaar9d489562020-07-30 20:08:50 +02001305 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001306 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001307 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001308
1309 return OK;
1310}
1311
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001312/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001313 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001314 */
1315 int
1316write_list(FILE *fd, list_T *list, int binary)
1317{
1318 listitem_T *li;
1319 int c;
1320 int ret = OK;
1321 char_u *s;
1322
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001323 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001324 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001325 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001326 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001327 {
1328 if (*s == '\n')
1329 c = putc(NUL, fd);
1330 else
1331 c = putc(*s, fd);
1332 if (c == EOF)
1333 {
1334 ret = FAIL;
1335 break;
1336 }
1337 }
1338 if (!binary || li->li_next != NULL)
1339 if (putc('\n', fd) == EOF)
1340 {
1341 ret = FAIL;
1342 break;
1343 }
1344 if (ret == FAIL)
1345 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001346 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001347 break;
1348 }
1349 }
1350 return ret;
1351}
1352
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001353/*
1354 * Initialize a static list with 10 items.
1355 */
1356 void
1357init_static_list(staticList10_T *sl)
1358{
1359 list_T *l = &sl->sl_list;
1360 int i;
1361
1362 memset(sl, 0, sizeof(staticList10_T));
1363 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001364 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001365 l->lv_refcount = DO_NOT_FREE_CNT;
1366 l->lv_lock = VAR_FIXED;
1367 sl->sl_list.lv_len = 10;
1368
1369 for (i = 0; i < 10; ++i)
1370 {
1371 listitem_T *li = &sl->sl_items[i];
1372
1373 if (i == 0)
1374 li->li_prev = NULL;
1375 else
1376 li->li_prev = li - 1;
1377 if (i == 9)
1378 li->li_next = NULL;
1379 else
1380 li->li_next = li + 1;
1381 }
1382}
1383
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001384/*
1385 * "list2str()" function
1386 */
1387 void
1388f_list2str(typval_T *argvars, typval_T *rettv)
1389{
1390 list_T *l;
1391 listitem_T *li;
1392 garray_T ga;
1393 int utf8 = FALSE;
1394
1395 rettv->v_type = VAR_STRING;
1396 rettv->vval.v_string = NULL;
1397 if (argvars[0].v_type != VAR_LIST)
1398 {
1399 emsg(_(e_invarg));
1400 return;
1401 }
1402
1403 l = argvars[0].vval.v_list;
1404 if (l == NULL)
1405 return; // empty list results in empty string
1406
1407 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001408 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001409
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001410 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001411 ga_init2(&ga, 1, 80);
1412 if (has_mbyte || utf8)
1413 {
1414 char_u buf[MB_MAXBYTES + 1];
1415 int (*char2bytes)(int, char_u *);
1416
1417 if (utf8 || enc_utf8)
1418 char2bytes = utf_char2bytes;
1419 else
1420 char2bytes = mb_char2bytes;
1421
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001422 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001423 {
1424 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1425 ga_concat(&ga, buf);
1426 }
1427 ga_append(&ga, NUL);
1428 }
1429 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1430 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001431 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001432 ga_append(&ga, tv_get_number(&li->li_tv));
1433 ga_append(&ga, NUL);
1434 }
1435
1436 rettv->v_type = VAR_STRING;
1437 rettv->vval.v_string = ga.ga_data;
1438}
1439
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001440 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001441list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1442{
1443 list_T *l;
1444 listitem_T *item, *item2;
1445 listitem_T *li;
1446 int error = FALSE;
1447 int idx;
1448
1449 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001450 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001451 return;
1452
1453 idx = (long)tv_get_number_chk(&argvars[1], &error);
1454 if (error)
1455 ; // type error: do nothing, errmsg already given
1456 else if ((item = list_find(l, idx)) == NULL)
1457 semsg(_(e_listidx), idx);
1458 else
1459 {
1460 if (argvars[2].v_type == VAR_UNKNOWN)
1461 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001462 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001463 vimlist_remove(l, item, item);
1464 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001466 }
1467 else
1468 {
1469 // Remove range of items, return list with values.
1470 int end = (long)tv_get_number_chk(&argvars[2], &error);
1471
1472 if (error)
1473 ; // type error: do nothing
1474 else if ((item2 = list_find(l, end)) == NULL)
1475 semsg(_(e_listidx), end);
1476 else
1477 {
1478 int cnt = 0;
1479
1480 for (li = item; li != NULL; li = li->li_next)
1481 {
1482 ++cnt;
1483 if (li == item2)
1484 break;
1485 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001486 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001487 emsg(_(e_invrange));
1488 else
1489 {
1490 vimlist_remove(l, item, item2);
1491 if (rettv_list_alloc(rettv) == OK)
1492 {
1493 l = rettv->vval.v_list;
1494 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001495 l->lv_u.mat.lv_last = item2;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001496 item->li_prev = NULL;
1497 item2->li_next = NULL;
1498 l->lv_len = cnt;
1499 }
1500 }
1501 }
1502 }
1503 }
1504}
1505
1506static int item_compare(const void *s1, const void *s2);
1507static int item_compare2(const void *s1, const void *s2);
1508
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001509// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001510typedef struct
1511{
1512 listitem_T *item;
1513 int idx;
1514} sortItem_T;
1515
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001516// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001517typedef struct
1518{
1519 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001520 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001521 int item_compare_numeric;
1522 int item_compare_numbers;
1523#ifdef FEAT_FLOAT
1524 int item_compare_float;
1525#endif
1526 char_u *item_compare_func;
1527 partial_T *item_compare_partial;
1528 dict_T *item_compare_selfdict;
1529 int item_compare_func_err;
1530 int item_compare_keep_zero;
1531} sortinfo_T;
1532static sortinfo_T *sortinfo = NULL;
1533#define ITEM_COMPARE_FAIL 999
1534
1535/*
1536 * Compare functions for f_sort() and f_uniq() below.
1537 */
1538 static int
1539item_compare(const void *s1, const void *s2)
1540{
1541 sortItem_T *si1, *si2;
1542 typval_T *tv1, *tv2;
1543 char_u *p1, *p2;
1544 char_u *tofree1 = NULL, *tofree2 = NULL;
1545 int res;
1546 char_u numbuf1[NUMBUFLEN];
1547 char_u numbuf2[NUMBUFLEN];
1548
1549 si1 = (sortItem_T *)s1;
1550 si2 = (sortItem_T *)s2;
1551 tv1 = &si1->item->li_tv;
1552 tv2 = &si2->item->li_tv;
1553
1554 if (sortinfo->item_compare_numbers)
1555 {
1556 varnumber_T v1 = tv_get_number(tv1);
1557 varnumber_T v2 = tv_get_number(tv2);
1558
1559 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1560 }
1561
1562#ifdef FEAT_FLOAT
1563 if (sortinfo->item_compare_float)
1564 {
1565 float_T v1 = tv_get_float(tv1);
1566 float_T v2 = tv_get_float(tv2);
1567
1568 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1569 }
1570#endif
1571
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001572 // tv2string() puts quotes around a string and allocates memory. Don't do
1573 // that for string variables. Use a single quote when comparing with a
1574 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001575 if (tv1->v_type == VAR_STRING)
1576 {
1577 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1578 p1 = (char_u *)"'";
1579 else
1580 p1 = tv1->vval.v_string;
1581 }
1582 else
1583 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1584 if (tv2->v_type == VAR_STRING)
1585 {
1586 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1587 p2 = (char_u *)"'";
1588 else
1589 p2 = tv2->vval.v_string;
1590 }
1591 else
1592 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1593 if (p1 == NULL)
1594 p1 = (char_u *)"";
1595 if (p2 == NULL)
1596 p2 = (char_u *)"";
1597 if (!sortinfo->item_compare_numeric)
1598 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001599 if (sortinfo->item_compare_lc)
1600 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001601 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001602 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001603 }
1604 else
1605 {
1606 double n1, n2;
1607 n1 = strtod((char *)p1, (char **)&p1);
1608 n2 = strtod((char *)p2, (char **)&p2);
1609 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1610 }
1611
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001612 // When the result would be zero, compare the item indexes. Makes the
1613 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001614 if (res == 0 && !sortinfo->item_compare_keep_zero)
1615 res = si1->idx > si2->idx ? 1 : -1;
1616
1617 vim_free(tofree1);
1618 vim_free(tofree2);
1619 return res;
1620}
1621
1622 static int
1623item_compare2(const void *s1, const void *s2)
1624{
1625 sortItem_T *si1, *si2;
1626 int res;
1627 typval_T rettv;
1628 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001629 char_u *func_name;
1630 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001631 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001632
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001633 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001634 if (sortinfo->item_compare_func_err)
1635 return 0;
1636
1637 si1 = (sortItem_T *)s1;
1638 si2 = (sortItem_T *)s2;
1639
1640 if (partial == NULL)
1641 func_name = sortinfo->item_compare_func;
1642 else
1643 func_name = partial_name(partial);
1644
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001645 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1646 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001647 copy_tv(&si1->item->li_tv, &argv[0]);
1648 copy_tv(&si2->item->li_tv, &argv[1]);
1649
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001650 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001651 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001652 funcexe.evaluate = TRUE;
1653 funcexe.partial = partial;
1654 funcexe.selfdict = sortinfo->item_compare_selfdict;
1655 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001656 clear_tv(&argv[0]);
1657 clear_tv(&argv[1]);
1658
1659 if (res == FAIL)
1660 res = ITEM_COMPARE_FAIL;
1661 else
1662 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1663 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001664 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001665 clear_tv(&rettv);
1666
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001667 // When the result would be zero, compare the pointers themselves. Makes
1668 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001669 if (res == 0 && !sortinfo->item_compare_keep_zero)
1670 res = si1->idx > si2->idx ? 1 : -1;
1671
1672 return res;
1673}
1674
1675/*
1676 * "sort()" or "uniq()" function
1677 */
1678 static void
1679do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1680{
1681 list_T *l;
1682 listitem_T *li;
1683 sortItem_T *ptrs;
1684 sortinfo_T *old_sortinfo;
1685 sortinfo_T info;
1686 long len;
1687 long i;
1688
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001689 // Pointer to current info struct used in compare function. Save and
1690 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001691 old_sortinfo = sortinfo;
1692 sortinfo = &info;
1693
1694 if (argvars[0].v_type != VAR_LIST)
1695 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1696 else
1697 {
1698 l = argvars[0].vval.v_list;
Bram Moolenaara187c432020-09-16 21:08:28 +02001699 if (l == NULL || value_check_lock(l->lv_lock,
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001700 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1701 TRUE))
1702 goto theend;
1703 rettv_list_set(rettv, l);
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001704 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001705
1706 len = list_len(l);
1707 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001708 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001709
1710 info.item_compare_ic = FALSE;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001711 info.item_compare_lc = FALSE;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001712 info.item_compare_numeric = FALSE;
1713 info.item_compare_numbers = FALSE;
1714#ifdef FEAT_FLOAT
1715 info.item_compare_float = FALSE;
1716#endif
1717 info.item_compare_func = NULL;
1718 info.item_compare_partial = NULL;
1719 info.item_compare_selfdict = NULL;
1720 if (argvars[1].v_type != VAR_UNKNOWN)
1721 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001722 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001723 if (argvars[1].v_type == VAR_FUNC)
1724 info.item_compare_func = argvars[1].vval.v_string;
1725 else if (argvars[1].v_type == VAR_PARTIAL)
1726 info.item_compare_partial = argvars[1].vval.v_partial;
1727 else
1728 {
1729 int error = FALSE;
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001730 int nr = 0;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001731
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001732 if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001733 {
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001734 nr = tv_get_number_chk(&argvars[1], &error);
1735 if (error)
1736 goto theend; // type error; errmsg already given
1737 if (nr == 1)
1738 info.item_compare_ic = TRUE;
1739 }
1740 if (nr != 1)
1741 {
1742 if (argvars[1].v_type != VAR_NUMBER)
1743 info.item_compare_func = tv_get_string(&argvars[1]);
1744 else if (nr != 0)
1745 {
1746 emsg(_(e_invarg));
1747 goto theend;
1748 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001749 }
1750 if (info.item_compare_func != NULL)
1751 {
1752 if (*info.item_compare_func == NUL)
1753 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001754 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001755 info.item_compare_func = NULL;
1756 }
1757 else if (STRCMP(info.item_compare_func, "n") == 0)
1758 {
1759 info.item_compare_func = NULL;
1760 info.item_compare_numeric = TRUE;
1761 }
1762 else if (STRCMP(info.item_compare_func, "N") == 0)
1763 {
1764 info.item_compare_func = NULL;
1765 info.item_compare_numbers = TRUE;
1766 }
1767#ifdef FEAT_FLOAT
1768 else if (STRCMP(info.item_compare_func, "f") == 0)
1769 {
1770 info.item_compare_func = NULL;
1771 info.item_compare_float = TRUE;
1772 }
1773#endif
1774 else if (STRCMP(info.item_compare_func, "i") == 0)
1775 {
1776 info.item_compare_func = NULL;
1777 info.item_compare_ic = TRUE;
1778 }
Bram Moolenaar55e29612020-11-01 13:57:44 +01001779 else if (STRCMP(info.item_compare_func, "l") == 0)
1780 {
1781 info.item_compare_func = NULL;
1782 info.item_compare_lc = TRUE;
1783 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001784 }
1785 }
1786
1787 if (argvars[2].v_type != VAR_UNKNOWN)
1788 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001789 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001790 if (argvars[2].v_type != VAR_DICT)
1791 {
1792 emsg(_(e_dictreq));
1793 goto theend;
1794 }
1795 info.item_compare_selfdict = argvars[2].vval.v_dict;
1796 }
1797 }
1798
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001799 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001800 ptrs = ALLOC_MULT(sortItem_T, len);
1801 if (ptrs == NULL)
1802 goto theend;
1803
1804 i = 0;
1805 if (sort)
1806 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001807 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001808 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001809 {
1810 ptrs[i].item = li;
1811 ptrs[i].idx = i;
1812 ++i;
1813 }
1814
1815 info.item_compare_func_err = FALSE;
1816 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001817 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001818 if ((info.item_compare_func != NULL
1819 || info.item_compare_partial != NULL)
1820 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
1821 == ITEM_COMPARE_FAIL)
1822 emsg(_("E702: Sort compare function failed"));
1823 else
1824 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001825 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001826 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
1827 info.item_compare_func == NULL
1828 && info.item_compare_partial == NULL
1829 ? item_compare : item_compare2);
1830
1831 if (!info.item_compare_func_err)
1832 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001833 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001834 l->lv_first = l->lv_u.mat.lv_last
1835 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001836 l->lv_len = 0;
1837 for (i = 0; i < len; ++i)
1838 list_append(l, ptrs[i].item);
1839 }
1840 }
1841 }
1842 else
1843 {
1844 int (*item_compare_func_ptr)(const void *, const void *);
1845
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001846 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001847 info.item_compare_func_err = FALSE;
1848 info.item_compare_keep_zero = TRUE;
1849 item_compare_func_ptr = info.item_compare_func != NULL
1850 || info.item_compare_partial != NULL
1851 ? item_compare2 : item_compare;
1852
1853 for (li = l->lv_first; li != NULL && li->li_next != NULL;
1854 li = li->li_next)
1855 {
1856 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
1857 == 0)
1858 ptrs[i++].item = li;
1859 if (info.item_compare_func_err)
1860 {
1861 emsg(_("E882: Uniq compare function failed"));
1862 break;
1863 }
1864 }
1865
1866 if (!info.item_compare_func_err)
1867 {
1868 while (--i >= 0)
1869 {
1870 li = ptrs[i].item->li_next;
1871 ptrs[i].item->li_next = li->li_next;
1872 if (li->li_next != NULL)
1873 li->li_next->li_prev = ptrs[i].item;
1874 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001875 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001876 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001878 l->lv_len--;
1879 }
1880 }
1881 }
1882
1883 vim_free(ptrs);
1884 }
1885theend:
1886 sortinfo = old_sortinfo;
1887}
1888
1889/*
1890 * "sort({list})" function
1891 */
1892 void
1893f_sort(typval_T *argvars, typval_T *rettv)
1894{
1895 do_sort_uniq(argvars, rettv, TRUE);
1896}
1897
1898/*
1899 * "uniq({list})" function
1900 */
1901 void
1902f_uniq(typval_T *argvars, typval_T *rettv)
1903{
1904 do_sort_uniq(argvars, rettv, FALSE);
1905}
1906
Bram Moolenaarea696852020-11-09 18:31:39 +01001907typedef enum {
1908 FILTERMAP_FILTER,
1909 FILTERMAP_MAP,
1910 FILTERMAP_MAPNEW
1911} filtermap_T;
1912
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001913/*
1914 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01001915 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001916 */
1917 static int
Bram Moolenaarea696852020-11-09 18:31:39 +01001918filter_map_one(
1919 typval_T *tv, // original value
1920 typval_T *expr, // callback
1921 filtermap_T filtermap,
1922 typval_T *newtv, // for map() and mapnew(): new value
1923 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001924{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001925 typval_T argv[3];
1926 int retval = FAIL;
1927
1928 copy_tv(tv, get_vim_var_tv(VV_VAL));
1929 argv[0] = *get_vim_var_tv(VV_KEY);
1930 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01001931 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001932 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01001933 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001934 {
1935 int error = FALSE;
1936
1937 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02001938 if (in_vim9script())
Bram Moolenaarea696852020-11-09 18:31:39 +01001939 *remp = !tv2bool(newtv);
Bram Moolenaar56acb092020-08-16 14:48:19 +02001940 else
Bram Moolenaarea696852020-11-09 18:31:39 +01001941 *remp = (tv_get_number_chk(newtv, &error) == 0);
1942 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001943 // On type error, nothing has been removed; return FAIL to stop the
1944 // loop. The error message was given by tv_get_number_chk().
1945 if (error)
1946 goto theend;
1947 }
1948 retval = OK;
1949theend:
1950 clear_tv(get_vim_var_tv(VV_VAL));
1951 return retval;
1952}
1953
1954/*
1955 * Implementation of map() and filter().
1956 */
1957 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01001958filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001959{
1960 typval_T *expr;
1961 listitem_T *li, *nli;
1962 list_T *l = NULL;
1963 dictitem_T *di;
1964 hashtab_T *ht;
1965 hashitem_T *hi;
1966 dict_T *d = NULL;
1967 blob_T *b = NULL;
1968 int rem;
1969 int todo;
Bram Moolenaarea696852020-11-09 18:31:39 +01001970 char_u *ermsg = (char_u *)(filtermap == FILTERMAP_MAP ? "map()"
1971 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
1972 : "filter()");
1973 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
1974 ? N_("map() argument")
1975 : filtermap == FILTERMAP_MAPNEW
1976 ? N_("mapnew() argument")
1977 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001978 int save_did_emsg;
1979 int idx = 0;
1980
Bram Moolenaarea696852020-11-09 18:31:39 +01001981 // map() and filter() return the first argument, also on failure.
1982 if (filtermap != FILTERMAP_MAPNEW)
1983 copy_tv(&argvars[0], rettv);
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02001984
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001985 if (argvars[0].v_type == VAR_BLOB)
1986 {
Bram Moolenaarea696852020-11-09 18:31:39 +01001987 if (filtermap == FILTERMAP_MAPNEW)
1988 {
1989 rettv->v_type = VAR_BLOB;
1990 rettv->vval.v_blob = NULL;
1991 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001992 if ((b = argvars[0].vval.v_blob) == NULL)
1993 return;
1994 }
1995 else if (argvars[0].v_type == VAR_LIST)
1996 {
Bram Moolenaarea696852020-11-09 18:31:39 +01001997 if (filtermap == FILTERMAP_MAPNEW)
1998 {
1999 rettv->v_type = VAR_LIST;
2000 rettv->vval.v_list = NULL;
2001 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002002 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002003 || (filtermap == FILTERMAP_FILTER
2004 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002005 return;
2006 }
2007 else if (argvars[0].v_type == VAR_DICT)
2008 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002009 if (filtermap == FILTERMAP_MAPNEW)
2010 {
2011 rettv->v_type = VAR_DICT;
2012 rettv->vval.v_dict = NULL;
2013 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002014 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002015 || (filtermap == FILTERMAP_FILTER
2016 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002017 return;
2018 }
2019 else
2020 {
Bram Moolenaarfcb0b612020-05-26 20:22:01 +02002021 semsg(_(e_listdictblobarg), ermsg);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002022 return;
2023 }
2024
2025 expr = &argvars[1];
2026 // On type errors, the preceding call has already displayed an error
2027 // message. Avoid a misleading error message for an empty string that
2028 // was not passed as argument.
2029 if (expr->v_type != VAR_UNKNOWN)
2030 {
2031 typval_T save_val;
2032 typval_T save_key;
2033
2034 prepare_vimvar(VV_VAL, &save_val);
2035 prepare_vimvar(VV_KEY, &save_key);
2036
2037 // We reset "did_emsg" to be able to detect whether an error
2038 // occurred during evaluation of the expression.
2039 save_did_emsg = did_emsg;
2040 did_emsg = FALSE;
2041
2042 if (argvars[0].v_type == VAR_DICT)
2043 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002044 int prev_lock = d->dv_lock;
Bram Moolenaarea696852020-11-09 18:31:39 +01002045 dict_T *d_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002046
Bram Moolenaarea696852020-11-09 18:31:39 +01002047 if (filtermap == FILTERMAP_MAPNEW)
2048 {
2049 if (rettv_dict_alloc(rettv) == FAIL)
2050 return;
2051 d_ret = rettv->vval.v_dict;
2052 }
2053
2054 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002055 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002056 ht = &d->dv_hashtab;
2057 hash_lock(ht);
2058 todo = (int)ht->ht_used;
2059 for (hi = ht->ht_array; todo > 0; ++hi)
2060 {
2061 if (!HASHITEM_EMPTY(hi))
2062 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002063 int r;
2064 typval_T newtv;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002065
2066 --todo;
2067 di = HI2DI(hi);
Bram Moolenaarea696852020-11-09 18:31:39 +01002068 if (filtermap != FILTERMAP_FILTER
2069 && (value_check_lock(di->di_tv.v_lock,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002070 arg_errmsg, TRUE)
2071 || var_check_ro(di->di_flags,
2072 arg_errmsg, TRUE)))
2073 break;
2074 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaarea696852020-11-09 18:31:39 +01002075 r = filter_map_one(&di->di_tv, expr, filtermap,
2076 &newtv, &rem);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002077 clear_tv(get_vim_var_tv(VV_KEY));
2078 if (r == FAIL || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002079 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002080 clear_tv(&newtv);
2081 break;
2082 }
2083 if (filtermap == FILTERMAP_MAP)
2084 {
2085 // map(): replace the dict item value
2086 clear_tv(&di->di_tv);
2087 newtv.v_lock = 0;
2088 di->di_tv = newtv;
2089 }
2090 else if (filtermap == FILTERMAP_MAPNEW)
2091 {
2092 // mapnew(): add the item value to the new dict
2093 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
2094 clear_tv(&newtv);
2095 if (r == FAIL)
2096 break;
2097 }
2098 else if (filtermap == FILTERMAP_FILTER && rem)
2099 {
2100 // filter(false): remove the item from the dict
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002101 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2102 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2103 break;
2104 dictitem_remove(d, di);
2105 }
2106 }
2107 }
2108 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002109 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002110 }
2111 else if (argvars[0].v_type == VAR_BLOB)
2112 {
2113 int i;
2114 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002115 varnumber_T val;
Bram Moolenaarea696852020-11-09 18:31:39 +01002116 blob_T *b_ret = b;
2117
2118 if (filtermap == FILTERMAP_MAPNEW)
2119 {
2120 if (blob_copy(b, rettv) == FAIL)
2121 return;
2122 b_ret = rettv->vval.v_blob;
2123 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002124
2125 // set_vim_var_nr() doesn't set the type
2126 set_vim_var_type(VV_KEY, VAR_NUMBER);
2127
2128 for (i = 0; i < b->bv_ga.ga_len; i++)
2129 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002130 typval_T newtv;
2131
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002132 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002133 val = blob_get(b, i);
2134 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002135 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaarea696852020-11-09 18:31:39 +01002136 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
2137 || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002138 break;
Bram Moolenaarea696852020-11-09 18:31:39 +01002139 if (newtv.v_type != VAR_NUMBER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002140 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002141 clear_tv(&newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002142 emsg(_(e_invalblob));
2143 break;
2144 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002145 if (filtermap != FILTERMAP_FILTER)
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002146 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002147 if (newtv.vval.v_number != val)
2148 blob_set(b_ret, i, newtv.vval.v_number);
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002149 }
2150 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002151 {
2152 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2153
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002154 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002155 (size_t)b->bv_ga.ga_len - i - 1);
2156 --b->bv_ga.ga_len;
2157 --i;
2158 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002159 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002160 }
2161 }
2162 else // argvars[0].v_type == VAR_LIST
2163 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002164 int prev_lock = l->lv_lock;
2165 list_T *l_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002166
Bram Moolenaarea696852020-11-09 18:31:39 +01002167 if (filtermap == FILTERMAP_MAPNEW)
2168 {
2169 if (rettv_list_alloc(rettv) == FAIL)
2170 return;
2171 l_ret = rettv->vval.v_list;
2172 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002173 // set_vim_var_nr() doesn't set the type
2174 set_vim_var_type(VV_KEY, VAR_NUMBER);
2175
Bram Moolenaarea696852020-11-09 18:31:39 +01002176 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002177 l->lv_lock = VAR_LOCKED;
Bram Moolenaarea696852020-11-09 18:31:39 +01002178
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002179 if (l->lv_first == &range_list_item)
2180 {
2181 varnumber_T val = l->lv_u.nonmat.lv_start;
2182 int len = l->lv_len;
2183 int stride = l->lv_u.nonmat.lv_stride;
2184
2185 // List from range(): loop over the numbers
2186 l->lv_first = NULL;
2187 l->lv_u.mat.lv_last = NULL;
2188 l->lv_len = 0;
2189 l->lv_u.mat.lv_idx_item = NULL;
2190
2191 for (idx = 0; idx < len; ++idx)
Bram Moolenaarc56936e2020-11-10 11:43:56 +01002192 {
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002193 typval_T tv;
2194 typval_T newtv;
2195
2196 tv.v_type = VAR_NUMBER;
2197 tv.v_lock = 0;
2198 tv.vval.v_number = val;
2199 set_vim_var_nr(VV_KEY, idx);
2200 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2201 == FAIL)
Bram Moolenaarea696852020-11-09 18:31:39 +01002202 break;
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002203 if (did_emsg)
2204 {
2205 clear_tv(&newtv);
2206 break;
2207 }
2208 if (filtermap != FILTERMAP_FILTER)
2209 {
2210 // map(), mapnew(): always append the new value to the
2211 // list
2212 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2213 ? l : l_ret, &newtv) == FAIL)
2214 break;
2215 }
2216 else if (!rem)
2217 {
2218 // filter(): append the list item value when not rem
2219 if (list_append_tv_move(l, &tv) == FAIL)
2220 break;
2221 }
2222
2223 val += stride;
Bram Moolenaarea696852020-11-09 18:31:39 +01002224 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002225 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002226 else
2227 {
2228 // Materialized list from range(): loop over the items
2229 for (li = l->lv_first; li != NULL; li = nli)
2230 {
2231 typval_T newtv;
2232
2233 if (filtermap != FILTERMAP_FILTER && value_check_lock(
2234 li->li_tv.v_lock, arg_errmsg, TRUE))
2235 break;
2236 nli = li->li_next;
2237 set_vim_var_nr(VV_KEY, idx);
2238 if (filter_map_one(&li->li_tv, expr, filtermap,
2239 &newtv, &rem) == FAIL)
2240 break;
2241 if (did_emsg)
2242 {
2243 clear_tv(&newtv);
2244 break;
2245 }
2246 if (filtermap == FILTERMAP_MAP)
2247 {
2248 // map(): replace the list item value
2249 clear_tv(&li->li_tv);
2250 newtv.v_lock = 0;
2251 li->li_tv = newtv;
2252 }
2253 else if (filtermap == FILTERMAP_MAPNEW)
2254 {
2255 // mapnew(): append the list item value
2256 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2257 break;
2258 }
2259 else if (filtermap == FILTERMAP_FILTER && rem)
2260 listitem_remove(l, li);
2261 ++idx;
2262 }
2263 }
2264
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002265 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002266 }
2267
2268 restore_vimvar(VV_KEY, &save_key);
2269 restore_vimvar(VV_VAL, &save_val);
2270
2271 did_emsg |= save_did_emsg;
2272 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002273}
2274
2275/*
2276 * "filter()" function
2277 */
2278 void
2279f_filter(typval_T *argvars, typval_T *rettv)
2280{
Bram Moolenaarea696852020-11-09 18:31:39 +01002281 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002282}
2283
2284/*
2285 * "map()" function
2286 */
2287 void
2288f_map(typval_T *argvars, typval_T *rettv)
2289{
Bram Moolenaarea696852020-11-09 18:31:39 +01002290 filter_map(argvars, rettv, FILTERMAP_MAP);
2291}
2292
2293/*
2294 * "mapnew()" function
2295 */
2296 void
2297f_mapnew(typval_T *argvars, typval_T *rettv)
2298{
2299 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002300}
2301
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002302/*
2303 * "add(list, item)" function
2304 */
2305 void
2306f_add(typval_T *argvars, typval_T *rettv)
2307{
2308 list_T *l;
2309 blob_T *b;
2310
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002311 rettv->vval.v_number = 1; // Default: Failed
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002312 if (argvars[0].v_type == VAR_LIST)
2313 {
2314 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002315 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002316 (char_u *)N_("add() argument"), TRUE)
2317 && list_append_tv(l, &argvars[1]) == OK)
2318 copy_tv(&argvars[0], rettv);
2319 }
2320 else if (argvars[0].v_type == VAR_BLOB)
2321 {
2322 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002323 && !value_check_lock(b->bv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002324 (char_u *)N_("add() argument"), TRUE))
2325 {
2326 int error = FALSE;
2327 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2328
2329 if (!error)
2330 {
2331 ga_append(&b->bv_ga, (int)n);
2332 copy_tv(&argvars[0], rettv);
2333 }
2334 }
2335 }
2336 else
2337 emsg(_(e_listblobreq));
2338}
2339
2340/*
2341 * "count()" function
2342 */
2343 void
2344f_count(typval_T *argvars, typval_T *rettv)
2345{
2346 long n = 0;
2347 int ic = FALSE;
2348 int error = FALSE;
2349
2350 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002351 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002352
2353 if (argvars[0].v_type == VAR_STRING)
2354 {
2355 char_u *expr = tv_get_string_chk(&argvars[1]);
2356 char_u *p = argvars[0].vval.v_string;
2357 char_u *next;
2358
2359 if (!error && expr != NULL && *expr != NUL && p != NULL)
2360 {
2361 if (ic)
2362 {
2363 size_t len = STRLEN(expr);
2364
2365 while (*p != NUL)
2366 {
2367 if (MB_STRNICMP(p, expr, len) == 0)
2368 {
2369 ++n;
2370 p += len;
2371 }
2372 else
2373 MB_PTR_ADV(p);
2374 }
2375 }
2376 else
2377 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2378 != NULL)
2379 {
2380 ++n;
2381 p = next + STRLEN(expr);
2382 }
2383 }
2384
2385 }
2386 else if (argvars[0].v_type == VAR_LIST)
2387 {
2388 listitem_T *li;
2389 list_T *l;
2390 long idx;
2391
2392 if ((l = argvars[0].vval.v_list) != NULL)
2393 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002394 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002395 li = l->lv_first;
2396 if (argvars[2].v_type != VAR_UNKNOWN)
2397 {
2398 if (argvars[3].v_type != VAR_UNKNOWN)
2399 {
2400 idx = (long)tv_get_number_chk(&argvars[3], &error);
2401 if (!error)
2402 {
2403 li = list_find(l, idx);
2404 if (li == NULL)
2405 semsg(_(e_listidx), idx);
2406 }
2407 }
2408 if (error)
2409 li = NULL;
2410 }
2411
2412 for ( ; li != NULL; li = li->li_next)
2413 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2414 ++n;
2415 }
2416 }
2417 else if (argvars[0].v_type == VAR_DICT)
2418 {
2419 int todo;
2420 dict_T *d;
2421 hashitem_T *hi;
2422
2423 if ((d = argvars[0].vval.v_dict) != NULL)
2424 {
2425 if (argvars[2].v_type != VAR_UNKNOWN)
2426 {
2427 if (argvars[3].v_type != VAR_UNKNOWN)
2428 emsg(_(e_invarg));
2429 }
2430
2431 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2432 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2433 {
2434 if (!HASHITEM_EMPTY(hi))
2435 {
2436 --todo;
2437 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2438 ++n;
2439 }
2440 }
2441 }
2442 }
2443 else
2444 semsg(_(e_listdictarg), "count()");
2445 rettv->vval.v_number = n;
2446}
2447
2448/*
2449 * "extend(list, list [, idx])" function
2450 * "extend(dict, dict [, action])" function
2451 */
2452 void
2453f_extend(typval_T *argvars, typval_T *rettv)
2454{
2455 char_u *arg_errmsg = (char_u *)N_("extend() argument");
2456
2457 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2458 {
2459 list_T *l1, *l2;
2460 listitem_T *item;
2461 long before;
2462 int error = FALSE;
2463
2464 l1 = argvars[0].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002465 if (l1 == NULL)
2466 {
2467 emsg(_(e_cannot_extend_null_list));
2468 return;
2469 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002470 l2 = argvars[1].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002471 if (!value_check_lock(l1->lv_lock, arg_errmsg, TRUE) && l2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002472 {
2473 if (argvars[2].v_type != VAR_UNKNOWN)
2474 {
2475 before = (long)tv_get_number_chk(&argvars[2], &error);
2476 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002477 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002478
2479 if (before == l1->lv_len)
2480 item = NULL;
2481 else
2482 {
2483 item = list_find(l1, before);
2484 if (item == NULL)
2485 {
2486 semsg(_(e_listidx), before);
2487 return;
2488 }
2489 }
2490 }
2491 else
2492 item = NULL;
2493 list_extend(l1, l2, item);
2494
2495 copy_tv(&argvars[0], rettv);
2496 }
2497 }
2498 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2499 {
2500 dict_T *d1, *d2;
2501 char_u *action;
2502 int i;
2503
2504 d1 = argvars[0].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002505 if (d1 == NULL)
2506 {
2507 emsg(_(e_cannot_extend_null_dict));
2508 return;
2509 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002510 d2 = argvars[1].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002511 if (!value_check_lock(d1->dv_lock, arg_errmsg, TRUE) && d2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002512 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002513 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002514 if (argvars[2].v_type != VAR_UNKNOWN)
2515 {
2516 static char *(av[]) = {"keep", "force", "error"};
2517
2518 action = tv_get_string_chk(&argvars[2]);
2519 if (action == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002520 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002521 for (i = 0; i < 3; ++i)
2522 if (STRCMP(action, av[i]) == 0)
2523 break;
2524 if (i == 3)
2525 {
2526 semsg(_(e_invarg2), action);
2527 return;
2528 }
2529 }
2530 else
2531 action = (char_u *)"force";
2532
2533 dict_extend(d1, d2, action);
2534
2535 copy_tv(&argvars[0], rettv);
2536 }
2537 }
2538 else
2539 semsg(_(e_listdictarg), "extend()");
2540}
2541
2542/*
2543 * "insert()" function
2544 */
2545 void
2546f_insert(typval_T *argvars, typval_T *rettv)
2547{
2548 long before = 0;
2549 listitem_T *item;
2550 list_T *l;
2551 int error = FALSE;
2552
2553 if (argvars[0].v_type == VAR_BLOB)
2554 {
2555 int val, len;
2556 char_u *p;
2557
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002558 if (argvars[0].vval.v_blob == NULL)
2559 return;
2560
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002561 len = blob_len(argvars[0].vval.v_blob);
2562 if (argvars[2].v_type != VAR_UNKNOWN)
2563 {
2564 before = (long)tv_get_number_chk(&argvars[2], &error);
2565 if (error)
2566 return; // type error; errmsg already given
2567 if (before < 0 || before > len)
2568 {
2569 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2570 return;
2571 }
2572 }
2573 val = tv_get_number_chk(&argvars[1], &error);
2574 if (error)
2575 return;
2576 if (val < 0 || val > 255)
2577 {
2578 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2579 return;
2580 }
2581
2582 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
2583 return;
2584 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2585 mch_memmove(p + before + 1, p + before, (size_t)len - before);
2586 *(p + before) = val;
2587 ++argvars[0].vval.v_blob->bv_ga.ga_len;
2588
2589 copy_tv(&argvars[0], rettv);
2590 }
2591 else if (argvars[0].v_type != VAR_LIST)
2592 semsg(_(e_listblobarg), "insert()");
2593 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002594 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002595 (char_u *)N_("insert() argument"), TRUE))
2596 {
2597 if (argvars[2].v_type != VAR_UNKNOWN)
2598 before = (long)tv_get_number_chk(&argvars[2], &error);
2599 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002600 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002601
2602 if (before == l->lv_len)
2603 item = NULL;
2604 else
2605 {
2606 item = list_find(l, before);
2607 if (item == NULL)
2608 {
2609 semsg(_(e_listidx), before);
2610 l = NULL;
2611 }
2612 }
2613 if (l != NULL)
2614 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02002615 (void)list_insert_tv(l, &argvars[1], item);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002616 copy_tv(&argvars[0], rettv);
2617 }
2618 }
2619}
2620
2621/*
2622 * "remove()" function
2623 */
2624 void
2625f_remove(typval_T *argvars, typval_T *rettv)
2626{
2627 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2628
2629 if (argvars[0].v_type == VAR_DICT)
2630 dict_remove(argvars, rettv, arg_errmsg);
2631 else if (argvars[0].v_type == VAR_BLOB)
2632 blob_remove(argvars, rettv);
2633 else if (argvars[0].v_type == VAR_LIST)
2634 list_remove(argvars, rettv, arg_errmsg);
2635 else
2636 semsg(_(e_listdictblobarg), "remove()");
2637}
2638
2639/*
2640 * "reverse({list})" function
2641 */
2642 void
2643f_reverse(typval_T *argvars, typval_T *rettv)
2644{
2645 list_T *l;
2646 listitem_T *li, *ni;
2647
2648 if (argvars[0].v_type == VAR_BLOB)
2649 {
2650 blob_T *b = argvars[0].vval.v_blob;
2651 int i, len = blob_len(b);
2652
2653 for (i = 0; i < len / 2; i++)
2654 {
2655 int tmp = blob_get(b, i);
2656
2657 blob_set(b, i, blob_get(b, len - i - 1));
2658 blob_set(b, len - i - 1, tmp);
2659 }
2660 rettv_blob_set(rettv, b);
2661 return;
2662 }
2663
2664 if (argvars[0].v_type != VAR_LIST)
2665 semsg(_(e_listblobarg), "reverse()");
2666 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002667 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002668 (char_u *)N_("reverse() argument"), TRUE))
2669 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002670 if (l->lv_first == &range_list_item)
2671 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002672 varnumber_T new_start = l->lv_u.nonmat.lv_start
2673 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2674 l->lv_u.nonmat.lv_end = new_start
2675 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2676 l->lv_u.nonmat.lv_start = new_start;
2677 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002678 rettv_list_set(rettv, l);
2679 return;
2680 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002681 li = l->lv_u.mat.lv_last;
2682 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002683 l->lv_len = 0;
2684 while (li != NULL)
2685 {
2686 ni = li->li_prev;
2687 list_append(l, li);
2688 li = ni;
2689 }
2690 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002691 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002692 }
2693}
2694
Bram Moolenaar85629982020-06-01 18:39:20 +02002695/*
2696 * "reduce(list, { accumlator, element -> value } [, initial])" function
2697 */
2698 void
2699f_reduce(typval_T *argvars, typval_T *rettv)
2700{
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002701 typval_T initial;
Bram Moolenaar85629982020-06-01 18:39:20 +02002702 char_u *func_name;
2703 partial_T *partial = NULL;
2704 funcexe_T funcexe;
2705 typval_T argv[3];
2706
2707 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
2708 {
2709 emsg(_(e_listblobreq));
2710 return;
2711 }
2712
2713 if (argvars[1].v_type == VAR_FUNC)
2714 func_name = argvars[1].vval.v_string;
2715 else if (argvars[1].v_type == VAR_PARTIAL)
2716 {
2717 partial = argvars[1].vval.v_partial;
2718 func_name = partial_name(partial);
2719 }
2720 else
2721 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01002722 if (func_name == NULL || *func_name == NUL)
2723 {
2724 emsg(_(e_missing_function_argument));
2725 return;
2726 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002727
2728 vim_memset(&funcexe, 0, sizeof(funcexe));
2729 funcexe.evaluate = TRUE;
2730 funcexe.partial = partial;
2731
2732 if (argvars[0].v_type == VAR_LIST)
2733 {
2734 list_T *l = argvars[0].vval.v_list;
2735 listitem_T *li = NULL;
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002736 int r;
Bram Moolenaarca275a02020-06-24 22:07:46 +02002737 int called_emsg_start = called_emsg;
Bram Moolenaar85629982020-06-01 18:39:20 +02002738
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002739 if (l != NULL)
2740 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar85629982020-06-01 18:39:20 +02002741 if (argvars[2].v_type == VAR_UNKNOWN)
2742 {
2743 if (l == NULL || l->lv_first == NULL)
2744 {
2745 semsg(_(e_reduceempty), "List");
2746 return;
2747 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002748 initial = l->lv_first->li_tv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002749 li = l->lv_first->li_next;
2750 }
2751 else
2752 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002753 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002754 if (l != NULL)
2755 li = l->lv_first;
2756 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002757 copy_tv(&initial, rettv);
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002758
2759 if (l != NULL)
Bram Moolenaar85629982020-06-01 18:39:20 +02002760 {
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002761 int prev_locked = l->lv_lock;
2762
2763 l->lv_lock = VAR_FIXED; // disallow the list changing here
2764 for ( ; li != NULL; li = li->li_next)
2765 {
2766 argv[0] = *rettv;
2767 argv[1] = li->li_tv;
2768 rettv->v_type = VAR_UNKNOWN;
2769 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
2770 clear_tv(&argv[0]);
2771 if (r == FAIL || called_emsg != called_emsg_start)
2772 break;
2773 }
2774 l->lv_lock = prev_locked;
Bram Moolenaar85629982020-06-01 18:39:20 +02002775 }
2776 }
2777 else
2778 {
2779 blob_T *b = argvars[0].vval.v_blob;
2780 int i;
2781
2782 if (argvars[2].v_type == VAR_UNKNOWN)
2783 {
2784 if (b == NULL || b->bv_ga.ga_len == 0)
2785 {
2786 semsg(_(e_reduceempty), "Blob");
2787 return;
2788 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002789 initial.v_type = VAR_NUMBER;
2790 initial.vval.v_number = blob_get(b, 0);
Bram Moolenaar85629982020-06-01 18:39:20 +02002791 i = 1;
2792 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002793 else if (argvars[2].v_type != VAR_NUMBER)
2794 {
2795 emsg(_(e_number_exp));
2796 return;
2797 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002798 else
2799 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002800 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002801 i = 0;
2802 }
2803
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002804 copy_tv(&initial, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02002805 if (b != NULL)
2806 {
2807 for ( ; i < b->bv_ga.ga_len; i++)
2808 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002809 argv[0] = *rettv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002810 argv[1].v_type = VAR_NUMBER;
2811 argv[1].vval.v_number = blob_get(b, i);
2812 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
2813 return;
Bram Moolenaar85629982020-06-01 18:39:20 +02002814 }
2815 }
2816 }
2817}
2818
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002819#endif // defined(FEAT_EVAL)