blob: 4b9236a2908e114e663e013907e1b756064a2fa6 [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 Moolenaarf32f0992021-07-08 20:53:40 +0200600 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200601 */
602 int
603list_append_tv(list_T *l, typval_T *tv)
604{
Bram Moolenaar90fba562021-07-09 19:17:55 +0200605 listitem_T *li;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200606
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200607 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
608 && check_typval_arg_type(l->lv_type->tt_member, tv, 0) == FAIL)
609 return FAIL;
Bram Moolenaar90fba562021-07-09 19:17:55 +0200610 li = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200611 if (li == NULL)
612 return FAIL;
613 copy_tv(tv, &li->li_tv);
614 list_append(l, li);
615 return OK;
616}
617
618/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619 * As list_append_tv() but move the value instead of copying it.
620 * Return FAIL when out of memory.
621 */
622 int
623list_append_tv_move(list_T *l, typval_T *tv)
624{
625 listitem_T *li = listitem_alloc();
626
627 if (li == NULL)
628 return FAIL;
629 li->li_tv = *tv;
630 list_append(l, li);
631 return OK;
632}
633
634/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200635 * Add a dictionary to a list. Used by getqflist().
636 * Return FAIL when out of memory.
637 */
638 int
639list_append_dict(list_T *list, dict_T *dict)
640{
641 listitem_T *li = listitem_alloc();
642
643 if (li == NULL)
644 return FAIL;
645 li->li_tv.v_type = VAR_DICT;
646 li->li_tv.v_lock = 0;
647 li->li_tv.vval.v_dict = dict;
648 list_append(list, li);
649 ++dict->dv_refcount;
650 return OK;
651}
652
653/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100654 * Append list2 to list1.
655 * Return FAIL when out of memory.
656 */
657 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200658list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100659{
660 listitem_T *li = listitem_alloc();
661
662 if (li == NULL)
663 return FAIL;
664 li->li_tv.v_type = VAR_LIST;
665 li->li_tv.v_lock = 0;
666 li->li_tv.vval.v_list = list2;
667 list_append(list1, li);
668 ++list2->lv_refcount;
669 return OK;
670}
671
672/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200673 * Make a copy of "str" and append it as an item to list "l".
674 * When "len" >= 0 use "str[len]".
675 * Returns FAIL when out of memory.
676 */
677 int
678list_append_string(list_T *l, char_u *str, int len)
679{
680 listitem_T *li = listitem_alloc();
681
682 if (li == NULL)
683 return FAIL;
684 list_append(l, li);
685 li->li_tv.v_type = VAR_STRING;
686 li->li_tv.v_lock = 0;
687 if (str == NULL)
688 li->li_tv.vval.v_string = NULL;
689 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
690 : vim_strsave(str))) == NULL)
691 return FAIL;
692 return OK;
693}
694
695/*
696 * Append "n" to list "l".
697 * Returns FAIL when out of memory.
698 */
699 int
700list_append_number(list_T *l, varnumber_T n)
701{
702 listitem_T *li;
703
704 li = listitem_alloc();
705 if (li == NULL)
706 return FAIL;
707 li->li_tv.v_type = VAR_NUMBER;
708 li->li_tv.v_lock = 0;
709 li->li_tv.vval.v_number = n;
710 list_append(l, li);
711 return OK;
712}
713
714/*
715 * Insert typval_T "tv" in list "l" before "item".
716 * If "item" is NULL append at the end.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100717 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200718 */
719 int
720list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
721{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100722 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200723
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100724 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100725 && check_typval_arg_type(l->lv_type->tt_member, tv, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100726 return FAIL;
727 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200728 if (ni == NULL)
729 return FAIL;
730 copy_tv(tv, &ni->li_tv);
731 list_insert(l, ni, item);
732 return OK;
733}
734
735 void
736list_insert(list_T *l, listitem_T *ni, listitem_T *item)
737{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200738 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200739 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100740 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200741 list_append(l, ni);
742 else
743 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100744 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200745 ni->li_prev = item->li_prev;
746 ni->li_next = item;
747 if (item->li_prev == NULL)
748 {
749 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100750 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200751 }
752 else
753 {
754 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100755 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200756 }
757 item->li_prev = ni;
758 ++l->lv_len;
759 }
760}
761
762/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200763 * Flatten "list" to depth "maxdepth".
764 * It does nothing if "maxdepth" is 0.
765 * Returns FAIL when out of memory.
766 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100767 static void
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200768list_flatten(list_T *list, long maxdepth)
769{
770 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200771 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200772 int n;
773
774 if (maxdepth == 0)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100775 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200776 CHECK_LIST_MATERIALIZE(list);
777
778 n = 0;
779 item = list->lv_first;
780 while (item != NULL)
781 {
782 fast_breakcheck();
783 if (got_int)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100784 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200785
786 if (item->li_tv.v_type == VAR_LIST)
787 {
788 listitem_T *next = item->li_next;
789
790 vimlist_remove(list, item, item);
791 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100792 return;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200793 clear_tv(&item->li_tv);
794 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200795
796 if (item->li_prev == NULL)
797 item = list->lv_first;
798 else
799 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200800 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200801
802 if (++n >= maxdepth)
803 {
804 n = 0;
805 item = next;
806 }
807 }
808 else
809 {
810 n = 0;
811 item = item->li_next;
812 }
813 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200814}
815
816/*
Bram Moolenaar3b690062021-02-01 20:14:51 +0100817 * "flatten()" and "flattennew()" functions
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200818 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100819 static void
820flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200821{
822 list_T *l;
823 long maxdepth;
824 int error = FALSE;
825
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200826 if (in_vim9script()
827 && (check_for_list_arg(argvars, 0) == FAIL
828 || check_for_opt_number_arg(argvars, 1) == FAIL))
829 return;
830
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200831 if (argvars[0].v_type != VAR_LIST)
832 {
833 semsg(_(e_listarg), "flatten()");
834 return;
835 }
836
837 if (argvars[1].v_type == VAR_UNKNOWN)
838 maxdepth = 999999;
839 else
840 {
841 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
842 if (error)
843 return;
844 if (maxdepth < 0)
845 {
846 emsg(_("E900: maxdepth must be non-negative number"));
847 return;
848 }
849 }
850
851 l = argvars[0].vval.v_list;
Bram Moolenaar3b690062021-02-01 20:14:51 +0100852 rettv->v_type = VAR_LIST;
853 rettv->vval.v_list = l;
854 if (l == NULL)
855 return;
856
857 if (make_copy)
858 {
859 l = list_copy(l, TRUE, get_copyID());
860 rettv->vval.v_list = l;
861 if (l == NULL)
862 return;
863 }
864 else
865 {
866 if (value_check_lock(l->lv_lock,
867 (char_u *)N_("flatten() argument"), TRUE))
868 return;
869 ++l->lv_refcount;
870 }
871
872 list_flatten(l, maxdepth);
873}
874
875/*
876 * "flatten(list[, {maxdepth}])" function
877 */
878 void
879f_flatten(typval_T *argvars, typval_T *rettv)
880{
881 if (in_vim9script())
882 emsg(_(e_cannot_use_flatten_in_vim9_script));
883 else
884 flatten_common(argvars, rettv, FALSE);
885}
886
887/*
888 * "flattennew(list[, {maxdepth}])" function
889 */
890 void
891f_flattennew(typval_T *argvars, typval_T *rettv)
892{
893 flatten_common(argvars, rettv, TRUE);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200894}
895
896/*
Bram Moolenaar1a739232020-10-10 15:37:58 +0200897 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200898 * If "bef" is NULL append at the end, otherwise insert before this item.
899 * Returns FAIL when out of memory.
900 */
901 int
902list_extend(list_T *l1, list_T *l2, listitem_T *bef)
903{
904 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200905 int todo;
Bram Moolenaardcae51f2021-04-08 20:10:10 +0200906 listitem_T *bef_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200907
Bram Moolenaar1a739232020-10-10 15:37:58 +0200908 // NULL list is equivalent to an empty list: nothing to do.
909 if (l2 == NULL || l2->lv_len == 0)
910 return OK;
911
912 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200913 CHECK_LIST_MATERIALIZE(l1);
914 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915
Bram Moolenaardcae51f2021-04-08 20:10:10 +0200916 // When exending a list with itself, at some point we run into the item
917 // that was before "bef" and need to skip over the already inserted items
918 // to "bef".
919 bef_prev = bef == NULL ? NULL : bef->li_prev;
920
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100921 // We also quit the loop when we have inserted the original item count of
922 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaardcae51f2021-04-08 20:10:10 +0200923 for (item = l2->lv_first; item != NULL && --todo >= 0;
924 item = item == bef_prev ? bef : item->li_next)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200925 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
926 return FAIL;
927 return OK;
928}
929
930/*
931 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
932 * Return FAIL when out of memory.
933 */
934 int
935list_concat(list_T *l1, list_T *l2, typval_T *tv)
936{
937 list_T *l;
938
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100939 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +0200940 if (l1 == NULL)
941 l = list_alloc();
942 else
943 l = list_copy(l1, FALSE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200944 if (l == NULL)
945 return FAIL;
946 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +0100947 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200948 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200949 if (l1 == NULL)
950 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200951
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100952 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200953 return list_extend(l, l2, NULL);
954}
955
Bram Moolenaar9af78762020-06-16 11:34:42 +0200956 list_T *
957list_slice(list_T *ol, long n1, long n2)
958{
959 listitem_T *item;
960 list_T *l = list_alloc();
961
962 if (l == NULL)
963 return NULL;
964 for (item = list_find(ol, n1); n1 <= n2; ++n1)
965 {
966 if (list_append_tv(l, &item->li_tv) == FAIL)
967 {
968 list_free(l);
969 return NULL;
970 }
971 item = item->li_next;
972 }
973 return l;
974}
975
Bram Moolenaared591872020-08-15 22:14:53 +0200976 int
977list_slice_or_index(
978 list_T *list,
979 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +0100980 varnumber_T n1_arg,
981 varnumber_T n2_arg,
982 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +0200983 typval_T *rettv,
984 int verbose)
985{
986 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +0100987 varnumber_T n1 = n1_arg;
988 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +0200989 typval_T var1;
990
991 if (n1 < 0)
992 n1 = len + n1;
993 if (n1 < 0 || n1 >= len)
994 {
995 // For a range we allow invalid values and return an empty
996 // list. A list index out of range is an error.
997 if (!range)
998 {
999 if (verbose)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001000 semsg(_(e_listidx), (long)n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +02001001 return FAIL;
1002 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001003 n1 = n1 < 0 ? 0 : len;
Bram Moolenaared591872020-08-15 22:14:53 +02001004 }
1005 if (range)
1006 {
1007 list_T *l;
1008
1009 if (n2 < 0)
1010 n2 = len + n2;
1011 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01001012 n2 = len - (exclusive ? 0 : 1);
1013 if (exclusive)
1014 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +02001015 if (n2 < 0 || n2 + 1 < n1)
1016 n2 = -1;
1017 l = list_slice(list, n1, n2);
1018 if (l == NULL)
1019 return FAIL;
1020 clear_tv(rettv);
1021 rettv_list_set(rettv, l);
1022 }
1023 else
1024 {
1025 // copy the item to "var1" to avoid that freeing the list makes it
1026 // invalid.
1027 copy_tv(&list_find(list, n1)->li_tv, &var1);
1028 clear_tv(rettv);
1029 *rettv = var1;
1030 }
1031 return OK;
1032}
1033
Bram Moolenaarda861d62016-07-17 15:46:27 +02001034/*
1035 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1036 * The refcount of the new list is set to 1.
1037 * See item_copy() for "copyID".
1038 * Returns NULL when out of memory.
1039 */
1040 list_T *
1041list_copy(list_T *orig, int deep, int copyID)
1042{
1043 list_T *copy;
1044 listitem_T *item;
1045 listitem_T *ni;
1046
1047 if (orig == NULL)
1048 return NULL;
1049
1050 copy = list_alloc();
1051 if (copy != NULL)
1052 {
1053 if (copyID != 0)
1054 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001055 // Do this before adding the items, because one of the items may
1056 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001057 orig->lv_copyID = copyID;
1058 orig->lv_copylist = copy;
1059 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001060 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001061 for (item = orig->lv_first; item != NULL && !got_int;
1062 item = item->li_next)
1063 {
1064 ni = listitem_alloc();
1065 if (ni == NULL)
1066 break;
1067 if (deep)
1068 {
1069 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
1070 {
1071 vim_free(ni);
1072 break;
1073 }
1074 }
1075 else
1076 copy_tv(&item->li_tv, &ni->li_tv);
1077 list_append(copy, ni);
1078 }
1079 ++copy->lv_refcount;
1080 if (item != NULL)
1081 {
1082 list_unref(copy);
1083 copy = NULL;
1084 }
1085 }
1086
1087 return copy;
1088}
1089
1090/*
1091 * Remove items "item" to "item2" from list "l".
1092 * Does not free the listitem or the value!
1093 * This used to be called list_remove, but that conflicts with a Sun header
1094 * file.
1095 */
1096 void
1097vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1098{
1099 listitem_T *ip;
1100
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001101 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001103 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001104 for (ip = item; ip != NULL; ip = ip->li_next)
1105 {
1106 --l->lv_len;
1107 list_fix_watch(l, ip);
1108 if (ip == item2)
1109 break;
1110 }
1111
1112 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001113 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001114 else
1115 item2->li_next->li_prev = item->li_prev;
1116 if (item->li_prev == NULL)
1117 l->lv_first = item2->li_next;
1118 else
1119 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001120 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001121}
1122
1123/*
1124 * Return an allocated string with the string representation of a list.
1125 * May return NULL.
1126 */
1127 char_u *
1128list2string(typval_T *tv, int copyID, int restore_copyID)
1129{
1130 garray_T ga;
1131
1132 if (tv->vval.v_list == NULL)
1133 return NULL;
1134 ga_init2(&ga, (int)sizeof(char), 80);
1135 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001136 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001137 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1138 FALSE, restore_copyID, copyID) == FAIL)
1139 {
1140 vim_free(ga.ga_data);
1141 return NULL;
1142 }
1143 ga_append(&ga, ']');
1144 ga_append(&ga, NUL);
1145 return (char_u *)ga.ga_data;
1146}
1147
1148typedef struct join_S {
1149 char_u *s;
1150 char_u *tofree;
1151} join_T;
1152
1153 static int
1154list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001155 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001156 list_T *l,
1157 char_u *sep,
1158 int echo_style,
1159 int restore_copyID,
1160 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001161 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001162{
1163 int i;
1164 join_T *p;
1165 int len;
1166 int sumlen = 0;
1167 int first = TRUE;
1168 char_u *tofree;
1169 char_u numbuf[NUMBUFLEN];
1170 listitem_T *item;
1171 char_u *s;
1172
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001173 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001174 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001175 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1176 {
1177 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001178 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001179 if (s == NULL)
1180 return FAIL;
1181
1182 len = (int)STRLEN(s);
1183 sumlen += len;
1184
1185 (void)ga_grow(join_gap, 1);
1186 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1187 if (tofree != NULL || s != numbuf)
1188 {
1189 p->s = s;
1190 p->tofree = tofree;
1191 }
1192 else
1193 {
1194 p->s = vim_strnsave(s, len);
1195 p->tofree = p->s;
1196 }
1197
1198 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001199 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001200 break;
1201 }
1202
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001203 // Allocate result buffer with its total size, avoid re-allocation and
1204 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001205 if (join_gap->ga_len >= 2)
1206 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1207 if (ga_grow(gap, sumlen + 2) == FAIL)
1208 return FAIL;
1209
1210 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1211 {
1212 if (first)
1213 first = FALSE;
1214 else
1215 ga_concat(gap, sep);
1216 p = ((join_T *)join_gap->ga_data) + i;
1217
1218 if (p->s != NULL)
1219 ga_concat(gap, p->s);
1220 line_breakcheck();
1221 }
1222
1223 return OK;
1224}
1225
1226/*
1227 * Join list "l" into a string in "*gap", using separator "sep".
1228 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1229 * Return FAIL or OK.
1230 */
1231 int
1232list_join(
1233 garray_T *gap,
1234 list_T *l,
1235 char_u *sep,
1236 int echo_style,
1237 int restore_copyID,
1238 int copyID)
1239{
1240 garray_T join_ga;
1241 int retval;
1242 join_T *p;
1243 int i;
1244
1245 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001246 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001247 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1248 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1249 copyID, &join_ga);
1250
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001251 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001252 if (join_ga.ga_data != NULL)
1253 {
1254 p = (join_T *)join_ga.ga_data;
1255 for (i = 0; i < join_ga.ga_len; ++i)
1256 {
1257 vim_free(p->tofree);
1258 ++p;
1259 }
1260 ga_clear(&join_ga);
1261 }
1262
1263 return retval;
1264}
1265
1266/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001267 * "join()" function
1268 */
1269 void
1270f_join(typval_T *argvars, typval_T *rettv)
1271{
1272 garray_T ga;
1273 char_u *sep;
1274
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001275 if (in_vim9script()
1276 && (check_for_list_arg(argvars, 0) == FAIL
1277 || check_for_opt_string_arg(argvars, 1) == FAIL))
1278 return;
1279
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001280 if (argvars[0].v_type != VAR_LIST)
1281 {
1282 emsg(_(e_listreq));
1283 return;
1284 }
1285 if (argvars[0].vval.v_list == NULL)
1286 return;
1287 if (argvars[1].v_type == VAR_UNKNOWN)
1288 sep = (char_u *)" ";
1289 else
1290 sep = tv_get_string_chk(&argvars[1]);
1291
1292 rettv->v_type = VAR_STRING;
1293
1294 if (sep != NULL)
1295 {
1296 ga_init2(&ga, (int)sizeof(char), 80);
1297 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1298 ga_append(&ga, NUL);
1299 rettv->vval.v_string = (char_u *)ga.ga_data;
1300 }
1301 else
1302 rettv->vval.v_string = NULL;
1303}
1304
1305/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001306 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001307 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001308 * Return OK or FAIL.
1309 */
1310 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001311eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001312{
Bram Moolenaar71478202020-06-26 22:46:27 +02001313 int evaluate = evalarg == NULL ? FALSE
1314 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001315 list_T *l = NULL;
1316 typval_T tv;
1317 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001318 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001319 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001320
1321 if (evaluate)
1322 {
1323 l = list_alloc();
1324 if (l == NULL)
1325 return FAIL;
1326 }
1327
Bram Moolenaar962d7212020-07-04 14:15:00 +02001328 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001329 while (**arg != ']' && **arg != NUL)
1330 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001331 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001332 goto failret;
1333 if (evaluate)
1334 {
1335 item = listitem_alloc();
1336 if (item != NULL)
1337 {
1338 item->li_tv = tv;
1339 item->li_tv.v_lock = 0;
1340 list_append(l, item);
1341 }
1342 else
1343 clear_tv(&tv);
1344 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001345 // Legacy Vim script allowed a space before the comma.
1346 if (!vim9script)
1347 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001348
Bram Moolenaare6e03172020-06-27 16:36:05 +02001349 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001350 had_comma = **arg == ',';
1351 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001352 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001353 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001354 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001355 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001356 goto failret;
1357 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001358 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001359 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001360
Bram Moolenaare6b53242020-07-01 17:28:33 +02001361 // The "]" can be on the next line. But a double quoted string may
1362 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001363 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001364 if (**arg == ']')
1365 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001366
1367 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001368 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001370 {
1371 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001372 semsg(_(e_no_white_space_allowed_before_str_str),
1373 ",", *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001374 else
1375 semsg(_("E696: Missing comma in List: %s"), *arg);
1376 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001377 goto failret;
1378 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001379 }
1380
1381 if (**arg != ']')
1382 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001384 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001385failret:
1386 if (evaluate)
1387 list_free(l);
1388 return FAIL;
1389 }
1390
Bram Moolenaar9d489562020-07-30 20:08:50 +02001391 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001392 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001393 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001394
1395 return OK;
1396}
1397
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001398/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001399 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001400 */
1401 int
1402write_list(FILE *fd, list_T *list, int binary)
1403{
1404 listitem_T *li;
1405 int c;
1406 int ret = OK;
1407 char_u *s;
1408
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001409 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001410 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001411 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001412 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001413 {
1414 if (*s == '\n')
1415 c = putc(NUL, fd);
1416 else
1417 c = putc(*s, fd);
1418 if (c == EOF)
1419 {
1420 ret = FAIL;
1421 break;
1422 }
1423 }
1424 if (!binary || li->li_next != NULL)
1425 if (putc('\n', fd) == EOF)
1426 {
1427 ret = FAIL;
1428 break;
1429 }
1430 if (ret == FAIL)
1431 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001432 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001433 break;
1434 }
1435 }
1436 return ret;
1437}
1438
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001439/*
1440 * Initialize a static list with 10 items.
1441 */
1442 void
1443init_static_list(staticList10_T *sl)
1444{
1445 list_T *l = &sl->sl_list;
1446 int i;
1447
1448 memset(sl, 0, sizeof(staticList10_T));
1449 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001450 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001451 l->lv_refcount = DO_NOT_FREE_CNT;
1452 l->lv_lock = VAR_FIXED;
1453 sl->sl_list.lv_len = 10;
1454
1455 for (i = 0; i < 10; ++i)
1456 {
1457 listitem_T *li = &sl->sl_items[i];
1458
1459 if (i == 0)
1460 li->li_prev = NULL;
1461 else
1462 li->li_prev = li - 1;
1463 if (i == 9)
1464 li->li_next = NULL;
1465 else
1466 li->li_next = li + 1;
1467 }
1468}
1469
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001470/*
1471 * "list2str()" function
1472 */
1473 void
1474f_list2str(typval_T *argvars, typval_T *rettv)
1475{
1476 list_T *l;
1477 listitem_T *li;
1478 garray_T ga;
1479 int utf8 = FALSE;
1480
1481 rettv->v_type = VAR_STRING;
1482 rettv->vval.v_string = NULL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001483
1484 if (in_vim9script()
1485 && (check_for_list_arg(argvars, 0) == FAIL
1486 || check_for_opt_bool_arg(argvars, 1) == FAIL))
1487 return;
1488
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001489 if (argvars[0].v_type != VAR_LIST)
1490 {
1491 emsg(_(e_invarg));
1492 return;
1493 }
1494
1495 l = argvars[0].vval.v_list;
1496 if (l == NULL)
1497 return; // empty list results in empty string
1498
1499 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001500 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001501
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001502 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001503 ga_init2(&ga, 1, 80);
1504 if (has_mbyte || utf8)
1505 {
1506 char_u buf[MB_MAXBYTES + 1];
1507 int (*char2bytes)(int, char_u *);
1508
1509 if (utf8 || enc_utf8)
1510 char2bytes = utf_char2bytes;
1511 else
1512 char2bytes = mb_char2bytes;
1513
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001514 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001515 {
1516 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1517 ga_concat(&ga, buf);
1518 }
1519 ga_append(&ga, NUL);
1520 }
1521 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1522 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001523 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001524 ga_append(&ga, tv_get_number(&li->li_tv));
1525 ga_append(&ga, NUL);
1526 }
1527
1528 rettv->v_type = VAR_STRING;
1529 rettv->vval.v_string = ga.ga_data;
1530}
1531
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001532 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001533list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1534{
1535 list_T *l;
1536 listitem_T *item, *item2;
1537 listitem_T *li;
1538 int error = FALSE;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001539 long idx;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001540
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001541 if (in_vim9script()
1542 && (check_for_list_arg(argvars, 0) == FAIL
1543 || check_for_number_arg(argvars, 1) == FAIL
1544 || check_for_opt_number_arg(argvars, 2) == FAIL))
1545 return;
1546
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001547 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001548 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001549 return;
1550
1551 idx = (long)tv_get_number_chk(&argvars[1], &error);
1552 if (error)
1553 ; // type error: do nothing, errmsg already given
1554 else if ((item = list_find(l, idx)) == NULL)
1555 semsg(_(e_listidx), idx);
1556 else
1557 {
1558 if (argvars[2].v_type == VAR_UNKNOWN)
1559 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001560 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001561 vimlist_remove(l, item, item);
1562 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001564 }
1565 else
1566 {
1567 // Remove range of items, return list with values.
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001568 long end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001569
1570 if (error)
1571 ; // type error: do nothing
1572 else if ((item2 = list_find(l, end)) == NULL)
1573 semsg(_(e_listidx), end);
1574 else
1575 {
1576 int cnt = 0;
1577
1578 for (li = item; li != NULL; li = li->li_next)
1579 {
1580 ++cnt;
1581 if (li == item2)
1582 break;
1583 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001584 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar108010a2021-06-27 22:03:33 +02001585 emsg(_(e_invalid_range));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001586 else
1587 {
1588 vimlist_remove(l, item, item2);
1589 if (rettv_list_alloc(rettv) == OK)
1590 {
Bram Moolenaar885971e2021-07-18 22:25:29 +02001591 list_T *rl = rettv->vval.v_list;
1592
1593 if (l->lv_with_items > 0)
1594 {
1595 // need to copy the list items and move the value
1596 while (item != NULL)
1597 {
1598 li = listitem_alloc();
1599 if (li == NULL)
1600 return;
1601 li->li_tv = item->li_tv;
1602 init_tv(&item->li_tv);
1603 list_append(rl, li);
1604 if (item == item2)
1605 break;
1606 item = item->li_next;
1607 }
1608 }
1609 else
1610 {
1611 rl->lv_first = item;
1612 rl->lv_u.mat.lv_last = item2;
1613 item->li_prev = NULL;
1614 item2->li_next = NULL;
1615 rl->lv_len = cnt;
1616 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001617 }
1618 }
1619 }
1620 }
1621 }
1622}
1623
1624static int item_compare(const void *s1, const void *s2);
1625static int item_compare2(const void *s1, const void *s2);
1626
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001627// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001628typedef struct
1629{
1630 listitem_T *item;
1631 int idx;
1632} sortItem_T;
1633
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001634// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001635typedef struct
1636{
1637 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001638 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001639 int item_compare_numeric;
1640 int item_compare_numbers;
1641#ifdef FEAT_FLOAT
1642 int item_compare_float;
1643#endif
1644 char_u *item_compare_func;
1645 partial_T *item_compare_partial;
1646 dict_T *item_compare_selfdict;
1647 int item_compare_func_err;
1648 int item_compare_keep_zero;
1649} sortinfo_T;
1650static sortinfo_T *sortinfo = NULL;
1651#define ITEM_COMPARE_FAIL 999
1652
1653/*
1654 * Compare functions for f_sort() and f_uniq() below.
1655 */
1656 static int
1657item_compare(const void *s1, const void *s2)
1658{
1659 sortItem_T *si1, *si2;
1660 typval_T *tv1, *tv2;
1661 char_u *p1, *p2;
1662 char_u *tofree1 = NULL, *tofree2 = NULL;
1663 int res;
1664 char_u numbuf1[NUMBUFLEN];
1665 char_u numbuf2[NUMBUFLEN];
1666
1667 si1 = (sortItem_T *)s1;
1668 si2 = (sortItem_T *)s2;
1669 tv1 = &si1->item->li_tv;
1670 tv2 = &si2->item->li_tv;
1671
1672 if (sortinfo->item_compare_numbers)
1673 {
1674 varnumber_T v1 = tv_get_number(tv1);
1675 varnumber_T v2 = tv_get_number(tv2);
1676
1677 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1678 }
1679
1680#ifdef FEAT_FLOAT
1681 if (sortinfo->item_compare_float)
1682 {
1683 float_T v1 = tv_get_float(tv1);
1684 float_T v2 = tv_get_float(tv2);
1685
1686 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1687 }
1688#endif
1689
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001690 // tv2string() puts quotes around a string and allocates memory. Don't do
1691 // that for string variables. Use a single quote when comparing with a
1692 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001693 if (tv1->v_type == VAR_STRING)
1694 {
1695 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1696 p1 = (char_u *)"'";
1697 else
1698 p1 = tv1->vval.v_string;
1699 }
1700 else
1701 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1702 if (tv2->v_type == VAR_STRING)
1703 {
1704 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1705 p2 = (char_u *)"'";
1706 else
1707 p2 = tv2->vval.v_string;
1708 }
1709 else
1710 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1711 if (p1 == NULL)
1712 p1 = (char_u *)"";
1713 if (p2 == NULL)
1714 p2 = (char_u *)"";
1715 if (!sortinfo->item_compare_numeric)
1716 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001717 if (sortinfo->item_compare_lc)
1718 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001719 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001720 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001721 }
1722 else
1723 {
1724 double n1, n2;
1725 n1 = strtod((char *)p1, (char **)&p1);
1726 n2 = strtod((char *)p2, (char **)&p2);
1727 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1728 }
1729
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001730 // When the result would be zero, compare the item indexes. Makes the
1731 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001732 if (res == 0 && !sortinfo->item_compare_keep_zero)
1733 res = si1->idx > si2->idx ? 1 : -1;
1734
1735 vim_free(tofree1);
1736 vim_free(tofree2);
1737 return res;
1738}
1739
1740 static int
1741item_compare2(const void *s1, const void *s2)
1742{
1743 sortItem_T *si1, *si2;
1744 int res;
1745 typval_T rettv;
1746 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001747 char_u *func_name;
1748 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001749 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001750
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001751 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001752 if (sortinfo->item_compare_func_err)
1753 return 0;
1754
1755 si1 = (sortItem_T *)s1;
1756 si2 = (sortItem_T *)s2;
1757
1758 if (partial == NULL)
1759 func_name = sortinfo->item_compare_func;
1760 else
1761 func_name = partial_name(partial);
1762
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001763 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1764 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001765 copy_tv(&si1->item->li_tv, &argv[0]);
1766 copy_tv(&si2->item->li_tv, &argv[1]);
1767
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001768 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001769 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001770 funcexe.evaluate = TRUE;
1771 funcexe.partial = partial;
1772 funcexe.selfdict = sortinfo->item_compare_selfdict;
1773 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001774 clear_tv(&argv[0]);
1775 clear_tv(&argv[1]);
1776
1777 if (res == FAIL)
1778 res = ITEM_COMPARE_FAIL;
1779 else
1780 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1781 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001782 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001783 clear_tv(&rettv);
1784
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001785 // When the result would be zero, compare the pointers themselves. Makes
1786 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001787 if (res == 0 && !sortinfo->item_compare_keep_zero)
1788 res = si1->idx > si2->idx ? 1 : -1;
1789
1790 return res;
1791}
1792
1793/*
1794 * "sort()" or "uniq()" function
1795 */
1796 static void
1797do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1798{
1799 list_T *l;
1800 listitem_T *li;
1801 sortItem_T *ptrs;
1802 sortinfo_T *old_sortinfo;
1803 sortinfo_T info;
1804 long len;
1805 long i;
1806
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001807 // Pointer to current info struct used in compare function. Save and
1808 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001809 old_sortinfo = sortinfo;
1810 sortinfo = &info;
1811
1812 if (argvars[0].v_type != VAR_LIST)
1813 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1814 else
1815 {
1816 l = argvars[0].vval.v_list;
Bram Moolenaara187c432020-09-16 21:08:28 +02001817 if (l == NULL || value_check_lock(l->lv_lock,
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001818 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1819 TRUE))
1820 goto theend;
1821 rettv_list_set(rettv, l);
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001822 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001823
1824 len = list_len(l);
1825 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001826 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001827
1828 info.item_compare_ic = FALSE;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001829 info.item_compare_lc = FALSE;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001830 info.item_compare_numeric = FALSE;
1831 info.item_compare_numbers = FALSE;
1832#ifdef FEAT_FLOAT
1833 info.item_compare_float = FALSE;
1834#endif
1835 info.item_compare_func = NULL;
1836 info.item_compare_partial = NULL;
1837 info.item_compare_selfdict = NULL;
1838 if (argvars[1].v_type != VAR_UNKNOWN)
1839 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001840 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001841 if (argvars[1].v_type == VAR_FUNC)
1842 info.item_compare_func = argvars[1].vval.v_string;
1843 else if (argvars[1].v_type == VAR_PARTIAL)
1844 info.item_compare_partial = argvars[1].vval.v_partial;
1845 else
1846 {
1847 int error = FALSE;
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001848 int nr = 0;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001849
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001850 if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001851 {
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001852 nr = tv_get_number_chk(&argvars[1], &error);
1853 if (error)
1854 goto theend; // type error; errmsg already given
1855 if (nr == 1)
1856 info.item_compare_ic = TRUE;
1857 }
1858 if (nr != 1)
1859 {
1860 if (argvars[1].v_type != VAR_NUMBER)
1861 info.item_compare_func = tv_get_string(&argvars[1]);
1862 else if (nr != 0)
1863 {
1864 emsg(_(e_invarg));
1865 goto theend;
1866 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001867 }
1868 if (info.item_compare_func != NULL)
1869 {
1870 if (*info.item_compare_func == NUL)
1871 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001872 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001873 info.item_compare_func = NULL;
1874 }
1875 else if (STRCMP(info.item_compare_func, "n") == 0)
1876 {
1877 info.item_compare_func = NULL;
1878 info.item_compare_numeric = TRUE;
1879 }
1880 else if (STRCMP(info.item_compare_func, "N") == 0)
1881 {
1882 info.item_compare_func = NULL;
1883 info.item_compare_numbers = TRUE;
1884 }
1885#ifdef FEAT_FLOAT
1886 else if (STRCMP(info.item_compare_func, "f") == 0)
1887 {
1888 info.item_compare_func = NULL;
1889 info.item_compare_float = TRUE;
1890 }
1891#endif
1892 else if (STRCMP(info.item_compare_func, "i") == 0)
1893 {
1894 info.item_compare_func = NULL;
1895 info.item_compare_ic = TRUE;
1896 }
Bram Moolenaar55e29612020-11-01 13:57:44 +01001897 else if (STRCMP(info.item_compare_func, "l") == 0)
1898 {
1899 info.item_compare_func = NULL;
1900 info.item_compare_lc = TRUE;
1901 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001902 }
1903 }
1904
1905 if (argvars[2].v_type != VAR_UNKNOWN)
1906 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001907 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001908 if (argvars[2].v_type != VAR_DICT)
1909 {
1910 emsg(_(e_dictreq));
1911 goto theend;
1912 }
1913 info.item_compare_selfdict = argvars[2].vval.v_dict;
1914 }
1915 }
1916
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001917 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001918 ptrs = ALLOC_MULT(sortItem_T, len);
1919 if (ptrs == NULL)
1920 goto theend;
1921
1922 i = 0;
1923 if (sort)
1924 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001925 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001926 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001927 {
1928 ptrs[i].item = li;
1929 ptrs[i].idx = i;
1930 ++i;
1931 }
1932
1933 info.item_compare_func_err = FALSE;
1934 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001935 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001936 if ((info.item_compare_func != NULL
1937 || info.item_compare_partial != NULL)
1938 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
1939 == ITEM_COMPARE_FAIL)
1940 emsg(_("E702: Sort compare function failed"));
1941 else
1942 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001943 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001944 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
1945 info.item_compare_func == NULL
1946 && info.item_compare_partial == NULL
1947 ? item_compare : item_compare2);
1948
1949 if (!info.item_compare_func_err)
1950 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001951 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001952 l->lv_first = l->lv_u.mat.lv_last
1953 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001954 l->lv_len = 0;
1955 for (i = 0; i < len; ++i)
1956 list_append(l, ptrs[i].item);
1957 }
1958 }
1959 }
1960 else
1961 {
1962 int (*item_compare_func_ptr)(const void *, const void *);
1963
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001964 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001965 info.item_compare_func_err = FALSE;
1966 info.item_compare_keep_zero = TRUE;
1967 item_compare_func_ptr = info.item_compare_func != NULL
1968 || info.item_compare_partial != NULL
1969 ? item_compare2 : item_compare;
1970
1971 for (li = l->lv_first; li != NULL && li->li_next != NULL;
1972 li = li->li_next)
1973 {
1974 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
1975 == 0)
1976 ptrs[i++].item = li;
1977 if (info.item_compare_func_err)
1978 {
1979 emsg(_("E882: Uniq compare function failed"));
1980 break;
1981 }
1982 }
1983
1984 if (!info.item_compare_func_err)
1985 {
1986 while (--i >= 0)
1987 {
1988 li = ptrs[i].item->li_next;
1989 ptrs[i].item->li_next = li->li_next;
1990 if (li->li_next != NULL)
1991 li->li_next->li_prev = ptrs[i].item;
1992 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001993 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001994 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001996 l->lv_len--;
1997 }
1998 }
1999 }
2000
2001 vim_free(ptrs);
2002 }
2003theend:
2004 sortinfo = old_sortinfo;
2005}
2006
2007/*
2008 * "sort({list})" function
2009 */
2010 void
2011f_sort(typval_T *argvars, typval_T *rettv)
2012{
2013 do_sort_uniq(argvars, rettv, TRUE);
2014}
2015
2016/*
2017 * "uniq({list})" function
2018 */
2019 void
2020f_uniq(typval_T *argvars, typval_T *rettv)
2021{
2022 do_sort_uniq(argvars, rettv, FALSE);
2023}
2024
Bram Moolenaarea696852020-11-09 18:31:39 +01002025typedef enum {
2026 FILTERMAP_FILTER,
2027 FILTERMAP_MAP,
2028 FILTERMAP_MAPNEW
2029} filtermap_T;
2030
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002031/*
2032 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01002033 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002034 */
2035 static int
Bram Moolenaarea696852020-11-09 18:31:39 +01002036filter_map_one(
2037 typval_T *tv, // original value
2038 typval_T *expr, // callback
2039 filtermap_T filtermap,
2040 typval_T *newtv, // for map() and mapnew(): new value
2041 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002042{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002043 typval_T argv[3];
2044 int retval = FAIL;
2045
2046 copy_tv(tv, get_vim_var_tv(VV_VAL));
2047 argv[0] = *get_vim_var_tv(VV_KEY);
2048 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01002049 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002050 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002051 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002052 {
2053 int error = FALSE;
2054
2055 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02002056 if (in_vim9script())
Bram Moolenaarea696852020-11-09 18:31:39 +01002057 *remp = !tv2bool(newtv);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002058 else
Bram Moolenaarea696852020-11-09 18:31:39 +01002059 *remp = (tv_get_number_chk(newtv, &error) == 0);
2060 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002061 // On type error, nothing has been removed; return FAIL to stop the
2062 // loop. The error message was given by tv_get_number_chk().
2063 if (error)
2064 goto theend;
2065 }
2066 retval = OK;
2067theend:
2068 clear_tv(get_vim_var_tv(VV_VAL));
2069 return retval;
2070}
2071
2072/*
2073 * Implementation of map() and filter().
2074 */
2075 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01002076filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002077{
2078 typval_T *expr;
2079 listitem_T *li, *nli;
2080 list_T *l = NULL;
2081 dictitem_T *di;
2082 hashtab_T *ht;
2083 hashitem_T *hi;
2084 dict_T *d = NULL;
2085 blob_T *b = NULL;
2086 int rem;
2087 int todo;
Bram Moolenaarea696852020-11-09 18:31:39 +01002088 char_u *ermsg = (char_u *)(filtermap == FILTERMAP_MAP ? "map()"
2089 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
2090 : "filter()");
2091 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2092 ? N_("map() argument")
2093 : filtermap == FILTERMAP_MAPNEW
2094 ? N_("mapnew() argument")
2095 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002096 int save_did_emsg;
2097 int idx = 0;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002098 type_T *type = NULL;
2099 garray_T type_list;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002100
Bram Moolenaarea696852020-11-09 18:31:39 +01002101 // map() and filter() return the first argument, also on failure.
2102 if (filtermap != FILTERMAP_MAPNEW)
2103 copy_tv(&argvars[0], rettv);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002104 if (filtermap == FILTERMAP_MAP && in_vim9script())
2105 {
2106 // Check that map() does not change the type of the dict.
2107 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaarf2253962021-04-13 20:53:13 +02002108 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002109 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002110
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002111 if (argvars[0].v_type == VAR_BLOB)
2112 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002113 if (filtermap == FILTERMAP_MAPNEW)
2114 {
2115 rettv->v_type = VAR_BLOB;
2116 rettv->vval.v_blob = NULL;
2117 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002118 if ((b = argvars[0].vval.v_blob) == NULL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002119 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002120 }
2121 else if (argvars[0].v_type == VAR_LIST)
2122 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002123 if (filtermap == FILTERMAP_MAPNEW)
2124 {
2125 rettv->v_type = VAR_LIST;
2126 rettv->vval.v_list = NULL;
2127 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002128 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002129 || (filtermap == FILTERMAP_FILTER
2130 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002131 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002132 }
2133 else if (argvars[0].v_type == VAR_DICT)
2134 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002135 if (filtermap == FILTERMAP_MAPNEW)
2136 {
2137 rettv->v_type = VAR_DICT;
2138 rettv->vval.v_dict = NULL;
2139 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002140 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002141 || (filtermap == FILTERMAP_FILTER
2142 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002143 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002144 }
2145 else
2146 {
Bram Moolenaarfcb0b612020-05-26 20:22:01 +02002147 semsg(_(e_listdictblobarg), ermsg);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002148 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002149 }
2150
2151 expr = &argvars[1];
2152 // On type errors, the preceding call has already displayed an error
2153 // message. Avoid a misleading error message for an empty string that
2154 // was not passed as argument.
2155 if (expr->v_type != VAR_UNKNOWN)
2156 {
2157 typval_T save_val;
2158 typval_T save_key;
2159
2160 prepare_vimvar(VV_VAL, &save_val);
2161 prepare_vimvar(VV_KEY, &save_key);
2162
2163 // We reset "did_emsg" to be able to detect whether an error
2164 // occurred during evaluation of the expression.
2165 save_did_emsg = did_emsg;
2166 did_emsg = FALSE;
2167
2168 if (argvars[0].v_type == VAR_DICT)
2169 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002170 int prev_lock = d->dv_lock;
Bram Moolenaarea696852020-11-09 18:31:39 +01002171 dict_T *d_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002172
Bram Moolenaarea696852020-11-09 18:31:39 +01002173 if (filtermap == FILTERMAP_MAPNEW)
2174 {
2175 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002176 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002177 d_ret = rettv->vval.v_dict;
2178 }
2179
2180 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002181 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002182 ht = &d->dv_hashtab;
2183 hash_lock(ht);
2184 todo = (int)ht->ht_used;
2185 for (hi = ht->ht_array; todo > 0; ++hi)
2186 {
2187 if (!HASHITEM_EMPTY(hi))
2188 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002189 int r;
2190 typval_T newtv;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002191
2192 --todo;
2193 di = HI2DI(hi);
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002194 if (filtermap == FILTERMAP_MAP
Bram Moolenaarea696852020-11-09 18:31:39 +01002195 && (value_check_lock(di->di_tv.v_lock,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002196 arg_errmsg, TRUE)
2197 || var_check_ro(di->di_flags,
2198 arg_errmsg, TRUE)))
2199 break;
2200 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaar027c4ab2021-02-21 16:20:18 +01002201 newtv.v_type = VAR_UNKNOWN;
Bram Moolenaarea696852020-11-09 18:31:39 +01002202 r = filter_map_one(&di->di_tv, expr, filtermap,
2203 &newtv, &rem);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002204 clear_tv(get_vim_var_tv(VV_KEY));
2205 if (r == FAIL || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002206 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002207 clear_tv(&newtv);
2208 break;
2209 }
2210 if (filtermap == FILTERMAP_MAP)
2211 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002212 if (type != NULL && check_typval_arg_type(
2213 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002214 {
2215 clear_tv(&newtv);
2216 break;
2217 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002218 // map(): replace the dict item value
2219 clear_tv(&di->di_tv);
2220 newtv.v_lock = 0;
2221 di->di_tv = newtv;
2222 }
2223 else if (filtermap == FILTERMAP_MAPNEW)
2224 {
2225 // mapnew(): add the item value to the new dict
2226 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
2227 clear_tv(&newtv);
2228 if (r == FAIL)
2229 break;
2230 }
2231 else if (filtermap == FILTERMAP_FILTER && rem)
2232 {
2233 // filter(false): remove the item from the dict
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002234 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2235 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2236 break;
2237 dictitem_remove(d, di);
2238 }
2239 }
2240 }
2241 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002242 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002243 }
2244 else if (argvars[0].v_type == VAR_BLOB)
2245 {
2246 int i;
2247 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002248 varnumber_T val;
Bram Moolenaarea696852020-11-09 18:31:39 +01002249 blob_T *b_ret = b;
2250
2251 if (filtermap == FILTERMAP_MAPNEW)
2252 {
2253 if (blob_copy(b, rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002254 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002255 b_ret = rettv->vval.v_blob;
2256 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002257
2258 // set_vim_var_nr() doesn't set the type
2259 set_vim_var_type(VV_KEY, VAR_NUMBER);
2260
2261 for (i = 0; i < b->bv_ga.ga_len; i++)
2262 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002263 typval_T newtv;
2264
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002265 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002266 val = blob_get(b, i);
2267 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002268 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaarea696852020-11-09 18:31:39 +01002269 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
2270 || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002271 break;
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002272 if (newtv.v_type != VAR_NUMBER && newtv.v_type != VAR_BOOL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002273 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002274 clear_tv(&newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002275 emsg(_(e_invalblob));
2276 break;
2277 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002278 if (filtermap != FILTERMAP_FILTER)
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002279 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002280 if (newtv.vval.v_number != val)
2281 blob_set(b_ret, i, newtv.vval.v_number);
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002282 }
2283 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002284 {
2285 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2286
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002287 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002288 (size_t)b->bv_ga.ga_len - i - 1);
2289 --b->bv_ga.ga_len;
2290 --i;
2291 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002292 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002293 }
2294 }
2295 else // argvars[0].v_type == VAR_LIST
2296 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002297 int prev_lock = l->lv_lock;
2298 list_T *l_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002299
Bram Moolenaarea696852020-11-09 18:31:39 +01002300 if (filtermap == FILTERMAP_MAPNEW)
2301 {
2302 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002303 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002304 l_ret = rettv->vval.v_list;
2305 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002306 // set_vim_var_nr() doesn't set the type
2307 set_vim_var_type(VV_KEY, VAR_NUMBER);
2308
Bram Moolenaarea696852020-11-09 18:31:39 +01002309 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002310 l->lv_lock = VAR_LOCKED;
Bram Moolenaarea696852020-11-09 18:31:39 +01002311
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002312 if (l->lv_first == &range_list_item)
2313 {
2314 varnumber_T val = l->lv_u.nonmat.lv_start;
2315 int len = l->lv_len;
2316 int stride = l->lv_u.nonmat.lv_stride;
2317
2318 // List from range(): loop over the numbers
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002319 if (filtermap != FILTERMAP_MAPNEW)
2320 {
2321 l->lv_first = NULL;
2322 l->lv_u.mat.lv_last = NULL;
2323 l->lv_len = 0;
2324 l->lv_u.mat.lv_idx_item = NULL;
2325 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002326
2327 for (idx = 0; idx < len; ++idx)
Bram Moolenaarc56936e2020-11-10 11:43:56 +01002328 {
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002329 typval_T tv;
2330 typval_T newtv;
2331
2332 tv.v_type = VAR_NUMBER;
2333 tv.v_lock = 0;
2334 tv.vval.v_number = val;
2335 set_vim_var_nr(VV_KEY, idx);
2336 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2337 == FAIL)
Bram Moolenaarea696852020-11-09 18:31:39 +01002338 break;
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002339 if (did_emsg)
2340 {
2341 clear_tv(&newtv);
2342 break;
2343 }
2344 if (filtermap != FILTERMAP_FILTER)
2345 {
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002346 if (filtermap == FILTERMAP_MAP && type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002347 && check_typval_arg_type(
2348 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002349 {
2350 clear_tv(&newtv);
2351 break;
2352 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002353 // map(), mapnew(): always append the new value to the
2354 // list
2355 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2356 ? l : l_ret, &newtv) == FAIL)
2357 break;
2358 }
2359 else if (!rem)
2360 {
2361 // filter(): append the list item value when not rem
2362 if (list_append_tv_move(l, &tv) == FAIL)
2363 break;
2364 }
2365
2366 val += stride;
Bram Moolenaarea696852020-11-09 18:31:39 +01002367 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002368 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002369 else
2370 {
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002371 // Materialized list: loop over the items
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002372 for (li = l->lv_first; li != NULL; li = nli)
2373 {
2374 typval_T newtv;
2375
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002376 if (filtermap == FILTERMAP_MAP && value_check_lock(
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002377 li->li_tv.v_lock, arg_errmsg, TRUE))
2378 break;
2379 nli = li->li_next;
2380 set_vim_var_nr(VV_KEY, idx);
2381 if (filter_map_one(&li->li_tv, expr, filtermap,
2382 &newtv, &rem) == FAIL)
2383 break;
2384 if (did_emsg)
2385 {
2386 clear_tv(&newtv);
2387 break;
2388 }
2389 if (filtermap == FILTERMAP_MAP)
2390 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002391 if (type != NULL && check_typval_arg_type(
2392 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002393 {
2394 clear_tv(&newtv);
2395 break;
2396 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002397 // map(): replace the list item value
2398 clear_tv(&li->li_tv);
2399 newtv.v_lock = 0;
2400 li->li_tv = newtv;
2401 }
2402 else if (filtermap == FILTERMAP_MAPNEW)
2403 {
2404 // mapnew(): append the list item value
2405 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2406 break;
2407 }
2408 else if (filtermap == FILTERMAP_FILTER && rem)
2409 listitem_remove(l, li);
2410 ++idx;
2411 }
2412 }
2413
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002414 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002415 }
2416
2417 restore_vimvar(VV_KEY, &save_key);
2418 restore_vimvar(VV_VAL, &save_val);
2419
2420 did_emsg |= save_did_emsg;
2421 }
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002422
2423theend:
2424 if (type != NULL)
2425 clear_type_list(&type_list);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002426}
2427
2428/*
2429 * "filter()" function
2430 */
2431 void
2432f_filter(typval_T *argvars, typval_T *rettv)
2433{
Bram Moolenaarea696852020-11-09 18:31:39 +01002434 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002435}
2436
2437/*
2438 * "map()" function
2439 */
2440 void
2441f_map(typval_T *argvars, typval_T *rettv)
2442{
Bram Moolenaarea696852020-11-09 18:31:39 +01002443 filter_map(argvars, rettv, FILTERMAP_MAP);
2444}
2445
2446/*
2447 * "mapnew()" function
2448 */
2449 void
2450f_mapnew(typval_T *argvars, typval_T *rettv)
2451{
2452 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002453}
2454
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002455/*
2456 * "add(list, item)" function
2457 */
2458 void
2459f_add(typval_T *argvars, typval_T *rettv)
2460{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002461 rettv->vval.v_number = 1; // Default: Failed
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002462 if (argvars[0].v_type == VAR_LIST)
2463 {
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002464 list_T *l = argvars[0].vval.v_list;
2465
2466 if (l == NULL)
2467 {
2468 if (in_vim9script())
2469 emsg(_(e_cannot_add_to_null_list));
2470 }
2471 else if (!value_check_lock(l->lv_lock,
2472 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002473 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002474 {
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002475 copy_tv(&argvars[0], rettv);
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002476 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002477 }
2478 else if (argvars[0].v_type == VAR_BLOB)
2479 {
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002480 blob_T *b = argvars[0].vval.v_blob;
2481
2482 if (b == NULL)
2483 {
2484 if (in_vim9script())
2485 emsg(_(e_cannot_add_to_null_blob));
2486 }
2487 else if (!value_check_lock(b->bv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002488 (char_u *)N_("add() argument"), TRUE))
2489 {
2490 int error = FALSE;
2491 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2492
2493 if (!error)
2494 {
2495 ga_append(&b->bv_ga, (int)n);
2496 copy_tv(&argvars[0], rettv);
2497 }
2498 }
2499 }
2500 else
2501 emsg(_(e_listblobreq));
2502}
2503
2504/*
2505 * "count()" function
2506 */
2507 void
2508f_count(typval_T *argvars, typval_T *rettv)
2509{
2510 long n = 0;
2511 int ic = FALSE;
2512 int error = FALSE;
2513
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002514 if (in_vim9script()
2515 && ((argvars[0].v_type != VAR_STRING
2516 && argvars[0].v_type != VAR_LIST
2517 && argvars[0].v_type != VAR_DICT
2518 && check_for_string_arg(argvars, 0) == FAIL)
2519 || check_for_opt_bool_arg(argvars, 2) == FAIL
2520 || (argvars[2].v_type != VAR_UNKNOWN
2521 && check_for_opt_number_arg(argvars, 3) == FAIL)))
2522 return;
2523
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002524 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002525 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002526
2527 if (argvars[0].v_type == VAR_STRING)
2528 {
2529 char_u *expr = tv_get_string_chk(&argvars[1]);
2530 char_u *p = argvars[0].vval.v_string;
2531 char_u *next;
2532
2533 if (!error && expr != NULL && *expr != NUL && p != NULL)
2534 {
2535 if (ic)
2536 {
2537 size_t len = STRLEN(expr);
2538
2539 while (*p != NUL)
2540 {
2541 if (MB_STRNICMP(p, expr, len) == 0)
2542 {
2543 ++n;
2544 p += len;
2545 }
2546 else
2547 MB_PTR_ADV(p);
2548 }
2549 }
2550 else
2551 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2552 != NULL)
2553 {
2554 ++n;
2555 p = next + STRLEN(expr);
2556 }
2557 }
2558
2559 }
2560 else if (argvars[0].v_type == VAR_LIST)
2561 {
2562 listitem_T *li;
2563 list_T *l;
2564 long idx;
2565
2566 if ((l = argvars[0].vval.v_list) != NULL)
2567 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002568 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002569 li = l->lv_first;
2570 if (argvars[2].v_type != VAR_UNKNOWN)
2571 {
2572 if (argvars[3].v_type != VAR_UNKNOWN)
2573 {
2574 idx = (long)tv_get_number_chk(&argvars[3], &error);
2575 if (!error)
2576 {
2577 li = list_find(l, idx);
2578 if (li == NULL)
2579 semsg(_(e_listidx), idx);
2580 }
2581 }
2582 if (error)
2583 li = NULL;
2584 }
2585
2586 for ( ; li != NULL; li = li->li_next)
2587 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2588 ++n;
2589 }
2590 }
2591 else if (argvars[0].v_type == VAR_DICT)
2592 {
2593 int todo;
2594 dict_T *d;
2595 hashitem_T *hi;
2596
2597 if ((d = argvars[0].vval.v_dict) != NULL)
2598 {
2599 if (argvars[2].v_type != VAR_UNKNOWN)
2600 {
2601 if (argvars[3].v_type != VAR_UNKNOWN)
2602 emsg(_(e_invarg));
2603 }
2604
2605 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2606 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2607 {
2608 if (!HASHITEM_EMPTY(hi))
2609 {
2610 --todo;
2611 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2612 ++n;
2613 }
2614 }
2615 }
2616 }
2617 else
2618 semsg(_(e_listdictarg), "count()");
2619 rettv->vval.v_number = n;
2620}
2621
2622/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002623 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002624 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002625 static void
2626extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002627{
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002628 type_T *type = NULL;
2629 garray_T type_list;
2630
2631 if (!is_new && in_vim9script())
2632 {
2633 // Check that map() does not change the type of the dict.
2634 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaarf2253962021-04-13 20:53:13 +02002635 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002636 }
2637
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002638 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2639 {
2640 list_T *l1, *l2;
2641 listitem_T *item;
2642 long before;
2643 int error = FALSE;
2644
2645 l1 = argvars[0].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002646 if (l1 == NULL)
2647 {
2648 emsg(_(e_cannot_extend_null_list));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002649 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002650 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002651 l2 = argvars[1].vval.v_list;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002652 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
2653 && l2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002654 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002655 if (is_new)
2656 {
2657 l1 = list_copy(l1, FALSE, get_copyID());
2658 if (l1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002659 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002660 }
2661
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002662 if (argvars[2].v_type != VAR_UNKNOWN)
2663 {
2664 before = (long)tv_get_number_chk(&argvars[2], &error);
2665 if (error)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002666 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002667
2668 if (before == l1->lv_len)
2669 item = NULL;
2670 else
2671 {
2672 item = list_find(l1, before);
2673 if (item == NULL)
2674 {
2675 semsg(_(e_listidx), before);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002676 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002677 }
2678 }
2679 }
2680 else
2681 item = NULL;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002682 if (type != NULL && check_typval_arg_type(
2683 type, &argvars[1], 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002684 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002685 list_extend(l1, l2, item);
2686
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002687 if (is_new)
2688 {
2689 rettv->v_type = VAR_LIST;
2690 rettv->vval.v_list = l1;
2691 rettv->v_lock = FALSE;
2692 }
2693 else
2694 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002695 }
2696 }
2697 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2698 {
2699 dict_T *d1, *d2;
2700 char_u *action;
2701 int i;
2702
2703 d1 = argvars[0].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002704 if (d1 == NULL)
2705 {
2706 emsg(_(e_cannot_extend_null_dict));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002707 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002708 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002709 d2 = argvars[1].vval.v_dict;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002710 if ((is_new || !value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
2711 && d2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002712 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002713 if (is_new)
2714 {
2715 d1 = dict_copy(d1, FALSE, get_copyID());
2716 if (d1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002717 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002718 }
2719
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002720 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002721 if (argvars[2].v_type != VAR_UNKNOWN)
2722 {
2723 static char *(av[]) = {"keep", "force", "error"};
2724
2725 action = tv_get_string_chk(&argvars[2]);
2726 if (action == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002727 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002728 for (i = 0; i < 3; ++i)
2729 if (STRCMP(action, av[i]) == 0)
2730 break;
2731 if (i == 3)
2732 {
2733 semsg(_(e_invarg2), action);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002734 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002735 }
2736 }
2737 else
2738 action = (char_u *)"force";
2739
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002740 if (type != NULL && check_typval_arg_type(
2741 type, &argvars[1], 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002742 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002743 dict_extend(d1, d2, action);
2744
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002745 if (is_new)
2746 {
2747 rettv->v_type = VAR_DICT;
2748 rettv->vval.v_dict = d1;
2749 rettv->v_lock = FALSE;
2750 }
2751 else
2752 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002753 }
2754 }
2755 else
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002756 semsg(_(e_listdictarg), is_new ? "extendnew()" : "extend()");
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002757
2758theend:
2759 if (type != NULL)
2760 clear_type_list(&type_list);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002761}
2762
2763/*
2764 * "extend(list, list [, idx])" function
2765 * "extend(dict, dict [, action])" function
2766 */
2767 void
2768f_extend(typval_T *argvars, typval_T *rettv)
2769{
2770 char_u *errmsg = (char_u *)N_("extend() argument");
2771
2772 extend(argvars, rettv, errmsg, FALSE);
2773}
2774
2775/*
2776 * "extendnew(list, list [, idx])" function
2777 * "extendnew(dict, dict [, action])" function
2778 */
2779 void
2780f_extendnew(typval_T *argvars, typval_T *rettv)
2781{
2782 char_u *errmsg = (char_u *)N_("extendnew() argument");
2783
2784 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002785}
2786
2787/*
2788 * "insert()" function
2789 */
2790 void
2791f_insert(typval_T *argvars, typval_T *rettv)
2792{
2793 long before = 0;
2794 listitem_T *item;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002795 int error = FALSE;
2796
2797 if (argvars[0].v_type == VAR_BLOB)
2798 {
2799 int val, len;
2800 char_u *p;
2801
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002802 if (argvars[0].vval.v_blob == NULL)
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002803 {
2804 if (in_vim9script())
2805 emsg(_(e_cannot_add_to_null_blob));
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002806 return;
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002807 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002808
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002809 len = blob_len(argvars[0].vval.v_blob);
2810 if (argvars[2].v_type != VAR_UNKNOWN)
2811 {
2812 before = (long)tv_get_number_chk(&argvars[2], &error);
2813 if (error)
2814 return; // type error; errmsg already given
2815 if (before < 0 || before > len)
2816 {
2817 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2818 return;
2819 }
2820 }
2821 val = tv_get_number_chk(&argvars[1], &error);
2822 if (error)
2823 return;
2824 if (val < 0 || val > 255)
2825 {
2826 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2827 return;
2828 }
2829
2830 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
2831 return;
2832 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2833 mch_memmove(p + before + 1, p + before, (size_t)len - before);
2834 *(p + before) = val;
2835 ++argvars[0].vval.v_blob->bv_ga.ga_len;
2836
2837 copy_tv(&argvars[0], rettv);
2838 }
2839 else if (argvars[0].v_type != VAR_LIST)
2840 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002841 else
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002842 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002843 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002844
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002845 if (l == NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002846 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002847 if (in_vim9script())
2848 emsg(_(e_cannot_add_to_null_list));
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002849 }
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002850 else if (!value_check_lock(l->lv_lock,
2851 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002852 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002853 if (argvars[2].v_type != VAR_UNKNOWN)
2854 before = (long)tv_get_number_chk(&argvars[2], &error);
2855 if (error)
2856 return; // type error; errmsg already given
2857
2858 if (before == l->lv_len)
2859 item = NULL;
2860 else
2861 {
2862 item = list_find(l, before);
2863 if (item == NULL)
2864 {
2865 semsg(_(e_listidx), before);
2866 l = NULL;
2867 }
2868 }
2869 if (l != NULL)
2870 {
2871 (void)list_insert_tv(l, &argvars[1], item);
2872 copy_tv(&argvars[0], rettv);
2873 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002874 }
2875 }
2876}
2877
2878/*
2879 * "remove()" function
2880 */
2881 void
2882f_remove(typval_T *argvars, typval_T *rettv)
2883{
2884 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2885
2886 if (argvars[0].v_type == VAR_DICT)
2887 dict_remove(argvars, rettv, arg_errmsg);
2888 else if (argvars[0].v_type == VAR_BLOB)
2889 blob_remove(argvars, rettv);
2890 else if (argvars[0].v_type == VAR_LIST)
2891 list_remove(argvars, rettv, arg_errmsg);
2892 else
2893 semsg(_(e_listdictblobarg), "remove()");
2894}
2895
2896/*
2897 * "reverse({list})" function
2898 */
2899 void
2900f_reverse(typval_T *argvars, typval_T *rettv)
2901{
2902 list_T *l;
2903 listitem_T *li, *ni;
2904
2905 if (argvars[0].v_type == VAR_BLOB)
2906 {
2907 blob_T *b = argvars[0].vval.v_blob;
2908 int i, len = blob_len(b);
2909
2910 for (i = 0; i < len / 2; i++)
2911 {
2912 int tmp = blob_get(b, i);
2913
2914 blob_set(b, i, blob_get(b, len - i - 1));
2915 blob_set(b, len - i - 1, tmp);
2916 }
2917 rettv_blob_set(rettv, b);
2918 return;
2919 }
2920
2921 if (argvars[0].v_type != VAR_LIST)
2922 semsg(_(e_listblobarg), "reverse()");
2923 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002924 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002925 (char_u *)N_("reverse() argument"), TRUE))
2926 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002927 if (l->lv_first == &range_list_item)
2928 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002929 varnumber_T new_start = l->lv_u.nonmat.lv_start
2930 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2931 l->lv_u.nonmat.lv_end = new_start
2932 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2933 l->lv_u.nonmat.lv_start = new_start;
2934 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002935 rettv_list_set(rettv, l);
2936 return;
2937 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002938 li = l->lv_u.mat.lv_last;
2939 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002940 l->lv_len = 0;
2941 while (li != NULL)
2942 {
2943 ni = li->li_prev;
2944 list_append(l, li);
2945 li = ni;
2946 }
2947 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002948 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002949 }
2950}
2951
Bram Moolenaar85629982020-06-01 18:39:20 +02002952/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002953 * "reduce(list, { accumulator, element -> value } [, initial])" function
Bram Moolenaar85629982020-06-01 18:39:20 +02002954 */
2955 void
2956f_reduce(typval_T *argvars, typval_T *rettv)
2957{
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002958 typval_T initial;
Bram Moolenaar85629982020-06-01 18:39:20 +02002959 char_u *func_name;
2960 partial_T *partial = NULL;
2961 funcexe_T funcexe;
2962 typval_T argv[3];
2963
2964 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
2965 {
2966 emsg(_(e_listblobreq));
2967 return;
2968 }
2969
2970 if (argvars[1].v_type == VAR_FUNC)
2971 func_name = argvars[1].vval.v_string;
2972 else if (argvars[1].v_type == VAR_PARTIAL)
2973 {
2974 partial = argvars[1].vval.v_partial;
2975 func_name = partial_name(partial);
2976 }
2977 else
2978 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01002979 if (func_name == NULL || *func_name == NUL)
2980 {
2981 emsg(_(e_missing_function_argument));
2982 return;
2983 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002984
2985 vim_memset(&funcexe, 0, sizeof(funcexe));
2986 funcexe.evaluate = TRUE;
2987 funcexe.partial = partial;
2988
2989 if (argvars[0].v_type == VAR_LIST)
2990 {
2991 list_T *l = argvars[0].vval.v_list;
2992 listitem_T *li = NULL;
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002993 int r;
Bram Moolenaarca275a02020-06-24 22:07:46 +02002994 int called_emsg_start = called_emsg;
Bram Moolenaar85629982020-06-01 18:39:20 +02002995
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002996 if (l != NULL)
2997 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar85629982020-06-01 18:39:20 +02002998 if (argvars[2].v_type == VAR_UNKNOWN)
2999 {
3000 if (l == NULL || l->lv_first == NULL)
3001 {
3002 semsg(_(e_reduceempty), "List");
3003 return;
3004 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003005 initial = l->lv_first->li_tv;
Bram Moolenaar85629982020-06-01 18:39:20 +02003006 li = l->lv_first->li_next;
3007 }
3008 else
3009 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003010 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02003011 if (l != NULL)
3012 li = l->lv_first;
3013 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003014 copy_tv(&initial, rettv);
Bram Moolenaarfda20c42020-06-29 20:09:36 +02003015
3016 if (l != NULL)
Bram Moolenaar85629982020-06-01 18:39:20 +02003017 {
Bram Moolenaarfda20c42020-06-29 20:09:36 +02003018 int prev_locked = l->lv_lock;
3019
3020 l->lv_lock = VAR_FIXED; // disallow the list changing here
3021 for ( ; li != NULL; li = li->li_next)
3022 {
3023 argv[0] = *rettv;
3024 argv[1] = li->li_tv;
3025 rettv->v_type = VAR_UNKNOWN;
3026 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
3027 clear_tv(&argv[0]);
3028 if (r == FAIL || called_emsg != called_emsg_start)
3029 break;
3030 }
3031 l->lv_lock = prev_locked;
Bram Moolenaar85629982020-06-01 18:39:20 +02003032 }
3033 }
3034 else
3035 {
3036 blob_T *b = argvars[0].vval.v_blob;
3037 int i;
3038
3039 if (argvars[2].v_type == VAR_UNKNOWN)
3040 {
3041 if (b == NULL || b->bv_ga.ga_len == 0)
3042 {
3043 semsg(_(e_reduceempty), "Blob");
3044 return;
3045 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003046 initial.v_type = VAR_NUMBER;
3047 initial.vval.v_number = blob_get(b, 0);
Bram Moolenaar85629982020-06-01 18:39:20 +02003048 i = 1;
3049 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003050 else if (argvars[2].v_type != VAR_NUMBER)
3051 {
3052 emsg(_(e_number_exp));
3053 return;
3054 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003055 else
3056 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003057 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02003058 i = 0;
3059 }
3060
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003061 copy_tv(&initial, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003062 if (b != NULL)
3063 {
3064 for ( ; i < b->bv_ga.ga_len; i++)
3065 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003066 argv[0] = *rettv;
Bram Moolenaar85629982020-06-01 18:39:20 +02003067 argv[1].v_type = VAR_NUMBER;
3068 argv[1].vval.v_number = blob_get(b, i);
3069 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
3070 return;
Bram Moolenaar85629982020-06-01 18:39:20 +02003071 }
3072 }
3073 }
3074}
3075
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02003076#endif // defined(FEAT_EVAL)