blob: 2b44ebacb85d670d8382450a25296ecdff7ac0ab [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
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100273 free_type(l->lv_type);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200274 vim_free(l);
275}
276
277 void
278list_free_items(int copyID)
279{
280 list_T *ll, *ll_next;
281
282 for (ll = first_list; ll != NULL; ll = ll_next)
283 {
284 ll_next = ll->lv_used_next;
285 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
286 && ll->lv_watch == NULL)
287 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100288 // Free the List and ordinary items it contains, but don't recurse
289 // into Lists and Dictionaries, they will be in the list of dicts
290 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200291 list_free_list(ll);
292 }
293 }
294}
295
296 void
297list_free(list_T *l)
298{
299 if (!in_free_unref_items)
300 {
301 list_free_contents(l);
302 list_free_list(l);
303 }
304}
305
306/*
307 * Allocate a list item.
308 * It is not initialized, don't forget to set v_lock.
309 */
310 listitem_T *
311listitem_alloc(void)
312{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200313 return ALLOC_ONE(listitem_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200314}
315
316/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100317 * Free a list item, unless it was allocated together with the list itself.
318 * Does not clear the value. Does not notify watchers.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200319 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200320 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100321list_free_item(list_T *l, listitem_T *item)
322{
323 if (l->lv_with_items == 0 || item < (listitem_T *)l
324 || item >= (listitem_T *)(l + 1) + l->lv_with_items)
325 vim_free(item);
326}
327
328/*
329 * Free a list item, unless it was allocated together with the list itself.
330 * Also clears the value. Does not notify watchers.
331 */
332 void
333listitem_free(list_T *l, listitem_T *item)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200334{
335 clear_tv(&item->li_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 list_free_item(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200337}
338
339/*
340 * Remove a list item from a List and free it. Also clears the value.
341 */
342 void
343listitem_remove(list_T *l, listitem_T *item)
344{
345 vimlist_remove(l, item, item);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 listitem_free(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200347}
348
349/*
350 * Get the number of items in a list.
351 */
352 long
353list_len(list_T *l)
354{
355 if (l == NULL)
356 return 0L;
357 return l->lv_len;
358}
359
360/*
361 * Return TRUE when two lists have exactly the same values.
362 */
363 int
364list_equal(
365 list_T *l1,
366 list_T *l2,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100367 int ic, // ignore case for strings
368 int recursive) // TRUE when used recursively
Bram Moolenaarda861d62016-07-17 15:46:27 +0200369{
370 listitem_T *item1, *item2;
371
Bram Moolenaarda861d62016-07-17 15:46:27 +0200372 if (l1 == l2)
373 return TRUE;
374 if (list_len(l1) != list_len(l2))
375 return FALSE;
Bram Moolenaar7b293c72020-04-09 21:33:22 +0200376 if (list_len(l1) == 0)
377 // empty and NULL list are considered equal
378 return TRUE;
379 if (l1 == NULL || l2 == NULL)
380 return FALSE;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200381
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200382 CHECK_LIST_MATERIALIZE(l1);
383 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384
Bram Moolenaarda861d62016-07-17 15:46:27 +0200385 for (item1 = l1->lv_first, item2 = l2->lv_first;
386 item1 != NULL && item2 != NULL;
387 item1 = item1->li_next, item2 = item2->li_next)
388 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
389 return FALSE;
390 return item1 == NULL && item2 == NULL;
391}
392
393/*
394 * Locate item with index "n" in list "l" and return it.
395 * A negative index is counted from the end; -1 is the last item.
396 * Returns NULL when "n" is out of range.
397 */
398 listitem_T *
399list_find(list_T *l, long n)
400{
401 listitem_T *item;
402 long idx;
403
404 if (l == NULL)
405 return NULL;
406
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100407 // Negative index is relative to the end.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200408 if (n < 0)
409 n = l->lv_len + n;
410
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100411 // Check for index out of range.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200412 if (n < 0 || n >= l->lv_len)
413 return NULL;
414
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200415 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100417 // When there is a cached index may start search from there.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100418 if (l->lv_u.mat.lv_idx_item != NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200419 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100420 if (n < l->lv_u.mat.lv_idx / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200421 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100422 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200423 item = l->lv_first;
424 idx = 0;
425 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100426 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200427 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100428 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100429 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200430 idx = l->lv_len - 1;
431 }
432 else
433 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100434 // closest to the cached index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100435 item = l->lv_u.mat.lv_idx_item;
436 idx = l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200437 }
438 }
439 else
440 {
441 if (n < l->lv_len / 2)
442 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100443 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200444 item = l->lv_first;
445 idx = 0;
446 }
447 else
448 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100449 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100450 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200451 idx = l->lv_len - 1;
452 }
453 }
454
455 while (n > idx)
456 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100457 // search forward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200458 item = item->li_next;
459 ++idx;
460 }
461 while (n < idx)
462 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100463 // search backward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200464 item = item->li_prev;
465 --idx;
466 }
467
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100468 // cache the used index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100469 l->lv_u.mat.lv_idx = idx;
470 l->lv_u.mat.lv_idx_item = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200471
472 return item;
473}
474
475/*
476 * Get list item "l[idx]" as a number.
477 */
478 long
479list_find_nr(
480 list_T *l,
481 long idx,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100482 int *errorp) // set to TRUE when something wrong
Bram Moolenaarda861d62016-07-17 15:46:27 +0200483{
484 listitem_T *li;
485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486 if (l != NULL && l->lv_first == &range_list_item)
487 {
488 long n = idx;
489
490 // not materialized range() list: compute the value.
491 // Negative index is relative to the end.
492 if (n < 0)
493 n = l->lv_len + n;
494
495 // Check for index out of range.
496 if (n < 0 || n >= l->lv_len)
497 {
498 if (errorp != NULL)
499 *errorp = TRUE;
500 return -1L;
501 }
502
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100503 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 }
505
Bram Moolenaarda861d62016-07-17 15:46:27 +0200506 li = list_find(l, idx);
507 if (li == NULL)
508 {
509 if (errorp != NULL)
510 *errorp = TRUE;
511 return -1L;
512 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100513 return (long)tv_get_number_chk(&li->li_tv, errorp);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200514}
515
516/*
517 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
518 */
519 char_u *
520list_find_str(list_T *l, long idx)
521{
522 listitem_T *li;
523
524 li = list_find(l, idx - 1);
525 if (li == NULL)
526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 semsg(_(e_listidx), idx);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200528 return NULL;
529 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100530 return tv_get_string(&li->li_tv);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200531}
532
533/*
534 * Locate "item" list "l" and return its index.
535 * Returns -1 when "item" is not in the list.
536 */
537 long
538list_idx_of_item(list_T *l, listitem_T *item)
539{
540 long idx = 0;
541 listitem_T *li;
542
543 if (l == NULL)
544 return -1;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200545 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200546 idx = 0;
547 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
548 ++idx;
549 if (li == NULL)
550 return -1;
551 return idx;
552}
553
554/*
555 * Append item "item" to the end of list "l".
556 */
557 void
558list_append(list_T *l, listitem_T *item)
559{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200560 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100561 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200562 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100563 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200564 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100565 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200566 item->li_prev = NULL;
567 }
568 else
569 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100570 l->lv_u.mat.lv_last->li_next = item;
571 item->li_prev = l->lv_u.mat.lv_last;
572 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200573 }
574 ++l->lv_len;
575 item->li_next = NULL;
576}
577
578/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200580 * Return FAIL when out of memory.
581 */
582 int
583list_append_tv(list_T *l, typval_T *tv)
584{
585 listitem_T *li = listitem_alloc();
586
587 if (li == NULL)
588 return FAIL;
589 copy_tv(tv, &li->li_tv);
590 list_append(l, li);
591 return OK;
592}
593
594/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595 * As list_append_tv() but move the value instead of copying it.
596 * Return FAIL when out of memory.
597 */
598 int
599list_append_tv_move(list_T *l, typval_T *tv)
600{
601 listitem_T *li = listitem_alloc();
602
603 if (li == NULL)
604 return FAIL;
605 li->li_tv = *tv;
606 list_append(l, li);
607 return OK;
608}
609
610/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200611 * Add a dictionary to a list. Used by getqflist().
612 * Return FAIL when out of memory.
613 */
614 int
615list_append_dict(list_T *list, dict_T *dict)
616{
617 listitem_T *li = listitem_alloc();
618
619 if (li == NULL)
620 return FAIL;
621 li->li_tv.v_type = VAR_DICT;
622 li->li_tv.v_lock = 0;
623 li->li_tv.vval.v_dict = dict;
624 list_append(list, li);
625 ++dict->dv_refcount;
626 return OK;
627}
628
629/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100630 * Append list2 to list1.
631 * Return FAIL when out of memory.
632 */
633 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200634list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100635{
636 listitem_T *li = listitem_alloc();
637
638 if (li == NULL)
639 return FAIL;
640 li->li_tv.v_type = VAR_LIST;
641 li->li_tv.v_lock = 0;
642 li->li_tv.vval.v_list = list2;
643 list_append(list1, li);
644 ++list2->lv_refcount;
645 return OK;
646}
647
648/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200649 * Make a copy of "str" and append it as an item to list "l".
650 * When "len" >= 0 use "str[len]".
651 * Returns FAIL when out of memory.
652 */
653 int
654list_append_string(list_T *l, char_u *str, int len)
655{
656 listitem_T *li = listitem_alloc();
657
658 if (li == NULL)
659 return FAIL;
660 list_append(l, li);
661 li->li_tv.v_type = VAR_STRING;
662 li->li_tv.v_lock = 0;
663 if (str == NULL)
664 li->li_tv.vval.v_string = NULL;
665 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
666 : vim_strsave(str))) == NULL)
667 return FAIL;
668 return OK;
669}
670
671/*
672 * Append "n" to list "l".
673 * Returns FAIL when out of memory.
674 */
675 int
676list_append_number(list_T *l, varnumber_T n)
677{
678 listitem_T *li;
679
680 li = listitem_alloc();
681 if (li == NULL)
682 return FAIL;
683 li->li_tv.v_type = VAR_NUMBER;
684 li->li_tv.v_lock = 0;
685 li->li_tv.vval.v_number = n;
686 list_append(l, li);
687 return OK;
688}
689
690/*
691 * Insert typval_T "tv" in list "l" before "item".
692 * If "item" is NULL append at the end.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100693 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200694 */
695 int
696list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
697{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100698 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200699
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100700 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
701 && check_typval_type(l->lv_type->tt_member, tv, 0) == FAIL)
702 return FAIL;
703 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200704 if (ni == NULL)
705 return FAIL;
706 copy_tv(tv, &ni->li_tv);
707 list_insert(l, ni, item);
708 return OK;
709}
710
711 void
712list_insert(list_T *l, listitem_T *ni, listitem_T *item)
713{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200714 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200715 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100716 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200717 list_append(l, ni);
718 else
719 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100720 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200721 ni->li_prev = item->li_prev;
722 ni->li_next = item;
723 if (item->li_prev == NULL)
724 {
725 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100726 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200727 }
728 else
729 {
730 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100731 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200732 }
733 item->li_prev = ni;
734 ++l->lv_len;
735 }
736}
737
738/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200739 * Flatten "list" to depth "maxdepth".
740 * It does nothing if "maxdepth" is 0.
741 * Returns FAIL when out of memory.
742 */
743 static int
744list_flatten(list_T *list, long maxdepth)
745{
746 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200747 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200748 int n;
749
750 if (maxdepth == 0)
751 return OK;
752 CHECK_LIST_MATERIALIZE(list);
753
754 n = 0;
755 item = list->lv_first;
756 while (item != NULL)
757 {
758 fast_breakcheck();
759 if (got_int)
760 return FAIL;
761
762 if (item->li_tv.v_type == VAR_LIST)
763 {
764 listitem_T *next = item->li_next;
765
766 vimlist_remove(list, item, item);
767 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
768 return FAIL;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200769 clear_tv(&item->li_tv);
770 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200771
772 if (item->li_prev == NULL)
773 item = list->lv_first;
774 else
775 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200776 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200777
778 if (++n >= maxdepth)
779 {
780 n = 0;
781 item = next;
782 }
783 }
784 else
785 {
786 n = 0;
787 item = item->li_next;
788 }
789 }
790
791 return OK;
792}
793
794/*
795 * "flatten(list[, {maxdepth}])" function
796 */
797 void
798f_flatten(typval_T *argvars, typval_T *rettv)
799{
800 list_T *l;
801 long maxdepth;
802 int error = FALSE;
803
804 if (argvars[0].v_type != VAR_LIST)
805 {
806 semsg(_(e_listarg), "flatten()");
807 return;
808 }
809
810 if (argvars[1].v_type == VAR_UNKNOWN)
811 maxdepth = 999999;
812 else
813 {
814 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
815 if (error)
816 return;
817 if (maxdepth < 0)
818 {
819 emsg(_("E900: maxdepth must be non-negative number"));
820 return;
821 }
822 }
823
824 l = argvars[0].vval.v_list;
Bram Moolenaara187c432020-09-16 21:08:28 +0200825 if (l != NULL && !value_check_lock(l->lv_lock,
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200826 (char_u *)N_("flatten() argument"), TRUE)
827 && list_flatten(l, maxdepth) == OK)
828 copy_tv(&argvars[0], rettv);
829}
830
831/*
Bram Moolenaar1a739232020-10-10 15:37:58 +0200832 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200833 * If "bef" is NULL append at the end, otherwise insert before this item.
834 * Returns FAIL when out of memory.
835 */
836 int
837list_extend(list_T *l1, list_T *l2, listitem_T *bef)
838{
839 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200840 int todo;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200841
Bram Moolenaar1a739232020-10-10 15:37:58 +0200842 // NULL list is equivalent to an empty list: nothing to do.
843 if (l2 == NULL || l2->lv_len == 0)
844 return OK;
845
846 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200847 CHECK_LIST_MATERIALIZE(l1);
848 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100850 // We also quit the loop when we have inserted the original item count of
851 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200852 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
853 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
854 return FAIL;
855 return OK;
856}
857
858/*
859 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
860 * Return FAIL when out of memory.
861 */
862 int
863list_concat(list_T *l1, list_T *l2, typval_T *tv)
864{
865 list_T *l;
866
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100867 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +0200868 if (l1 == NULL)
869 l = list_alloc();
870 else
871 l = list_copy(l1, FALSE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200872 if (l == NULL)
873 return FAIL;
874 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +0100875 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200876 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200877 if (l1 == NULL)
878 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200879
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100880 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200881 return list_extend(l, l2, NULL);
882}
883
Bram Moolenaar9af78762020-06-16 11:34:42 +0200884 list_T *
885list_slice(list_T *ol, long n1, long n2)
886{
887 listitem_T *item;
888 list_T *l = list_alloc();
889
890 if (l == NULL)
891 return NULL;
892 for (item = list_find(ol, n1); n1 <= n2; ++n1)
893 {
894 if (list_append_tv(l, &item->li_tv) == FAIL)
895 {
896 list_free(l);
897 return NULL;
898 }
899 item = item->li_next;
900 }
901 return l;
902}
903
Bram Moolenaared591872020-08-15 22:14:53 +0200904 int
905list_slice_or_index(
906 list_T *list,
907 int range,
908 long n1_arg,
909 long n2_arg,
910 typval_T *rettv,
911 int verbose)
912{
913 long len = list_len(list);
914 long n1 = n1_arg;
915 long n2 = n2_arg;
916 typval_T var1;
917
918 if (n1 < 0)
919 n1 = len + n1;
920 if (n1 < 0 || n1 >= len)
921 {
922 // For a range we allow invalid values and return an empty
923 // list. A list index out of range is an error.
924 if (!range)
925 {
926 if (verbose)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100927 semsg(_(e_listidx), n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +0200928 return FAIL;
929 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200930 n1 = n1 < 0 ? 0 : len;
Bram Moolenaared591872020-08-15 22:14:53 +0200931 }
932 if (range)
933 {
934 list_T *l;
935
936 if (n2 < 0)
937 n2 = len + n2;
938 else if (n2 >= len)
939 n2 = len - 1;
940 if (n2 < 0 || n2 + 1 < n1)
941 n2 = -1;
942 l = list_slice(list, n1, n2);
943 if (l == NULL)
944 return FAIL;
945 clear_tv(rettv);
946 rettv_list_set(rettv, l);
947 }
948 else
949 {
950 // copy the item to "var1" to avoid that freeing the list makes it
951 // invalid.
952 copy_tv(&list_find(list, n1)->li_tv, &var1);
953 clear_tv(rettv);
954 *rettv = var1;
955 }
956 return OK;
957}
958
Bram Moolenaarda861d62016-07-17 15:46:27 +0200959/*
960 * Make a copy of list "orig". Shallow if "deep" is FALSE.
961 * The refcount of the new list is set to 1.
962 * See item_copy() for "copyID".
963 * Returns NULL when out of memory.
964 */
965 list_T *
966list_copy(list_T *orig, int deep, int copyID)
967{
968 list_T *copy;
969 listitem_T *item;
970 listitem_T *ni;
971
972 if (orig == NULL)
973 return NULL;
974
975 copy = list_alloc();
976 if (copy != NULL)
977 {
978 if (copyID != 0)
979 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100980 // Do this before adding the items, because one of the items may
981 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200982 orig->lv_copyID = copyID;
983 orig->lv_copylist = copy;
984 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200985 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200986 for (item = orig->lv_first; item != NULL && !got_int;
987 item = item->li_next)
988 {
989 ni = listitem_alloc();
990 if (ni == NULL)
991 break;
992 if (deep)
993 {
994 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
995 {
996 vim_free(ni);
997 break;
998 }
999 }
1000 else
1001 copy_tv(&item->li_tv, &ni->li_tv);
1002 list_append(copy, ni);
1003 }
1004 ++copy->lv_refcount;
1005 if (item != NULL)
1006 {
1007 list_unref(copy);
1008 copy = NULL;
1009 }
1010 }
1011
1012 return copy;
1013}
1014
1015/*
1016 * Remove items "item" to "item2" from list "l".
1017 * Does not free the listitem or the value!
1018 * This used to be called list_remove, but that conflicts with a Sun header
1019 * file.
1020 */
1021 void
1022vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1023{
1024 listitem_T *ip;
1025
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001026 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001028 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001029 for (ip = item; ip != NULL; ip = ip->li_next)
1030 {
1031 --l->lv_len;
1032 list_fix_watch(l, ip);
1033 if (ip == item2)
1034 break;
1035 }
1036
1037 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001038 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001039 else
1040 item2->li_next->li_prev = item->li_prev;
1041 if (item->li_prev == NULL)
1042 l->lv_first = item2->li_next;
1043 else
1044 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001045 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001046}
1047
1048/*
1049 * Return an allocated string with the string representation of a list.
1050 * May return NULL.
1051 */
1052 char_u *
1053list2string(typval_T *tv, int copyID, int restore_copyID)
1054{
1055 garray_T ga;
1056
1057 if (tv->vval.v_list == NULL)
1058 return NULL;
1059 ga_init2(&ga, (int)sizeof(char), 80);
1060 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001061 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001062 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1063 FALSE, restore_copyID, copyID) == FAIL)
1064 {
1065 vim_free(ga.ga_data);
1066 return NULL;
1067 }
1068 ga_append(&ga, ']');
1069 ga_append(&ga, NUL);
1070 return (char_u *)ga.ga_data;
1071}
1072
1073typedef struct join_S {
1074 char_u *s;
1075 char_u *tofree;
1076} join_T;
1077
1078 static int
1079list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001080 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001081 list_T *l,
1082 char_u *sep,
1083 int echo_style,
1084 int restore_copyID,
1085 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001086 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001087{
1088 int i;
1089 join_T *p;
1090 int len;
1091 int sumlen = 0;
1092 int first = TRUE;
1093 char_u *tofree;
1094 char_u numbuf[NUMBUFLEN];
1095 listitem_T *item;
1096 char_u *s;
1097
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001098 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001099 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001100 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1101 {
1102 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001103 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001104 if (s == NULL)
1105 return FAIL;
1106
1107 len = (int)STRLEN(s);
1108 sumlen += len;
1109
1110 (void)ga_grow(join_gap, 1);
1111 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1112 if (tofree != NULL || s != numbuf)
1113 {
1114 p->s = s;
1115 p->tofree = tofree;
1116 }
1117 else
1118 {
1119 p->s = vim_strnsave(s, len);
1120 p->tofree = p->s;
1121 }
1122
1123 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001124 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001125 break;
1126 }
1127
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001128 // Allocate result buffer with its total size, avoid re-allocation and
1129 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001130 if (join_gap->ga_len >= 2)
1131 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1132 if (ga_grow(gap, sumlen + 2) == FAIL)
1133 return FAIL;
1134
1135 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1136 {
1137 if (first)
1138 first = FALSE;
1139 else
1140 ga_concat(gap, sep);
1141 p = ((join_T *)join_gap->ga_data) + i;
1142
1143 if (p->s != NULL)
1144 ga_concat(gap, p->s);
1145 line_breakcheck();
1146 }
1147
1148 return OK;
1149}
1150
1151/*
1152 * Join list "l" into a string in "*gap", using separator "sep".
1153 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1154 * Return FAIL or OK.
1155 */
1156 int
1157list_join(
1158 garray_T *gap,
1159 list_T *l,
1160 char_u *sep,
1161 int echo_style,
1162 int restore_copyID,
1163 int copyID)
1164{
1165 garray_T join_ga;
1166 int retval;
1167 join_T *p;
1168 int i;
1169
1170 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001171 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001172 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1173 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1174 copyID, &join_ga);
1175
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001176 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001177 if (join_ga.ga_data != NULL)
1178 {
1179 p = (join_T *)join_ga.ga_data;
1180 for (i = 0; i < join_ga.ga_len; ++i)
1181 {
1182 vim_free(p->tofree);
1183 ++p;
1184 }
1185 ga_clear(&join_ga);
1186 }
1187
1188 return retval;
1189}
1190
1191/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001192 * "join()" function
1193 */
1194 void
1195f_join(typval_T *argvars, typval_T *rettv)
1196{
1197 garray_T ga;
1198 char_u *sep;
1199
1200 if (argvars[0].v_type != VAR_LIST)
1201 {
1202 emsg(_(e_listreq));
1203 return;
1204 }
1205 if (argvars[0].vval.v_list == NULL)
1206 return;
1207 if (argvars[1].v_type == VAR_UNKNOWN)
1208 sep = (char_u *)" ";
1209 else
1210 sep = tv_get_string_chk(&argvars[1]);
1211
1212 rettv->v_type = VAR_STRING;
1213
1214 if (sep != NULL)
1215 {
1216 ga_init2(&ga, (int)sizeof(char), 80);
1217 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1218 ga_append(&ga, NUL);
1219 rettv->vval.v_string = (char_u *)ga.ga_data;
1220 }
1221 else
1222 rettv->vval.v_string = NULL;
1223}
1224
1225/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001226 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001227 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001228 * Return OK or FAIL.
1229 */
1230 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001231eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001232{
Bram Moolenaar71478202020-06-26 22:46:27 +02001233 int evaluate = evalarg == NULL ? FALSE
1234 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001235 list_T *l = NULL;
1236 typval_T tv;
1237 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001238 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001239 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001240
1241 if (evaluate)
1242 {
1243 l = list_alloc();
1244 if (l == NULL)
1245 return FAIL;
1246 }
1247
Bram Moolenaar962d7212020-07-04 14:15:00 +02001248 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001249 while (**arg != ']' && **arg != NUL)
1250 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001251 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001252 goto failret;
1253 if (evaluate)
1254 {
1255 item = listitem_alloc();
1256 if (item != NULL)
1257 {
1258 item->li_tv = tv;
1259 item->li_tv.v_lock = 0;
1260 list_append(l, item);
1261 }
1262 else
1263 clear_tv(&tv);
1264 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001265 // Legacy Vim script allowed a space before the comma.
1266 if (!vim9script)
1267 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001268
Bram Moolenaare6e03172020-06-27 16:36:05 +02001269 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001270 had_comma = **arg == ',';
1271 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001272 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001273 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001274 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001275 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaare6e03172020-06-27 16:36:05 +02001276 goto failret;
1277 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001278 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001279 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001280
Bram Moolenaare6b53242020-07-01 17:28:33 +02001281 // The "]" can be on the next line. But a double quoted string may
1282 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001283 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001284 if (**arg == ']')
1285 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001286
1287 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001288 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001290 {
1291 if (**arg == ',')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001292 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02001293 else
1294 semsg(_("E696: Missing comma in List: %s"), *arg);
1295 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001296 goto failret;
1297 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001298 }
1299
1300 if (**arg != ']')
1301 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001302 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001303 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001304failret:
1305 if (evaluate)
1306 list_free(l);
1307 return FAIL;
1308 }
1309
Bram Moolenaar9d489562020-07-30 20:08:50 +02001310 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001311 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001312 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001313
1314 return OK;
1315}
1316
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001318 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001319 */
1320 int
1321write_list(FILE *fd, list_T *list, int binary)
1322{
1323 listitem_T *li;
1324 int c;
1325 int ret = OK;
1326 char_u *s;
1327
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001328 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001329 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001330 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001331 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001332 {
1333 if (*s == '\n')
1334 c = putc(NUL, fd);
1335 else
1336 c = putc(*s, fd);
1337 if (c == EOF)
1338 {
1339 ret = FAIL;
1340 break;
1341 }
1342 }
1343 if (!binary || li->li_next != NULL)
1344 if (putc('\n', fd) == EOF)
1345 {
1346 ret = FAIL;
1347 break;
1348 }
1349 if (ret == FAIL)
1350 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001351 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001352 break;
1353 }
1354 }
1355 return ret;
1356}
1357
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001358/*
1359 * Initialize a static list with 10 items.
1360 */
1361 void
1362init_static_list(staticList10_T *sl)
1363{
1364 list_T *l = &sl->sl_list;
1365 int i;
1366
1367 memset(sl, 0, sizeof(staticList10_T));
1368 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001369 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001370 l->lv_refcount = DO_NOT_FREE_CNT;
1371 l->lv_lock = VAR_FIXED;
1372 sl->sl_list.lv_len = 10;
1373
1374 for (i = 0; i < 10; ++i)
1375 {
1376 listitem_T *li = &sl->sl_items[i];
1377
1378 if (i == 0)
1379 li->li_prev = NULL;
1380 else
1381 li->li_prev = li - 1;
1382 if (i == 9)
1383 li->li_next = NULL;
1384 else
1385 li->li_next = li + 1;
1386 }
1387}
1388
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001389/*
1390 * "list2str()" function
1391 */
1392 void
1393f_list2str(typval_T *argvars, typval_T *rettv)
1394{
1395 list_T *l;
1396 listitem_T *li;
1397 garray_T ga;
1398 int utf8 = FALSE;
1399
1400 rettv->v_type = VAR_STRING;
1401 rettv->vval.v_string = NULL;
1402 if (argvars[0].v_type != VAR_LIST)
1403 {
1404 emsg(_(e_invarg));
1405 return;
1406 }
1407
1408 l = argvars[0].vval.v_list;
1409 if (l == NULL)
1410 return; // empty list results in empty string
1411
1412 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001413 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001414
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001415 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001416 ga_init2(&ga, 1, 80);
1417 if (has_mbyte || utf8)
1418 {
1419 char_u buf[MB_MAXBYTES + 1];
1420 int (*char2bytes)(int, char_u *);
1421
1422 if (utf8 || enc_utf8)
1423 char2bytes = utf_char2bytes;
1424 else
1425 char2bytes = mb_char2bytes;
1426
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001427 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001428 {
1429 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1430 ga_concat(&ga, buf);
1431 }
1432 ga_append(&ga, NUL);
1433 }
1434 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1435 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001436 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001437 ga_append(&ga, tv_get_number(&li->li_tv));
1438 ga_append(&ga, NUL);
1439 }
1440
1441 rettv->v_type = VAR_STRING;
1442 rettv->vval.v_string = ga.ga_data;
1443}
1444
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001445 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001446list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1447{
1448 list_T *l;
1449 listitem_T *item, *item2;
1450 listitem_T *li;
1451 int error = FALSE;
1452 int idx;
1453
1454 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001455 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001456 return;
1457
1458 idx = (long)tv_get_number_chk(&argvars[1], &error);
1459 if (error)
1460 ; // type error: do nothing, errmsg already given
1461 else if ((item = list_find(l, idx)) == NULL)
1462 semsg(_(e_listidx), idx);
1463 else
1464 {
1465 if (argvars[2].v_type == VAR_UNKNOWN)
1466 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001467 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001468 vimlist_remove(l, item, item);
1469 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001471 }
1472 else
1473 {
1474 // Remove range of items, return list with values.
1475 int end = (long)tv_get_number_chk(&argvars[2], &error);
1476
1477 if (error)
1478 ; // type error: do nothing
1479 else if ((item2 = list_find(l, end)) == NULL)
1480 semsg(_(e_listidx), end);
1481 else
1482 {
1483 int cnt = 0;
1484
1485 for (li = item; li != NULL; li = li->li_next)
1486 {
1487 ++cnt;
1488 if (li == item2)
1489 break;
1490 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001491 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001492 emsg(_(e_invrange));
1493 else
1494 {
1495 vimlist_remove(l, item, item2);
1496 if (rettv_list_alloc(rettv) == OK)
1497 {
1498 l = rettv->vval.v_list;
1499 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001500 l->lv_u.mat.lv_last = item2;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001501 item->li_prev = NULL;
1502 item2->li_next = NULL;
1503 l->lv_len = cnt;
1504 }
1505 }
1506 }
1507 }
1508 }
1509}
1510
1511static int item_compare(const void *s1, const void *s2);
1512static int item_compare2(const void *s1, const void *s2);
1513
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001514// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001515typedef struct
1516{
1517 listitem_T *item;
1518 int idx;
1519} sortItem_T;
1520
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001521// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001522typedef struct
1523{
1524 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001525 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001526 int item_compare_numeric;
1527 int item_compare_numbers;
1528#ifdef FEAT_FLOAT
1529 int item_compare_float;
1530#endif
1531 char_u *item_compare_func;
1532 partial_T *item_compare_partial;
1533 dict_T *item_compare_selfdict;
1534 int item_compare_func_err;
1535 int item_compare_keep_zero;
1536} sortinfo_T;
1537static sortinfo_T *sortinfo = NULL;
1538#define ITEM_COMPARE_FAIL 999
1539
1540/*
1541 * Compare functions for f_sort() and f_uniq() below.
1542 */
1543 static int
1544item_compare(const void *s1, const void *s2)
1545{
1546 sortItem_T *si1, *si2;
1547 typval_T *tv1, *tv2;
1548 char_u *p1, *p2;
1549 char_u *tofree1 = NULL, *tofree2 = NULL;
1550 int res;
1551 char_u numbuf1[NUMBUFLEN];
1552 char_u numbuf2[NUMBUFLEN];
1553
1554 si1 = (sortItem_T *)s1;
1555 si2 = (sortItem_T *)s2;
1556 tv1 = &si1->item->li_tv;
1557 tv2 = &si2->item->li_tv;
1558
1559 if (sortinfo->item_compare_numbers)
1560 {
1561 varnumber_T v1 = tv_get_number(tv1);
1562 varnumber_T v2 = tv_get_number(tv2);
1563
1564 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1565 }
1566
1567#ifdef FEAT_FLOAT
1568 if (sortinfo->item_compare_float)
1569 {
1570 float_T v1 = tv_get_float(tv1);
1571 float_T v2 = tv_get_float(tv2);
1572
1573 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1574 }
1575#endif
1576
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001577 // tv2string() puts quotes around a string and allocates memory. Don't do
1578 // that for string variables. Use a single quote when comparing with a
1579 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001580 if (tv1->v_type == VAR_STRING)
1581 {
1582 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1583 p1 = (char_u *)"'";
1584 else
1585 p1 = tv1->vval.v_string;
1586 }
1587 else
1588 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1589 if (tv2->v_type == VAR_STRING)
1590 {
1591 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1592 p2 = (char_u *)"'";
1593 else
1594 p2 = tv2->vval.v_string;
1595 }
1596 else
1597 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1598 if (p1 == NULL)
1599 p1 = (char_u *)"";
1600 if (p2 == NULL)
1601 p2 = (char_u *)"";
1602 if (!sortinfo->item_compare_numeric)
1603 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001604 if (sortinfo->item_compare_lc)
1605 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001606 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001607 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001608 }
1609 else
1610 {
1611 double n1, n2;
1612 n1 = strtod((char *)p1, (char **)&p1);
1613 n2 = strtod((char *)p2, (char **)&p2);
1614 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1615 }
1616
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001617 // When the result would be zero, compare the item indexes. Makes the
1618 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001619 if (res == 0 && !sortinfo->item_compare_keep_zero)
1620 res = si1->idx > si2->idx ? 1 : -1;
1621
1622 vim_free(tofree1);
1623 vim_free(tofree2);
1624 return res;
1625}
1626
1627 static int
1628item_compare2(const void *s1, const void *s2)
1629{
1630 sortItem_T *si1, *si2;
1631 int res;
1632 typval_T rettv;
1633 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001634 char_u *func_name;
1635 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001636 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001637
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001638 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001639 if (sortinfo->item_compare_func_err)
1640 return 0;
1641
1642 si1 = (sortItem_T *)s1;
1643 si2 = (sortItem_T *)s2;
1644
1645 if (partial == NULL)
1646 func_name = sortinfo->item_compare_func;
1647 else
1648 func_name = partial_name(partial);
1649
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001650 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1651 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001652 copy_tv(&si1->item->li_tv, &argv[0]);
1653 copy_tv(&si2->item->li_tv, &argv[1]);
1654
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001655 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001656 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001657 funcexe.evaluate = TRUE;
1658 funcexe.partial = partial;
1659 funcexe.selfdict = sortinfo->item_compare_selfdict;
1660 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001661 clear_tv(&argv[0]);
1662 clear_tv(&argv[1]);
1663
1664 if (res == FAIL)
1665 res = ITEM_COMPARE_FAIL;
1666 else
1667 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1668 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001669 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001670 clear_tv(&rettv);
1671
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001672 // When the result would be zero, compare the pointers themselves. Makes
1673 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001674 if (res == 0 && !sortinfo->item_compare_keep_zero)
1675 res = si1->idx > si2->idx ? 1 : -1;
1676
1677 return res;
1678}
1679
1680/*
1681 * "sort()" or "uniq()" function
1682 */
1683 static void
1684do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1685{
1686 list_T *l;
1687 listitem_T *li;
1688 sortItem_T *ptrs;
1689 sortinfo_T *old_sortinfo;
1690 sortinfo_T info;
1691 long len;
1692 long i;
1693
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001694 // Pointer to current info struct used in compare function. Save and
1695 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001696 old_sortinfo = sortinfo;
1697 sortinfo = &info;
1698
1699 if (argvars[0].v_type != VAR_LIST)
1700 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1701 else
1702 {
1703 l = argvars[0].vval.v_list;
Bram Moolenaara187c432020-09-16 21:08:28 +02001704 if (l == NULL || value_check_lock(l->lv_lock,
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001705 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1706 TRUE))
1707 goto theend;
1708 rettv_list_set(rettv, l);
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001709 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001710
1711 len = list_len(l);
1712 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001713 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001714
1715 info.item_compare_ic = FALSE;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001716 info.item_compare_lc = FALSE;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001717 info.item_compare_numeric = FALSE;
1718 info.item_compare_numbers = FALSE;
1719#ifdef FEAT_FLOAT
1720 info.item_compare_float = FALSE;
1721#endif
1722 info.item_compare_func = NULL;
1723 info.item_compare_partial = NULL;
1724 info.item_compare_selfdict = NULL;
1725 if (argvars[1].v_type != VAR_UNKNOWN)
1726 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001727 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001728 if (argvars[1].v_type == VAR_FUNC)
1729 info.item_compare_func = argvars[1].vval.v_string;
1730 else if (argvars[1].v_type == VAR_PARTIAL)
1731 info.item_compare_partial = argvars[1].vval.v_partial;
1732 else
1733 {
1734 int error = FALSE;
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001735 int nr = 0;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001736
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001737 if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001738 {
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001739 nr = tv_get_number_chk(&argvars[1], &error);
1740 if (error)
1741 goto theend; // type error; errmsg already given
1742 if (nr == 1)
1743 info.item_compare_ic = TRUE;
1744 }
1745 if (nr != 1)
1746 {
1747 if (argvars[1].v_type != VAR_NUMBER)
1748 info.item_compare_func = tv_get_string(&argvars[1]);
1749 else if (nr != 0)
1750 {
1751 emsg(_(e_invarg));
1752 goto theend;
1753 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001754 }
1755 if (info.item_compare_func != NULL)
1756 {
1757 if (*info.item_compare_func == NUL)
1758 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001759 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001760 info.item_compare_func = NULL;
1761 }
1762 else if (STRCMP(info.item_compare_func, "n") == 0)
1763 {
1764 info.item_compare_func = NULL;
1765 info.item_compare_numeric = TRUE;
1766 }
1767 else if (STRCMP(info.item_compare_func, "N") == 0)
1768 {
1769 info.item_compare_func = NULL;
1770 info.item_compare_numbers = TRUE;
1771 }
1772#ifdef FEAT_FLOAT
1773 else if (STRCMP(info.item_compare_func, "f") == 0)
1774 {
1775 info.item_compare_func = NULL;
1776 info.item_compare_float = TRUE;
1777 }
1778#endif
1779 else if (STRCMP(info.item_compare_func, "i") == 0)
1780 {
1781 info.item_compare_func = NULL;
1782 info.item_compare_ic = TRUE;
1783 }
Bram Moolenaar55e29612020-11-01 13:57:44 +01001784 else if (STRCMP(info.item_compare_func, "l") == 0)
1785 {
1786 info.item_compare_func = NULL;
1787 info.item_compare_lc = TRUE;
1788 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001789 }
1790 }
1791
1792 if (argvars[2].v_type != VAR_UNKNOWN)
1793 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001794 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001795 if (argvars[2].v_type != VAR_DICT)
1796 {
1797 emsg(_(e_dictreq));
1798 goto theend;
1799 }
1800 info.item_compare_selfdict = argvars[2].vval.v_dict;
1801 }
1802 }
1803
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001804 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001805 ptrs = ALLOC_MULT(sortItem_T, len);
1806 if (ptrs == NULL)
1807 goto theend;
1808
1809 i = 0;
1810 if (sort)
1811 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001812 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001813 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001814 {
1815 ptrs[i].item = li;
1816 ptrs[i].idx = i;
1817 ++i;
1818 }
1819
1820 info.item_compare_func_err = FALSE;
1821 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001822 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001823 if ((info.item_compare_func != NULL
1824 || info.item_compare_partial != NULL)
1825 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
1826 == ITEM_COMPARE_FAIL)
1827 emsg(_("E702: Sort compare function failed"));
1828 else
1829 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001830 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001831 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
1832 info.item_compare_func == NULL
1833 && info.item_compare_partial == NULL
1834 ? item_compare : item_compare2);
1835
1836 if (!info.item_compare_func_err)
1837 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001838 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001839 l->lv_first = l->lv_u.mat.lv_last
1840 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001841 l->lv_len = 0;
1842 for (i = 0; i < len; ++i)
1843 list_append(l, ptrs[i].item);
1844 }
1845 }
1846 }
1847 else
1848 {
1849 int (*item_compare_func_ptr)(const void *, const void *);
1850
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001851 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001852 info.item_compare_func_err = FALSE;
1853 info.item_compare_keep_zero = TRUE;
1854 item_compare_func_ptr = info.item_compare_func != NULL
1855 || info.item_compare_partial != NULL
1856 ? item_compare2 : item_compare;
1857
1858 for (li = l->lv_first; li != NULL && li->li_next != NULL;
1859 li = li->li_next)
1860 {
1861 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
1862 == 0)
1863 ptrs[i++].item = li;
1864 if (info.item_compare_func_err)
1865 {
1866 emsg(_("E882: Uniq compare function failed"));
1867 break;
1868 }
1869 }
1870
1871 if (!info.item_compare_func_err)
1872 {
1873 while (--i >= 0)
1874 {
1875 li = ptrs[i].item->li_next;
1876 ptrs[i].item->li_next = li->li_next;
1877 if (li->li_next != NULL)
1878 li->li_next->li_prev = ptrs[i].item;
1879 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001880 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001881 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001883 l->lv_len--;
1884 }
1885 }
1886 }
1887
1888 vim_free(ptrs);
1889 }
1890theend:
1891 sortinfo = old_sortinfo;
1892}
1893
1894/*
1895 * "sort({list})" function
1896 */
1897 void
1898f_sort(typval_T *argvars, typval_T *rettv)
1899{
1900 do_sort_uniq(argvars, rettv, TRUE);
1901}
1902
1903/*
1904 * "uniq({list})" function
1905 */
1906 void
1907f_uniq(typval_T *argvars, typval_T *rettv)
1908{
1909 do_sort_uniq(argvars, rettv, FALSE);
1910}
1911
Bram Moolenaarea696852020-11-09 18:31:39 +01001912typedef enum {
1913 FILTERMAP_FILTER,
1914 FILTERMAP_MAP,
1915 FILTERMAP_MAPNEW
1916} filtermap_T;
1917
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001918/*
1919 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01001920 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001921 */
1922 static int
Bram Moolenaarea696852020-11-09 18:31:39 +01001923filter_map_one(
1924 typval_T *tv, // original value
1925 typval_T *expr, // callback
1926 filtermap_T filtermap,
1927 typval_T *newtv, // for map() and mapnew(): new value
1928 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001929{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001930 typval_T argv[3];
1931 int retval = FAIL;
1932
1933 copy_tv(tv, get_vim_var_tv(VV_VAL));
1934 argv[0] = *get_vim_var_tv(VV_KEY);
1935 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01001936 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001937 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01001938 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001939 {
1940 int error = FALSE;
1941
1942 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02001943 if (in_vim9script())
Bram Moolenaarea696852020-11-09 18:31:39 +01001944 *remp = !tv2bool(newtv);
Bram Moolenaar56acb092020-08-16 14:48:19 +02001945 else
Bram Moolenaarea696852020-11-09 18:31:39 +01001946 *remp = (tv_get_number_chk(newtv, &error) == 0);
1947 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001948 // On type error, nothing has been removed; return FAIL to stop the
1949 // loop. The error message was given by tv_get_number_chk().
1950 if (error)
1951 goto theend;
1952 }
1953 retval = OK;
1954theend:
1955 clear_tv(get_vim_var_tv(VV_VAL));
1956 return retval;
1957}
1958
1959/*
1960 * Implementation of map() and filter().
1961 */
1962 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01001963filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001964{
1965 typval_T *expr;
1966 listitem_T *li, *nli;
1967 list_T *l = NULL;
1968 dictitem_T *di;
1969 hashtab_T *ht;
1970 hashitem_T *hi;
1971 dict_T *d = NULL;
1972 blob_T *b = NULL;
1973 int rem;
1974 int todo;
Bram Moolenaarea696852020-11-09 18:31:39 +01001975 char_u *ermsg = (char_u *)(filtermap == FILTERMAP_MAP ? "map()"
1976 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
1977 : "filter()");
1978 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
1979 ? N_("map() argument")
1980 : filtermap == FILTERMAP_MAPNEW
1981 ? N_("mapnew() argument")
1982 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001983 int save_did_emsg;
1984 int idx = 0;
1985
Bram Moolenaarea696852020-11-09 18:31:39 +01001986 // map() and filter() return the first argument, also on failure.
1987 if (filtermap != FILTERMAP_MAPNEW)
1988 copy_tv(&argvars[0], rettv);
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02001989
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001990 if (argvars[0].v_type == VAR_BLOB)
1991 {
Bram Moolenaarea696852020-11-09 18:31:39 +01001992 if (filtermap == FILTERMAP_MAPNEW)
1993 {
1994 rettv->v_type = VAR_BLOB;
1995 rettv->vval.v_blob = NULL;
1996 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001997 if ((b = argvars[0].vval.v_blob) == NULL)
1998 return;
1999 }
2000 else if (argvars[0].v_type == VAR_LIST)
2001 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002002 if (filtermap == FILTERMAP_MAPNEW)
2003 {
2004 rettv->v_type = VAR_LIST;
2005 rettv->vval.v_list = NULL;
2006 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002007 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002008 || (filtermap == FILTERMAP_FILTER
2009 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002010 return;
2011 }
2012 else if (argvars[0].v_type == VAR_DICT)
2013 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002014 if (filtermap == FILTERMAP_MAPNEW)
2015 {
2016 rettv->v_type = VAR_DICT;
2017 rettv->vval.v_dict = NULL;
2018 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002019 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002020 || (filtermap == FILTERMAP_FILTER
2021 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002022 return;
2023 }
2024 else
2025 {
Bram Moolenaarfcb0b612020-05-26 20:22:01 +02002026 semsg(_(e_listdictblobarg), ermsg);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002027 return;
2028 }
2029
2030 expr = &argvars[1];
2031 // On type errors, the preceding call has already displayed an error
2032 // message. Avoid a misleading error message for an empty string that
2033 // was not passed as argument.
2034 if (expr->v_type != VAR_UNKNOWN)
2035 {
2036 typval_T save_val;
2037 typval_T save_key;
2038
2039 prepare_vimvar(VV_VAL, &save_val);
2040 prepare_vimvar(VV_KEY, &save_key);
2041
2042 // We reset "did_emsg" to be able to detect whether an error
2043 // occurred during evaluation of the expression.
2044 save_did_emsg = did_emsg;
2045 did_emsg = FALSE;
2046
2047 if (argvars[0].v_type == VAR_DICT)
2048 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002049 int prev_lock = d->dv_lock;
Bram Moolenaarea696852020-11-09 18:31:39 +01002050 dict_T *d_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002051
Bram Moolenaarea696852020-11-09 18:31:39 +01002052 if (filtermap == FILTERMAP_MAPNEW)
2053 {
2054 if (rettv_dict_alloc(rettv) == FAIL)
2055 return;
2056 d_ret = rettv->vval.v_dict;
2057 }
2058
2059 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002060 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002061 ht = &d->dv_hashtab;
2062 hash_lock(ht);
2063 todo = (int)ht->ht_used;
2064 for (hi = ht->ht_array; todo > 0; ++hi)
2065 {
2066 if (!HASHITEM_EMPTY(hi))
2067 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002068 int r;
2069 typval_T newtv;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002070
2071 --todo;
2072 di = HI2DI(hi);
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002073 if (filtermap == FILTERMAP_MAP
Bram Moolenaarea696852020-11-09 18:31:39 +01002074 && (value_check_lock(di->di_tv.v_lock,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002075 arg_errmsg, TRUE)
2076 || var_check_ro(di->di_flags,
2077 arg_errmsg, TRUE)))
2078 break;
2079 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaarea696852020-11-09 18:31:39 +01002080 r = filter_map_one(&di->di_tv, expr, filtermap,
2081 &newtv, &rem);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002082 clear_tv(get_vim_var_tv(VV_KEY));
2083 if (r == FAIL || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002084 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002085 clear_tv(&newtv);
2086 break;
2087 }
2088 if (filtermap == FILTERMAP_MAP)
2089 {
2090 // map(): replace the dict item value
2091 clear_tv(&di->di_tv);
2092 newtv.v_lock = 0;
2093 di->di_tv = newtv;
2094 }
2095 else if (filtermap == FILTERMAP_MAPNEW)
2096 {
2097 // mapnew(): add the item value to the new dict
2098 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
2099 clear_tv(&newtv);
2100 if (r == FAIL)
2101 break;
2102 }
2103 else if (filtermap == FILTERMAP_FILTER && rem)
2104 {
2105 // filter(false): remove the item from the dict
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002106 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2107 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2108 break;
2109 dictitem_remove(d, di);
2110 }
2111 }
2112 }
2113 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002114 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002115 }
2116 else if (argvars[0].v_type == VAR_BLOB)
2117 {
2118 int i;
2119 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002120 varnumber_T val;
Bram Moolenaarea696852020-11-09 18:31:39 +01002121 blob_T *b_ret = b;
2122
2123 if (filtermap == FILTERMAP_MAPNEW)
2124 {
2125 if (blob_copy(b, rettv) == FAIL)
2126 return;
2127 b_ret = rettv->vval.v_blob;
2128 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002129
2130 // set_vim_var_nr() doesn't set the type
2131 set_vim_var_type(VV_KEY, VAR_NUMBER);
2132
2133 for (i = 0; i < b->bv_ga.ga_len; i++)
2134 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002135 typval_T newtv;
2136
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002137 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002138 val = blob_get(b, i);
2139 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002140 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaarea696852020-11-09 18:31:39 +01002141 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
2142 || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002143 break;
Bram Moolenaarea696852020-11-09 18:31:39 +01002144 if (newtv.v_type != VAR_NUMBER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002145 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002146 clear_tv(&newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002147 emsg(_(e_invalblob));
2148 break;
2149 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002150 if (filtermap != FILTERMAP_FILTER)
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002151 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002152 if (newtv.vval.v_number != val)
2153 blob_set(b_ret, i, newtv.vval.v_number);
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002154 }
2155 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002156 {
2157 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2158
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002159 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002160 (size_t)b->bv_ga.ga_len - i - 1);
2161 --b->bv_ga.ga_len;
2162 --i;
2163 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002164 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002165 }
2166 }
2167 else // argvars[0].v_type == VAR_LIST
2168 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002169 int prev_lock = l->lv_lock;
2170 list_T *l_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002171
Bram Moolenaarea696852020-11-09 18:31:39 +01002172 if (filtermap == FILTERMAP_MAPNEW)
2173 {
2174 if (rettv_list_alloc(rettv) == FAIL)
2175 return;
2176 l_ret = rettv->vval.v_list;
2177 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002178 // set_vim_var_nr() doesn't set the type
2179 set_vim_var_type(VV_KEY, VAR_NUMBER);
2180
Bram Moolenaarea696852020-11-09 18:31:39 +01002181 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002182 l->lv_lock = VAR_LOCKED;
Bram Moolenaarea696852020-11-09 18:31:39 +01002183
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002184 if (l->lv_first == &range_list_item)
2185 {
2186 varnumber_T val = l->lv_u.nonmat.lv_start;
2187 int len = l->lv_len;
2188 int stride = l->lv_u.nonmat.lv_stride;
2189
2190 // List from range(): loop over the numbers
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002191 if (filtermap != FILTERMAP_MAPNEW)
2192 {
2193 l->lv_first = NULL;
2194 l->lv_u.mat.lv_last = NULL;
2195 l->lv_len = 0;
2196 l->lv_u.mat.lv_idx_item = NULL;
2197 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002198
2199 for (idx = 0; idx < len; ++idx)
Bram Moolenaarc56936e2020-11-10 11:43:56 +01002200 {
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002201 typval_T tv;
2202 typval_T newtv;
2203
2204 tv.v_type = VAR_NUMBER;
2205 tv.v_lock = 0;
2206 tv.vval.v_number = val;
2207 set_vim_var_nr(VV_KEY, idx);
2208 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2209 == FAIL)
Bram Moolenaarea696852020-11-09 18:31:39 +01002210 break;
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002211 if (did_emsg)
2212 {
2213 clear_tv(&newtv);
2214 break;
2215 }
2216 if (filtermap != FILTERMAP_FILTER)
2217 {
2218 // map(), mapnew(): always append the new value to the
2219 // list
2220 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2221 ? l : l_ret, &newtv) == FAIL)
2222 break;
2223 }
2224 else if (!rem)
2225 {
2226 // filter(): append the list item value when not rem
2227 if (list_append_tv_move(l, &tv) == FAIL)
2228 break;
2229 }
2230
2231 val += stride;
Bram Moolenaarea696852020-11-09 18:31:39 +01002232 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002233 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002234 else
2235 {
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002236 // Materialized list: loop over the items
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002237 for (li = l->lv_first; li != NULL; li = nli)
2238 {
2239 typval_T newtv;
2240
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002241 if (filtermap == FILTERMAP_MAP && value_check_lock(
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002242 li->li_tv.v_lock, arg_errmsg, TRUE))
2243 break;
2244 nli = li->li_next;
2245 set_vim_var_nr(VV_KEY, idx);
2246 if (filter_map_one(&li->li_tv, expr, filtermap,
2247 &newtv, &rem) == FAIL)
2248 break;
2249 if (did_emsg)
2250 {
2251 clear_tv(&newtv);
2252 break;
2253 }
2254 if (filtermap == FILTERMAP_MAP)
2255 {
2256 // map(): replace the list item value
2257 clear_tv(&li->li_tv);
2258 newtv.v_lock = 0;
2259 li->li_tv = newtv;
2260 }
2261 else if (filtermap == FILTERMAP_MAPNEW)
2262 {
2263 // mapnew(): append the list item value
2264 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2265 break;
2266 }
2267 else if (filtermap == FILTERMAP_FILTER && rem)
2268 listitem_remove(l, li);
2269 ++idx;
2270 }
2271 }
2272
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002273 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002274 }
2275
2276 restore_vimvar(VV_KEY, &save_key);
2277 restore_vimvar(VV_VAL, &save_val);
2278
2279 did_emsg |= save_did_emsg;
2280 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002281}
2282
2283/*
2284 * "filter()" function
2285 */
2286 void
2287f_filter(typval_T *argvars, typval_T *rettv)
2288{
Bram Moolenaarea696852020-11-09 18:31:39 +01002289 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002290}
2291
2292/*
2293 * "map()" function
2294 */
2295 void
2296f_map(typval_T *argvars, typval_T *rettv)
2297{
Bram Moolenaarea696852020-11-09 18:31:39 +01002298 filter_map(argvars, rettv, FILTERMAP_MAP);
2299}
2300
2301/*
2302 * "mapnew()" function
2303 */
2304 void
2305f_mapnew(typval_T *argvars, typval_T *rettv)
2306{
2307 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002308}
2309
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002310/*
2311 * "add(list, item)" function
2312 */
2313 void
2314f_add(typval_T *argvars, typval_T *rettv)
2315{
2316 list_T *l;
2317 blob_T *b;
2318
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002319 rettv->vval.v_number = 1; // Default: Failed
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002320 if (argvars[0].v_type == VAR_LIST)
2321 {
2322 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002323 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002324 (char_u *)N_("add() argument"), TRUE)
2325 && list_append_tv(l, &argvars[1]) == OK)
2326 copy_tv(&argvars[0], rettv);
2327 }
2328 else if (argvars[0].v_type == VAR_BLOB)
2329 {
2330 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002331 && !value_check_lock(b->bv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002332 (char_u *)N_("add() argument"), TRUE))
2333 {
2334 int error = FALSE;
2335 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2336
2337 if (!error)
2338 {
2339 ga_append(&b->bv_ga, (int)n);
2340 copy_tv(&argvars[0], rettv);
2341 }
2342 }
2343 }
2344 else
2345 emsg(_(e_listblobreq));
2346}
2347
2348/*
2349 * "count()" function
2350 */
2351 void
2352f_count(typval_T *argvars, typval_T *rettv)
2353{
2354 long n = 0;
2355 int ic = FALSE;
2356 int error = FALSE;
2357
2358 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002359 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002360
2361 if (argvars[0].v_type == VAR_STRING)
2362 {
2363 char_u *expr = tv_get_string_chk(&argvars[1]);
2364 char_u *p = argvars[0].vval.v_string;
2365 char_u *next;
2366
2367 if (!error && expr != NULL && *expr != NUL && p != NULL)
2368 {
2369 if (ic)
2370 {
2371 size_t len = STRLEN(expr);
2372
2373 while (*p != NUL)
2374 {
2375 if (MB_STRNICMP(p, expr, len) == 0)
2376 {
2377 ++n;
2378 p += len;
2379 }
2380 else
2381 MB_PTR_ADV(p);
2382 }
2383 }
2384 else
2385 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2386 != NULL)
2387 {
2388 ++n;
2389 p = next + STRLEN(expr);
2390 }
2391 }
2392
2393 }
2394 else if (argvars[0].v_type == VAR_LIST)
2395 {
2396 listitem_T *li;
2397 list_T *l;
2398 long idx;
2399
2400 if ((l = argvars[0].vval.v_list) != NULL)
2401 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002402 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002403 li = l->lv_first;
2404 if (argvars[2].v_type != VAR_UNKNOWN)
2405 {
2406 if (argvars[3].v_type != VAR_UNKNOWN)
2407 {
2408 idx = (long)tv_get_number_chk(&argvars[3], &error);
2409 if (!error)
2410 {
2411 li = list_find(l, idx);
2412 if (li == NULL)
2413 semsg(_(e_listidx), idx);
2414 }
2415 }
2416 if (error)
2417 li = NULL;
2418 }
2419
2420 for ( ; li != NULL; li = li->li_next)
2421 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2422 ++n;
2423 }
2424 }
2425 else if (argvars[0].v_type == VAR_DICT)
2426 {
2427 int todo;
2428 dict_T *d;
2429 hashitem_T *hi;
2430
2431 if ((d = argvars[0].vval.v_dict) != NULL)
2432 {
2433 if (argvars[2].v_type != VAR_UNKNOWN)
2434 {
2435 if (argvars[3].v_type != VAR_UNKNOWN)
2436 emsg(_(e_invarg));
2437 }
2438
2439 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2440 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2441 {
2442 if (!HASHITEM_EMPTY(hi))
2443 {
2444 --todo;
2445 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2446 ++n;
2447 }
2448 }
2449 }
2450 }
2451 else
2452 semsg(_(e_listdictarg), "count()");
2453 rettv->vval.v_number = n;
2454}
2455
2456/*
2457 * "extend(list, list [, idx])" function
2458 * "extend(dict, dict [, action])" function
2459 */
2460 void
2461f_extend(typval_T *argvars, typval_T *rettv)
2462{
2463 char_u *arg_errmsg = (char_u *)N_("extend() argument");
2464
2465 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2466 {
2467 list_T *l1, *l2;
2468 listitem_T *item;
2469 long before;
2470 int error = FALSE;
2471
2472 l1 = argvars[0].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002473 if (l1 == NULL)
2474 {
2475 emsg(_(e_cannot_extend_null_list));
2476 return;
2477 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002478 l2 = argvars[1].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002479 if (!value_check_lock(l1->lv_lock, arg_errmsg, TRUE) && l2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002480 {
2481 if (argvars[2].v_type != VAR_UNKNOWN)
2482 {
2483 before = (long)tv_get_number_chk(&argvars[2], &error);
2484 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002485 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002486
2487 if (before == l1->lv_len)
2488 item = NULL;
2489 else
2490 {
2491 item = list_find(l1, before);
2492 if (item == NULL)
2493 {
2494 semsg(_(e_listidx), before);
2495 return;
2496 }
2497 }
2498 }
2499 else
2500 item = NULL;
2501 list_extend(l1, l2, item);
2502
2503 copy_tv(&argvars[0], rettv);
2504 }
2505 }
2506 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2507 {
2508 dict_T *d1, *d2;
2509 char_u *action;
2510 int i;
2511
2512 d1 = argvars[0].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002513 if (d1 == NULL)
2514 {
2515 emsg(_(e_cannot_extend_null_dict));
2516 return;
2517 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002518 d2 = argvars[1].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002519 if (!value_check_lock(d1->dv_lock, arg_errmsg, TRUE) && d2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002520 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002521 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002522 if (argvars[2].v_type != VAR_UNKNOWN)
2523 {
2524 static char *(av[]) = {"keep", "force", "error"};
2525
2526 action = tv_get_string_chk(&argvars[2]);
2527 if (action == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002528 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002529 for (i = 0; i < 3; ++i)
2530 if (STRCMP(action, av[i]) == 0)
2531 break;
2532 if (i == 3)
2533 {
2534 semsg(_(e_invarg2), action);
2535 return;
2536 }
2537 }
2538 else
2539 action = (char_u *)"force";
2540
2541 dict_extend(d1, d2, action);
2542
2543 copy_tv(&argvars[0], rettv);
2544 }
2545 }
2546 else
2547 semsg(_(e_listdictarg), "extend()");
2548}
2549
2550/*
2551 * "insert()" function
2552 */
2553 void
2554f_insert(typval_T *argvars, typval_T *rettv)
2555{
2556 long before = 0;
2557 listitem_T *item;
2558 list_T *l;
2559 int error = FALSE;
2560
2561 if (argvars[0].v_type == VAR_BLOB)
2562 {
2563 int val, len;
2564 char_u *p;
2565
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002566 if (argvars[0].vval.v_blob == NULL)
2567 return;
2568
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002569 len = blob_len(argvars[0].vval.v_blob);
2570 if (argvars[2].v_type != VAR_UNKNOWN)
2571 {
2572 before = (long)tv_get_number_chk(&argvars[2], &error);
2573 if (error)
2574 return; // type error; errmsg already given
2575 if (before < 0 || before > len)
2576 {
2577 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2578 return;
2579 }
2580 }
2581 val = tv_get_number_chk(&argvars[1], &error);
2582 if (error)
2583 return;
2584 if (val < 0 || val > 255)
2585 {
2586 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2587 return;
2588 }
2589
2590 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
2591 return;
2592 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2593 mch_memmove(p + before + 1, p + before, (size_t)len - before);
2594 *(p + before) = val;
2595 ++argvars[0].vval.v_blob->bv_ga.ga_len;
2596
2597 copy_tv(&argvars[0], rettv);
2598 }
2599 else if (argvars[0].v_type != VAR_LIST)
2600 semsg(_(e_listblobarg), "insert()");
2601 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002602 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002603 (char_u *)N_("insert() argument"), TRUE))
2604 {
2605 if (argvars[2].v_type != VAR_UNKNOWN)
2606 before = (long)tv_get_number_chk(&argvars[2], &error);
2607 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002608 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002609
2610 if (before == l->lv_len)
2611 item = NULL;
2612 else
2613 {
2614 item = list_find(l, before);
2615 if (item == NULL)
2616 {
2617 semsg(_(e_listidx), before);
2618 l = NULL;
2619 }
2620 }
2621 if (l != NULL)
2622 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02002623 (void)list_insert_tv(l, &argvars[1], item);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002624 copy_tv(&argvars[0], rettv);
2625 }
2626 }
2627}
2628
2629/*
2630 * "remove()" function
2631 */
2632 void
2633f_remove(typval_T *argvars, typval_T *rettv)
2634{
2635 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2636
2637 if (argvars[0].v_type == VAR_DICT)
2638 dict_remove(argvars, rettv, arg_errmsg);
2639 else if (argvars[0].v_type == VAR_BLOB)
2640 blob_remove(argvars, rettv);
2641 else if (argvars[0].v_type == VAR_LIST)
2642 list_remove(argvars, rettv, arg_errmsg);
2643 else
2644 semsg(_(e_listdictblobarg), "remove()");
2645}
2646
2647/*
2648 * "reverse({list})" function
2649 */
2650 void
2651f_reverse(typval_T *argvars, typval_T *rettv)
2652{
2653 list_T *l;
2654 listitem_T *li, *ni;
2655
2656 if (argvars[0].v_type == VAR_BLOB)
2657 {
2658 blob_T *b = argvars[0].vval.v_blob;
2659 int i, len = blob_len(b);
2660
2661 for (i = 0; i < len / 2; i++)
2662 {
2663 int tmp = blob_get(b, i);
2664
2665 blob_set(b, i, blob_get(b, len - i - 1));
2666 blob_set(b, len - i - 1, tmp);
2667 }
2668 rettv_blob_set(rettv, b);
2669 return;
2670 }
2671
2672 if (argvars[0].v_type != VAR_LIST)
2673 semsg(_(e_listblobarg), "reverse()");
2674 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002675 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002676 (char_u *)N_("reverse() argument"), TRUE))
2677 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002678 if (l->lv_first == &range_list_item)
2679 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002680 varnumber_T new_start = l->lv_u.nonmat.lv_start
2681 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2682 l->lv_u.nonmat.lv_end = new_start
2683 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2684 l->lv_u.nonmat.lv_start = new_start;
2685 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002686 rettv_list_set(rettv, l);
2687 return;
2688 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002689 li = l->lv_u.mat.lv_last;
2690 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002691 l->lv_len = 0;
2692 while (li != NULL)
2693 {
2694 ni = li->li_prev;
2695 list_append(l, li);
2696 li = ni;
2697 }
2698 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002699 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002700 }
2701}
2702
Bram Moolenaar85629982020-06-01 18:39:20 +02002703/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002704 * "reduce(list, { accumulator, element -> value } [, initial])" function
Bram Moolenaar85629982020-06-01 18:39:20 +02002705 */
2706 void
2707f_reduce(typval_T *argvars, typval_T *rettv)
2708{
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002709 typval_T initial;
Bram Moolenaar85629982020-06-01 18:39:20 +02002710 char_u *func_name;
2711 partial_T *partial = NULL;
2712 funcexe_T funcexe;
2713 typval_T argv[3];
2714
2715 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
2716 {
2717 emsg(_(e_listblobreq));
2718 return;
2719 }
2720
2721 if (argvars[1].v_type == VAR_FUNC)
2722 func_name = argvars[1].vval.v_string;
2723 else if (argvars[1].v_type == VAR_PARTIAL)
2724 {
2725 partial = argvars[1].vval.v_partial;
2726 func_name = partial_name(partial);
2727 }
2728 else
2729 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01002730 if (func_name == NULL || *func_name == NUL)
2731 {
2732 emsg(_(e_missing_function_argument));
2733 return;
2734 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002735
2736 vim_memset(&funcexe, 0, sizeof(funcexe));
2737 funcexe.evaluate = TRUE;
2738 funcexe.partial = partial;
2739
2740 if (argvars[0].v_type == VAR_LIST)
2741 {
2742 list_T *l = argvars[0].vval.v_list;
2743 listitem_T *li = NULL;
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002744 int r;
Bram Moolenaarca275a02020-06-24 22:07:46 +02002745 int called_emsg_start = called_emsg;
Bram Moolenaar85629982020-06-01 18:39:20 +02002746
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002747 if (l != NULL)
2748 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar85629982020-06-01 18:39:20 +02002749 if (argvars[2].v_type == VAR_UNKNOWN)
2750 {
2751 if (l == NULL || l->lv_first == NULL)
2752 {
2753 semsg(_(e_reduceempty), "List");
2754 return;
2755 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002756 initial = l->lv_first->li_tv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002757 li = l->lv_first->li_next;
2758 }
2759 else
2760 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002761 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002762 if (l != NULL)
2763 li = l->lv_first;
2764 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002765 copy_tv(&initial, rettv);
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002766
2767 if (l != NULL)
Bram Moolenaar85629982020-06-01 18:39:20 +02002768 {
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002769 int prev_locked = l->lv_lock;
2770
2771 l->lv_lock = VAR_FIXED; // disallow the list changing here
2772 for ( ; li != NULL; li = li->li_next)
2773 {
2774 argv[0] = *rettv;
2775 argv[1] = li->li_tv;
2776 rettv->v_type = VAR_UNKNOWN;
2777 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
2778 clear_tv(&argv[0]);
2779 if (r == FAIL || called_emsg != called_emsg_start)
2780 break;
2781 }
2782 l->lv_lock = prev_locked;
Bram Moolenaar85629982020-06-01 18:39:20 +02002783 }
2784 }
2785 else
2786 {
2787 blob_T *b = argvars[0].vval.v_blob;
2788 int i;
2789
2790 if (argvars[2].v_type == VAR_UNKNOWN)
2791 {
2792 if (b == NULL || b->bv_ga.ga_len == 0)
2793 {
2794 semsg(_(e_reduceempty), "Blob");
2795 return;
2796 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002797 initial.v_type = VAR_NUMBER;
2798 initial.vval.v_number = blob_get(b, 0);
Bram Moolenaar85629982020-06-01 18:39:20 +02002799 i = 1;
2800 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002801 else if (argvars[2].v_type != VAR_NUMBER)
2802 {
2803 emsg(_(e_number_exp));
2804 return;
2805 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002806 else
2807 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002808 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002809 i = 0;
2810 }
2811
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002812 copy_tv(&initial, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02002813 if (b != NULL)
2814 {
2815 for ( ; i < b->bv_ga.ga_len; i++)
2816 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002817 argv[0] = *rettv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002818 argv[1].v_type = VAR_NUMBER;
2819 argv[1].vval.v_number = blob_get(b, i);
2820 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
2821 return;
Bram Moolenaar85629982020-06-01 18:39:20 +02002822 }
2823 }
2824 }
2825}
2826
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002827#endif // defined(FEAT_EVAL)