blob: 873f9e63de5db6f7bea0cc9d91fd986ee1a8fcd2 [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/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100534 * Like list_find() but when a negative index is used that is not found use
535 * zero and set "idx" to zero. Used for first index of a range.
536 */
537 listitem_T *
538list_find_index(list_T *l, long *idx)
539{
540 listitem_T *li = list_find(l, *idx);
541
542 if (li == NULL)
543 {
544 if (*idx < 0)
545 {
546 *idx = 0;
547 li = list_find(l, *idx);
548 }
549 }
550 return li;
551}
552
553/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200554 * Locate "item" list "l" and return its index.
555 * Returns -1 when "item" is not in the list.
556 */
557 long
558list_idx_of_item(list_T *l, listitem_T *item)
559{
560 long idx = 0;
561 listitem_T *li;
562
563 if (l == NULL)
564 return -1;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200565 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200566 idx = 0;
567 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
568 ++idx;
569 if (li == NULL)
570 return -1;
571 return idx;
572}
573
574/*
575 * Append item "item" to the end of list "l".
576 */
577 void
578list_append(list_T *l, listitem_T *item)
579{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200580 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100581 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200582 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100583 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200584 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100585 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200586 item->li_prev = NULL;
587 }
588 else
589 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100590 l->lv_u.mat.lv_last->li_next = item;
591 item->li_prev = l->lv_u.mat.lv_last;
592 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200593 }
594 ++l->lv_len;
595 item->li_next = NULL;
596}
597
598/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200600 * Return FAIL when out of memory.
601 */
602 int
603list_append_tv(list_T *l, typval_T *tv)
604{
605 listitem_T *li = listitem_alloc();
606
607 if (li == NULL)
608 return FAIL;
609 copy_tv(tv, &li->li_tv);
610 list_append(l, li);
611 return OK;
612}
613
614/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 * As list_append_tv() but move the value instead of copying it.
616 * Return FAIL when out of memory.
617 */
618 int
619list_append_tv_move(list_T *l, typval_T *tv)
620{
621 listitem_T *li = listitem_alloc();
622
623 if (li == NULL)
624 return FAIL;
625 li->li_tv = *tv;
626 list_append(l, li);
627 return OK;
628}
629
630/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200631 * Add a dictionary to a list. Used by getqflist().
632 * Return FAIL when out of memory.
633 */
634 int
635list_append_dict(list_T *list, dict_T *dict)
636{
637 listitem_T *li = listitem_alloc();
638
639 if (li == NULL)
640 return FAIL;
641 li->li_tv.v_type = VAR_DICT;
642 li->li_tv.v_lock = 0;
643 li->li_tv.vval.v_dict = dict;
644 list_append(list, li);
645 ++dict->dv_refcount;
646 return OK;
647}
648
649/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100650 * Append list2 to list1.
651 * Return FAIL when out of memory.
652 */
653 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200654list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100655{
656 listitem_T *li = listitem_alloc();
657
658 if (li == NULL)
659 return FAIL;
660 li->li_tv.v_type = VAR_LIST;
661 li->li_tv.v_lock = 0;
662 li->li_tv.vval.v_list = list2;
663 list_append(list1, li);
664 ++list2->lv_refcount;
665 return OK;
666}
667
668/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200669 * Make a copy of "str" and append it as an item to list "l".
670 * When "len" >= 0 use "str[len]".
671 * Returns FAIL when out of memory.
672 */
673 int
674list_append_string(list_T *l, char_u *str, int len)
675{
676 listitem_T *li = listitem_alloc();
677
678 if (li == NULL)
679 return FAIL;
680 list_append(l, li);
681 li->li_tv.v_type = VAR_STRING;
682 li->li_tv.v_lock = 0;
683 if (str == NULL)
684 li->li_tv.vval.v_string = NULL;
685 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
686 : vim_strsave(str))) == NULL)
687 return FAIL;
688 return OK;
689}
690
691/*
692 * Append "n" to list "l".
693 * Returns FAIL when out of memory.
694 */
695 int
696list_append_number(list_T *l, varnumber_T n)
697{
698 listitem_T *li;
699
700 li = listitem_alloc();
701 if (li == NULL)
702 return FAIL;
703 li->li_tv.v_type = VAR_NUMBER;
704 li->li_tv.v_lock = 0;
705 li->li_tv.vval.v_number = n;
706 list_append(l, li);
707 return OK;
708}
709
710/*
711 * Insert typval_T "tv" in list "l" before "item".
712 * If "item" is NULL append at the end.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100713 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200714 */
715 int
716list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
717{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100718 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200719
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100720 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100721 && check_typval_arg_type(l->lv_type->tt_member, tv, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100722 return FAIL;
723 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200724 if (ni == NULL)
725 return FAIL;
726 copy_tv(tv, &ni->li_tv);
727 list_insert(l, ni, item);
728 return OK;
729}
730
731 void
732list_insert(list_T *l, listitem_T *ni, listitem_T *item)
733{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200734 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200735 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100736 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200737 list_append(l, ni);
738 else
739 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100740 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200741 ni->li_prev = item->li_prev;
742 ni->li_next = item;
743 if (item->li_prev == NULL)
744 {
745 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100746 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200747 }
748 else
749 {
750 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100751 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200752 }
753 item->li_prev = ni;
754 ++l->lv_len;
755 }
756}
757
758/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200759 * Flatten "list" to depth "maxdepth".
760 * It does nothing if "maxdepth" is 0.
761 * Returns FAIL when out of memory.
762 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100763 static void
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200764list_flatten(list_T *list, long maxdepth)
765{
766 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200767 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200768 int n;
769
770 if (maxdepth == 0)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100771 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200772 CHECK_LIST_MATERIALIZE(list);
773
774 n = 0;
775 item = list->lv_first;
776 while (item != NULL)
777 {
778 fast_breakcheck();
779 if (got_int)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100780 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200781
782 if (item->li_tv.v_type == VAR_LIST)
783 {
784 listitem_T *next = item->li_next;
785
786 vimlist_remove(list, item, item);
787 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100788 return;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200789 clear_tv(&item->li_tv);
790 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200791
792 if (item->li_prev == NULL)
793 item = list->lv_first;
794 else
795 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200796 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200797
798 if (++n >= maxdepth)
799 {
800 n = 0;
801 item = next;
802 }
803 }
804 else
805 {
806 n = 0;
807 item = item->li_next;
808 }
809 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200810}
811
812/*
Bram Moolenaar3b690062021-02-01 20:14:51 +0100813 * "flatten()" and "flattennew()" functions
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200814 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100815 static void
816flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200817{
818 list_T *l;
819 long maxdepth;
820 int error = FALSE;
821
822 if (argvars[0].v_type != VAR_LIST)
823 {
824 semsg(_(e_listarg), "flatten()");
825 return;
826 }
827
828 if (argvars[1].v_type == VAR_UNKNOWN)
829 maxdepth = 999999;
830 else
831 {
832 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
833 if (error)
834 return;
835 if (maxdepth < 0)
836 {
837 emsg(_("E900: maxdepth must be non-negative number"));
838 return;
839 }
840 }
841
842 l = argvars[0].vval.v_list;
Bram Moolenaar3b690062021-02-01 20:14:51 +0100843 rettv->v_type = VAR_LIST;
844 rettv->vval.v_list = l;
845 if (l == NULL)
846 return;
847
848 if (make_copy)
849 {
850 l = list_copy(l, TRUE, get_copyID());
851 rettv->vval.v_list = l;
852 if (l == NULL)
853 return;
854 }
855 else
856 {
857 if (value_check_lock(l->lv_lock,
858 (char_u *)N_("flatten() argument"), TRUE))
859 return;
860 ++l->lv_refcount;
861 }
862
863 list_flatten(l, maxdepth);
864}
865
866/*
867 * "flatten(list[, {maxdepth}])" function
868 */
869 void
870f_flatten(typval_T *argvars, typval_T *rettv)
871{
872 if (in_vim9script())
873 emsg(_(e_cannot_use_flatten_in_vim9_script));
874 else
875 flatten_common(argvars, rettv, FALSE);
876}
877
878/*
879 * "flattennew(list[, {maxdepth}])" function
880 */
881 void
882f_flattennew(typval_T *argvars, typval_T *rettv)
883{
884 flatten_common(argvars, rettv, TRUE);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200885}
886
887/*
Bram Moolenaar1a739232020-10-10 15:37:58 +0200888 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200889 * If "bef" is NULL append at the end, otherwise insert before this item.
890 * Returns FAIL when out of memory.
891 */
892 int
893list_extend(list_T *l1, list_T *l2, listitem_T *bef)
894{
895 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200896 int todo;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200897
Bram Moolenaar1a739232020-10-10 15:37:58 +0200898 // NULL list is equivalent to an empty list: nothing to do.
899 if (l2 == NULL || l2->lv_len == 0)
900 return OK;
901
902 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200903 CHECK_LIST_MATERIALIZE(l1);
904 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100906 // We also quit the loop when we have inserted the original item count of
907 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200908 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
909 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
910 return FAIL;
911 return OK;
912}
913
914/*
915 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
916 * Return FAIL when out of memory.
917 */
918 int
919list_concat(list_T *l1, list_T *l2, typval_T *tv)
920{
921 list_T *l;
922
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100923 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +0200924 if (l1 == NULL)
925 l = list_alloc();
926 else
927 l = list_copy(l1, FALSE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200928 if (l == NULL)
929 return FAIL;
930 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +0100931 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200932 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200933 if (l1 == NULL)
934 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200935
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100936 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200937 return list_extend(l, l2, NULL);
938}
939
Bram Moolenaar9af78762020-06-16 11:34:42 +0200940 list_T *
941list_slice(list_T *ol, long n1, long n2)
942{
943 listitem_T *item;
944 list_T *l = list_alloc();
945
946 if (l == NULL)
947 return NULL;
948 for (item = list_find(ol, n1); n1 <= n2; ++n1)
949 {
950 if (list_append_tv(l, &item->li_tv) == FAIL)
951 {
952 list_free(l);
953 return NULL;
954 }
955 item = item->li_next;
956 }
957 return l;
958}
959
Bram Moolenaared591872020-08-15 22:14:53 +0200960 int
961list_slice_or_index(
962 list_T *list,
963 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +0100964 varnumber_T n1_arg,
965 varnumber_T n2_arg,
966 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +0200967 typval_T *rettv,
968 int verbose)
969{
970 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +0100971 varnumber_T n1 = n1_arg;
972 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +0200973 typval_T var1;
974
975 if (n1 < 0)
976 n1 = len + n1;
977 if (n1 < 0 || n1 >= len)
978 {
979 // For a range we allow invalid values and return an empty
980 // list. A list index out of range is an error.
981 if (!range)
982 {
983 if (verbose)
Bram Moolenaar239f8d92021-01-17 13:21:20 +0100984 semsg(_(e_listidx), (long)n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +0200985 return FAIL;
986 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200987 n1 = n1 < 0 ? 0 : len;
Bram Moolenaared591872020-08-15 22:14:53 +0200988 }
989 if (range)
990 {
991 list_T *l;
992
993 if (n2 < 0)
994 n2 = len + n2;
995 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +0100996 n2 = len - (exclusive ? 0 : 1);
997 if (exclusive)
998 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +0200999 if (n2 < 0 || n2 + 1 < n1)
1000 n2 = -1;
1001 l = list_slice(list, n1, n2);
1002 if (l == NULL)
1003 return FAIL;
1004 clear_tv(rettv);
1005 rettv_list_set(rettv, l);
1006 }
1007 else
1008 {
1009 // copy the item to "var1" to avoid that freeing the list makes it
1010 // invalid.
1011 copy_tv(&list_find(list, n1)->li_tv, &var1);
1012 clear_tv(rettv);
1013 *rettv = var1;
1014 }
1015 return OK;
1016}
1017
Bram Moolenaarda861d62016-07-17 15:46:27 +02001018/*
1019 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1020 * The refcount of the new list is set to 1.
1021 * See item_copy() for "copyID".
1022 * Returns NULL when out of memory.
1023 */
1024 list_T *
1025list_copy(list_T *orig, int deep, int copyID)
1026{
1027 list_T *copy;
1028 listitem_T *item;
1029 listitem_T *ni;
1030
1031 if (orig == NULL)
1032 return NULL;
1033
1034 copy = list_alloc();
1035 if (copy != NULL)
1036 {
1037 if (copyID != 0)
1038 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001039 // Do this before adding the items, because one of the items may
1040 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001041 orig->lv_copyID = copyID;
1042 orig->lv_copylist = copy;
1043 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001044 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001045 for (item = orig->lv_first; item != NULL && !got_int;
1046 item = item->li_next)
1047 {
1048 ni = listitem_alloc();
1049 if (ni == NULL)
1050 break;
1051 if (deep)
1052 {
1053 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
1054 {
1055 vim_free(ni);
1056 break;
1057 }
1058 }
1059 else
1060 copy_tv(&item->li_tv, &ni->li_tv);
1061 list_append(copy, ni);
1062 }
1063 ++copy->lv_refcount;
1064 if (item != NULL)
1065 {
1066 list_unref(copy);
1067 copy = NULL;
1068 }
1069 }
1070
1071 return copy;
1072}
1073
1074/*
1075 * Remove items "item" to "item2" from list "l".
1076 * Does not free the listitem or the value!
1077 * This used to be called list_remove, but that conflicts with a Sun header
1078 * file.
1079 */
1080 void
1081vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1082{
1083 listitem_T *ip;
1084
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001085 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001087 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001088 for (ip = item; ip != NULL; ip = ip->li_next)
1089 {
1090 --l->lv_len;
1091 list_fix_watch(l, ip);
1092 if (ip == item2)
1093 break;
1094 }
1095
1096 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001097 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001098 else
1099 item2->li_next->li_prev = item->li_prev;
1100 if (item->li_prev == NULL)
1101 l->lv_first = item2->li_next;
1102 else
1103 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001104 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001105}
1106
1107/*
1108 * Return an allocated string with the string representation of a list.
1109 * May return NULL.
1110 */
1111 char_u *
1112list2string(typval_T *tv, int copyID, int restore_copyID)
1113{
1114 garray_T ga;
1115
1116 if (tv->vval.v_list == NULL)
1117 return NULL;
1118 ga_init2(&ga, (int)sizeof(char), 80);
1119 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001120 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001121 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1122 FALSE, restore_copyID, copyID) == FAIL)
1123 {
1124 vim_free(ga.ga_data);
1125 return NULL;
1126 }
1127 ga_append(&ga, ']');
1128 ga_append(&ga, NUL);
1129 return (char_u *)ga.ga_data;
1130}
1131
1132typedef struct join_S {
1133 char_u *s;
1134 char_u *tofree;
1135} join_T;
1136
1137 static int
1138list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001139 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001140 list_T *l,
1141 char_u *sep,
1142 int echo_style,
1143 int restore_copyID,
1144 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001145 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001146{
1147 int i;
1148 join_T *p;
1149 int len;
1150 int sumlen = 0;
1151 int first = TRUE;
1152 char_u *tofree;
1153 char_u numbuf[NUMBUFLEN];
1154 listitem_T *item;
1155 char_u *s;
1156
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001157 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001158 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001159 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1160 {
1161 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001162 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001163 if (s == NULL)
1164 return FAIL;
1165
1166 len = (int)STRLEN(s);
1167 sumlen += len;
1168
1169 (void)ga_grow(join_gap, 1);
1170 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1171 if (tofree != NULL || s != numbuf)
1172 {
1173 p->s = s;
1174 p->tofree = tofree;
1175 }
1176 else
1177 {
1178 p->s = vim_strnsave(s, len);
1179 p->tofree = p->s;
1180 }
1181
1182 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001183 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001184 break;
1185 }
1186
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001187 // Allocate result buffer with its total size, avoid re-allocation and
1188 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001189 if (join_gap->ga_len >= 2)
1190 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1191 if (ga_grow(gap, sumlen + 2) == FAIL)
1192 return FAIL;
1193
1194 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1195 {
1196 if (first)
1197 first = FALSE;
1198 else
1199 ga_concat(gap, sep);
1200 p = ((join_T *)join_gap->ga_data) + i;
1201
1202 if (p->s != NULL)
1203 ga_concat(gap, p->s);
1204 line_breakcheck();
1205 }
1206
1207 return OK;
1208}
1209
1210/*
1211 * Join list "l" into a string in "*gap", using separator "sep".
1212 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1213 * Return FAIL or OK.
1214 */
1215 int
1216list_join(
1217 garray_T *gap,
1218 list_T *l,
1219 char_u *sep,
1220 int echo_style,
1221 int restore_copyID,
1222 int copyID)
1223{
1224 garray_T join_ga;
1225 int retval;
1226 join_T *p;
1227 int i;
1228
1229 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001230 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001231 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1232 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1233 copyID, &join_ga);
1234
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001235 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001236 if (join_ga.ga_data != NULL)
1237 {
1238 p = (join_T *)join_ga.ga_data;
1239 for (i = 0; i < join_ga.ga_len; ++i)
1240 {
1241 vim_free(p->tofree);
1242 ++p;
1243 }
1244 ga_clear(&join_ga);
1245 }
1246
1247 return retval;
1248}
1249
1250/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001251 * "join()" function
1252 */
1253 void
1254f_join(typval_T *argvars, typval_T *rettv)
1255{
1256 garray_T ga;
1257 char_u *sep;
1258
1259 if (argvars[0].v_type != VAR_LIST)
1260 {
1261 emsg(_(e_listreq));
1262 return;
1263 }
1264 if (argvars[0].vval.v_list == NULL)
1265 return;
1266 if (argvars[1].v_type == VAR_UNKNOWN)
1267 sep = (char_u *)" ";
1268 else
1269 sep = tv_get_string_chk(&argvars[1]);
1270
1271 rettv->v_type = VAR_STRING;
1272
1273 if (sep != NULL)
1274 {
1275 ga_init2(&ga, (int)sizeof(char), 80);
1276 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1277 ga_append(&ga, NUL);
1278 rettv->vval.v_string = (char_u *)ga.ga_data;
1279 }
1280 else
1281 rettv->vval.v_string = NULL;
1282}
1283
1284/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001285 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001286 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001287 * Return OK or FAIL.
1288 */
1289 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001290eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001291{
Bram Moolenaar71478202020-06-26 22:46:27 +02001292 int evaluate = evalarg == NULL ? FALSE
1293 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001294 list_T *l = NULL;
1295 typval_T tv;
1296 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001297 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001298 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001299
1300 if (evaluate)
1301 {
1302 l = list_alloc();
1303 if (l == NULL)
1304 return FAIL;
1305 }
1306
Bram Moolenaar962d7212020-07-04 14:15:00 +02001307 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001308 while (**arg != ']' && **arg != NUL)
1309 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001310 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001311 goto failret;
1312 if (evaluate)
1313 {
1314 item = listitem_alloc();
1315 if (item != NULL)
1316 {
1317 item->li_tv = tv;
1318 item->li_tv.v_lock = 0;
1319 list_append(l, item);
1320 }
1321 else
1322 clear_tv(&tv);
1323 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001324 // Legacy Vim script allowed a space before the comma.
1325 if (!vim9script)
1326 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001327
Bram Moolenaare6e03172020-06-27 16:36:05 +02001328 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001329 had_comma = **arg == ',';
1330 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001331 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001332 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001333 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001334 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001335 goto failret;
1336 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001337 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001338 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001339
Bram Moolenaare6b53242020-07-01 17:28:33 +02001340 // The "]" can be on the next line. But a double quoted string may
1341 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001342 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001343 if (**arg == ']')
1344 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001345
1346 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001347 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001349 {
1350 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001351 semsg(_(e_no_white_space_allowed_before_str_str),
1352 ",", *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001353 else
1354 semsg(_("E696: Missing comma in List: %s"), *arg);
1355 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001356 goto failret;
1357 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001358 }
1359
1360 if (**arg != ']')
1361 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001363 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001364failret:
1365 if (evaluate)
1366 list_free(l);
1367 return FAIL;
1368 }
1369
Bram Moolenaar9d489562020-07-30 20:08:50 +02001370 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001371 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001372 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001373
1374 return OK;
1375}
1376
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001377/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001378 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001379 */
1380 int
1381write_list(FILE *fd, list_T *list, int binary)
1382{
1383 listitem_T *li;
1384 int c;
1385 int ret = OK;
1386 char_u *s;
1387
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001388 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001389 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001390 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001391 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001392 {
1393 if (*s == '\n')
1394 c = putc(NUL, fd);
1395 else
1396 c = putc(*s, fd);
1397 if (c == EOF)
1398 {
1399 ret = FAIL;
1400 break;
1401 }
1402 }
1403 if (!binary || li->li_next != NULL)
1404 if (putc('\n', fd) == EOF)
1405 {
1406 ret = FAIL;
1407 break;
1408 }
1409 if (ret == FAIL)
1410 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001411 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001412 break;
1413 }
1414 }
1415 return ret;
1416}
1417
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001418/*
1419 * Initialize a static list with 10 items.
1420 */
1421 void
1422init_static_list(staticList10_T *sl)
1423{
1424 list_T *l = &sl->sl_list;
1425 int i;
1426
1427 memset(sl, 0, sizeof(staticList10_T));
1428 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001429 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001430 l->lv_refcount = DO_NOT_FREE_CNT;
1431 l->lv_lock = VAR_FIXED;
1432 sl->sl_list.lv_len = 10;
1433
1434 for (i = 0; i < 10; ++i)
1435 {
1436 listitem_T *li = &sl->sl_items[i];
1437
1438 if (i == 0)
1439 li->li_prev = NULL;
1440 else
1441 li->li_prev = li - 1;
1442 if (i == 9)
1443 li->li_next = NULL;
1444 else
1445 li->li_next = li + 1;
1446 }
1447}
1448
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001449/*
1450 * "list2str()" function
1451 */
1452 void
1453f_list2str(typval_T *argvars, typval_T *rettv)
1454{
1455 list_T *l;
1456 listitem_T *li;
1457 garray_T ga;
1458 int utf8 = FALSE;
1459
1460 rettv->v_type = VAR_STRING;
1461 rettv->vval.v_string = NULL;
1462 if (argvars[0].v_type != VAR_LIST)
1463 {
1464 emsg(_(e_invarg));
1465 return;
1466 }
1467
1468 l = argvars[0].vval.v_list;
1469 if (l == NULL)
1470 return; // empty list results in empty string
1471
1472 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001473 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001474
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001475 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001476 ga_init2(&ga, 1, 80);
1477 if (has_mbyte || utf8)
1478 {
1479 char_u buf[MB_MAXBYTES + 1];
1480 int (*char2bytes)(int, char_u *);
1481
1482 if (utf8 || enc_utf8)
1483 char2bytes = utf_char2bytes;
1484 else
1485 char2bytes = mb_char2bytes;
1486
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001487 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001488 {
1489 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1490 ga_concat(&ga, buf);
1491 }
1492 ga_append(&ga, NUL);
1493 }
1494 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1495 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001496 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001497 ga_append(&ga, tv_get_number(&li->li_tv));
1498 ga_append(&ga, NUL);
1499 }
1500
1501 rettv->v_type = VAR_STRING;
1502 rettv->vval.v_string = ga.ga_data;
1503}
1504
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001505 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001506list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1507{
1508 list_T *l;
1509 listitem_T *item, *item2;
1510 listitem_T *li;
1511 int error = FALSE;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001512 long idx;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001513
1514 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001515 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001516 return;
1517
1518 idx = (long)tv_get_number_chk(&argvars[1], &error);
1519 if (error)
1520 ; // type error: do nothing, errmsg already given
1521 else if ((item = list_find(l, idx)) == NULL)
1522 semsg(_(e_listidx), idx);
1523 else
1524 {
1525 if (argvars[2].v_type == VAR_UNKNOWN)
1526 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001527 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001528 vimlist_remove(l, item, item);
1529 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001531 }
1532 else
1533 {
1534 // Remove range of items, return list with values.
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001535 long end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001536
1537 if (error)
1538 ; // type error: do nothing
1539 else if ((item2 = list_find(l, end)) == NULL)
1540 semsg(_(e_listidx), end);
1541 else
1542 {
1543 int cnt = 0;
1544
1545 for (li = item; li != NULL; li = li->li_next)
1546 {
1547 ++cnt;
1548 if (li == item2)
1549 break;
1550 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001551 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001552 emsg(_(e_invrange));
1553 else
1554 {
1555 vimlist_remove(l, item, item2);
1556 if (rettv_list_alloc(rettv) == OK)
1557 {
1558 l = rettv->vval.v_list;
1559 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001560 l->lv_u.mat.lv_last = item2;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001561 item->li_prev = NULL;
1562 item2->li_next = NULL;
1563 l->lv_len = cnt;
1564 }
1565 }
1566 }
1567 }
1568 }
1569}
1570
1571static int item_compare(const void *s1, const void *s2);
1572static int item_compare2(const void *s1, const void *s2);
1573
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001574// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001575typedef struct
1576{
1577 listitem_T *item;
1578 int idx;
1579} sortItem_T;
1580
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001581// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001582typedef struct
1583{
1584 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001585 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001586 int item_compare_numeric;
1587 int item_compare_numbers;
1588#ifdef FEAT_FLOAT
1589 int item_compare_float;
1590#endif
1591 char_u *item_compare_func;
1592 partial_T *item_compare_partial;
1593 dict_T *item_compare_selfdict;
1594 int item_compare_func_err;
1595 int item_compare_keep_zero;
1596} sortinfo_T;
1597static sortinfo_T *sortinfo = NULL;
1598#define ITEM_COMPARE_FAIL 999
1599
1600/*
1601 * Compare functions for f_sort() and f_uniq() below.
1602 */
1603 static int
1604item_compare(const void *s1, const void *s2)
1605{
1606 sortItem_T *si1, *si2;
1607 typval_T *tv1, *tv2;
1608 char_u *p1, *p2;
1609 char_u *tofree1 = NULL, *tofree2 = NULL;
1610 int res;
1611 char_u numbuf1[NUMBUFLEN];
1612 char_u numbuf2[NUMBUFLEN];
1613
1614 si1 = (sortItem_T *)s1;
1615 si2 = (sortItem_T *)s2;
1616 tv1 = &si1->item->li_tv;
1617 tv2 = &si2->item->li_tv;
1618
1619 if (sortinfo->item_compare_numbers)
1620 {
1621 varnumber_T v1 = tv_get_number(tv1);
1622 varnumber_T v2 = tv_get_number(tv2);
1623
1624 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1625 }
1626
1627#ifdef FEAT_FLOAT
1628 if (sortinfo->item_compare_float)
1629 {
1630 float_T v1 = tv_get_float(tv1);
1631 float_T v2 = tv_get_float(tv2);
1632
1633 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1634 }
1635#endif
1636
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001637 // tv2string() puts quotes around a string and allocates memory. Don't do
1638 // that for string variables. Use a single quote when comparing with a
1639 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001640 if (tv1->v_type == VAR_STRING)
1641 {
1642 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1643 p1 = (char_u *)"'";
1644 else
1645 p1 = tv1->vval.v_string;
1646 }
1647 else
1648 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1649 if (tv2->v_type == VAR_STRING)
1650 {
1651 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1652 p2 = (char_u *)"'";
1653 else
1654 p2 = tv2->vval.v_string;
1655 }
1656 else
1657 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1658 if (p1 == NULL)
1659 p1 = (char_u *)"";
1660 if (p2 == NULL)
1661 p2 = (char_u *)"";
1662 if (!sortinfo->item_compare_numeric)
1663 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001664 if (sortinfo->item_compare_lc)
1665 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001666 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001667 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001668 }
1669 else
1670 {
1671 double n1, n2;
1672 n1 = strtod((char *)p1, (char **)&p1);
1673 n2 = strtod((char *)p2, (char **)&p2);
1674 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1675 }
1676
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001677 // When the result would be zero, compare the item indexes. Makes the
1678 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001679 if (res == 0 && !sortinfo->item_compare_keep_zero)
1680 res = si1->idx > si2->idx ? 1 : -1;
1681
1682 vim_free(tofree1);
1683 vim_free(tofree2);
1684 return res;
1685}
1686
1687 static int
1688item_compare2(const void *s1, const void *s2)
1689{
1690 sortItem_T *si1, *si2;
1691 int res;
1692 typval_T rettv;
1693 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001694 char_u *func_name;
1695 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001696 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001697
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001698 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001699 if (sortinfo->item_compare_func_err)
1700 return 0;
1701
1702 si1 = (sortItem_T *)s1;
1703 si2 = (sortItem_T *)s2;
1704
1705 if (partial == NULL)
1706 func_name = sortinfo->item_compare_func;
1707 else
1708 func_name = partial_name(partial);
1709
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001710 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1711 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001712 copy_tv(&si1->item->li_tv, &argv[0]);
1713 copy_tv(&si2->item->li_tv, &argv[1]);
1714
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001715 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001716 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001717 funcexe.evaluate = TRUE;
1718 funcexe.partial = partial;
1719 funcexe.selfdict = sortinfo->item_compare_selfdict;
1720 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001721 clear_tv(&argv[0]);
1722 clear_tv(&argv[1]);
1723
1724 if (res == FAIL)
1725 res = ITEM_COMPARE_FAIL;
1726 else
1727 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1728 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001729 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001730 clear_tv(&rettv);
1731
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001732 // When the result would be zero, compare the pointers themselves. Makes
1733 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001734 if (res == 0 && !sortinfo->item_compare_keep_zero)
1735 res = si1->idx > si2->idx ? 1 : -1;
1736
1737 return res;
1738}
1739
1740/*
1741 * "sort()" or "uniq()" function
1742 */
1743 static void
1744do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1745{
1746 list_T *l;
1747 listitem_T *li;
1748 sortItem_T *ptrs;
1749 sortinfo_T *old_sortinfo;
1750 sortinfo_T info;
1751 long len;
1752 long i;
1753
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001754 // Pointer to current info struct used in compare function. Save and
1755 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001756 old_sortinfo = sortinfo;
1757 sortinfo = &info;
1758
1759 if (argvars[0].v_type != VAR_LIST)
1760 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1761 else
1762 {
1763 l = argvars[0].vval.v_list;
Bram Moolenaara187c432020-09-16 21:08:28 +02001764 if (l == NULL || value_check_lock(l->lv_lock,
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001765 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1766 TRUE))
1767 goto theend;
1768 rettv_list_set(rettv, l);
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001769 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001770
1771 len = list_len(l);
1772 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001773 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001774
1775 info.item_compare_ic = FALSE;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001776 info.item_compare_lc = FALSE;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001777 info.item_compare_numeric = FALSE;
1778 info.item_compare_numbers = FALSE;
1779#ifdef FEAT_FLOAT
1780 info.item_compare_float = FALSE;
1781#endif
1782 info.item_compare_func = NULL;
1783 info.item_compare_partial = NULL;
1784 info.item_compare_selfdict = NULL;
1785 if (argvars[1].v_type != VAR_UNKNOWN)
1786 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001787 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001788 if (argvars[1].v_type == VAR_FUNC)
1789 info.item_compare_func = argvars[1].vval.v_string;
1790 else if (argvars[1].v_type == VAR_PARTIAL)
1791 info.item_compare_partial = argvars[1].vval.v_partial;
1792 else
1793 {
1794 int error = FALSE;
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001795 int nr = 0;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001796
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001797 if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001798 {
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001799 nr = tv_get_number_chk(&argvars[1], &error);
1800 if (error)
1801 goto theend; // type error; errmsg already given
1802 if (nr == 1)
1803 info.item_compare_ic = TRUE;
1804 }
1805 if (nr != 1)
1806 {
1807 if (argvars[1].v_type != VAR_NUMBER)
1808 info.item_compare_func = tv_get_string(&argvars[1]);
1809 else if (nr != 0)
1810 {
1811 emsg(_(e_invarg));
1812 goto theend;
1813 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001814 }
1815 if (info.item_compare_func != NULL)
1816 {
1817 if (*info.item_compare_func == NUL)
1818 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001819 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001820 info.item_compare_func = NULL;
1821 }
1822 else if (STRCMP(info.item_compare_func, "n") == 0)
1823 {
1824 info.item_compare_func = NULL;
1825 info.item_compare_numeric = TRUE;
1826 }
1827 else if (STRCMP(info.item_compare_func, "N") == 0)
1828 {
1829 info.item_compare_func = NULL;
1830 info.item_compare_numbers = TRUE;
1831 }
1832#ifdef FEAT_FLOAT
1833 else if (STRCMP(info.item_compare_func, "f") == 0)
1834 {
1835 info.item_compare_func = NULL;
1836 info.item_compare_float = TRUE;
1837 }
1838#endif
1839 else if (STRCMP(info.item_compare_func, "i") == 0)
1840 {
1841 info.item_compare_func = NULL;
1842 info.item_compare_ic = TRUE;
1843 }
Bram Moolenaar55e29612020-11-01 13:57:44 +01001844 else if (STRCMP(info.item_compare_func, "l") == 0)
1845 {
1846 info.item_compare_func = NULL;
1847 info.item_compare_lc = TRUE;
1848 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001849 }
1850 }
1851
1852 if (argvars[2].v_type != VAR_UNKNOWN)
1853 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001854 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001855 if (argvars[2].v_type != VAR_DICT)
1856 {
1857 emsg(_(e_dictreq));
1858 goto theend;
1859 }
1860 info.item_compare_selfdict = argvars[2].vval.v_dict;
1861 }
1862 }
1863
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001864 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001865 ptrs = ALLOC_MULT(sortItem_T, len);
1866 if (ptrs == NULL)
1867 goto theend;
1868
1869 i = 0;
1870 if (sort)
1871 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001872 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001873 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001874 {
1875 ptrs[i].item = li;
1876 ptrs[i].idx = i;
1877 ++i;
1878 }
1879
1880 info.item_compare_func_err = FALSE;
1881 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001882 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001883 if ((info.item_compare_func != NULL
1884 || info.item_compare_partial != NULL)
1885 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
1886 == ITEM_COMPARE_FAIL)
1887 emsg(_("E702: Sort compare function failed"));
1888 else
1889 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001890 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001891 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
1892 info.item_compare_func == NULL
1893 && info.item_compare_partial == NULL
1894 ? item_compare : item_compare2);
1895
1896 if (!info.item_compare_func_err)
1897 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001898 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001899 l->lv_first = l->lv_u.mat.lv_last
1900 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001901 l->lv_len = 0;
1902 for (i = 0; i < len; ++i)
1903 list_append(l, ptrs[i].item);
1904 }
1905 }
1906 }
1907 else
1908 {
1909 int (*item_compare_func_ptr)(const void *, const void *);
1910
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001911 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001912 info.item_compare_func_err = FALSE;
1913 info.item_compare_keep_zero = TRUE;
1914 item_compare_func_ptr = info.item_compare_func != NULL
1915 || info.item_compare_partial != NULL
1916 ? item_compare2 : item_compare;
1917
1918 for (li = l->lv_first; li != NULL && li->li_next != NULL;
1919 li = li->li_next)
1920 {
1921 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
1922 == 0)
1923 ptrs[i++].item = li;
1924 if (info.item_compare_func_err)
1925 {
1926 emsg(_("E882: Uniq compare function failed"));
1927 break;
1928 }
1929 }
1930
1931 if (!info.item_compare_func_err)
1932 {
1933 while (--i >= 0)
1934 {
1935 li = ptrs[i].item->li_next;
1936 ptrs[i].item->li_next = li->li_next;
1937 if (li->li_next != NULL)
1938 li->li_next->li_prev = ptrs[i].item;
1939 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001940 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001941 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001943 l->lv_len--;
1944 }
1945 }
1946 }
1947
1948 vim_free(ptrs);
1949 }
1950theend:
1951 sortinfo = old_sortinfo;
1952}
1953
1954/*
1955 * "sort({list})" function
1956 */
1957 void
1958f_sort(typval_T *argvars, typval_T *rettv)
1959{
1960 do_sort_uniq(argvars, rettv, TRUE);
1961}
1962
1963/*
1964 * "uniq({list})" function
1965 */
1966 void
1967f_uniq(typval_T *argvars, typval_T *rettv)
1968{
1969 do_sort_uniq(argvars, rettv, FALSE);
1970}
1971
Bram Moolenaarea696852020-11-09 18:31:39 +01001972typedef enum {
1973 FILTERMAP_FILTER,
1974 FILTERMAP_MAP,
1975 FILTERMAP_MAPNEW
1976} filtermap_T;
1977
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001978/*
1979 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01001980 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001981 */
1982 static int
Bram Moolenaarea696852020-11-09 18:31:39 +01001983filter_map_one(
1984 typval_T *tv, // original value
1985 typval_T *expr, // callback
1986 filtermap_T filtermap,
1987 typval_T *newtv, // for map() and mapnew(): new value
1988 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001989{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001990 typval_T argv[3];
1991 int retval = FAIL;
1992
1993 copy_tv(tv, get_vim_var_tv(VV_VAL));
1994 argv[0] = *get_vim_var_tv(VV_KEY);
1995 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01001996 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001997 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01001998 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001999 {
2000 int error = FALSE;
2001
2002 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02002003 if (in_vim9script())
Bram Moolenaarea696852020-11-09 18:31:39 +01002004 *remp = !tv2bool(newtv);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002005 else
Bram Moolenaarea696852020-11-09 18:31:39 +01002006 *remp = (tv_get_number_chk(newtv, &error) == 0);
2007 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002008 // On type error, nothing has been removed; return FAIL to stop the
2009 // loop. The error message was given by tv_get_number_chk().
2010 if (error)
2011 goto theend;
2012 }
2013 retval = OK;
2014theend:
2015 clear_tv(get_vim_var_tv(VV_VAL));
2016 return retval;
2017}
2018
2019/*
2020 * Implementation of map() and filter().
2021 */
2022 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01002023filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002024{
2025 typval_T *expr;
2026 listitem_T *li, *nli;
2027 list_T *l = NULL;
2028 dictitem_T *di;
2029 hashtab_T *ht;
2030 hashitem_T *hi;
2031 dict_T *d = NULL;
2032 blob_T *b = NULL;
2033 int rem;
2034 int todo;
Bram Moolenaarea696852020-11-09 18:31:39 +01002035 char_u *ermsg = (char_u *)(filtermap == FILTERMAP_MAP ? "map()"
2036 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
2037 : "filter()");
2038 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2039 ? N_("map() argument")
2040 : filtermap == FILTERMAP_MAPNEW
2041 ? N_("mapnew() argument")
2042 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002043 int save_did_emsg;
2044 int idx = 0;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002045 type_T *type = NULL;
2046 garray_T type_list;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002047
Bram Moolenaarea696852020-11-09 18:31:39 +01002048 // map() and filter() return the first argument, also on failure.
2049 if (filtermap != FILTERMAP_MAPNEW)
2050 copy_tv(&argvars[0], rettv);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002051 if (filtermap == FILTERMAP_MAP && in_vim9script())
2052 {
2053 // Check that map() does not change the type of the dict.
2054 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaar108cf012021-03-18 22:15:04 +01002055 type = typval2type(argvars, get_copyID(), &type_list);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002056 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002057
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002058 if (argvars[0].v_type == VAR_BLOB)
2059 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002060 if (filtermap == FILTERMAP_MAPNEW)
2061 {
2062 rettv->v_type = VAR_BLOB;
2063 rettv->vval.v_blob = NULL;
2064 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002065 if ((b = argvars[0].vval.v_blob) == NULL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002066 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002067 }
2068 else if (argvars[0].v_type == VAR_LIST)
2069 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002070 if (filtermap == FILTERMAP_MAPNEW)
2071 {
2072 rettv->v_type = VAR_LIST;
2073 rettv->vval.v_list = NULL;
2074 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002075 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002076 || (filtermap == FILTERMAP_FILTER
2077 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002078 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002079 }
2080 else if (argvars[0].v_type == VAR_DICT)
2081 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002082 if (filtermap == FILTERMAP_MAPNEW)
2083 {
2084 rettv->v_type = VAR_DICT;
2085 rettv->vval.v_dict = NULL;
2086 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002087 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002088 || (filtermap == FILTERMAP_FILTER
2089 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002090 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002091 }
2092 else
2093 {
Bram Moolenaarfcb0b612020-05-26 20:22:01 +02002094 semsg(_(e_listdictblobarg), ermsg);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002095 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002096 }
2097
2098 expr = &argvars[1];
2099 // On type errors, the preceding call has already displayed an error
2100 // message. Avoid a misleading error message for an empty string that
2101 // was not passed as argument.
2102 if (expr->v_type != VAR_UNKNOWN)
2103 {
2104 typval_T save_val;
2105 typval_T save_key;
2106
2107 prepare_vimvar(VV_VAL, &save_val);
2108 prepare_vimvar(VV_KEY, &save_key);
2109
2110 // We reset "did_emsg" to be able to detect whether an error
2111 // occurred during evaluation of the expression.
2112 save_did_emsg = did_emsg;
2113 did_emsg = FALSE;
2114
2115 if (argvars[0].v_type == VAR_DICT)
2116 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002117 int prev_lock = d->dv_lock;
Bram Moolenaarea696852020-11-09 18:31:39 +01002118 dict_T *d_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002119
Bram Moolenaarea696852020-11-09 18:31:39 +01002120 if (filtermap == FILTERMAP_MAPNEW)
2121 {
2122 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002123 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002124 d_ret = rettv->vval.v_dict;
2125 }
2126
2127 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002128 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002129 ht = &d->dv_hashtab;
2130 hash_lock(ht);
2131 todo = (int)ht->ht_used;
2132 for (hi = ht->ht_array; todo > 0; ++hi)
2133 {
2134 if (!HASHITEM_EMPTY(hi))
2135 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002136 int r;
2137 typval_T newtv;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002138
2139 --todo;
2140 di = HI2DI(hi);
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002141 if (filtermap == FILTERMAP_MAP
Bram Moolenaarea696852020-11-09 18:31:39 +01002142 && (value_check_lock(di->di_tv.v_lock,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002143 arg_errmsg, TRUE)
2144 || var_check_ro(di->di_flags,
2145 arg_errmsg, TRUE)))
2146 break;
2147 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaar027c4ab2021-02-21 16:20:18 +01002148 newtv.v_type = VAR_UNKNOWN;
Bram Moolenaarea696852020-11-09 18:31:39 +01002149 r = filter_map_one(&di->di_tv, expr, filtermap,
2150 &newtv, &rem);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002151 clear_tv(get_vim_var_tv(VV_KEY));
2152 if (r == FAIL || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002153 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002154 clear_tv(&newtv);
2155 break;
2156 }
2157 if (filtermap == FILTERMAP_MAP)
2158 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002159 if (type != NULL && check_typval_arg_type(
2160 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002161 {
2162 clear_tv(&newtv);
2163 break;
2164 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002165 // map(): replace the dict item value
2166 clear_tv(&di->di_tv);
2167 newtv.v_lock = 0;
2168 di->di_tv = newtv;
2169 }
2170 else if (filtermap == FILTERMAP_MAPNEW)
2171 {
2172 // mapnew(): add the item value to the new dict
2173 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
2174 clear_tv(&newtv);
2175 if (r == FAIL)
2176 break;
2177 }
2178 else if (filtermap == FILTERMAP_FILTER && rem)
2179 {
2180 // filter(false): remove the item from the dict
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002181 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2182 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2183 break;
2184 dictitem_remove(d, di);
2185 }
2186 }
2187 }
2188 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002189 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002190 }
2191 else if (argvars[0].v_type == VAR_BLOB)
2192 {
2193 int i;
2194 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002195 varnumber_T val;
Bram Moolenaarea696852020-11-09 18:31:39 +01002196 blob_T *b_ret = b;
2197
2198 if (filtermap == FILTERMAP_MAPNEW)
2199 {
2200 if (blob_copy(b, rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002201 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002202 b_ret = rettv->vval.v_blob;
2203 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002204
2205 // set_vim_var_nr() doesn't set the type
2206 set_vim_var_type(VV_KEY, VAR_NUMBER);
2207
2208 for (i = 0; i < b->bv_ga.ga_len; i++)
2209 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002210 typval_T newtv;
2211
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002212 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002213 val = blob_get(b, i);
2214 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002215 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaarea696852020-11-09 18:31:39 +01002216 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
2217 || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002218 break;
Bram Moolenaarea696852020-11-09 18:31:39 +01002219 if (newtv.v_type != VAR_NUMBER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002220 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002221 clear_tv(&newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002222 emsg(_(e_invalblob));
2223 break;
2224 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002225 if (filtermap != FILTERMAP_FILTER)
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002226 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002227 if (newtv.vval.v_number != val)
2228 blob_set(b_ret, i, newtv.vval.v_number);
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002229 }
2230 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002231 {
2232 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2233
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002234 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002235 (size_t)b->bv_ga.ga_len - i - 1);
2236 --b->bv_ga.ga_len;
2237 --i;
2238 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002239 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002240 }
2241 }
2242 else // argvars[0].v_type == VAR_LIST
2243 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002244 int prev_lock = l->lv_lock;
2245 list_T *l_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002246
Bram Moolenaarea696852020-11-09 18:31:39 +01002247 if (filtermap == FILTERMAP_MAPNEW)
2248 {
2249 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002250 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002251 l_ret = rettv->vval.v_list;
2252 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002253 // set_vim_var_nr() doesn't set the type
2254 set_vim_var_type(VV_KEY, VAR_NUMBER);
2255
Bram Moolenaarea696852020-11-09 18:31:39 +01002256 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002257 l->lv_lock = VAR_LOCKED;
Bram Moolenaarea696852020-11-09 18:31:39 +01002258
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002259 if (l->lv_first == &range_list_item)
2260 {
2261 varnumber_T val = l->lv_u.nonmat.lv_start;
2262 int len = l->lv_len;
2263 int stride = l->lv_u.nonmat.lv_stride;
2264
2265 // List from range(): loop over the numbers
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002266 if (filtermap != FILTERMAP_MAPNEW)
2267 {
2268 l->lv_first = NULL;
2269 l->lv_u.mat.lv_last = NULL;
2270 l->lv_len = 0;
2271 l->lv_u.mat.lv_idx_item = NULL;
2272 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002273
2274 for (idx = 0; idx < len; ++idx)
Bram Moolenaarc56936e2020-11-10 11:43:56 +01002275 {
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002276 typval_T tv;
2277 typval_T newtv;
2278
2279 tv.v_type = VAR_NUMBER;
2280 tv.v_lock = 0;
2281 tv.vval.v_number = val;
2282 set_vim_var_nr(VV_KEY, idx);
2283 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2284 == FAIL)
Bram Moolenaarea696852020-11-09 18:31:39 +01002285 break;
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002286 if (did_emsg)
2287 {
2288 clear_tv(&newtv);
2289 break;
2290 }
2291 if (filtermap != FILTERMAP_FILTER)
2292 {
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002293 if (filtermap == FILTERMAP_MAP && type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002294 && check_typval_arg_type(
2295 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002296 {
2297 clear_tv(&newtv);
2298 break;
2299 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002300 // map(), mapnew(): always append the new value to the
2301 // list
2302 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2303 ? l : l_ret, &newtv) == FAIL)
2304 break;
2305 }
2306 else if (!rem)
2307 {
2308 // filter(): append the list item value when not rem
2309 if (list_append_tv_move(l, &tv) == FAIL)
2310 break;
2311 }
2312
2313 val += stride;
Bram Moolenaarea696852020-11-09 18:31:39 +01002314 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002315 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002316 else
2317 {
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002318 // Materialized list: loop over the items
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002319 for (li = l->lv_first; li != NULL; li = nli)
2320 {
2321 typval_T newtv;
2322
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002323 if (filtermap == FILTERMAP_MAP && value_check_lock(
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002324 li->li_tv.v_lock, arg_errmsg, TRUE))
2325 break;
2326 nli = li->li_next;
2327 set_vim_var_nr(VV_KEY, idx);
2328 if (filter_map_one(&li->li_tv, expr, filtermap,
2329 &newtv, &rem) == FAIL)
2330 break;
2331 if (did_emsg)
2332 {
2333 clear_tv(&newtv);
2334 break;
2335 }
2336 if (filtermap == FILTERMAP_MAP)
2337 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002338 if (type != NULL && check_typval_arg_type(
2339 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002340 {
2341 clear_tv(&newtv);
2342 break;
2343 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002344 // map(): replace the list item value
2345 clear_tv(&li->li_tv);
2346 newtv.v_lock = 0;
2347 li->li_tv = newtv;
2348 }
2349 else if (filtermap == FILTERMAP_MAPNEW)
2350 {
2351 // mapnew(): append the list item value
2352 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2353 break;
2354 }
2355 else if (filtermap == FILTERMAP_FILTER && rem)
2356 listitem_remove(l, li);
2357 ++idx;
2358 }
2359 }
2360
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002361 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002362 }
2363
2364 restore_vimvar(VV_KEY, &save_key);
2365 restore_vimvar(VV_VAL, &save_val);
2366
2367 did_emsg |= save_did_emsg;
2368 }
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002369
2370theend:
2371 if (type != NULL)
2372 clear_type_list(&type_list);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002373}
2374
2375/*
2376 * "filter()" function
2377 */
2378 void
2379f_filter(typval_T *argvars, typval_T *rettv)
2380{
Bram Moolenaarea696852020-11-09 18:31:39 +01002381 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002382}
2383
2384/*
2385 * "map()" function
2386 */
2387 void
2388f_map(typval_T *argvars, typval_T *rettv)
2389{
Bram Moolenaarea696852020-11-09 18:31:39 +01002390 filter_map(argvars, rettv, FILTERMAP_MAP);
2391}
2392
2393/*
2394 * "mapnew()" function
2395 */
2396 void
2397f_mapnew(typval_T *argvars, typval_T *rettv)
2398{
2399 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002400}
2401
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002402/*
2403 * "add(list, item)" function
2404 */
2405 void
2406f_add(typval_T *argvars, typval_T *rettv)
2407{
2408 list_T *l;
2409 blob_T *b;
2410
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002411 rettv->vval.v_number = 1; // Default: Failed
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002412 if (argvars[0].v_type == VAR_LIST)
2413 {
2414 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002415 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002416 (char_u *)N_("add() argument"), TRUE)
2417 && list_append_tv(l, &argvars[1]) == OK)
2418 copy_tv(&argvars[0], rettv);
2419 }
2420 else if (argvars[0].v_type == VAR_BLOB)
2421 {
2422 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002423 && !value_check_lock(b->bv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002424 (char_u *)N_("add() argument"), TRUE))
2425 {
2426 int error = FALSE;
2427 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2428
2429 if (!error)
2430 {
2431 ga_append(&b->bv_ga, (int)n);
2432 copy_tv(&argvars[0], rettv);
2433 }
2434 }
2435 }
2436 else
2437 emsg(_(e_listblobreq));
2438}
2439
2440/*
2441 * "count()" function
2442 */
2443 void
2444f_count(typval_T *argvars, typval_T *rettv)
2445{
2446 long n = 0;
2447 int ic = FALSE;
2448 int error = FALSE;
2449
2450 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002451 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002452
2453 if (argvars[0].v_type == VAR_STRING)
2454 {
2455 char_u *expr = tv_get_string_chk(&argvars[1]);
2456 char_u *p = argvars[0].vval.v_string;
2457 char_u *next;
2458
2459 if (!error && expr != NULL && *expr != NUL && p != NULL)
2460 {
2461 if (ic)
2462 {
2463 size_t len = STRLEN(expr);
2464
2465 while (*p != NUL)
2466 {
2467 if (MB_STRNICMP(p, expr, len) == 0)
2468 {
2469 ++n;
2470 p += len;
2471 }
2472 else
2473 MB_PTR_ADV(p);
2474 }
2475 }
2476 else
2477 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2478 != NULL)
2479 {
2480 ++n;
2481 p = next + STRLEN(expr);
2482 }
2483 }
2484
2485 }
2486 else if (argvars[0].v_type == VAR_LIST)
2487 {
2488 listitem_T *li;
2489 list_T *l;
2490 long idx;
2491
2492 if ((l = argvars[0].vval.v_list) != NULL)
2493 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002494 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002495 li = l->lv_first;
2496 if (argvars[2].v_type != VAR_UNKNOWN)
2497 {
2498 if (argvars[3].v_type != VAR_UNKNOWN)
2499 {
2500 idx = (long)tv_get_number_chk(&argvars[3], &error);
2501 if (!error)
2502 {
2503 li = list_find(l, idx);
2504 if (li == NULL)
2505 semsg(_(e_listidx), idx);
2506 }
2507 }
2508 if (error)
2509 li = NULL;
2510 }
2511
2512 for ( ; li != NULL; li = li->li_next)
2513 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2514 ++n;
2515 }
2516 }
2517 else if (argvars[0].v_type == VAR_DICT)
2518 {
2519 int todo;
2520 dict_T *d;
2521 hashitem_T *hi;
2522
2523 if ((d = argvars[0].vval.v_dict) != NULL)
2524 {
2525 if (argvars[2].v_type != VAR_UNKNOWN)
2526 {
2527 if (argvars[3].v_type != VAR_UNKNOWN)
2528 emsg(_(e_invarg));
2529 }
2530
2531 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2532 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2533 {
2534 if (!HASHITEM_EMPTY(hi))
2535 {
2536 --todo;
2537 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2538 ++n;
2539 }
2540 }
2541 }
2542 }
2543 else
2544 semsg(_(e_listdictarg), "count()");
2545 rettv->vval.v_number = n;
2546}
2547
2548/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002549 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002550 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002551 static void
2552extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002553{
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002554 type_T *type = NULL;
2555 garray_T type_list;
2556
2557 if (!is_new && in_vim9script())
2558 {
2559 // Check that map() does not change the type of the dict.
2560 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaar108cf012021-03-18 22:15:04 +01002561 type = typval2type(argvars, get_copyID(), &type_list);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002562 }
2563
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002564 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2565 {
2566 list_T *l1, *l2;
2567 listitem_T *item;
2568 long before;
2569 int error = FALSE;
2570
2571 l1 = argvars[0].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002572 if (l1 == NULL)
2573 {
2574 emsg(_(e_cannot_extend_null_list));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002575 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002576 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002577 l2 = argvars[1].vval.v_list;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002578 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
2579 && l2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002580 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002581 if (is_new)
2582 {
2583 l1 = list_copy(l1, FALSE, get_copyID());
2584 if (l1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002585 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002586 }
2587
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002588 if (argvars[2].v_type != VAR_UNKNOWN)
2589 {
2590 before = (long)tv_get_number_chk(&argvars[2], &error);
2591 if (error)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002592 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002593
2594 if (before == l1->lv_len)
2595 item = NULL;
2596 else
2597 {
2598 item = list_find(l1, before);
2599 if (item == NULL)
2600 {
2601 semsg(_(e_listidx), before);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002602 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002603 }
2604 }
2605 }
2606 else
2607 item = NULL;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002608 if (type != NULL && check_typval_arg_type(
2609 type, &argvars[1], 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002610 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002611 list_extend(l1, l2, item);
2612
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002613 if (is_new)
2614 {
2615 rettv->v_type = VAR_LIST;
2616 rettv->vval.v_list = l1;
2617 rettv->v_lock = FALSE;
2618 }
2619 else
2620 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002621 }
2622 }
2623 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2624 {
2625 dict_T *d1, *d2;
2626 char_u *action;
2627 int i;
2628
2629 d1 = argvars[0].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002630 if (d1 == NULL)
2631 {
2632 emsg(_(e_cannot_extend_null_dict));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002633 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002634 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002635 d2 = argvars[1].vval.v_dict;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002636 if ((is_new || !value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
2637 && d2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002638 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002639 if (is_new)
2640 {
2641 d1 = dict_copy(d1, FALSE, get_copyID());
2642 if (d1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002643 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002644 }
2645
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002646 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002647 if (argvars[2].v_type != VAR_UNKNOWN)
2648 {
2649 static char *(av[]) = {"keep", "force", "error"};
2650
2651 action = tv_get_string_chk(&argvars[2]);
2652 if (action == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002653 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002654 for (i = 0; i < 3; ++i)
2655 if (STRCMP(action, av[i]) == 0)
2656 break;
2657 if (i == 3)
2658 {
2659 semsg(_(e_invarg2), action);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002660 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002661 }
2662 }
2663 else
2664 action = (char_u *)"force";
2665
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002666 if (type != NULL && check_typval_arg_type(
2667 type, &argvars[1], 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002668 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002669 dict_extend(d1, d2, action);
2670
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002671 if (is_new)
2672 {
2673 rettv->v_type = VAR_DICT;
2674 rettv->vval.v_dict = d1;
2675 rettv->v_lock = FALSE;
2676 }
2677 else
2678 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002679 }
2680 }
2681 else
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002682 semsg(_(e_listdictarg), is_new ? "extendnew()" : "extend()");
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002683
2684theend:
2685 if (type != NULL)
2686 clear_type_list(&type_list);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002687}
2688
2689/*
2690 * "extend(list, list [, idx])" function
2691 * "extend(dict, dict [, action])" function
2692 */
2693 void
2694f_extend(typval_T *argvars, typval_T *rettv)
2695{
2696 char_u *errmsg = (char_u *)N_("extend() argument");
2697
2698 extend(argvars, rettv, errmsg, FALSE);
2699}
2700
2701/*
2702 * "extendnew(list, list [, idx])" function
2703 * "extendnew(dict, dict [, action])" function
2704 */
2705 void
2706f_extendnew(typval_T *argvars, typval_T *rettv)
2707{
2708 char_u *errmsg = (char_u *)N_("extendnew() argument");
2709
2710 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002711}
2712
2713/*
2714 * "insert()" function
2715 */
2716 void
2717f_insert(typval_T *argvars, typval_T *rettv)
2718{
2719 long before = 0;
2720 listitem_T *item;
2721 list_T *l;
2722 int error = FALSE;
2723
2724 if (argvars[0].v_type == VAR_BLOB)
2725 {
2726 int val, len;
2727 char_u *p;
2728
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002729 if (argvars[0].vval.v_blob == NULL)
2730 return;
2731
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002732 len = blob_len(argvars[0].vval.v_blob);
2733 if (argvars[2].v_type != VAR_UNKNOWN)
2734 {
2735 before = (long)tv_get_number_chk(&argvars[2], &error);
2736 if (error)
2737 return; // type error; errmsg already given
2738 if (before < 0 || before > len)
2739 {
2740 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2741 return;
2742 }
2743 }
2744 val = tv_get_number_chk(&argvars[1], &error);
2745 if (error)
2746 return;
2747 if (val < 0 || val > 255)
2748 {
2749 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2750 return;
2751 }
2752
2753 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
2754 return;
2755 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2756 mch_memmove(p + before + 1, p + before, (size_t)len - before);
2757 *(p + before) = val;
2758 ++argvars[0].vval.v_blob->bv_ga.ga_len;
2759
2760 copy_tv(&argvars[0], rettv);
2761 }
2762 else if (argvars[0].v_type != VAR_LIST)
2763 semsg(_(e_listblobarg), "insert()");
2764 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002765 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002766 (char_u *)N_("insert() argument"), TRUE))
2767 {
2768 if (argvars[2].v_type != VAR_UNKNOWN)
2769 before = (long)tv_get_number_chk(&argvars[2], &error);
2770 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002771 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002772
2773 if (before == l->lv_len)
2774 item = NULL;
2775 else
2776 {
2777 item = list_find(l, before);
2778 if (item == NULL)
2779 {
2780 semsg(_(e_listidx), before);
2781 l = NULL;
2782 }
2783 }
2784 if (l != NULL)
2785 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02002786 (void)list_insert_tv(l, &argvars[1], item);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002787 copy_tv(&argvars[0], rettv);
2788 }
2789 }
2790}
2791
2792/*
2793 * "remove()" function
2794 */
2795 void
2796f_remove(typval_T *argvars, typval_T *rettv)
2797{
2798 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2799
2800 if (argvars[0].v_type == VAR_DICT)
2801 dict_remove(argvars, rettv, arg_errmsg);
2802 else if (argvars[0].v_type == VAR_BLOB)
2803 blob_remove(argvars, rettv);
2804 else if (argvars[0].v_type == VAR_LIST)
2805 list_remove(argvars, rettv, arg_errmsg);
2806 else
2807 semsg(_(e_listdictblobarg), "remove()");
2808}
2809
2810/*
2811 * "reverse({list})" function
2812 */
2813 void
2814f_reverse(typval_T *argvars, typval_T *rettv)
2815{
2816 list_T *l;
2817 listitem_T *li, *ni;
2818
2819 if (argvars[0].v_type == VAR_BLOB)
2820 {
2821 blob_T *b = argvars[0].vval.v_blob;
2822 int i, len = blob_len(b);
2823
2824 for (i = 0; i < len / 2; i++)
2825 {
2826 int tmp = blob_get(b, i);
2827
2828 blob_set(b, i, blob_get(b, len - i - 1));
2829 blob_set(b, len - i - 1, tmp);
2830 }
2831 rettv_blob_set(rettv, b);
2832 return;
2833 }
2834
2835 if (argvars[0].v_type != VAR_LIST)
2836 semsg(_(e_listblobarg), "reverse()");
2837 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002838 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002839 (char_u *)N_("reverse() argument"), TRUE))
2840 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002841 if (l->lv_first == &range_list_item)
2842 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002843 varnumber_T new_start = l->lv_u.nonmat.lv_start
2844 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2845 l->lv_u.nonmat.lv_end = new_start
2846 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2847 l->lv_u.nonmat.lv_start = new_start;
2848 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002849 rettv_list_set(rettv, l);
2850 return;
2851 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002852 li = l->lv_u.mat.lv_last;
2853 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002854 l->lv_len = 0;
2855 while (li != NULL)
2856 {
2857 ni = li->li_prev;
2858 list_append(l, li);
2859 li = ni;
2860 }
2861 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002862 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002863 }
2864}
2865
Bram Moolenaar85629982020-06-01 18:39:20 +02002866/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002867 * "reduce(list, { accumulator, element -> value } [, initial])" function
Bram Moolenaar85629982020-06-01 18:39:20 +02002868 */
2869 void
2870f_reduce(typval_T *argvars, typval_T *rettv)
2871{
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002872 typval_T initial;
Bram Moolenaar85629982020-06-01 18:39:20 +02002873 char_u *func_name;
2874 partial_T *partial = NULL;
2875 funcexe_T funcexe;
2876 typval_T argv[3];
2877
2878 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
2879 {
2880 emsg(_(e_listblobreq));
2881 return;
2882 }
2883
2884 if (argvars[1].v_type == VAR_FUNC)
2885 func_name = argvars[1].vval.v_string;
2886 else if (argvars[1].v_type == VAR_PARTIAL)
2887 {
2888 partial = argvars[1].vval.v_partial;
2889 func_name = partial_name(partial);
2890 }
2891 else
2892 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01002893 if (func_name == NULL || *func_name == NUL)
2894 {
2895 emsg(_(e_missing_function_argument));
2896 return;
2897 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002898
2899 vim_memset(&funcexe, 0, sizeof(funcexe));
2900 funcexe.evaluate = TRUE;
2901 funcexe.partial = partial;
2902
2903 if (argvars[0].v_type == VAR_LIST)
2904 {
2905 list_T *l = argvars[0].vval.v_list;
2906 listitem_T *li = NULL;
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002907 int r;
Bram Moolenaarca275a02020-06-24 22:07:46 +02002908 int called_emsg_start = called_emsg;
Bram Moolenaar85629982020-06-01 18:39:20 +02002909
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002910 if (l != NULL)
2911 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar85629982020-06-01 18:39:20 +02002912 if (argvars[2].v_type == VAR_UNKNOWN)
2913 {
2914 if (l == NULL || l->lv_first == NULL)
2915 {
2916 semsg(_(e_reduceempty), "List");
2917 return;
2918 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002919 initial = l->lv_first->li_tv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002920 li = l->lv_first->li_next;
2921 }
2922 else
2923 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002924 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002925 if (l != NULL)
2926 li = l->lv_first;
2927 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002928 copy_tv(&initial, rettv);
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002929
2930 if (l != NULL)
Bram Moolenaar85629982020-06-01 18:39:20 +02002931 {
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002932 int prev_locked = l->lv_lock;
2933
2934 l->lv_lock = VAR_FIXED; // disallow the list changing here
2935 for ( ; li != NULL; li = li->li_next)
2936 {
2937 argv[0] = *rettv;
2938 argv[1] = li->li_tv;
2939 rettv->v_type = VAR_UNKNOWN;
2940 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
2941 clear_tv(&argv[0]);
2942 if (r == FAIL || called_emsg != called_emsg_start)
2943 break;
2944 }
2945 l->lv_lock = prev_locked;
Bram Moolenaar85629982020-06-01 18:39:20 +02002946 }
2947 }
2948 else
2949 {
2950 blob_T *b = argvars[0].vval.v_blob;
2951 int i;
2952
2953 if (argvars[2].v_type == VAR_UNKNOWN)
2954 {
2955 if (b == NULL || b->bv_ga.ga_len == 0)
2956 {
2957 semsg(_(e_reduceempty), "Blob");
2958 return;
2959 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002960 initial.v_type = VAR_NUMBER;
2961 initial.vval.v_number = blob_get(b, 0);
Bram Moolenaar85629982020-06-01 18:39:20 +02002962 i = 1;
2963 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002964 else if (argvars[2].v_type != VAR_NUMBER)
2965 {
2966 emsg(_(e_number_exp));
2967 return;
2968 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002969 else
2970 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002971 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002972 i = 0;
2973 }
2974
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002975 copy_tv(&initial, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02002976 if (b != NULL)
2977 {
2978 for ( ; i < b->bv_ga.ga_len; i++)
2979 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002980 argv[0] = *rettv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002981 argv[1].v_type = VAR_NUMBER;
2982 argv[1].vval.v_number = blob_get(b, i);
2983 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
2984 return;
Bram Moolenaar85629982020-06-01 18:39:20 +02002985 }
2986 }
2987 }
2988}
2989
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002990#endif // defined(FEAT_EVAL)