blob: b00ef492469ccd3175eb958894482fc2e5d3ab7c [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
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200608 && check_typval_arg_type(l->lv_type->tt_member, tv,
609 NULL, 0) == FAIL)
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200610 return FAIL;
Bram Moolenaar90fba562021-07-09 19:17:55 +0200611 li = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200612 if (li == NULL)
613 return FAIL;
614 copy_tv(tv, &li->li_tv);
615 list_append(l, li);
616 return OK;
617}
618
619/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620 * As list_append_tv() but move the value instead of copying it.
621 * Return FAIL when out of memory.
622 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200623 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624list_append_tv_move(list_T *l, typval_T *tv)
625{
626 listitem_T *li = listitem_alloc();
627
628 if (li == NULL)
629 return FAIL;
630 li->li_tv = *tv;
631 list_append(l, li);
632 return OK;
633}
634
635/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200636 * Add a dictionary to a list. Used by getqflist().
637 * Return FAIL when out of memory.
638 */
639 int
640list_append_dict(list_T *list, dict_T *dict)
641{
642 listitem_T *li = listitem_alloc();
643
644 if (li == NULL)
645 return FAIL;
646 li->li_tv.v_type = VAR_DICT;
647 li->li_tv.v_lock = 0;
648 li->li_tv.vval.v_dict = dict;
649 list_append(list, li);
650 ++dict->dv_refcount;
651 return OK;
652}
653
654/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100655 * Append list2 to list1.
656 * Return FAIL when out of memory.
657 */
658 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200659list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100660{
661 listitem_T *li = listitem_alloc();
662
663 if (li == NULL)
664 return FAIL;
665 li->li_tv.v_type = VAR_LIST;
666 li->li_tv.v_lock = 0;
667 li->li_tv.vval.v_list = list2;
668 list_append(list1, li);
669 ++list2->lv_refcount;
670 return OK;
671}
672
673/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200674 * Make a copy of "str" and append it as an item to list "l".
675 * When "len" >= 0 use "str[len]".
676 * Returns FAIL when out of memory.
677 */
678 int
679list_append_string(list_T *l, char_u *str, int len)
680{
681 listitem_T *li = listitem_alloc();
682
683 if (li == NULL)
684 return FAIL;
685 list_append(l, li);
686 li->li_tv.v_type = VAR_STRING;
687 li->li_tv.v_lock = 0;
688 if (str == NULL)
689 li->li_tv.vval.v_string = NULL;
690 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
691 : vim_strsave(str))) == NULL)
692 return FAIL;
693 return OK;
694}
695
696/*
697 * Append "n" to list "l".
698 * Returns FAIL when out of memory.
699 */
700 int
701list_append_number(list_T *l, varnumber_T n)
702{
703 listitem_T *li;
704
705 li = listitem_alloc();
706 if (li == NULL)
707 return FAIL;
708 li->li_tv.v_type = VAR_NUMBER;
709 li->li_tv.v_lock = 0;
710 li->li_tv.vval.v_number = n;
711 list_append(l, li);
712 return OK;
713}
714
715/*
716 * Insert typval_T "tv" in list "l" before "item".
717 * If "item" is NULL append at the end.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100718 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200719 */
720 int
721list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
722{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100723 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200724
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100725 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200726 && check_typval_arg_type(l->lv_type->tt_member, tv,
727 NULL, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100728 return FAIL;
729 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200730 if (ni == NULL)
731 return FAIL;
732 copy_tv(tv, &ni->li_tv);
733 list_insert(l, ni, item);
734 return OK;
735}
736
737 void
738list_insert(list_T *l, listitem_T *ni, listitem_T *item)
739{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200740 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200741 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100742 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200743 list_append(l, ni);
744 else
745 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100746 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200747 ni->li_prev = item->li_prev;
748 ni->li_next = item;
749 if (item->li_prev == NULL)
750 {
751 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100752 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200753 }
754 else
755 {
756 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100757 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200758 }
759 item->li_prev = ni;
760 ++l->lv_len;
761 }
762}
763
764/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200765 * Get the list item in "l" with index "n1". "n1" is adjusted if needed.
766 * In Vim9, it is at the end of the list, add an item.
767 * Return NULL if there is no such item.
768 */
769 listitem_T *
770check_range_index_one(list_T *l, long *n1, int quiet)
771{
772 listitem_T *li = list_find_index(l, n1);
773
774 if (li == NULL)
775 {
776 // Vim9: Allow for adding an item at the end.
777 if (in_vim9script() && *n1 == l->lv_len && l->lv_lock == 0)
778 {
779 list_append_number(l, 0);
780 li = list_find_index(l, n1);
781 }
782 if (li == NULL)
783 {
784 if (!quiet)
785 semsg(_(e_listidx), *n1);
786 return NULL;
787 }
788 }
789 return li;
790}
791
792/*
793 * Check that "n2" can be used as the second index in a range of list "l".
794 * If "n1" or "n2" is negative it is changed to the positive index.
795 * "li1" is the item for item "n1".
796 * Return OK or FAIL.
797 */
798 int
799check_range_index_two(
800 list_T *l,
801 long *n1,
802 listitem_T *li1,
803 long *n2,
804 int quiet)
805{
806 if (*n2 < 0)
807 {
808 listitem_T *ni = list_find(l, *n2);
809
810 if (ni == NULL)
811 {
812 if (!quiet)
813 semsg(_(e_listidx), *n2);
814 return FAIL;
815 }
816 *n2 = list_idx_of_item(l, ni);
817 }
818
819 // Check that n2 isn't before n1.
820 if (*n1 < 0)
821 *n1 = list_idx_of_item(l, li1);
822 if (*n2 < *n1)
823 {
824 if (!quiet)
825 semsg(_(e_listidx), *n2);
826 return FAIL;
827 }
828 return OK;
829}
830
831/*
832 * Assign values from list "src" into a range of "dest".
833 * "idx1_arg" is the index of the first item in "dest" to be replaced.
834 * "idx2" is the index of last item to be replaced, but when "empty_idx2" is
835 * TRUE then replace all items after "idx1".
836 * "op" is the operator, normally "=" but can be "+=" and the like.
837 * "varname" is used for error messages.
838 * Returns OK or FAIL.
839 */
840 int
841list_assign_range(
842 list_T *dest,
843 list_T *src,
844 long idx1_arg,
845 long idx2,
846 int empty_idx2,
847 char_u *op,
848 char_u *varname)
849{
850 listitem_T *src_li;
851 listitem_T *dest_li;
852 long idx1 = idx1_arg;
853 listitem_T *first_li = list_find_index(dest, &idx1);
854 long idx;
855
856 /*
857 * Check whether any of the list items is locked before making any changes.
858 */
859 idx = idx1;
860 dest_li = first_li;
861 for (src_li = src->lv_first; src_li != NULL && dest_li != NULL; )
862 {
863 if (value_check_lock(dest_li->li_tv.v_lock, varname, FALSE))
864 return FAIL;
865 src_li = src_li->li_next;
866 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
867 break;
868 dest_li = dest_li->li_next;
869 ++idx;
870 }
871
872 /*
873 * Assign the List values to the list items.
874 */
875 idx = idx1;
876 dest_li = first_li;
877 for (src_li = src->lv_first; src_li != NULL; )
878 {
879 if (op != NULL && *op != '=')
880 tv_op(&dest_li->li_tv, &src_li->li_tv, op);
881 else
882 {
883 clear_tv(&dest_li->li_tv);
884 copy_tv(&src_li->li_tv, &dest_li->li_tv);
885 }
886 src_li = src_li->li_next;
887 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
888 break;
889 if (dest_li->li_next == NULL)
890 {
891 // Need to add an empty item.
892 if (list_append_number(dest, 0) == FAIL)
893 {
894 src_li = NULL;
895 break;
896 }
897 }
898 dest_li = dest_li->li_next;
899 ++idx;
900 }
901 if (src_li != NULL)
902 {
903 emsg(_(e_list_value_has_more_items_than_targets));
904 return FAIL;
905 }
906 if (empty_idx2
907 ? (dest_li != NULL && dest_li->li_next != NULL)
908 : idx != idx2)
909 {
910 emsg(_(e_list_value_does_not_have_enough_items));
911 return FAIL;
912 }
913 return OK;
914}
915
916/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200917 * Flatten "list" to depth "maxdepth".
918 * It does nothing if "maxdepth" is 0.
919 * Returns FAIL when out of memory.
920 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100921 static void
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200922list_flatten(list_T *list, long maxdepth)
923{
924 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200925 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200926 int n;
927
928 if (maxdepth == 0)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100929 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200930 CHECK_LIST_MATERIALIZE(list);
931
932 n = 0;
933 item = list->lv_first;
934 while (item != NULL)
935 {
936 fast_breakcheck();
937 if (got_int)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100938 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200939
940 if (item->li_tv.v_type == VAR_LIST)
941 {
942 listitem_T *next = item->li_next;
943
944 vimlist_remove(list, item, item);
945 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100946 return;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200947 clear_tv(&item->li_tv);
948 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200949
950 if (item->li_prev == NULL)
951 item = list->lv_first;
952 else
953 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200954 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200955
956 if (++n >= maxdepth)
957 {
958 n = 0;
959 item = next;
960 }
961 }
962 else
963 {
964 n = 0;
965 item = item->li_next;
966 }
967 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200968}
969
970/*
Bram Moolenaar3b690062021-02-01 20:14:51 +0100971 * "flatten()" and "flattennew()" functions
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200972 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100973 static void
974flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200975{
976 list_T *l;
977 long maxdepth;
978 int error = FALSE;
979
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200980 if (in_vim9script()
981 && (check_for_list_arg(argvars, 0) == FAIL
982 || check_for_opt_number_arg(argvars, 1) == FAIL))
983 return;
984
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200985 if (argvars[0].v_type != VAR_LIST)
986 {
987 semsg(_(e_listarg), "flatten()");
988 return;
989 }
990
991 if (argvars[1].v_type == VAR_UNKNOWN)
992 maxdepth = 999999;
993 else
994 {
995 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
996 if (error)
997 return;
998 if (maxdepth < 0)
999 {
1000 emsg(_("E900: maxdepth must be non-negative number"));
1001 return;
1002 }
1003 }
1004
1005 l = argvars[0].vval.v_list;
Bram Moolenaar3b690062021-02-01 20:14:51 +01001006 rettv->v_type = VAR_LIST;
1007 rettv->vval.v_list = l;
1008 if (l == NULL)
1009 return;
1010
1011 if (make_copy)
1012 {
1013 l = list_copy(l, TRUE, get_copyID());
1014 rettv->vval.v_list = l;
1015 if (l == NULL)
1016 return;
1017 }
1018 else
1019 {
1020 if (value_check_lock(l->lv_lock,
1021 (char_u *)N_("flatten() argument"), TRUE))
1022 return;
1023 ++l->lv_refcount;
1024 }
1025
1026 list_flatten(l, maxdepth);
1027}
1028
1029/*
1030 * "flatten(list[, {maxdepth}])" function
1031 */
1032 void
1033f_flatten(typval_T *argvars, typval_T *rettv)
1034{
1035 if (in_vim9script())
1036 emsg(_(e_cannot_use_flatten_in_vim9_script));
1037 else
1038 flatten_common(argvars, rettv, FALSE);
1039}
1040
1041/*
1042 * "flattennew(list[, {maxdepth}])" function
1043 */
1044 void
1045f_flattennew(typval_T *argvars, typval_T *rettv)
1046{
1047 flatten_common(argvars, rettv, TRUE);
Bram Moolenaar077a1e62020-06-08 20:50:43 +02001048}
1049
1050/*
Bram Moolenaar1a739232020-10-10 15:37:58 +02001051 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001052 * If "bef" is NULL append at the end, otherwise insert before this item.
1053 * Returns FAIL when out of memory.
1054 */
1055 int
1056list_extend(list_T *l1, list_T *l2, listitem_T *bef)
1057{
1058 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001059 int todo;
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001060 listitem_T *bef_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001061
Bram Moolenaar1a739232020-10-10 15:37:58 +02001062 // NULL list is equivalent to an empty list: nothing to do.
1063 if (l2 == NULL || l2->lv_len == 0)
1064 return OK;
1065
1066 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001067 CHECK_LIST_MATERIALIZE(l1);
1068 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001070 // When exending a list with itself, at some point we run into the item
1071 // that was before "bef" and need to skip over the already inserted items
1072 // to "bef".
1073 bef_prev = bef == NULL ? NULL : bef->li_prev;
1074
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001075 // We also quit the loop when we have inserted the original item count of
1076 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001077 for (item = l2->lv_first; item != NULL && --todo >= 0;
1078 item = item == bef_prev ? bef : item->li_next)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001079 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
1080 return FAIL;
1081 return OK;
1082}
1083
1084/*
1085 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
1086 * Return FAIL when out of memory.
1087 */
1088 int
1089list_concat(list_T *l1, list_T *l2, typval_T *tv)
1090{
1091 list_T *l;
1092
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001093 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +02001094 if (l1 == NULL)
1095 l = list_alloc();
1096 else
1097 l = list_copy(l1, FALSE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001098 if (l == NULL)
1099 return FAIL;
1100 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +01001101 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001102 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001103 if (l1 == NULL)
1104 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001105
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001106 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +02001107 return list_extend(l, l2, NULL);
1108}
1109
Bram Moolenaar9af78762020-06-16 11:34:42 +02001110 list_T *
1111list_slice(list_T *ol, long n1, long n2)
1112{
1113 listitem_T *item;
1114 list_T *l = list_alloc();
1115
1116 if (l == NULL)
1117 return NULL;
1118 for (item = list_find(ol, n1); n1 <= n2; ++n1)
1119 {
1120 if (list_append_tv(l, &item->li_tv) == FAIL)
1121 {
1122 list_free(l);
1123 return NULL;
1124 }
1125 item = item->li_next;
1126 }
1127 return l;
1128}
1129
Bram Moolenaared591872020-08-15 22:14:53 +02001130 int
1131list_slice_or_index(
1132 list_T *list,
1133 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01001134 varnumber_T n1_arg,
1135 varnumber_T n2_arg,
1136 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +02001137 typval_T *rettv,
1138 int verbose)
1139{
1140 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001141 varnumber_T n1 = n1_arg;
1142 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +02001143 typval_T var1;
1144
1145 if (n1 < 0)
1146 n1 = len + n1;
1147 if (n1 < 0 || n1 >= len)
1148 {
1149 // For a range we allow invalid values and return an empty
1150 // list. A list index out of range is an error.
1151 if (!range)
1152 {
1153 if (verbose)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001154 semsg(_(e_listidx), (long)n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +02001155 return FAIL;
1156 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001157 n1 = n1 < 0 ? 0 : len;
Bram Moolenaared591872020-08-15 22:14:53 +02001158 }
1159 if (range)
1160 {
1161 list_T *l;
1162
1163 if (n2 < 0)
1164 n2 = len + n2;
1165 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01001166 n2 = len - (exclusive ? 0 : 1);
1167 if (exclusive)
1168 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +02001169 if (n2 < 0 || n2 + 1 < n1)
1170 n2 = -1;
1171 l = list_slice(list, n1, n2);
1172 if (l == NULL)
1173 return FAIL;
1174 clear_tv(rettv);
1175 rettv_list_set(rettv, l);
1176 }
1177 else
1178 {
1179 // copy the item to "var1" to avoid that freeing the list makes it
1180 // invalid.
1181 copy_tv(&list_find(list, n1)->li_tv, &var1);
1182 clear_tv(rettv);
1183 *rettv = var1;
1184 }
1185 return OK;
1186}
1187
Bram Moolenaarda861d62016-07-17 15:46:27 +02001188/*
1189 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1190 * The refcount of the new list is set to 1.
1191 * See item_copy() for "copyID".
1192 * Returns NULL when out of memory.
1193 */
1194 list_T *
1195list_copy(list_T *orig, int deep, int copyID)
1196{
1197 list_T *copy;
1198 listitem_T *item;
1199 listitem_T *ni;
1200
1201 if (orig == NULL)
1202 return NULL;
1203
1204 copy = list_alloc();
1205 if (copy != NULL)
1206 {
1207 if (copyID != 0)
1208 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001209 // Do this before adding the items, because one of the items may
1210 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001211 orig->lv_copyID = copyID;
1212 orig->lv_copylist = copy;
1213 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001214 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001215 for (item = orig->lv_first; item != NULL && !got_int;
1216 item = item->li_next)
1217 {
1218 ni = listitem_alloc();
1219 if (ni == NULL)
1220 break;
1221 if (deep)
1222 {
1223 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
1224 {
1225 vim_free(ni);
1226 break;
1227 }
1228 }
1229 else
1230 copy_tv(&item->li_tv, &ni->li_tv);
1231 list_append(copy, ni);
1232 }
1233 ++copy->lv_refcount;
1234 if (item != NULL)
1235 {
1236 list_unref(copy);
1237 copy = NULL;
1238 }
1239 }
1240
1241 return copy;
1242}
1243
1244/*
1245 * Remove items "item" to "item2" from list "l".
1246 * Does not free the listitem or the value!
1247 * This used to be called list_remove, but that conflicts with a Sun header
1248 * file.
1249 */
1250 void
1251vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1252{
1253 listitem_T *ip;
1254
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001255 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001256
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001257 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001258 for (ip = item; ip != NULL; ip = ip->li_next)
1259 {
1260 --l->lv_len;
1261 list_fix_watch(l, ip);
1262 if (ip == item2)
1263 break;
1264 }
1265
1266 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001267 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001268 else
1269 item2->li_next->li_prev = item->li_prev;
1270 if (item->li_prev == NULL)
1271 l->lv_first = item2->li_next;
1272 else
1273 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001274 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001275}
1276
1277/*
1278 * Return an allocated string with the string representation of a list.
1279 * May return NULL.
1280 */
1281 char_u *
1282list2string(typval_T *tv, int copyID, int restore_copyID)
1283{
1284 garray_T ga;
1285
1286 if (tv->vval.v_list == NULL)
1287 return NULL;
1288 ga_init2(&ga, (int)sizeof(char), 80);
1289 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001290 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001291 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1292 FALSE, restore_copyID, copyID) == FAIL)
1293 {
1294 vim_free(ga.ga_data);
1295 return NULL;
1296 }
1297 ga_append(&ga, ']');
1298 ga_append(&ga, NUL);
1299 return (char_u *)ga.ga_data;
1300}
1301
1302typedef struct join_S {
1303 char_u *s;
1304 char_u *tofree;
1305} join_T;
1306
1307 static int
1308list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001309 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001310 list_T *l,
1311 char_u *sep,
1312 int echo_style,
1313 int restore_copyID,
1314 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001315 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001316{
1317 int i;
1318 join_T *p;
1319 int len;
1320 int sumlen = 0;
1321 int first = TRUE;
1322 char_u *tofree;
1323 char_u numbuf[NUMBUFLEN];
1324 listitem_T *item;
1325 char_u *s;
1326
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001327 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001328 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001329 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1330 {
1331 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001332 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001333 if (s == NULL)
1334 return FAIL;
1335
1336 len = (int)STRLEN(s);
1337 sumlen += len;
1338
1339 (void)ga_grow(join_gap, 1);
1340 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1341 if (tofree != NULL || s != numbuf)
1342 {
1343 p->s = s;
1344 p->tofree = tofree;
1345 }
1346 else
1347 {
1348 p->s = vim_strnsave(s, len);
1349 p->tofree = p->s;
1350 }
1351
1352 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001353 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001354 break;
1355 }
1356
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001357 // Allocate result buffer with its total size, avoid re-allocation and
1358 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001359 if (join_gap->ga_len >= 2)
1360 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1361 if (ga_grow(gap, sumlen + 2) == FAIL)
1362 return FAIL;
1363
1364 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1365 {
1366 if (first)
1367 first = FALSE;
1368 else
1369 ga_concat(gap, sep);
1370 p = ((join_T *)join_gap->ga_data) + i;
1371
1372 if (p->s != NULL)
1373 ga_concat(gap, p->s);
1374 line_breakcheck();
1375 }
1376
1377 return OK;
1378}
1379
1380/*
1381 * Join list "l" into a string in "*gap", using separator "sep".
1382 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1383 * Return FAIL or OK.
1384 */
1385 int
1386list_join(
1387 garray_T *gap,
1388 list_T *l,
1389 char_u *sep,
1390 int echo_style,
1391 int restore_copyID,
1392 int copyID)
1393{
1394 garray_T join_ga;
1395 int retval;
1396 join_T *p;
1397 int i;
1398
1399 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001400 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001401 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1402 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1403 copyID, &join_ga);
1404
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001405 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001406 if (join_ga.ga_data != NULL)
1407 {
1408 p = (join_T *)join_ga.ga_data;
1409 for (i = 0; i < join_ga.ga_len; ++i)
1410 {
1411 vim_free(p->tofree);
1412 ++p;
1413 }
1414 ga_clear(&join_ga);
1415 }
1416
1417 return retval;
1418}
1419
1420/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001421 * "join()" function
1422 */
1423 void
1424f_join(typval_T *argvars, typval_T *rettv)
1425{
1426 garray_T ga;
1427 char_u *sep;
1428
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001429 if (in_vim9script()
1430 && (check_for_list_arg(argvars, 0) == FAIL
1431 || check_for_opt_string_arg(argvars, 1) == FAIL))
1432 return;
1433
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001434 if (argvars[0].v_type != VAR_LIST)
1435 {
1436 emsg(_(e_listreq));
1437 return;
1438 }
1439 if (argvars[0].vval.v_list == NULL)
1440 return;
1441 if (argvars[1].v_type == VAR_UNKNOWN)
1442 sep = (char_u *)" ";
1443 else
1444 sep = tv_get_string_chk(&argvars[1]);
1445
1446 rettv->v_type = VAR_STRING;
1447
1448 if (sep != NULL)
1449 {
1450 ga_init2(&ga, (int)sizeof(char), 80);
1451 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1452 ga_append(&ga, NUL);
1453 rettv->vval.v_string = (char_u *)ga.ga_data;
1454 }
1455 else
1456 rettv->vval.v_string = NULL;
1457}
1458
1459/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001460 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001461 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001462 * Return OK or FAIL.
1463 */
1464 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001465eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001466{
Bram Moolenaar71478202020-06-26 22:46:27 +02001467 int evaluate = evalarg == NULL ? FALSE
1468 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001469 list_T *l = NULL;
1470 typval_T tv;
1471 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001472 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001473 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001474
1475 if (evaluate)
1476 {
1477 l = list_alloc();
1478 if (l == NULL)
1479 return FAIL;
1480 }
1481
Bram Moolenaar962d7212020-07-04 14:15:00 +02001482 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001483 while (**arg != ']' && **arg != NUL)
1484 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001485 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001486 goto failret;
1487 if (evaluate)
1488 {
1489 item = listitem_alloc();
1490 if (item != NULL)
1491 {
1492 item->li_tv = tv;
1493 item->li_tv.v_lock = 0;
1494 list_append(l, item);
1495 }
1496 else
1497 clear_tv(&tv);
1498 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001499 // Legacy Vim script allowed a space before the comma.
1500 if (!vim9script)
1501 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001502
Bram Moolenaare6e03172020-06-27 16:36:05 +02001503 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001504 had_comma = **arg == ',';
1505 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001506 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001507 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001508 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001509 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001510 goto failret;
1511 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001512 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001513 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001514
Bram Moolenaare6b53242020-07-01 17:28:33 +02001515 // The "]" can be on the next line. But a double quoted string may
1516 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001517 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001518 if (**arg == ']')
1519 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001520
1521 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001522 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001524 {
1525 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001526 semsg(_(e_no_white_space_allowed_before_str_str),
1527 ",", *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001528 else
1529 semsg(_("E696: Missing comma in List: %s"), *arg);
1530 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001531 goto failret;
1532 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001533 }
1534
1535 if (**arg != ']')
1536 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001538 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001539failret:
1540 if (evaluate)
1541 list_free(l);
1542 return FAIL;
1543 }
1544
Bram Moolenaar9d489562020-07-30 20:08:50 +02001545 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001546 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001547 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001548
1549 return OK;
1550}
1551
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001552/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001553 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001554 */
1555 int
1556write_list(FILE *fd, list_T *list, int binary)
1557{
1558 listitem_T *li;
1559 int c;
1560 int ret = OK;
1561 char_u *s;
1562
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001563 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001564 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001565 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001566 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001567 {
1568 if (*s == '\n')
1569 c = putc(NUL, fd);
1570 else
1571 c = putc(*s, fd);
1572 if (c == EOF)
1573 {
1574 ret = FAIL;
1575 break;
1576 }
1577 }
1578 if (!binary || li->li_next != NULL)
1579 if (putc('\n', fd) == EOF)
1580 {
1581 ret = FAIL;
1582 break;
1583 }
1584 if (ret == FAIL)
1585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001586 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001587 break;
1588 }
1589 }
1590 return ret;
1591}
1592
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001593/*
1594 * Initialize a static list with 10 items.
1595 */
1596 void
1597init_static_list(staticList10_T *sl)
1598{
1599 list_T *l = &sl->sl_list;
1600 int i;
1601
1602 memset(sl, 0, sizeof(staticList10_T));
1603 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001604 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001605 l->lv_refcount = DO_NOT_FREE_CNT;
1606 l->lv_lock = VAR_FIXED;
1607 sl->sl_list.lv_len = 10;
1608
1609 for (i = 0; i < 10; ++i)
1610 {
1611 listitem_T *li = &sl->sl_items[i];
1612
1613 if (i == 0)
1614 li->li_prev = NULL;
1615 else
1616 li->li_prev = li - 1;
1617 if (i == 9)
1618 li->li_next = NULL;
1619 else
1620 li->li_next = li + 1;
1621 }
1622}
1623
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001624/*
1625 * "list2str()" function
1626 */
1627 void
1628f_list2str(typval_T *argvars, typval_T *rettv)
1629{
1630 list_T *l;
1631 listitem_T *li;
1632 garray_T ga;
1633 int utf8 = FALSE;
1634
1635 rettv->v_type = VAR_STRING;
1636 rettv->vval.v_string = NULL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001637
1638 if (in_vim9script()
1639 && (check_for_list_arg(argvars, 0) == FAIL
1640 || check_for_opt_bool_arg(argvars, 1) == FAIL))
1641 return;
1642
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001643 if (argvars[0].v_type != VAR_LIST)
1644 {
1645 emsg(_(e_invarg));
1646 return;
1647 }
1648
1649 l = argvars[0].vval.v_list;
1650 if (l == NULL)
1651 return; // empty list results in empty string
1652
1653 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001654 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001655
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001656 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001657 ga_init2(&ga, 1, 80);
1658 if (has_mbyte || utf8)
1659 {
1660 char_u buf[MB_MAXBYTES + 1];
1661 int (*char2bytes)(int, char_u *);
1662
1663 if (utf8 || enc_utf8)
1664 char2bytes = utf_char2bytes;
1665 else
1666 char2bytes = mb_char2bytes;
1667
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001668 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001669 {
1670 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1671 ga_concat(&ga, buf);
1672 }
1673 ga_append(&ga, NUL);
1674 }
1675 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1676 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001677 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001678 ga_append(&ga, tv_get_number(&li->li_tv));
1679 ga_append(&ga, NUL);
1680 }
1681
1682 rettv->v_type = VAR_STRING;
1683 rettv->vval.v_string = ga.ga_data;
1684}
1685
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001686 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001687list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1688{
1689 list_T *l;
1690 listitem_T *item, *item2;
1691 listitem_T *li;
1692 int error = FALSE;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001693 long idx;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001694
1695 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001696 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001697 return;
1698
1699 idx = (long)tv_get_number_chk(&argvars[1], &error);
1700 if (error)
1701 ; // type error: do nothing, errmsg already given
1702 else if ((item = list_find(l, idx)) == NULL)
1703 semsg(_(e_listidx), idx);
1704 else
1705 {
1706 if (argvars[2].v_type == VAR_UNKNOWN)
1707 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001708 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001709 vimlist_remove(l, item, item);
1710 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001712 }
1713 else
1714 {
1715 // Remove range of items, return list with values.
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001716 long end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001717
1718 if (error)
1719 ; // type error: do nothing
1720 else if ((item2 = list_find(l, end)) == NULL)
1721 semsg(_(e_listidx), end);
1722 else
1723 {
1724 int cnt = 0;
1725
1726 for (li = item; li != NULL; li = li->li_next)
1727 {
1728 ++cnt;
1729 if (li == item2)
1730 break;
1731 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001732 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar108010a2021-06-27 22:03:33 +02001733 emsg(_(e_invalid_range));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001734 else
1735 {
1736 vimlist_remove(l, item, item2);
1737 if (rettv_list_alloc(rettv) == OK)
1738 {
Bram Moolenaar885971e2021-07-18 22:25:29 +02001739 list_T *rl = rettv->vval.v_list;
1740
1741 if (l->lv_with_items > 0)
1742 {
1743 // need to copy the list items and move the value
1744 while (item != NULL)
1745 {
1746 li = listitem_alloc();
1747 if (li == NULL)
1748 return;
1749 li->li_tv = item->li_tv;
1750 init_tv(&item->li_tv);
1751 list_append(rl, li);
1752 if (item == item2)
1753 break;
1754 item = item->li_next;
1755 }
1756 }
1757 else
1758 {
1759 rl->lv_first = item;
1760 rl->lv_u.mat.lv_last = item2;
1761 item->li_prev = NULL;
1762 item2->li_next = NULL;
1763 rl->lv_len = cnt;
1764 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001765 }
1766 }
1767 }
1768 }
1769 }
1770}
1771
1772static int item_compare(const void *s1, const void *s2);
1773static int item_compare2(const void *s1, const void *s2);
1774
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001775// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001776typedef struct
1777{
1778 listitem_T *item;
1779 int idx;
1780} sortItem_T;
1781
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001782// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001783typedef struct
1784{
1785 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001786 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001787 int item_compare_numeric;
1788 int item_compare_numbers;
1789#ifdef FEAT_FLOAT
1790 int item_compare_float;
1791#endif
1792 char_u *item_compare_func;
1793 partial_T *item_compare_partial;
1794 dict_T *item_compare_selfdict;
1795 int item_compare_func_err;
1796 int item_compare_keep_zero;
1797} sortinfo_T;
1798static sortinfo_T *sortinfo = NULL;
1799#define ITEM_COMPARE_FAIL 999
1800
1801/*
1802 * Compare functions for f_sort() and f_uniq() below.
1803 */
1804 static int
1805item_compare(const void *s1, const void *s2)
1806{
1807 sortItem_T *si1, *si2;
1808 typval_T *tv1, *tv2;
1809 char_u *p1, *p2;
1810 char_u *tofree1 = NULL, *tofree2 = NULL;
1811 int res;
1812 char_u numbuf1[NUMBUFLEN];
1813 char_u numbuf2[NUMBUFLEN];
1814
1815 si1 = (sortItem_T *)s1;
1816 si2 = (sortItem_T *)s2;
1817 tv1 = &si1->item->li_tv;
1818 tv2 = &si2->item->li_tv;
1819
1820 if (sortinfo->item_compare_numbers)
1821 {
1822 varnumber_T v1 = tv_get_number(tv1);
1823 varnumber_T v2 = tv_get_number(tv2);
1824
1825 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1826 }
1827
1828#ifdef FEAT_FLOAT
1829 if (sortinfo->item_compare_float)
1830 {
1831 float_T v1 = tv_get_float(tv1);
1832 float_T v2 = tv_get_float(tv2);
1833
1834 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1835 }
1836#endif
1837
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001838 // tv2string() puts quotes around a string and allocates memory. Don't do
1839 // that for string variables. Use a single quote when comparing with a
1840 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001841 if (tv1->v_type == VAR_STRING)
1842 {
1843 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1844 p1 = (char_u *)"'";
1845 else
1846 p1 = tv1->vval.v_string;
1847 }
1848 else
1849 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1850 if (tv2->v_type == VAR_STRING)
1851 {
1852 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1853 p2 = (char_u *)"'";
1854 else
1855 p2 = tv2->vval.v_string;
1856 }
1857 else
1858 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1859 if (p1 == NULL)
1860 p1 = (char_u *)"";
1861 if (p2 == NULL)
1862 p2 = (char_u *)"";
1863 if (!sortinfo->item_compare_numeric)
1864 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001865 if (sortinfo->item_compare_lc)
1866 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001867 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001868 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001869 }
1870 else
1871 {
1872 double n1, n2;
1873 n1 = strtod((char *)p1, (char **)&p1);
1874 n2 = strtod((char *)p2, (char **)&p2);
1875 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1876 }
1877
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001878 // When the result would be zero, compare the item indexes. Makes the
1879 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001880 if (res == 0 && !sortinfo->item_compare_keep_zero)
1881 res = si1->idx > si2->idx ? 1 : -1;
1882
1883 vim_free(tofree1);
1884 vim_free(tofree2);
1885 return res;
1886}
1887
1888 static int
1889item_compare2(const void *s1, const void *s2)
1890{
1891 sortItem_T *si1, *si2;
1892 int res;
1893 typval_T rettv;
1894 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001895 char_u *func_name;
1896 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001897 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001898
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001899 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001900 if (sortinfo->item_compare_func_err)
1901 return 0;
1902
1903 si1 = (sortItem_T *)s1;
1904 si2 = (sortItem_T *)s2;
1905
1906 if (partial == NULL)
1907 func_name = sortinfo->item_compare_func;
1908 else
1909 func_name = partial_name(partial);
1910
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001911 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1912 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001913 copy_tv(&si1->item->li_tv, &argv[0]);
1914 copy_tv(&si2->item->li_tv, &argv[1]);
1915
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001916 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001917 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001918 funcexe.evaluate = TRUE;
1919 funcexe.partial = partial;
1920 funcexe.selfdict = sortinfo->item_compare_selfdict;
1921 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001922 clear_tv(&argv[0]);
1923 clear_tv(&argv[1]);
1924
1925 if (res == FAIL)
1926 res = ITEM_COMPARE_FAIL;
1927 else
1928 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1929 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001930 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001931 clear_tv(&rettv);
1932
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001933 // When the result would be zero, compare the pointers themselves. Makes
1934 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001935 if (res == 0 && !sortinfo->item_compare_keep_zero)
1936 res = si1->idx > si2->idx ? 1 : -1;
1937
1938 return res;
1939}
1940
1941/*
1942 * "sort()" or "uniq()" function
1943 */
1944 static void
1945do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1946{
1947 list_T *l;
1948 listitem_T *li;
1949 sortItem_T *ptrs;
1950 sortinfo_T *old_sortinfo;
1951 sortinfo_T info;
1952 long len;
1953 long i;
1954
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001955 if (in_vim9script()
1956 && (check_for_list_arg(argvars, 0) == FAIL
1957 || (argvars[1].v_type != VAR_UNKNOWN
1958 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
1959 return;
1960
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001961 // Pointer to current info struct used in compare function. Save and
1962 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001963 old_sortinfo = sortinfo;
1964 sortinfo = &info;
1965
1966 if (argvars[0].v_type != VAR_LIST)
1967 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1968 else
1969 {
1970 l = argvars[0].vval.v_list;
Bram Moolenaara187c432020-09-16 21:08:28 +02001971 if (l == NULL || value_check_lock(l->lv_lock,
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001972 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1973 TRUE))
1974 goto theend;
1975 rettv_list_set(rettv, l);
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001976 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001977
1978 len = list_len(l);
1979 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001980 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001981
1982 info.item_compare_ic = FALSE;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001983 info.item_compare_lc = FALSE;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001984 info.item_compare_numeric = FALSE;
1985 info.item_compare_numbers = FALSE;
1986#ifdef FEAT_FLOAT
1987 info.item_compare_float = FALSE;
1988#endif
1989 info.item_compare_func = NULL;
1990 info.item_compare_partial = NULL;
1991 info.item_compare_selfdict = NULL;
1992 if (argvars[1].v_type != VAR_UNKNOWN)
1993 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001994 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001995 if (argvars[1].v_type == VAR_FUNC)
1996 info.item_compare_func = argvars[1].vval.v_string;
1997 else if (argvars[1].v_type == VAR_PARTIAL)
1998 info.item_compare_partial = argvars[1].vval.v_partial;
1999 else
2000 {
2001 int error = FALSE;
Bram Moolenaar08e51f42020-09-16 23:23:36 +02002002 int nr = 0;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002003
Bram Moolenaar08e51f42020-09-16 23:23:36 +02002004 if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002005 {
Bram Moolenaar08e51f42020-09-16 23:23:36 +02002006 nr = tv_get_number_chk(&argvars[1], &error);
2007 if (error)
2008 goto theend; // type error; errmsg already given
2009 if (nr == 1)
2010 info.item_compare_ic = TRUE;
2011 }
2012 if (nr != 1)
2013 {
2014 if (argvars[1].v_type != VAR_NUMBER)
2015 info.item_compare_func = tv_get_string(&argvars[1]);
2016 else if (nr != 0)
2017 {
2018 emsg(_(e_invarg));
2019 goto theend;
2020 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002021 }
2022 if (info.item_compare_func != NULL)
2023 {
2024 if (*info.item_compare_func == NUL)
2025 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002026 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002027 info.item_compare_func = NULL;
2028 }
2029 else if (STRCMP(info.item_compare_func, "n") == 0)
2030 {
2031 info.item_compare_func = NULL;
2032 info.item_compare_numeric = TRUE;
2033 }
2034 else if (STRCMP(info.item_compare_func, "N") == 0)
2035 {
2036 info.item_compare_func = NULL;
2037 info.item_compare_numbers = TRUE;
2038 }
2039#ifdef FEAT_FLOAT
2040 else if (STRCMP(info.item_compare_func, "f") == 0)
2041 {
2042 info.item_compare_func = NULL;
2043 info.item_compare_float = TRUE;
2044 }
2045#endif
2046 else if (STRCMP(info.item_compare_func, "i") == 0)
2047 {
2048 info.item_compare_func = NULL;
2049 info.item_compare_ic = TRUE;
2050 }
Bram Moolenaar55e29612020-11-01 13:57:44 +01002051 else if (STRCMP(info.item_compare_func, "l") == 0)
2052 {
2053 info.item_compare_func = NULL;
2054 info.item_compare_lc = TRUE;
2055 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002056 }
2057 }
2058
2059 if (argvars[2].v_type != VAR_UNKNOWN)
2060 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002061 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002062 if (argvars[2].v_type != VAR_DICT)
2063 {
2064 emsg(_(e_dictreq));
2065 goto theend;
2066 }
2067 info.item_compare_selfdict = argvars[2].vval.v_dict;
2068 }
2069 }
2070
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002071 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002072 ptrs = ALLOC_MULT(sortItem_T, len);
2073 if (ptrs == NULL)
2074 goto theend;
2075
2076 i = 0;
2077 if (sort)
2078 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002079 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002080 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002081 {
2082 ptrs[i].item = li;
2083 ptrs[i].idx = i;
2084 ++i;
2085 }
2086
2087 info.item_compare_func_err = FALSE;
2088 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002089 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002090 if ((info.item_compare_func != NULL
2091 || info.item_compare_partial != NULL)
2092 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
2093 == ITEM_COMPARE_FAIL)
2094 emsg(_("E702: Sort compare function failed"));
2095 else
2096 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002097 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002098 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
2099 info.item_compare_func == NULL
2100 && info.item_compare_partial == NULL
2101 ? item_compare : item_compare2);
2102
2103 if (!info.item_compare_func_err)
2104 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002105 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002106 l->lv_first = l->lv_u.mat.lv_last
2107 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002108 l->lv_len = 0;
2109 for (i = 0; i < len; ++i)
2110 list_append(l, ptrs[i].item);
2111 }
2112 }
2113 }
2114 else
2115 {
2116 int (*item_compare_func_ptr)(const void *, const void *);
2117
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002118 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002119 info.item_compare_func_err = FALSE;
2120 info.item_compare_keep_zero = TRUE;
2121 item_compare_func_ptr = info.item_compare_func != NULL
2122 || info.item_compare_partial != NULL
2123 ? item_compare2 : item_compare;
2124
2125 for (li = l->lv_first; li != NULL && li->li_next != NULL;
2126 li = li->li_next)
2127 {
2128 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
2129 == 0)
2130 ptrs[i++].item = li;
2131 if (info.item_compare_func_err)
2132 {
2133 emsg(_("E882: Uniq compare function failed"));
2134 break;
2135 }
2136 }
2137
2138 if (!info.item_compare_func_err)
2139 {
2140 while (--i >= 0)
2141 {
2142 li = ptrs[i].item->li_next;
2143 ptrs[i].item->li_next = li->li_next;
2144 if (li->li_next != NULL)
2145 li->li_next->li_prev = ptrs[i].item;
2146 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002147 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002148 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002150 l->lv_len--;
2151 }
2152 }
2153 }
2154
2155 vim_free(ptrs);
2156 }
2157theend:
2158 sortinfo = old_sortinfo;
2159}
2160
2161/*
2162 * "sort({list})" function
2163 */
2164 void
2165f_sort(typval_T *argvars, typval_T *rettv)
2166{
2167 do_sort_uniq(argvars, rettv, TRUE);
2168}
2169
2170/*
2171 * "uniq({list})" function
2172 */
2173 void
2174f_uniq(typval_T *argvars, typval_T *rettv)
2175{
2176 do_sort_uniq(argvars, rettv, FALSE);
2177}
2178
Bram Moolenaarea696852020-11-09 18:31:39 +01002179typedef enum {
2180 FILTERMAP_FILTER,
2181 FILTERMAP_MAP,
2182 FILTERMAP_MAPNEW
2183} filtermap_T;
2184
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002185/*
2186 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01002187 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002188 */
2189 static int
Bram Moolenaarea696852020-11-09 18:31:39 +01002190filter_map_one(
2191 typval_T *tv, // original value
2192 typval_T *expr, // callback
2193 filtermap_T filtermap,
2194 typval_T *newtv, // for map() and mapnew(): new value
2195 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002196{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002197 typval_T argv[3];
2198 int retval = FAIL;
2199
2200 copy_tv(tv, get_vim_var_tv(VV_VAL));
2201 argv[0] = *get_vim_var_tv(VV_KEY);
2202 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01002203 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002204 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002205 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002206 {
2207 int error = FALSE;
2208
2209 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02002210 if (in_vim9script())
Bram Moolenaarea696852020-11-09 18:31:39 +01002211 *remp = !tv2bool(newtv);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002212 else
Bram Moolenaarea696852020-11-09 18:31:39 +01002213 *remp = (tv_get_number_chk(newtv, &error) == 0);
2214 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002215 // On type error, nothing has been removed; return FAIL to stop the
2216 // loop. The error message was given by tv_get_number_chk().
2217 if (error)
2218 goto theend;
2219 }
2220 retval = OK;
2221theend:
2222 clear_tv(get_vim_var_tv(VV_VAL));
2223 return retval;
2224}
2225
2226/*
2227 * Implementation of map() and filter().
2228 */
2229 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01002230filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002231{
2232 typval_T *expr;
2233 listitem_T *li, *nli;
2234 list_T *l = NULL;
2235 dictitem_T *di;
2236 hashtab_T *ht;
2237 hashitem_T *hi;
2238 dict_T *d = NULL;
2239 blob_T *b = NULL;
2240 int rem;
2241 int todo;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002242 char *func_name = filtermap == FILTERMAP_MAP ? "map()"
Bram Moolenaarea696852020-11-09 18:31:39 +01002243 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002244 : "filter()";
Bram Moolenaarea696852020-11-09 18:31:39 +01002245 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2246 ? N_("map() argument")
2247 : filtermap == FILTERMAP_MAPNEW
2248 ? N_("mapnew() argument")
2249 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002250 int save_did_emsg;
2251 int idx = 0;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002252 type_T *type = NULL;
2253 garray_T type_list;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002254
Bram Moolenaarea696852020-11-09 18:31:39 +01002255 // map() and filter() return the first argument, also on failure.
2256 if (filtermap != FILTERMAP_MAPNEW)
2257 copy_tv(&argvars[0], rettv);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002258
2259 if (in_vim9script()
2260 && (check_for_list_or_dict_or_blob_arg(argvars, 0) == FAIL))
2261 return;
2262
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002263 if (filtermap == FILTERMAP_MAP && in_vim9script())
2264 {
2265 // Check that map() does not change the type of the dict.
2266 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaarf2253962021-04-13 20:53:13 +02002267 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002268 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002269
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002270 if (argvars[0].v_type == VAR_BLOB)
2271 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002272 if (filtermap == FILTERMAP_MAPNEW)
2273 {
2274 rettv->v_type = VAR_BLOB;
2275 rettv->vval.v_blob = NULL;
2276 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002277 if ((b = argvars[0].vval.v_blob) == NULL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002278 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002279 }
2280 else if (argvars[0].v_type == VAR_LIST)
2281 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002282 if (filtermap == FILTERMAP_MAPNEW)
2283 {
2284 rettv->v_type = VAR_LIST;
2285 rettv->vval.v_list = NULL;
2286 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002287 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002288 || (filtermap == FILTERMAP_FILTER
2289 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002290 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002291 }
2292 else if (argvars[0].v_type == VAR_DICT)
2293 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002294 if (filtermap == FILTERMAP_MAPNEW)
2295 {
2296 rettv->v_type = VAR_DICT;
2297 rettv->vval.v_dict = NULL;
2298 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002299 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002300 || (filtermap == FILTERMAP_FILTER
2301 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002302 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002303 }
2304 else
2305 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002306 semsg(_(e_listdictblobarg), func_name);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002307 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002308 }
2309
2310 expr = &argvars[1];
2311 // On type errors, the preceding call has already displayed an error
2312 // message. Avoid a misleading error message for an empty string that
2313 // was not passed as argument.
2314 if (expr->v_type != VAR_UNKNOWN)
2315 {
2316 typval_T save_val;
2317 typval_T save_key;
2318
2319 prepare_vimvar(VV_VAL, &save_val);
2320 prepare_vimvar(VV_KEY, &save_key);
2321
2322 // We reset "did_emsg" to be able to detect whether an error
2323 // occurred during evaluation of the expression.
2324 save_did_emsg = did_emsg;
2325 did_emsg = FALSE;
2326
2327 if (argvars[0].v_type == VAR_DICT)
2328 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002329 int prev_lock = d->dv_lock;
Bram Moolenaarea696852020-11-09 18:31:39 +01002330 dict_T *d_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002331
Bram Moolenaarea696852020-11-09 18:31:39 +01002332 if (filtermap == FILTERMAP_MAPNEW)
2333 {
2334 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002335 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002336 d_ret = rettv->vval.v_dict;
2337 }
2338
2339 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002340 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002341 ht = &d->dv_hashtab;
2342 hash_lock(ht);
2343 todo = (int)ht->ht_used;
2344 for (hi = ht->ht_array; todo > 0; ++hi)
2345 {
2346 if (!HASHITEM_EMPTY(hi))
2347 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002348 int r;
2349 typval_T newtv;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002350
2351 --todo;
2352 di = HI2DI(hi);
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002353 if (filtermap == FILTERMAP_MAP
Bram Moolenaarea696852020-11-09 18:31:39 +01002354 && (value_check_lock(di->di_tv.v_lock,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002355 arg_errmsg, TRUE)
2356 || var_check_ro(di->di_flags,
2357 arg_errmsg, TRUE)))
2358 break;
2359 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaar027c4ab2021-02-21 16:20:18 +01002360 newtv.v_type = VAR_UNKNOWN;
Bram Moolenaarea696852020-11-09 18:31:39 +01002361 r = filter_map_one(&di->di_tv, expr, filtermap,
2362 &newtv, &rem);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002363 clear_tv(get_vim_var_tv(VV_KEY));
2364 if (r == FAIL || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002365 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002366 clear_tv(&newtv);
2367 break;
2368 }
2369 if (filtermap == FILTERMAP_MAP)
2370 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002371 if (type != NULL && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002372 type->tt_member, &newtv,
2373 func_name, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002374 {
2375 clear_tv(&newtv);
2376 break;
2377 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002378 // map(): replace the dict item value
2379 clear_tv(&di->di_tv);
2380 newtv.v_lock = 0;
2381 di->di_tv = newtv;
2382 }
2383 else if (filtermap == FILTERMAP_MAPNEW)
2384 {
2385 // mapnew(): add the item value to the new dict
2386 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
2387 clear_tv(&newtv);
2388 if (r == FAIL)
2389 break;
2390 }
2391 else if (filtermap == FILTERMAP_FILTER && rem)
2392 {
2393 // filter(false): remove the item from the dict
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002394 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2395 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2396 break;
2397 dictitem_remove(d, di);
2398 }
2399 }
2400 }
2401 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002402 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002403 }
2404 else if (argvars[0].v_type == VAR_BLOB)
2405 {
2406 int i;
2407 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002408 varnumber_T val;
Bram Moolenaarea696852020-11-09 18:31:39 +01002409 blob_T *b_ret = b;
2410
2411 if (filtermap == FILTERMAP_MAPNEW)
2412 {
2413 if (blob_copy(b, rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002414 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002415 b_ret = rettv->vval.v_blob;
2416 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002417
2418 // set_vim_var_nr() doesn't set the type
2419 set_vim_var_type(VV_KEY, VAR_NUMBER);
2420
2421 for (i = 0; i < b->bv_ga.ga_len; i++)
2422 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002423 typval_T newtv;
2424
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002425 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002426 val = blob_get(b, i);
2427 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002428 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaarea696852020-11-09 18:31:39 +01002429 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
2430 || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002431 break;
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002432 if (newtv.v_type != VAR_NUMBER && newtv.v_type != VAR_BOOL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002433 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002434 clear_tv(&newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002435 emsg(_(e_invalblob));
2436 break;
2437 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002438 if (filtermap != FILTERMAP_FILTER)
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002439 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002440 if (newtv.vval.v_number != val)
2441 blob_set(b_ret, i, newtv.vval.v_number);
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002442 }
2443 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002444 {
2445 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2446
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002447 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002448 (size_t)b->bv_ga.ga_len - i - 1);
2449 --b->bv_ga.ga_len;
2450 --i;
2451 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002452 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002453 }
2454 }
2455 else // argvars[0].v_type == VAR_LIST
2456 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002457 int prev_lock = l->lv_lock;
2458 list_T *l_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002459
Bram Moolenaarea696852020-11-09 18:31:39 +01002460 if (filtermap == FILTERMAP_MAPNEW)
2461 {
2462 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002463 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002464 l_ret = rettv->vval.v_list;
2465 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002466 // set_vim_var_nr() doesn't set the type
2467 set_vim_var_type(VV_KEY, VAR_NUMBER);
2468
Bram Moolenaarea696852020-11-09 18:31:39 +01002469 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002470 l->lv_lock = VAR_LOCKED;
Bram Moolenaarea696852020-11-09 18:31:39 +01002471
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002472 if (l->lv_first == &range_list_item)
2473 {
2474 varnumber_T val = l->lv_u.nonmat.lv_start;
2475 int len = l->lv_len;
2476 int stride = l->lv_u.nonmat.lv_stride;
2477
2478 // List from range(): loop over the numbers
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002479 if (filtermap != FILTERMAP_MAPNEW)
2480 {
2481 l->lv_first = NULL;
2482 l->lv_u.mat.lv_last = NULL;
2483 l->lv_len = 0;
2484 l->lv_u.mat.lv_idx_item = NULL;
2485 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002486
2487 for (idx = 0; idx < len; ++idx)
Bram Moolenaarc56936e2020-11-10 11:43:56 +01002488 {
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002489 typval_T tv;
2490 typval_T newtv;
2491
2492 tv.v_type = VAR_NUMBER;
2493 tv.v_lock = 0;
2494 tv.vval.v_number = val;
2495 set_vim_var_nr(VV_KEY, idx);
2496 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2497 == FAIL)
Bram Moolenaarea696852020-11-09 18:31:39 +01002498 break;
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002499 if (did_emsg)
2500 {
2501 clear_tv(&newtv);
2502 break;
2503 }
2504 if (filtermap != FILTERMAP_FILTER)
2505 {
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002506 if (filtermap == FILTERMAP_MAP && type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002507 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002508 type->tt_member, &newtv,
2509 func_name, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002510 {
2511 clear_tv(&newtv);
2512 break;
2513 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002514 // map(), mapnew(): always append the new value to the
2515 // list
2516 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2517 ? l : l_ret, &newtv) == FAIL)
2518 break;
2519 }
2520 else if (!rem)
2521 {
2522 // filter(): append the list item value when not rem
2523 if (list_append_tv_move(l, &tv) == FAIL)
2524 break;
2525 }
2526
2527 val += stride;
Bram Moolenaarea696852020-11-09 18:31:39 +01002528 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002529 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002530 else
2531 {
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002532 // Materialized list: loop over the items
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002533 for (li = l->lv_first; li != NULL; li = nli)
2534 {
2535 typval_T newtv;
2536
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002537 if (filtermap == FILTERMAP_MAP && value_check_lock(
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002538 li->li_tv.v_lock, arg_errmsg, TRUE))
2539 break;
2540 nli = li->li_next;
2541 set_vim_var_nr(VV_KEY, idx);
2542 if (filter_map_one(&li->li_tv, expr, filtermap,
2543 &newtv, &rem) == FAIL)
2544 break;
2545 if (did_emsg)
2546 {
2547 clear_tv(&newtv);
2548 break;
2549 }
2550 if (filtermap == FILTERMAP_MAP)
2551 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002552 if (type != NULL && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002553 type->tt_member, &newtv, func_name, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002554 {
2555 clear_tv(&newtv);
2556 break;
2557 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002558 // map(): replace the list item value
2559 clear_tv(&li->li_tv);
2560 newtv.v_lock = 0;
2561 li->li_tv = newtv;
2562 }
2563 else if (filtermap == FILTERMAP_MAPNEW)
2564 {
2565 // mapnew(): append the list item value
2566 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2567 break;
2568 }
2569 else if (filtermap == FILTERMAP_FILTER && rem)
2570 listitem_remove(l, li);
2571 ++idx;
2572 }
2573 }
2574
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002575 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002576 }
2577
2578 restore_vimvar(VV_KEY, &save_key);
2579 restore_vimvar(VV_VAL, &save_val);
2580
2581 did_emsg |= save_did_emsg;
2582 }
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002583
2584theend:
2585 if (type != NULL)
2586 clear_type_list(&type_list);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002587}
2588
2589/*
2590 * "filter()" function
2591 */
2592 void
2593f_filter(typval_T *argvars, typval_T *rettv)
2594{
Bram Moolenaarea696852020-11-09 18:31:39 +01002595 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002596}
2597
2598/*
2599 * "map()" function
2600 */
2601 void
2602f_map(typval_T *argvars, typval_T *rettv)
2603{
Bram Moolenaarea696852020-11-09 18:31:39 +01002604 filter_map(argvars, rettv, FILTERMAP_MAP);
2605}
2606
2607/*
2608 * "mapnew()" function
2609 */
2610 void
2611f_mapnew(typval_T *argvars, typval_T *rettv)
2612{
2613 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002614}
2615
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002616/*
2617 * "add(list, item)" function
2618 */
2619 void
2620f_add(typval_T *argvars, typval_T *rettv)
2621{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002622 rettv->vval.v_number = 1; // Default: Failed
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002623
2624 if (in_vim9script()
2625 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2626 || (argvars[0].v_type == VAR_BLOB
2627 && check_for_number_arg(argvars, 1) == FAIL)))
2628 return;
2629
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002630 if (argvars[0].v_type == VAR_LIST)
2631 {
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002632 list_T *l = argvars[0].vval.v_list;
2633
2634 if (l == NULL)
2635 {
2636 if (in_vim9script())
2637 emsg(_(e_cannot_add_to_null_list));
2638 }
2639 else if (!value_check_lock(l->lv_lock,
2640 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002641 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002642 {
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002643 copy_tv(&argvars[0], rettv);
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002644 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002645 }
2646 else if (argvars[0].v_type == VAR_BLOB)
2647 {
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002648 blob_T *b = argvars[0].vval.v_blob;
2649
2650 if (b == NULL)
2651 {
2652 if (in_vim9script())
2653 emsg(_(e_cannot_add_to_null_blob));
2654 }
2655 else if (!value_check_lock(b->bv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002656 (char_u *)N_("add() argument"), TRUE))
2657 {
2658 int error = FALSE;
2659 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2660
2661 if (!error)
2662 {
2663 ga_append(&b->bv_ga, (int)n);
2664 copy_tv(&argvars[0], rettv);
2665 }
2666 }
2667 }
2668 else
2669 emsg(_(e_listblobreq));
2670}
2671
2672/*
2673 * "count()" function
2674 */
2675 void
2676f_count(typval_T *argvars, typval_T *rettv)
2677{
2678 long n = 0;
2679 int ic = FALSE;
2680 int error = FALSE;
2681
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002682 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002683 && (check_for_string_or_list_or_dict_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002684 || check_for_opt_bool_arg(argvars, 2) == FAIL
2685 || (argvars[2].v_type != VAR_UNKNOWN
2686 && check_for_opt_number_arg(argvars, 3) == FAIL)))
2687 return;
2688
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002689 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002690 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002691
2692 if (argvars[0].v_type == VAR_STRING)
2693 {
2694 char_u *expr = tv_get_string_chk(&argvars[1]);
2695 char_u *p = argvars[0].vval.v_string;
2696 char_u *next;
2697
2698 if (!error && expr != NULL && *expr != NUL && p != NULL)
2699 {
2700 if (ic)
2701 {
2702 size_t len = STRLEN(expr);
2703
2704 while (*p != NUL)
2705 {
2706 if (MB_STRNICMP(p, expr, len) == 0)
2707 {
2708 ++n;
2709 p += len;
2710 }
2711 else
2712 MB_PTR_ADV(p);
2713 }
2714 }
2715 else
2716 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2717 != NULL)
2718 {
2719 ++n;
2720 p = next + STRLEN(expr);
2721 }
2722 }
2723
2724 }
2725 else if (argvars[0].v_type == VAR_LIST)
2726 {
2727 listitem_T *li;
2728 list_T *l;
2729 long idx;
2730
2731 if ((l = argvars[0].vval.v_list) != NULL)
2732 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002733 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002734 li = l->lv_first;
2735 if (argvars[2].v_type != VAR_UNKNOWN)
2736 {
2737 if (argvars[3].v_type != VAR_UNKNOWN)
2738 {
2739 idx = (long)tv_get_number_chk(&argvars[3], &error);
2740 if (!error)
2741 {
2742 li = list_find(l, idx);
2743 if (li == NULL)
2744 semsg(_(e_listidx), idx);
2745 }
2746 }
2747 if (error)
2748 li = NULL;
2749 }
2750
2751 for ( ; li != NULL; li = li->li_next)
2752 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2753 ++n;
2754 }
2755 }
2756 else if (argvars[0].v_type == VAR_DICT)
2757 {
2758 int todo;
2759 dict_T *d;
2760 hashitem_T *hi;
2761
2762 if ((d = argvars[0].vval.v_dict) != NULL)
2763 {
2764 if (argvars[2].v_type != VAR_UNKNOWN)
2765 {
2766 if (argvars[3].v_type != VAR_UNKNOWN)
2767 emsg(_(e_invarg));
2768 }
2769
2770 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2771 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2772 {
2773 if (!HASHITEM_EMPTY(hi))
2774 {
2775 --todo;
2776 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2777 ++n;
2778 }
2779 }
2780 }
2781 }
2782 else
2783 semsg(_(e_listdictarg), "count()");
2784 rettv->vval.v_number = n;
2785}
2786
2787/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002788 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002789 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002790 static void
2791extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002792{
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002793 type_T *type = NULL;
2794 garray_T type_list;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002795 char *func_name = is_new ? "extendnew()" : "extend()";
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002796
2797 if (!is_new && in_vim9script())
2798 {
2799 // Check that map() does not change the type of the dict.
2800 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaarf2253962021-04-13 20:53:13 +02002801 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002802 }
2803
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002804 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2805 {
2806 list_T *l1, *l2;
2807 listitem_T *item;
2808 long before;
2809 int error = FALSE;
2810
2811 l1 = argvars[0].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002812 if (l1 == NULL)
2813 {
2814 emsg(_(e_cannot_extend_null_list));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002815 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002816 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002817 l2 = argvars[1].vval.v_list;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002818 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
2819 && l2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002820 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002821 if (is_new)
2822 {
2823 l1 = list_copy(l1, FALSE, get_copyID());
2824 if (l1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002825 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002826 }
2827
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002828 if (argvars[2].v_type != VAR_UNKNOWN)
2829 {
2830 before = (long)tv_get_number_chk(&argvars[2], &error);
2831 if (error)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002832 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002833
2834 if (before == l1->lv_len)
2835 item = NULL;
2836 else
2837 {
2838 item = list_find(l1, before);
2839 if (item == NULL)
2840 {
2841 semsg(_(e_listidx), before);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002842 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002843 }
2844 }
2845 }
2846 else
2847 item = NULL;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002848 if (type != NULL && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002849 type, &argvars[1], func_name, 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002850 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002851 list_extend(l1, l2, item);
2852
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002853 if (is_new)
2854 {
2855 rettv->v_type = VAR_LIST;
2856 rettv->vval.v_list = l1;
2857 rettv->v_lock = FALSE;
2858 }
2859 else
2860 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002861 }
2862 }
2863 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2864 {
2865 dict_T *d1, *d2;
2866 char_u *action;
2867 int i;
2868
2869 d1 = argvars[0].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002870 if (d1 == NULL)
2871 {
2872 emsg(_(e_cannot_extend_null_dict));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002873 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002874 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002875 d2 = argvars[1].vval.v_dict;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002876 if ((is_new || !value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
2877 && d2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002878 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002879 if (is_new)
2880 {
2881 d1 = dict_copy(d1, FALSE, get_copyID());
2882 if (d1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002883 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002884 }
2885
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002886 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002887 if (argvars[2].v_type != VAR_UNKNOWN)
2888 {
2889 static char *(av[]) = {"keep", "force", "error"};
2890
2891 action = tv_get_string_chk(&argvars[2]);
2892 if (action == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002893 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002894 for (i = 0; i < 3; ++i)
2895 if (STRCMP(action, av[i]) == 0)
2896 break;
2897 if (i == 3)
2898 {
2899 semsg(_(e_invarg2), action);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002900 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002901 }
2902 }
2903 else
2904 action = (char_u *)"force";
2905
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002906 if (type != NULL && check_typval_arg_type(type, &argvars[1],
2907 func_name, 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002908 goto theend;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002909 dict_extend(d1, d2, action, func_name);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002910
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002911 if (is_new)
2912 {
2913 rettv->v_type = VAR_DICT;
2914 rettv->vval.v_dict = d1;
2915 rettv->v_lock = FALSE;
2916 }
2917 else
2918 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002919 }
2920 }
2921 else
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002922 semsg(_(e_listdictarg), func_name);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002923
2924theend:
2925 if (type != NULL)
2926 clear_type_list(&type_list);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002927}
2928
2929/*
2930 * "extend(list, list [, idx])" function
2931 * "extend(dict, dict [, action])" function
2932 */
2933 void
2934f_extend(typval_T *argvars, typval_T *rettv)
2935{
2936 char_u *errmsg = (char_u *)N_("extend() argument");
2937
2938 extend(argvars, rettv, errmsg, FALSE);
2939}
2940
2941/*
2942 * "extendnew(list, list [, idx])" function
2943 * "extendnew(dict, dict [, action])" function
2944 */
2945 void
2946f_extendnew(typval_T *argvars, typval_T *rettv)
2947{
2948 char_u *errmsg = (char_u *)N_("extendnew() argument");
2949
2950 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002951}
2952
2953/*
2954 * "insert()" function
2955 */
2956 void
2957f_insert(typval_T *argvars, typval_T *rettv)
2958{
2959 long before = 0;
2960 listitem_T *item;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002961 int error = FALSE;
2962
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002963 if (in_vim9script()
2964 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2965 || (argvars[0].v_type == VAR_BLOB
2966 && check_for_number_arg(argvars, 1) == FAIL)
2967 || check_for_opt_number_arg(argvars, 2) == FAIL))
2968 return;
2969
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002970 if (argvars[0].v_type == VAR_BLOB)
2971 {
Sean Dewar80d73952021-08-04 19:25:54 +02002972 blob_T *b = argvars[0].vval.v_blob;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002973
Sean Dewar80d73952021-08-04 19:25:54 +02002974 if (b == NULL)
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002975 {
2976 if (in_vim9script())
2977 emsg(_(e_cannot_add_to_null_blob));
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002978 }
Sean Dewar80d73952021-08-04 19:25:54 +02002979 else if (!value_check_lock(b->bv_lock,
2980 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002981 {
Sean Dewar80d73952021-08-04 19:25:54 +02002982 int val, len;
2983 char_u *p;
2984
2985 len = blob_len(b);
2986 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002987 {
Sean Dewar80d73952021-08-04 19:25:54 +02002988 before = (long)tv_get_number_chk(&argvars[2], &error);
2989 if (error)
2990 return; // type error; errmsg already given
2991 if (before < 0 || before > len)
2992 {
2993 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2994 return;
2995 }
2996 }
2997 val = tv_get_number_chk(&argvars[1], &error);
2998 if (error)
2999 return;
3000 if (val < 0 || val > 255)
3001 {
3002 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003003 return;
3004 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003005
Sean Dewar80d73952021-08-04 19:25:54 +02003006 if (ga_grow(&b->bv_ga, 1) == FAIL)
3007 return;
3008 p = (char_u *)b->bv_ga.ga_data;
3009 mch_memmove(p + before + 1, p + before, (size_t)len - before);
3010 *(p + before) = val;
3011 ++b->bv_ga.ga_len;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003012
Sean Dewar80d73952021-08-04 19:25:54 +02003013 copy_tv(&argvars[0], rettv);
3014 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003015 }
3016 else if (argvars[0].v_type != VAR_LIST)
3017 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar39211cb2021-04-18 15:48:04 +02003018 else
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003019 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02003020 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003021
Bram Moolenaar39211cb2021-04-18 15:48:04 +02003022 if (l == NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003023 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02003024 if (in_vim9script())
3025 emsg(_(e_cannot_add_to_null_list));
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003026 }
Bram Moolenaar39211cb2021-04-18 15:48:04 +02003027 else if (!value_check_lock(l->lv_lock,
3028 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003029 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02003030 if (argvars[2].v_type != VAR_UNKNOWN)
3031 before = (long)tv_get_number_chk(&argvars[2], &error);
3032 if (error)
3033 return; // type error; errmsg already given
3034
3035 if (before == l->lv_len)
3036 item = NULL;
3037 else
3038 {
3039 item = list_find(l, before);
3040 if (item == NULL)
3041 {
3042 semsg(_(e_listidx), before);
3043 l = NULL;
3044 }
3045 }
3046 if (l != NULL)
3047 {
3048 (void)list_insert_tv(l, &argvars[1], item);
3049 copy_tv(&argvars[0], rettv);
3050 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003051 }
3052 }
3053}
3054
3055/*
3056 * "remove()" function
3057 */
3058 void
3059f_remove(typval_T *argvars, typval_T *rettv)
3060{
3061 char_u *arg_errmsg = (char_u *)N_("remove() argument");
3062
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02003063 if (in_vim9script()
3064 && (check_for_list_or_dict_or_blob_arg(argvars, 0) == FAIL
3065 || ((argvars[0].v_type == VAR_LIST
3066 || argvars[0].v_type == VAR_BLOB)
3067 && (check_for_number_arg(argvars, 1) == FAIL
3068 || check_for_opt_number_arg(argvars, 2) == FAIL))
3069 || (argvars[0].v_type == VAR_DICT
3070 && check_for_string_or_number_arg(argvars, 1) == FAIL)))
3071 return;
3072
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003073 if (argvars[0].v_type == VAR_DICT)
3074 dict_remove(argvars, rettv, arg_errmsg);
3075 else if (argvars[0].v_type == VAR_BLOB)
Sean Dewar80d73952021-08-04 19:25:54 +02003076 blob_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003077 else if (argvars[0].v_type == VAR_LIST)
3078 list_remove(argvars, rettv, arg_errmsg);
3079 else
3080 semsg(_(e_listdictblobarg), "remove()");
3081}
3082
3083/*
3084 * "reverse({list})" function
3085 */
3086 void
3087f_reverse(typval_T *argvars, typval_T *rettv)
3088{
3089 list_T *l;
3090 listitem_T *li, *ni;
3091
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02003092 if (in_vim9script() && check_for_list_or_blob_arg(argvars, 0) == FAIL)
3093 return;
3094
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003095 if (argvars[0].v_type == VAR_BLOB)
3096 {
3097 blob_T *b = argvars[0].vval.v_blob;
3098 int i, len = blob_len(b);
3099
3100 for (i = 0; i < len / 2; i++)
3101 {
3102 int tmp = blob_get(b, i);
3103
3104 blob_set(b, i, blob_get(b, len - i - 1));
3105 blob_set(b, len - i - 1, tmp);
3106 }
3107 rettv_blob_set(rettv, b);
3108 return;
3109 }
3110
3111 if (argvars[0].v_type != VAR_LIST)
3112 semsg(_(e_listblobarg), "reverse()");
3113 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02003114 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003115 (char_u *)N_("reverse() argument"), TRUE))
3116 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01003117 if (l->lv_first == &range_list_item)
3118 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003119 varnumber_T new_start = l->lv_u.nonmat.lv_start
3120 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
3121 l->lv_u.nonmat.lv_end = new_start
3122 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
3123 l->lv_u.nonmat.lv_start = new_start;
3124 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01003125 rettv_list_set(rettv, l);
3126 return;
3127 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003128 li = l->lv_u.mat.lv_last;
3129 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003130 l->lv_len = 0;
3131 while (li != NULL)
3132 {
3133 ni = li->li_prev;
3134 list_append(l, li);
3135 li = ni;
3136 }
3137 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01003138 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02003139 }
3140}
3141
Bram Moolenaar85629982020-06-01 18:39:20 +02003142/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003143 * "reduce(list, { accumulator, element -> value } [, initial])" function
Bram Moolenaar85629982020-06-01 18:39:20 +02003144 */
3145 void
3146f_reduce(typval_T *argvars, typval_T *rettv)
3147{
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003148 typval_T initial;
Bram Moolenaar85629982020-06-01 18:39:20 +02003149 char_u *func_name;
3150 partial_T *partial = NULL;
3151 funcexe_T funcexe;
3152 typval_T argv[3];
3153
3154 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
3155 {
3156 emsg(_(e_listblobreq));
3157 return;
3158 }
3159
3160 if (argvars[1].v_type == VAR_FUNC)
3161 func_name = argvars[1].vval.v_string;
3162 else if (argvars[1].v_type == VAR_PARTIAL)
3163 {
3164 partial = argvars[1].vval.v_partial;
3165 func_name = partial_name(partial);
3166 }
3167 else
3168 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01003169 if (func_name == NULL || *func_name == NUL)
3170 {
3171 emsg(_(e_missing_function_argument));
3172 return;
3173 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003174
3175 vim_memset(&funcexe, 0, sizeof(funcexe));
3176 funcexe.evaluate = TRUE;
3177 funcexe.partial = partial;
3178
3179 if (argvars[0].v_type == VAR_LIST)
3180 {
3181 list_T *l = argvars[0].vval.v_list;
3182 listitem_T *li = NULL;
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003183 int r;
Bram Moolenaarca275a02020-06-24 22:07:46 +02003184 int called_emsg_start = called_emsg;
Bram Moolenaar85629982020-06-01 18:39:20 +02003185
Bram Moolenaarfda20c42020-06-29 20:09:36 +02003186 if (l != NULL)
3187 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar85629982020-06-01 18:39:20 +02003188 if (argvars[2].v_type == VAR_UNKNOWN)
3189 {
3190 if (l == NULL || l->lv_first == NULL)
3191 {
3192 semsg(_(e_reduceempty), "List");
3193 return;
3194 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003195 initial = l->lv_first->li_tv;
Bram Moolenaar85629982020-06-01 18:39:20 +02003196 li = l->lv_first->li_next;
3197 }
3198 else
3199 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003200 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02003201 if (l != NULL)
3202 li = l->lv_first;
3203 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003204 copy_tv(&initial, rettv);
Bram Moolenaarfda20c42020-06-29 20:09:36 +02003205
3206 if (l != NULL)
Bram Moolenaar85629982020-06-01 18:39:20 +02003207 {
Bram Moolenaarfda20c42020-06-29 20:09:36 +02003208 int prev_locked = l->lv_lock;
3209
3210 l->lv_lock = VAR_FIXED; // disallow the list changing here
3211 for ( ; li != NULL; li = li->li_next)
3212 {
3213 argv[0] = *rettv;
3214 argv[1] = li->li_tv;
3215 rettv->v_type = VAR_UNKNOWN;
3216 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
3217 clear_tv(&argv[0]);
3218 if (r == FAIL || called_emsg != called_emsg_start)
3219 break;
3220 }
3221 l->lv_lock = prev_locked;
Bram Moolenaar85629982020-06-01 18:39:20 +02003222 }
3223 }
3224 else
3225 {
3226 blob_T *b = argvars[0].vval.v_blob;
3227 int i;
3228
3229 if (argvars[2].v_type == VAR_UNKNOWN)
3230 {
3231 if (b == NULL || b->bv_ga.ga_len == 0)
3232 {
3233 semsg(_(e_reduceempty), "Blob");
3234 return;
3235 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003236 initial.v_type = VAR_NUMBER;
3237 initial.vval.v_number = blob_get(b, 0);
Bram Moolenaar85629982020-06-01 18:39:20 +02003238 i = 1;
3239 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003240 else if (argvars[2].v_type != VAR_NUMBER)
3241 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003242 emsg(_(e_number_expected));
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003243 return;
3244 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003245 else
3246 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003247 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02003248 i = 0;
3249 }
3250
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003251 copy_tv(&initial, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003252 if (b != NULL)
3253 {
3254 for ( ; i < b->bv_ga.ga_len; i++)
3255 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003256 argv[0] = *rettv;
Bram Moolenaar85629982020-06-01 18:39:20 +02003257 argv[1].v_type = VAR_NUMBER;
3258 argv[1].vval.v_number = blob_get(b, i);
3259 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
3260 return;
Bram Moolenaar85629982020-06-01 18:39:20 +02003261 }
3262 }
3263 }
3264}
3265
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02003266#endif // defined(FEAT_EVAL)