blob: 6b275789d502134531b6019d14c2a8c1c4505f0f [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 Moolenaar460ae5d2022-01-01 14:19:49 +0000527 semsg(_(e_list_index_out_of_range_nr), 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)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000785 semsg(_(e_list_index_out_of_range_nr), *n1);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200786 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)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000813 semsg(_(e_list_index_out_of_range_nr), *n2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200814 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)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000825 semsg(_(e_list_index_out_of_range_nr), *n2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200826 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;
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200855 type_T *member_type = NULL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200856
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000857 // Check whether any of the list items is locked before making any changes.
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200858 idx = idx1;
859 dest_li = first_li;
860 for (src_li = src->lv_first; src_li != NULL && dest_li != NULL; )
861 {
862 if (value_check_lock(dest_li->li_tv.v_lock, varname, FALSE))
863 return FAIL;
864 src_li = src_li->li_next;
865 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
866 break;
867 dest_li = dest_li->li_next;
868 ++idx;
869 }
870
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200871 if (in_vim9script() && dest->lv_type != NULL
872 && dest->lv_type->tt_member != NULL)
873 member_type = dest->lv_type->tt_member;
874
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000875 // Assign the List values to the list items.
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200876 idx = idx1;
877 dest_li = first_li;
878 for (src_li = src->lv_first; src_li != NULL; )
879 {
880 if (op != NULL && *op != '=')
881 tv_op(&dest_li->li_tv, &src_li->li_tv, op);
882 else
883 {
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200884 if (member_type != NULL
885 && check_typval_arg_type(member_type, &src_li->li_tv,
886 NULL, 0) == FAIL)
887 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200888 clear_tv(&dest_li->li_tv);
889 copy_tv(&src_li->li_tv, &dest_li->li_tv);
890 }
891 src_li = src_li->li_next;
892 if (src_li == NULL || (!empty_idx2 && idx2 == idx))
893 break;
894 if (dest_li->li_next == NULL)
895 {
896 // Need to add an empty item.
897 if (list_append_number(dest, 0) == FAIL)
898 {
899 src_li = NULL;
900 break;
901 }
902 }
903 dest_li = dest_li->li_next;
904 ++idx;
905 }
906 if (src_li != NULL)
907 {
908 emsg(_(e_list_value_has_more_items_than_targets));
909 return FAIL;
910 }
911 if (empty_idx2
912 ? (dest_li != NULL && dest_li->li_next != NULL)
913 : idx != idx2)
914 {
915 emsg(_(e_list_value_does_not_have_enough_items));
916 return FAIL;
917 }
918 return OK;
919}
920
921/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200922 * Flatten "list" to depth "maxdepth".
923 * It does nothing if "maxdepth" is 0.
924 * Returns FAIL when out of memory.
925 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100926 static void
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200927list_flatten(list_T *list, long maxdepth)
928{
929 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200930 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200931 int n;
932
933 if (maxdepth == 0)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100934 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200935 CHECK_LIST_MATERIALIZE(list);
936
937 n = 0;
938 item = list->lv_first;
939 while (item != NULL)
940 {
941 fast_breakcheck();
942 if (got_int)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100943 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200944
945 if (item->li_tv.v_type == VAR_LIST)
946 {
947 listitem_T *next = item->li_next;
948
949 vimlist_remove(list, item, item);
950 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +0200951 {
952 list_free_item(list, item);
Bram Moolenaar3b690062021-02-01 20:14:51 +0100953 return;
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +0200954 }
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200955 clear_tv(&item->li_tv);
956 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200957
958 if (item->li_prev == NULL)
959 item = list->lv_first;
960 else
961 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200962 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200963
964 if (++n >= maxdepth)
965 {
966 n = 0;
967 item = next;
968 }
969 }
970 else
971 {
972 n = 0;
973 item = item->li_next;
974 }
975 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200976}
977
978/*
Bram Moolenaar3b690062021-02-01 20:14:51 +0100979 * "flatten()" and "flattennew()" functions
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200980 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100981 static void
982flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200983{
984 list_T *l;
985 long maxdepth;
986 int error = FALSE;
987
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200988 if (in_vim9script()
989 && (check_for_list_arg(argvars, 0) == FAIL
990 || check_for_opt_number_arg(argvars, 1) == FAIL))
991 return;
992
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200993 if (argvars[0].v_type != VAR_LIST)
994 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +0000995 semsg(_(e_argument_of_str_must_be_list), "flatten()");
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200996 return;
997 }
998
999 if (argvars[1].v_type == VAR_UNKNOWN)
1000 maxdepth = 999999;
1001 else
1002 {
1003 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
1004 if (error)
1005 return;
1006 if (maxdepth < 0)
1007 {
1008 emsg(_("E900: maxdepth must be non-negative number"));
1009 return;
1010 }
1011 }
1012
1013 l = argvars[0].vval.v_list;
Bram Moolenaar3b690062021-02-01 20:14:51 +01001014 rettv->v_type = VAR_LIST;
1015 rettv->vval.v_list = l;
1016 if (l == NULL)
1017 return;
1018
1019 if (make_copy)
1020 {
1021 l = list_copy(l, TRUE, get_copyID());
1022 rettv->vval.v_list = l;
1023 if (l == NULL)
1024 return;
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +02001025 // The type will change.
1026 free_type(l->lv_type);
1027 l->lv_type = NULL;
Bram Moolenaar3b690062021-02-01 20:14:51 +01001028 }
1029 else
1030 {
1031 if (value_check_lock(l->lv_lock,
1032 (char_u *)N_("flatten() argument"), TRUE))
1033 return;
1034 ++l->lv_refcount;
1035 }
1036
1037 list_flatten(l, maxdepth);
1038}
1039
1040/*
1041 * "flatten(list[, {maxdepth}])" function
1042 */
1043 void
1044f_flatten(typval_T *argvars, typval_T *rettv)
1045{
1046 if (in_vim9script())
1047 emsg(_(e_cannot_use_flatten_in_vim9_script));
1048 else
1049 flatten_common(argvars, rettv, FALSE);
1050}
1051
1052/*
1053 * "flattennew(list[, {maxdepth}])" function
1054 */
1055 void
1056f_flattennew(typval_T *argvars, typval_T *rettv)
1057{
1058 flatten_common(argvars, rettv, TRUE);
Bram Moolenaar077a1e62020-06-08 20:50:43 +02001059}
1060
1061/*
Bram Moolenaar1a739232020-10-10 15:37:58 +02001062 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001063 * If "bef" is NULL append at the end, otherwise insert before this item.
1064 * Returns FAIL when out of memory.
1065 */
1066 int
1067list_extend(list_T *l1, list_T *l2, listitem_T *bef)
1068{
1069 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001070 int todo;
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001071 listitem_T *bef_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001072
Bram Moolenaar1a739232020-10-10 15:37:58 +02001073 // NULL list is equivalent to an empty list: nothing to do.
1074 if (l2 == NULL || l2->lv_len == 0)
1075 return OK;
1076
1077 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001078 CHECK_LIST_MATERIALIZE(l1);
1079 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001081 // When exending a list with itself, at some point we run into the item
1082 // that was before "bef" and need to skip over the already inserted items
1083 // to "bef".
1084 bef_prev = bef == NULL ? NULL : bef->li_prev;
1085
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001086 // We also quit the loop when we have inserted the original item count of
1087 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001088 for (item = l2->lv_first; item != NULL && --todo >= 0;
1089 item = item == bef_prev ? bef : item->li_next)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001090 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
1091 return FAIL;
1092 return OK;
1093}
1094
1095/*
1096 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
1097 * Return FAIL when out of memory.
1098 */
1099 int
1100list_concat(list_T *l1, list_T *l2, typval_T *tv)
1101{
1102 list_T *l;
1103
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001104 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +02001105 if (l1 == NULL)
1106 l = list_alloc();
1107 else
1108 l = list_copy(l1, FALSE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001109 if (l == NULL)
1110 return FAIL;
1111 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +01001112 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001113 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +02001114 if (l1 == NULL)
1115 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001116
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001117 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +02001118 return list_extend(l, l2, NULL);
1119}
1120
Bram Moolenaar9af78762020-06-16 11:34:42 +02001121 list_T *
1122list_slice(list_T *ol, long n1, long n2)
1123{
1124 listitem_T *item;
1125 list_T *l = list_alloc();
1126
1127 if (l == NULL)
1128 return NULL;
1129 for (item = list_find(ol, n1); n1 <= n2; ++n1)
1130 {
1131 if (list_append_tv(l, &item->li_tv) == FAIL)
1132 {
1133 list_free(l);
1134 return NULL;
1135 }
1136 item = item->li_next;
1137 }
1138 return l;
1139}
1140
Bram Moolenaared591872020-08-15 22:14:53 +02001141 int
1142list_slice_or_index(
1143 list_T *list,
1144 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01001145 varnumber_T n1_arg,
1146 varnumber_T n2_arg,
1147 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +02001148 typval_T *rettv,
1149 int verbose)
1150{
1151 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001152 varnumber_T n1 = n1_arg;
1153 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +02001154 typval_T var1;
1155
1156 if (n1 < 0)
1157 n1 = len + n1;
1158 if (n1 < 0 || n1 >= len)
1159 {
Bram Moolenaar92f05f22021-08-12 21:12:56 +02001160 // For a range we allow invalid values and for legacy script return an
1161 // empty list, for Vim9 script start at the first item.
1162 // A list index out of range is an error.
Bram Moolenaared591872020-08-15 22:14:53 +02001163 if (!range)
1164 {
1165 if (verbose)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001166 semsg(_(e_list_index_out_of_range_nr), (long)n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +02001167 return FAIL;
1168 }
Bram Moolenaar92f05f22021-08-12 21:12:56 +02001169 if (in_vim9script())
1170 n1 = n1 < 0 ? 0 : len;
1171 else
1172 n1 = len;
Bram Moolenaared591872020-08-15 22:14:53 +02001173 }
1174 if (range)
1175 {
1176 list_T *l;
1177
1178 if (n2 < 0)
1179 n2 = len + n2;
1180 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01001181 n2 = len - (exclusive ? 0 : 1);
1182 if (exclusive)
1183 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +02001184 if (n2 < 0 || n2 + 1 < n1)
1185 n2 = -1;
1186 l = list_slice(list, n1, n2);
1187 if (l == NULL)
1188 return FAIL;
1189 clear_tv(rettv);
1190 rettv_list_set(rettv, l);
1191 }
1192 else
1193 {
1194 // copy the item to "var1" to avoid that freeing the list makes it
1195 // invalid.
1196 copy_tv(&list_find(list, n1)->li_tv, &var1);
1197 clear_tv(rettv);
1198 *rettv = var1;
1199 }
1200 return OK;
1201}
1202
Bram Moolenaarda861d62016-07-17 15:46:27 +02001203/*
1204 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1205 * The refcount of the new list is set to 1.
1206 * See item_copy() for "copyID".
1207 * Returns NULL when out of memory.
1208 */
1209 list_T *
1210list_copy(list_T *orig, int deep, int copyID)
1211{
1212 list_T *copy;
1213 listitem_T *item;
1214 listitem_T *ni;
1215
1216 if (orig == NULL)
1217 return NULL;
1218
1219 copy = list_alloc();
1220 if (copy != NULL)
1221 {
Bram Moolenaarb3bf33a2021-09-11 20:20:38 +02001222 copy->lv_type = alloc_type(orig->lv_type);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001223 if (copyID != 0)
1224 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001225 // Do this before adding the items, because one of the items may
1226 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001227 orig->lv_copyID = copyID;
1228 orig->lv_copylist = copy;
1229 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001230 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001231 for (item = orig->lv_first; item != NULL && !got_int;
1232 item = item->li_next)
1233 {
1234 ni = listitem_alloc();
1235 if (ni == NULL)
1236 break;
1237 if (deep)
1238 {
1239 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
1240 {
1241 vim_free(ni);
1242 break;
1243 }
1244 }
1245 else
1246 copy_tv(&item->li_tv, &ni->li_tv);
1247 list_append(copy, ni);
1248 }
1249 ++copy->lv_refcount;
1250 if (item != NULL)
1251 {
1252 list_unref(copy);
1253 copy = NULL;
1254 }
1255 }
1256
1257 return copy;
1258}
1259
1260/*
1261 * Remove items "item" to "item2" from list "l".
1262 * Does not free the listitem or the value!
1263 * This used to be called list_remove, but that conflicts with a Sun header
1264 * file.
1265 */
1266 void
1267vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1268{
1269 listitem_T *ip;
1270
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001271 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001273 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001274 for (ip = item; ip != NULL; ip = ip->li_next)
1275 {
1276 --l->lv_len;
1277 list_fix_watch(l, ip);
1278 if (ip == item2)
1279 break;
1280 }
1281
1282 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001283 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001284 else
1285 item2->li_next->li_prev = item->li_prev;
1286 if (item->li_prev == NULL)
1287 l->lv_first = item2->li_next;
1288 else
1289 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001290 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001291}
1292
1293/*
1294 * Return an allocated string with the string representation of a list.
1295 * May return NULL.
1296 */
1297 char_u *
1298list2string(typval_T *tv, int copyID, int restore_copyID)
1299{
1300 garray_T ga;
1301
1302 if (tv->vval.v_list == NULL)
1303 return NULL;
1304 ga_init2(&ga, (int)sizeof(char), 80);
1305 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001306 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001307 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1308 FALSE, restore_copyID, copyID) == FAIL)
1309 {
1310 vim_free(ga.ga_data);
1311 return NULL;
1312 }
1313 ga_append(&ga, ']');
1314 ga_append(&ga, NUL);
1315 return (char_u *)ga.ga_data;
1316}
1317
1318typedef struct join_S {
1319 char_u *s;
1320 char_u *tofree;
1321} join_T;
1322
1323 static int
1324list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001325 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001326 list_T *l,
1327 char_u *sep,
1328 int echo_style,
1329 int restore_copyID,
1330 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001331 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001332{
1333 int i;
1334 join_T *p;
1335 int len;
1336 int sumlen = 0;
1337 int first = TRUE;
1338 char_u *tofree;
1339 char_u numbuf[NUMBUFLEN];
1340 listitem_T *item;
1341 char_u *s;
1342
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001343 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001344 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001345 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1346 {
1347 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001348 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001349 if (s == NULL)
1350 return FAIL;
1351
1352 len = (int)STRLEN(s);
1353 sumlen += len;
1354
1355 (void)ga_grow(join_gap, 1);
1356 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1357 if (tofree != NULL || s != numbuf)
1358 {
1359 p->s = s;
1360 p->tofree = tofree;
1361 }
1362 else
1363 {
1364 p->s = vim_strnsave(s, len);
1365 p->tofree = p->s;
1366 }
1367
1368 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001369 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001370 break;
1371 }
1372
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001373 // Allocate result buffer with its total size, avoid re-allocation and
1374 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001375 if (join_gap->ga_len >= 2)
1376 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1377 if (ga_grow(gap, sumlen + 2) == FAIL)
1378 return FAIL;
1379
1380 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1381 {
1382 if (first)
1383 first = FALSE;
1384 else
1385 ga_concat(gap, sep);
1386 p = ((join_T *)join_gap->ga_data) + i;
1387
1388 if (p->s != NULL)
1389 ga_concat(gap, p->s);
1390 line_breakcheck();
1391 }
1392
1393 return OK;
1394}
1395
1396/*
1397 * Join list "l" into a string in "*gap", using separator "sep".
1398 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1399 * Return FAIL or OK.
1400 */
1401 int
1402list_join(
1403 garray_T *gap,
1404 list_T *l,
1405 char_u *sep,
1406 int echo_style,
1407 int restore_copyID,
1408 int copyID)
1409{
1410 garray_T join_ga;
1411 int retval;
1412 join_T *p;
1413 int i;
1414
1415 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001416 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001417 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1418 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1419 copyID, &join_ga);
1420
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001421 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001422 if (join_ga.ga_data != NULL)
1423 {
1424 p = (join_T *)join_ga.ga_data;
1425 for (i = 0; i < join_ga.ga_len; ++i)
1426 {
1427 vim_free(p->tofree);
1428 ++p;
1429 }
1430 ga_clear(&join_ga);
1431 }
1432
1433 return retval;
1434}
1435
1436/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001437 * "join()" function
1438 */
1439 void
1440f_join(typval_T *argvars, typval_T *rettv)
1441{
1442 garray_T ga;
1443 char_u *sep;
1444
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001445 if (in_vim9script()
1446 && (check_for_list_arg(argvars, 0) == FAIL
1447 || check_for_opt_string_arg(argvars, 1) == FAIL))
1448 return;
1449
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001450 if (argvars[0].v_type != VAR_LIST)
1451 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001452 emsg(_(e_list_required));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001453 return;
1454 }
Bram Moolenaaref982572021-08-12 19:27:57 +02001455 rettv->v_type = VAR_STRING;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001456 if (argvars[0].vval.v_list == NULL)
1457 return;
Bram Moolenaaref982572021-08-12 19:27:57 +02001458
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001459 if (argvars[1].v_type == VAR_UNKNOWN)
1460 sep = (char_u *)" ";
1461 else
1462 sep = tv_get_string_chk(&argvars[1]);
1463
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001464 if (sep != NULL)
1465 {
1466 ga_init2(&ga, (int)sizeof(char), 80);
1467 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1468 ga_append(&ga, NUL);
1469 rettv->vval.v_string = (char_u *)ga.ga_data;
1470 }
1471 else
1472 rettv->vval.v_string = NULL;
1473}
1474
1475/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001476 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001477 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001478 * Return OK or FAIL.
1479 */
1480 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001481eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001482{
Bram Moolenaar71478202020-06-26 22:46:27 +02001483 int evaluate = evalarg == NULL ? FALSE
1484 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001485 list_T *l = NULL;
1486 typval_T tv;
1487 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001488 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001489 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001490
1491 if (evaluate)
1492 {
1493 l = list_alloc();
1494 if (l == NULL)
1495 return FAIL;
1496 }
1497
Bram Moolenaar962d7212020-07-04 14:15:00 +02001498 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001499 while (**arg != ']' && **arg != NUL)
1500 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001501 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001502 goto failret;
1503 if (evaluate)
1504 {
1505 item = listitem_alloc();
1506 if (item != NULL)
1507 {
1508 item->li_tv = tv;
1509 item->li_tv.v_lock = 0;
1510 list_append(l, item);
1511 }
1512 else
1513 clear_tv(&tv);
1514 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001515 // Legacy Vim script allowed a space before the comma.
1516 if (!vim9script)
1517 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001518
Bram Moolenaare6e03172020-06-27 16:36:05 +02001519 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001520 had_comma = **arg == ',';
1521 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001522 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001523 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001524 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001525 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001526 goto failret;
1527 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001528 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001529 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001530
Bram Moolenaare6b53242020-07-01 17:28:33 +02001531 // The "]" can be on the next line. But a double quoted string may
1532 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001533 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001534 if (**arg == ']')
1535 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001536
1537 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001538 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001540 {
1541 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001542 semsg(_(e_no_white_space_allowed_before_str_str),
1543 ",", *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001544 else
1545 semsg(_("E696: Missing comma in List: %s"), *arg);
1546 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001547 goto failret;
1548 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001549 }
1550
1551 if (**arg != ']')
1552 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 if (do_error)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001554 semsg(_(e_missing_end_of_list_rsb_str), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001555failret:
1556 if (evaluate)
1557 list_free(l);
1558 return FAIL;
1559 }
1560
Bram Moolenaar9d489562020-07-30 20:08:50 +02001561 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001562 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001563 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001564
1565 return OK;
1566}
1567
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001568/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001569 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001570 */
1571 int
1572write_list(FILE *fd, list_T *list, int binary)
1573{
1574 listitem_T *li;
1575 int c;
1576 int ret = OK;
1577 char_u *s;
1578
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001579 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001580 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001582 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583 {
1584 if (*s == '\n')
1585 c = putc(NUL, fd);
1586 else
1587 c = putc(*s, fd);
1588 if (c == EOF)
1589 {
1590 ret = FAIL;
1591 break;
1592 }
1593 }
1594 if (!binary || li->li_next != NULL)
1595 if (putc('\n', fd) == EOF)
1596 {
1597 ret = FAIL;
1598 break;
1599 }
1600 if (ret == FAIL)
1601 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001602 emsg(_(e_error_while_writing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001603 break;
1604 }
1605 }
1606 return ret;
1607}
1608
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001609/*
1610 * Initialize a static list with 10 items.
1611 */
1612 void
1613init_static_list(staticList10_T *sl)
1614{
1615 list_T *l = &sl->sl_list;
1616 int i;
1617
1618 memset(sl, 0, sizeof(staticList10_T));
1619 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001620 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001621 l->lv_refcount = DO_NOT_FREE_CNT;
1622 l->lv_lock = VAR_FIXED;
1623 sl->sl_list.lv_len = 10;
1624
1625 for (i = 0; i < 10; ++i)
1626 {
1627 listitem_T *li = &sl->sl_items[i];
1628
1629 if (i == 0)
1630 li->li_prev = NULL;
1631 else
1632 li->li_prev = li - 1;
1633 if (i == 9)
1634 li->li_next = NULL;
1635 else
1636 li->li_next = li + 1;
1637 }
1638}
1639
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001640/*
1641 * "list2str()" function
1642 */
1643 void
1644f_list2str(typval_T *argvars, typval_T *rettv)
1645{
1646 list_T *l;
1647 listitem_T *li;
1648 garray_T ga;
1649 int utf8 = FALSE;
1650
1651 rettv->v_type = VAR_STRING;
1652 rettv->vval.v_string = NULL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001653
1654 if (in_vim9script()
1655 && (check_for_list_arg(argvars, 0) == FAIL
1656 || check_for_opt_bool_arg(argvars, 1) == FAIL))
1657 return;
1658
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001659 if (argvars[0].v_type != VAR_LIST)
1660 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001661 emsg(_(e_invalid_argument));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001662 return;
1663 }
1664
1665 l = argvars[0].vval.v_list;
1666 if (l == NULL)
1667 return; // empty list results in empty string
1668
1669 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001670 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001671
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001672 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001673 ga_init2(&ga, 1, 80);
1674 if (has_mbyte || utf8)
1675 {
1676 char_u buf[MB_MAXBYTES + 1];
1677 int (*char2bytes)(int, char_u *);
1678
1679 if (utf8 || enc_utf8)
1680 char2bytes = utf_char2bytes;
1681 else
1682 char2bytes = mb_char2bytes;
1683
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001684 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001685 {
1686 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1687 ga_concat(&ga, buf);
1688 }
1689 ga_append(&ga, NUL);
1690 }
1691 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1692 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001693 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001694 ga_append(&ga, tv_get_number(&li->li_tv));
1695 ga_append(&ga, NUL);
1696 }
1697
1698 rettv->v_type = VAR_STRING;
1699 rettv->vval.v_string = ga.ga_data;
1700}
1701
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001702/*
1703 * Remove item argvars[1] from List argvars[0]. If argvars[2] is supplied, then
1704 * remove the range of items from argvars[1] to argvars[2] (inclusive).
1705 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001706 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001707list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1708{
1709 list_T *l;
1710 listitem_T *item, *item2;
1711 listitem_T *li;
1712 int error = FALSE;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001713 long idx;
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001714 long end;
1715 int cnt = 0;
1716 list_T *rl;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001717
1718 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001719 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001720 return;
1721
1722 idx = (long)tv_get_number_chk(&argvars[1], &error);
1723 if (error)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001724 return; // type error: do nothing, errmsg already given
1725
1726 if ((item = list_find(l, idx)) == NULL)
1727 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001728 semsg(_(e_list_index_out_of_range_nr), idx);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001729 return;
1730 }
1731
1732 if (argvars[2].v_type == VAR_UNKNOWN)
1733 {
1734 // Remove one item, return its value.
1735 vimlist_remove(l, item, item);
1736 *rettv = item->li_tv;
1737 list_free_item(l, item);
1738 return;
1739 }
1740
1741 // Remove range of items, return list with values.
1742 end = (long)tv_get_number_chk(&argvars[2], &error);
1743 if (error)
1744 return; // type error: do nothing
1745
1746 if ((item2 = list_find(l, end)) == NULL)
1747 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001748 semsg(_(e_list_index_out_of_range_nr), end);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001749 return;
1750 }
1751
1752 for (li = item; li != NULL; li = li->li_next)
1753 {
1754 ++cnt;
1755 if (li == item2)
1756 break;
1757 }
1758 if (li == NULL) // didn't find "item2" after "item"
1759 {
1760 emsg(_(e_invalid_range));
1761 return;
1762 }
1763
1764 vimlist_remove(l, item, item2);
1765 if (rettv_list_alloc(rettv) != OK)
1766 return;
1767
1768 rl = rettv->vval.v_list;
1769
1770 if (l->lv_with_items > 0)
1771 {
1772 // need to copy the list items and move the value
1773 while (item != NULL)
1774 {
1775 li = listitem_alloc();
1776 if (li == NULL)
1777 return;
1778 li->li_tv = item->li_tv;
1779 init_tv(&item->li_tv);
1780 list_append(rl, li);
1781 if (item == item2)
1782 break;
1783 item = item->li_next;
1784 }
1785 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001786 else
1787 {
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00001788 rl->lv_first = item;
1789 rl->lv_u.mat.lv_last = item2;
1790 item->li_prev = NULL;
1791 item2->li_next = NULL;
1792 rl->lv_len = cnt;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001793 }
1794}
1795
1796static int item_compare(const void *s1, const void *s2);
1797static int item_compare2(const void *s1, const void *s2);
1798
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001799// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001800typedef struct
1801{
1802 listitem_T *item;
1803 int idx;
1804} sortItem_T;
1805
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001806// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001807typedef struct
1808{
1809 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001810 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001811 int item_compare_numeric;
1812 int item_compare_numbers;
1813#ifdef FEAT_FLOAT
1814 int item_compare_float;
1815#endif
1816 char_u *item_compare_func;
1817 partial_T *item_compare_partial;
1818 dict_T *item_compare_selfdict;
1819 int item_compare_func_err;
1820 int item_compare_keep_zero;
1821} sortinfo_T;
1822static sortinfo_T *sortinfo = NULL;
1823#define ITEM_COMPARE_FAIL 999
1824
1825/*
1826 * Compare functions for f_sort() and f_uniq() below.
1827 */
1828 static int
1829item_compare(const void *s1, const void *s2)
1830{
1831 sortItem_T *si1, *si2;
1832 typval_T *tv1, *tv2;
1833 char_u *p1, *p2;
1834 char_u *tofree1 = NULL, *tofree2 = NULL;
1835 int res;
1836 char_u numbuf1[NUMBUFLEN];
1837 char_u numbuf2[NUMBUFLEN];
1838
1839 si1 = (sortItem_T *)s1;
1840 si2 = (sortItem_T *)s2;
1841 tv1 = &si1->item->li_tv;
1842 tv2 = &si2->item->li_tv;
1843
1844 if (sortinfo->item_compare_numbers)
1845 {
1846 varnumber_T v1 = tv_get_number(tv1);
1847 varnumber_T v2 = tv_get_number(tv2);
1848
1849 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1850 }
1851
1852#ifdef FEAT_FLOAT
1853 if (sortinfo->item_compare_float)
1854 {
1855 float_T v1 = tv_get_float(tv1);
1856 float_T v2 = tv_get_float(tv2);
1857
1858 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1859 }
1860#endif
1861
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001862 // tv2string() puts quotes around a string and allocates memory. Don't do
1863 // that for string variables. Use a single quote when comparing with a
1864 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001865 if (tv1->v_type == VAR_STRING)
1866 {
1867 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1868 p1 = (char_u *)"'";
1869 else
1870 p1 = tv1->vval.v_string;
1871 }
1872 else
1873 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1874 if (tv2->v_type == VAR_STRING)
1875 {
1876 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1877 p2 = (char_u *)"'";
1878 else
1879 p2 = tv2->vval.v_string;
1880 }
1881 else
1882 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1883 if (p1 == NULL)
1884 p1 = (char_u *)"";
1885 if (p2 == NULL)
1886 p2 = (char_u *)"";
1887 if (!sortinfo->item_compare_numeric)
1888 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001889 if (sortinfo->item_compare_lc)
1890 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001891 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001892 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001893 }
1894 else
1895 {
1896 double n1, n2;
1897 n1 = strtod((char *)p1, (char **)&p1);
1898 n2 = strtod((char *)p2, (char **)&p2);
1899 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1900 }
1901
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001902 // When the result would be zero, compare the item indexes. Makes the
1903 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001904 if (res == 0 && !sortinfo->item_compare_keep_zero)
1905 res = si1->idx > si2->idx ? 1 : -1;
1906
1907 vim_free(tofree1);
1908 vim_free(tofree2);
1909 return res;
1910}
1911
1912 static int
1913item_compare2(const void *s1, const void *s2)
1914{
1915 sortItem_T *si1, *si2;
1916 int res;
1917 typval_T rettv;
1918 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001919 char_u *func_name;
1920 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001921 funcexe_T funcexe;
Bram Moolenaar23018f22021-12-27 11:54:37 +00001922 int did_emsg_before = did_emsg;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001923
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001924 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001925 if (sortinfo->item_compare_func_err)
1926 return 0;
1927
1928 si1 = (sortItem_T *)s1;
1929 si2 = (sortItem_T *)s2;
1930
1931 if (partial == NULL)
1932 func_name = sortinfo->item_compare_func;
1933 else
1934 func_name = partial_name(partial);
1935
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001936 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1937 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001938 copy_tv(&si1->item->li_tv, &argv[0]);
1939 copy_tv(&si2->item->li_tv, &argv[1]);
1940
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001941 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001942 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001943 funcexe.fe_evaluate = TRUE;
1944 funcexe.fe_partial = partial;
1945 funcexe.fe_selfdict = sortinfo->item_compare_selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001946 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001947 clear_tv(&argv[0]);
1948 clear_tv(&argv[1]);
1949
Bram Moolenaar23018f22021-12-27 11:54:37 +00001950 if (res == FAIL || did_emsg > did_emsg_before)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001951 res = ITEM_COMPARE_FAIL;
1952 else
Yasuhiro Matsumotoc04f6232021-09-19 17:01:39 +02001953 {
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001954 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Yasuhiro Matsumotoc04f6232021-09-19 17:01:39 +02001955 if (res > 0)
1956 res = 1;
1957 else if (res < 0)
1958 res = -1;
1959 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001960 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001961 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001962 clear_tv(&rettv);
1963
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001964 // When the result would be zero, compare the pointers themselves. Makes
1965 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001966 if (res == 0 && !sortinfo->item_compare_keep_zero)
1967 res = si1->idx > si2->idx ? 1 : -1;
1968
1969 return res;
1970}
1971
1972/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00001973 * sort() List "l"
1974 */
1975 static void
1976do_sort(list_T *l, sortinfo_T *info)
1977{
1978 long len;
1979 sortItem_T *ptrs;
1980 long i = 0;
1981 listitem_T *li;
1982
1983 len = list_len(l);
1984
1985 // Make an array with each entry pointing to an item in the List.
1986 ptrs = ALLOC_MULT(sortItem_T, len);
1987 if (ptrs == NULL)
1988 return;
1989
1990 // sort(): ptrs will be the list to sort
1991 FOR_ALL_LIST_ITEMS(l, li)
1992 {
1993 ptrs[i].item = li;
1994 ptrs[i].idx = i;
1995 ++i;
1996 }
1997
1998 info->item_compare_func_err = FALSE;
1999 info->item_compare_keep_zero = FALSE;
2000 // test the compare function
2001 if ((info->item_compare_func != NULL
2002 || info->item_compare_partial != NULL)
2003 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
2004 == ITEM_COMPARE_FAIL)
2005 emsg(_("E702: Sort compare function failed"));
2006 else
2007 {
2008 // Sort the array with item pointers.
2009 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
2010 info->item_compare_func == NULL
2011 && info->item_compare_partial == NULL
2012 ? item_compare : item_compare2);
2013
2014 if (!info->item_compare_func_err)
2015 {
2016 // Clear the List and append the items in sorted order.
2017 l->lv_first = l->lv_u.mat.lv_last
2018 = l->lv_u.mat.lv_idx_item = NULL;
2019 l->lv_len = 0;
2020 for (i = 0; i < len; ++i)
2021 list_append(l, ptrs[i].item);
2022 }
2023 }
2024
2025 vim_free(ptrs);
2026}
2027
2028/*
2029 * uniq() List "l"
2030 */
2031 static void
2032do_uniq(list_T *l, sortinfo_T *info)
2033{
2034 long len;
2035 sortItem_T *ptrs;
2036 long i = 0;
2037 listitem_T *li;
2038 int (*item_compare_func_ptr)(const void *, const void *);
2039
2040 len = list_len(l);
2041
2042 // Make an array with each entry pointing to an item in the List.
2043 ptrs = ALLOC_MULT(sortItem_T, len);
2044 if (ptrs == NULL)
2045 return;
2046
2047 // f_uniq(): ptrs will be a stack of items to remove
2048 info->item_compare_func_err = FALSE;
2049 info->item_compare_keep_zero = TRUE;
2050 item_compare_func_ptr = info->item_compare_func != NULL
2051 || info->item_compare_partial != NULL
2052 ? item_compare2 : item_compare;
2053
2054 for (li = l->lv_first; li != NULL && li->li_next != NULL;
2055 li = li->li_next)
2056 {
2057 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
2058 == 0)
2059 ptrs[i++].item = li;
2060 if (info->item_compare_func_err)
2061 {
2062 emsg(_("E882: Uniq compare function failed"));
2063 break;
2064 }
2065 }
2066
2067 if (!info->item_compare_func_err)
2068 {
2069 while (--i >= 0)
2070 {
2071 li = ptrs[i].item->li_next;
2072 ptrs[i].item->li_next = li->li_next;
2073 if (li->li_next != NULL)
2074 li->li_next->li_prev = ptrs[i].item;
2075 else
2076 l->lv_u.mat.lv_last = ptrs[i].item;
2077 list_fix_watch(l, li);
2078 listitem_free(l, li);
2079 l->lv_len--;
2080 }
2081 }
2082
2083 vim_free(ptrs);
2084}
2085
2086/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002087 * Parse the optional arguments supplied to the sort() or uniq() function and
2088 * return the values in "info".
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002089 */
2090 static int
2091parse_sort_uniq_args(typval_T *argvars, sortinfo_T *info)
2092{
2093 info->item_compare_ic = FALSE;
2094 info->item_compare_lc = FALSE;
2095 info->item_compare_numeric = FALSE;
2096 info->item_compare_numbers = FALSE;
2097#ifdef FEAT_FLOAT
2098 info->item_compare_float = FALSE;
2099#endif
2100 info->item_compare_func = NULL;
2101 info->item_compare_partial = NULL;
2102 info->item_compare_selfdict = NULL;
2103
2104 if (argvars[1].v_type == VAR_UNKNOWN)
2105 return OK;
2106
2107 // optional second argument: {func}
2108 if (argvars[1].v_type == VAR_FUNC)
2109 info->item_compare_func = argvars[1].vval.v_string;
2110 else if (argvars[1].v_type == VAR_PARTIAL)
2111 info->item_compare_partial = argvars[1].vval.v_partial;
2112 else
2113 {
2114 int error = FALSE;
2115 int nr = 0;
2116
2117 if (argvars[1].v_type == VAR_NUMBER)
2118 {
2119 nr = tv_get_number_chk(&argvars[1], &error);
2120 if (error)
2121 return FAIL;
2122 if (nr == 1)
2123 info->item_compare_ic = TRUE;
2124 }
2125 if (nr != 1)
2126 {
2127 if (argvars[1].v_type != VAR_NUMBER)
2128 info->item_compare_func = tv_get_string(&argvars[1]);
2129 else if (nr != 0)
2130 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002131 emsg(_(e_invalid_argument));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002132 return FAIL;
2133 }
2134 }
2135 if (info->item_compare_func != NULL)
2136 {
2137 if (*info->item_compare_func == NUL)
2138 {
2139 // empty string means default sort
2140 info->item_compare_func = NULL;
2141 }
2142 else if (STRCMP(info->item_compare_func, "n") == 0)
2143 {
2144 info->item_compare_func = NULL;
2145 info->item_compare_numeric = TRUE;
2146 }
2147 else if (STRCMP(info->item_compare_func, "N") == 0)
2148 {
2149 info->item_compare_func = NULL;
2150 info->item_compare_numbers = TRUE;
2151 }
2152#ifdef FEAT_FLOAT
2153 else if (STRCMP(info->item_compare_func, "f") == 0)
2154 {
2155 info->item_compare_func = NULL;
2156 info->item_compare_float = TRUE;
2157 }
2158#endif
2159 else if (STRCMP(info->item_compare_func, "i") == 0)
2160 {
2161 info->item_compare_func = NULL;
2162 info->item_compare_ic = TRUE;
2163 }
2164 else if (STRCMP(info->item_compare_func, "l") == 0)
2165 {
2166 info->item_compare_func = NULL;
2167 info->item_compare_lc = TRUE;
2168 }
2169 }
2170 }
2171
2172 if (argvars[2].v_type != VAR_UNKNOWN)
2173 {
2174 // optional third argument: {dict}
2175 if (argvars[2].v_type != VAR_DICT)
2176 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002177 emsg(_(e_dictionary_required));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002178 return FAIL;
2179 }
2180 info->item_compare_selfdict = argvars[2].vval.v_dict;
2181 }
2182
2183 return OK;
2184}
2185
2186/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002187 * "sort()" or "uniq()" function
2188 */
2189 static void
2190do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
2191{
2192 list_T *l;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002193 sortinfo_T *old_sortinfo;
2194 sortinfo_T info;
2195 long len;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002196
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002197 if (in_vim9script()
2198 && (check_for_list_arg(argvars, 0) == FAIL
2199 || (argvars[1].v_type != VAR_UNKNOWN
2200 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
2201 return;
2202
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002203 if (argvars[0].v_type != VAR_LIST)
2204 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002205 semsg(_(e_argument_of_str_must_be_list), sort ? "sort()" : "uniq()");
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002206 return;
2207 }
2208
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002209 // Pointer to current info struct used in compare function. Save and
2210 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002211 old_sortinfo = sortinfo;
2212 sortinfo = &info;
2213
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002214 l = argvars[0].vval.v_list;
2215 if (l != NULL && value_check_lock(l->lv_lock,
2216 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
2217 TRUE))
2218 goto theend;
2219 rettv_list_set(rettv, l);
2220 if (l == NULL)
2221 goto theend;
2222 CHECK_LIST_MATERIALIZE(l);
2223
2224 len = list_len(l);
2225 if (len <= 1)
2226 goto theend; // short list sorts pretty quickly
2227
2228 if (parse_sort_uniq_args(argvars, &info) == FAIL)
2229 goto theend;
2230
2231 if (sort)
2232 do_sort(l, &info);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002233 else
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002234 do_uniq(l, &info);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002235
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02002236theend:
2237 sortinfo = old_sortinfo;
2238}
2239
2240/*
2241 * "sort({list})" function
2242 */
2243 void
2244f_sort(typval_T *argvars, typval_T *rettv)
2245{
2246 do_sort_uniq(argvars, rettv, TRUE);
2247}
2248
2249/*
2250 * "uniq({list})" function
2251 */
2252 void
2253f_uniq(typval_T *argvars, typval_T *rettv)
2254{
2255 do_sort_uniq(argvars, rettv, FALSE);
2256}
2257
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002258/*
2259 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01002260 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002261 */
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002262 int
Bram Moolenaarea696852020-11-09 18:31:39 +01002263filter_map_one(
2264 typval_T *tv, // original value
2265 typval_T *expr, // callback
2266 filtermap_T filtermap,
2267 typval_T *newtv, // for map() and mapnew(): new value
2268 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002269{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002270 typval_T argv[3];
2271 int retval = FAIL;
2272
2273 copy_tv(tv, get_vim_var_tv(VV_VAL));
2274 argv[0] = *get_vim_var_tv(VV_KEY);
2275 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01002276 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002277 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002278 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002279 {
2280 int error = FALSE;
2281
2282 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02002283 if (in_vim9script())
Bram Moolenaar18024052021-12-25 21:43:28 +00002284 *remp = !tv_get_bool_chk(newtv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002285 else
Bram Moolenaarea696852020-11-09 18:31:39 +01002286 *remp = (tv_get_number_chk(newtv, &error) == 0);
2287 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002288 // On type error, nothing has been removed; return FAIL to stop the
2289 // loop. The error message was given by tv_get_number_chk().
2290 if (error)
2291 goto theend;
2292 }
2293 retval = OK;
2294theend:
2295 clear_tv(get_vim_var_tv(VV_VAL));
2296 return retval;
2297}
2298
2299/*
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002300 * Implementation of map() and filter() for a List. Apply "expr" to every item
2301 * in List "l" and return the result in "rettv".
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002302 */
2303 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002304list_filter_map(
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002305 list_T *l,
2306 filtermap_T filtermap,
2307 type_T *argtype,
2308 char *func_name,
2309 char_u *arg_errmsg,
2310 typval_T *expr,
2311 typval_T *rettv)
2312{
2313 int prev_lock;
2314 list_T *l_ret = NULL;
2315 int idx = 0;
2316 int rem;
2317 listitem_T *li, *nli;
2318
2319 if (filtermap == FILTERMAP_MAPNEW)
2320 {
2321 rettv->v_type = VAR_LIST;
2322 rettv->vval.v_list = NULL;
2323 }
2324 if (l == NULL
2325 || (filtermap == FILTERMAP_FILTER
2326 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
2327 return;
2328
2329 prev_lock = l->lv_lock;
2330
2331 if (filtermap == FILTERMAP_MAPNEW)
2332 {
2333 if (rettv_list_alloc(rettv) == FAIL)
2334 return;
2335 l_ret = rettv->vval.v_list;
2336 }
2337 // set_vim_var_nr() doesn't set the type
2338 set_vim_var_type(VV_KEY, VAR_NUMBER);
2339
2340 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
2341 l->lv_lock = VAR_LOCKED;
2342
2343 if (l->lv_first == &range_list_item)
2344 {
2345 varnumber_T val = l->lv_u.nonmat.lv_start;
2346 int len = l->lv_len;
2347 int stride = l->lv_u.nonmat.lv_stride;
2348
2349 // List from range(): loop over the numbers
2350 if (filtermap != FILTERMAP_MAPNEW)
2351 {
2352 l->lv_first = NULL;
2353 l->lv_u.mat.lv_last = NULL;
2354 l->lv_len = 0;
2355 l->lv_u.mat.lv_idx_item = NULL;
2356 }
2357
2358 for (idx = 0; idx < len; ++idx)
2359 {
2360 typval_T tv;
2361 typval_T newtv;
2362
2363 tv.v_type = VAR_NUMBER;
2364 tv.v_lock = 0;
2365 tv.vval.v_number = val;
2366 set_vim_var_nr(VV_KEY, idx);
2367 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2368 == FAIL)
2369 break;
2370 if (did_emsg)
2371 {
2372 clear_tv(&newtv);
2373 break;
2374 }
2375 if (filtermap != FILTERMAP_FILTER)
2376 {
2377 if (filtermap == FILTERMAP_MAP && argtype != NULL
2378 && check_typval_arg_type(
2379 argtype->tt_member, &newtv,
2380 func_name, 0) == FAIL)
2381 {
2382 clear_tv(&newtv);
2383 break;
2384 }
2385 // map(), mapnew(): always append the new value to the
2386 // list
2387 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2388 ? l : l_ret, &newtv) == FAIL)
2389 break;
2390 }
2391 else if (!rem)
2392 {
2393 // filter(): append the list item value when not rem
2394 if (list_append_tv_move(l, &tv) == FAIL)
2395 break;
2396 }
2397
2398 val += stride;
2399 }
2400 }
2401 else
2402 {
2403 // Materialized list: loop over the items
2404 for (li = l->lv_first; li != NULL; li = nli)
2405 {
2406 typval_T newtv;
2407
2408 if (filtermap == FILTERMAP_MAP && value_check_lock(
2409 li->li_tv.v_lock, arg_errmsg, TRUE))
2410 break;
2411 nli = li->li_next;
2412 set_vim_var_nr(VV_KEY, idx);
2413 if (filter_map_one(&li->li_tv, expr, filtermap,
2414 &newtv, &rem) == FAIL)
2415 break;
2416 if (did_emsg)
2417 {
2418 clear_tv(&newtv);
2419 break;
2420 }
2421 if (filtermap == FILTERMAP_MAP)
2422 {
2423 if (argtype != NULL && check_typval_arg_type(
2424 argtype->tt_member, &newtv, func_name, 0) == FAIL)
2425 {
2426 clear_tv(&newtv);
2427 break;
2428 }
2429 // map(): replace the list item value
2430 clear_tv(&li->li_tv);
2431 newtv.v_lock = 0;
2432 li->li_tv = newtv;
2433 }
2434 else if (filtermap == FILTERMAP_MAPNEW)
2435 {
2436 // mapnew(): append the list item value
2437 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2438 break;
2439 }
2440 else if (filtermap == FILTERMAP_FILTER && rem)
2441 listitem_remove(l, li);
2442 ++idx;
2443 }
2444 }
2445
2446 l->lv_lock = prev_lock;
2447}
2448
2449/*
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002450 * Implementation of map() and filter().
2451 */
2452 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01002453filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002454{
2455 typval_T *expr;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002456 char *func_name = filtermap == FILTERMAP_MAP ? "map()"
Bram Moolenaarea696852020-11-09 18:31:39 +01002457 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002458 : "filter()";
Bram Moolenaarea696852020-11-09 18:31:39 +01002459 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2460 ? N_("map() argument")
2461 : filtermap == FILTERMAP_MAPNEW
2462 ? N_("mapnew() argument")
2463 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002464 int save_did_emsg;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002465 type_T *type = NULL;
2466 garray_T type_list;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002467
Bram Moolenaarea696852020-11-09 18:31:39 +01002468 // map() and filter() return the first argument, also on failure.
Bram Moolenaar2d877592021-12-16 08:21:09 +00002469 if (filtermap != FILTERMAP_MAPNEW && argvars[0].v_type != VAR_STRING)
Bram Moolenaarea696852020-11-09 18:31:39 +01002470 copy_tv(&argvars[0], rettv);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002471
2472 if (in_vim9script()
Bram Moolenaar2d877592021-12-16 08:21:09 +00002473 && (check_for_list_or_dict_or_blob_or_string_arg(argvars, 0)
2474 == FAIL))
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002475 return;
2476
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002477 if (filtermap == FILTERMAP_MAP && in_vim9script())
2478 {
2479 // Check that map() does not change the type of the dict.
2480 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaar114dbda2022-01-03 12:28:03 +00002481 type = typval2type(argvars, get_copyID(), &type_list, TVTT_DO_MEMBER);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002482 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002483
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002484 if (argvars[0].v_type != VAR_BLOB
2485 && argvars[0].v_type != VAR_LIST
2486 && argvars[0].v_type != VAR_DICT
2487 && argvars[0].v_type != VAR_STRING)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002488 {
rbtnnc479ce02021-12-15 19:14:54 +00002489 semsg(_(e_argument_of_str_must_be_list_string_dictionary_or_blob),
2490 func_name);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002491 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002492 }
2493
2494 expr = &argvars[1];
2495 // On type errors, the preceding call has already displayed an error
2496 // message. Avoid a misleading error message for an empty string that
2497 // was not passed as argument.
2498 if (expr->v_type != VAR_UNKNOWN)
2499 {
2500 typval_T save_val;
2501 typval_T save_key;
2502
2503 prepare_vimvar(VV_VAL, &save_val);
2504 prepare_vimvar(VV_KEY, &save_key);
2505
2506 // We reset "did_emsg" to be able to detect whether an error
2507 // occurred during evaluation of the expression.
2508 save_did_emsg = did_emsg;
2509 did_emsg = FALSE;
2510
2511 if (argvars[0].v_type == VAR_DICT)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002512 dict_filter_map(argvars[0].vval.v_dict, filtermap, type, func_name,
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002513 arg_errmsg, expr, rettv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002514 else if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002515 blob_filter_map(argvars[0].vval.v_blob, filtermap, expr, rettv);
rbtnnc479ce02021-12-15 19:14:54 +00002516 else if (argvars[0].v_type == VAR_STRING)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002517 string_filter_map(tv_get_string(&argvars[0]), filtermap, expr,
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002518 rettv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002519 else // argvars[0].v_type == VAR_LIST
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002520 list_filter_map(argvars[0].vval.v_list, filtermap, type, func_name,
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002521 arg_errmsg, expr, rettv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002522
2523 restore_vimvar(VV_KEY, &save_key);
2524 restore_vimvar(VV_VAL, &save_val);
2525
2526 did_emsg |= save_did_emsg;
2527 }
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002528
2529theend:
2530 if (type != NULL)
2531 clear_type_list(&type_list);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002532}
2533
2534/*
2535 * "filter()" function
2536 */
2537 void
2538f_filter(typval_T *argvars, typval_T *rettv)
2539{
Bram Moolenaarea696852020-11-09 18:31:39 +01002540 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002541}
2542
2543/*
2544 * "map()" function
2545 */
2546 void
2547f_map(typval_T *argvars, typval_T *rettv)
2548{
Bram Moolenaarea696852020-11-09 18:31:39 +01002549 filter_map(argvars, rettv, FILTERMAP_MAP);
2550}
2551
2552/*
2553 * "mapnew()" function
2554 */
2555 void
2556f_mapnew(typval_T *argvars, typval_T *rettv)
2557{
2558 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002559}
2560
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002561/*
2562 * "add(list, item)" function
2563 */
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002564 static void
2565list_add(typval_T *argvars, typval_T *rettv)
2566{
2567 list_T *l = argvars[0].vval.v_list;
2568
2569 if (l == NULL)
2570 {
2571 if (in_vim9script())
2572 emsg(_(e_cannot_add_to_null_list));
2573 }
2574 else if (!value_check_lock(l->lv_lock,
2575 (char_u *)N_("add() argument"), TRUE)
2576 && list_append_tv(l, &argvars[1]) == OK)
2577 {
2578 copy_tv(&argvars[0], rettv);
2579 }
2580}
2581
2582/*
2583 * "add(object, item)" function
2584 */
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002585 void
2586f_add(typval_T *argvars, typval_T *rettv)
2587{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002588 rettv->vval.v_number = 1; // Default: Failed
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002589
2590 if (in_vim9script()
2591 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2592 || (argvars[0].v_type == VAR_BLOB
2593 && check_for_number_arg(argvars, 1) == FAIL)))
2594 return;
2595
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002596 if (argvars[0].v_type == VAR_LIST)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002597 list_add(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002598 else if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002599 blob_add(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002600 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002601 emsg(_(e_list_or_blob_required));
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002602}
2603
2604/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002605 * Count the number of times item "needle" occurs in List "l" starting at index
2606 * "idx". Case is ignored if "ic" is TRUE.
2607 */
2608 static long
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002609list_count(list_T *l, typval_T *needle, long idx, int ic)
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002610{
2611 long n = 0;
2612 listitem_T *li;
2613
2614 if (l == NULL)
2615 return 0;
2616
2617 CHECK_LIST_MATERIALIZE(l);
2618
2619 if (list_len(l) == 0)
2620 return 0;
2621
2622 li = list_find(l, idx);
2623 if (li == NULL)
2624 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002625 semsg(_(e_list_index_out_of_range_nr), idx);
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002626 return 0;
2627 }
2628
2629 for ( ; li != NULL; li = li->li_next)
2630 if (tv_equal(&li->li_tv, needle, ic, FALSE))
2631 ++n;
2632
2633 return n;
2634}
2635
2636/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002637 * "count()" function
2638 */
2639 void
2640f_count(typval_T *argvars, typval_T *rettv)
2641{
2642 long n = 0;
2643 int ic = FALSE;
2644 int error = FALSE;
2645
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002646 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002647 && (check_for_string_or_list_or_dict_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002648 || check_for_opt_bool_arg(argvars, 2) == FAIL
2649 || (argvars[2].v_type != VAR_UNKNOWN
2650 && check_for_opt_number_arg(argvars, 3) == FAIL)))
2651 return;
2652
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002653 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002654 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002655
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002656 if (!error && argvars[0].v_type == VAR_STRING)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002657 n = string_count(argvars[0].vval.v_string,
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002658 tv_get_string_chk(&argvars[1]), ic);
2659 else if (!error && argvars[0].v_type == VAR_LIST)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002660 {
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002661 long idx = 0;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002662
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002663 if (argvars[2].v_type != VAR_UNKNOWN
2664 && argvars[3].v_type != VAR_UNKNOWN)
2665 idx = (long)tv_get_number_chk(&argvars[3], &error);
2666 if (!error)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002667 n = list_count(argvars[0].vval.v_list, &argvars[1], idx, ic);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002668 }
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002669 else if (!error && argvars[0].v_type == VAR_DICT)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002670 {
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002671 if (argvars[2].v_type != VAR_UNKNOWN
2672 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002673 emsg(_(e_invalid_argument));
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002674 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002675 n = dict_count(argvars[0].vval.v_dict, &argvars[1], ic);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002676 }
2677 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002678 semsg(_(e_argument_of_str_must_be_list_or_dictionary), "count()");
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002679 rettv->vval.v_number = n;
2680}
2681
2682/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002683 * extend() a List. Append List argvars[1] to List argvars[0] before index
2684 * argvars[3] and return the resulting list in "rettv". "is_new" is TRUE for
2685 * extendnew().
2686 */
2687 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002688list_extend_func(
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002689 typval_T *argvars,
2690 type_T *type,
2691 char *func_name,
2692 char_u *arg_errmsg,
2693 int is_new,
2694 typval_T *rettv)
2695{
2696 list_T *l1, *l2;
2697 listitem_T *item;
2698 long before;
2699 int error = FALSE;
2700
2701 l1 = argvars[0].vval.v_list;
2702 if (l1 == NULL)
2703 {
2704 emsg(_(e_cannot_extend_null_list));
2705 return;
2706 }
2707 l2 = argvars[1].vval.v_list;
2708 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
2709 && l2 != NULL)
2710 {
2711 if (is_new)
2712 {
2713 l1 = list_copy(l1, FALSE, get_copyID());
2714 if (l1 == NULL)
2715 return;
2716 }
2717
2718 if (argvars[2].v_type != VAR_UNKNOWN)
2719 {
2720 before = (long)tv_get_number_chk(&argvars[2], &error);
2721 if (error)
2722 return; // type error; errmsg already given
2723
2724 if (before == l1->lv_len)
2725 item = NULL;
2726 else
2727 {
2728 item = list_find(l1, before);
2729 if (item == NULL)
2730 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002731 semsg(_(e_list_index_out_of_range_nr), before);
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002732 return;
2733 }
2734 }
2735 }
2736 else
2737 item = NULL;
2738 if (type != NULL && check_typval_arg_type(
2739 type, &argvars[1], func_name, 2) == FAIL)
2740 return;
2741 list_extend(l1, l2, item);
2742
2743 if (is_new)
2744 {
2745 rettv->v_type = VAR_LIST;
2746 rettv->vval.v_list = l1;
2747 rettv->v_lock = FALSE;
2748 }
2749 else
2750 copy_tv(&argvars[0], rettv);
2751 }
2752}
2753
2754/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002755 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002756 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002757 static void
2758extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002759{
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002760 type_T *type = NULL;
2761 garray_T type_list;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002762 char *func_name = is_new ? "extendnew()" : "extend()";
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002763
2764 if (!is_new && in_vim9script())
2765 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00002766 // Check that extend() does not change the type of the dict.
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002767 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaar114dbda2022-01-03 12:28:03 +00002768 type = typval2type(argvars, get_copyID(), &type_list, TVTT_DO_MEMBER);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002769 }
2770
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002771 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002772 list_extend_func(argvars, type, func_name, arg_errmsg, is_new, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002773 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002774 dict_extend_func(argvars, type, func_name, arg_errmsg, is_new, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002775 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002776 semsg(_(e_argument_of_str_must_be_list_or_dictionary), func_name);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002777
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002778 if (type != NULL)
2779 clear_type_list(&type_list);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002780}
2781
2782/*
2783 * "extend(list, list [, idx])" function
2784 * "extend(dict, dict [, action])" function
2785 */
2786 void
2787f_extend(typval_T *argvars, typval_T *rettv)
2788{
2789 char_u *errmsg = (char_u *)N_("extend() argument");
2790
2791 extend(argvars, rettv, errmsg, FALSE);
2792}
2793
2794/*
2795 * "extendnew(list, list [, idx])" function
2796 * "extendnew(dict, dict [, action])" function
2797 */
2798 void
2799f_extendnew(typval_T *argvars, typval_T *rettv)
2800{
2801 char_u *errmsg = (char_u *)N_("extendnew() argument");
2802
2803 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002804}
2805
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002806 static void
2807list_insert_func(typval_T *argvars, typval_T *rettv)
2808{
2809 list_T *l = argvars[0].vval.v_list;
2810 long before = 0;
2811 listitem_T *item;
2812 int error = FALSE;
2813
2814 if (l == NULL)
2815 {
2816 if (in_vim9script())
2817 emsg(_(e_cannot_add_to_null_list));
2818 return;
2819 }
2820
2821 if (value_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
2822 return;
2823
2824 if (argvars[2].v_type != VAR_UNKNOWN)
2825 before = (long)tv_get_number_chk(&argvars[2], &error);
2826 if (error)
2827 return; // type error; errmsg already given
2828
2829 if (before == l->lv_len)
2830 item = NULL;
2831 else
2832 {
2833 item = list_find(l, before);
2834 if (item == NULL)
2835 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002836 semsg(_(e_list_index_out_of_range_nr), before);
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002837 l = NULL;
2838 }
2839 }
2840 if (l != NULL)
2841 {
2842 (void)list_insert_tv(l, &argvars[1], item);
2843 copy_tv(&argvars[0], rettv);
2844 }
2845}
2846
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002847/*
2848 * "insert()" function
2849 */
2850 void
2851f_insert(typval_T *argvars, typval_T *rettv)
2852{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002853 if (in_vim9script()
2854 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2855 || (argvars[0].v_type == VAR_BLOB
2856 && check_for_number_arg(argvars, 1) == FAIL)
2857 || check_for_opt_number_arg(argvars, 2) == FAIL))
2858 return;
2859
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002860 if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002861 blob_insert_func(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002862 else if (argvars[0].v_type != VAR_LIST)
2863 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002864 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002865 list_insert_func(argvars, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002866}
2867
2868/*
2869 * "remove()" function
2870 */
2871 void
2872f_remove(typval_T *argvars, typval_T *rettv)
2873{
2874 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2875
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002876 if (in_vim9script()
2877 && (check_for_list_or_dict_or_blob_arg(argvars, 0) == FAIL
2878 || ((argvars[0].v_type == VAR_LIST
2879 || argvars[0].v_type == VAR_BLOB)
2880 && (check_for_number_arg(argvars, 1) == FAIL
2881 || check_for_opt_number_arg(argvars, 2) == FAIL))
2882 || (argvars[0].v_type == VAR_DICT
2883 && check_for_string_or_number_arg(argvars, 1) == FAIL)))
2884 return;
2885
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002886 if (argvars[0].v_type == VAR_DICT)
2887 dict_remove(argvars, rettv, arg_errmsg);
2888 else if (argvars[0].v_type == VAR_BLOB)
Sean Dewar80d73952021-08-04 19:25:54 +02002889 blob_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002890 else if (argvars[0].v_type == VAR_LIST)
2891 list_remove(argvars, rettv, arg_errmsg);
2892 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002893 semsg(_(e_argument_of_str_must_be_list_dictionary_or_blob), "remove()");
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002894}
2895
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002896 static void
2897list_reverse(list_T *l, typval_T *rettv)
2898{
2899 listitem_T *li, *ni;
2900
2901 rettv_list_set(rettv, l);
2902 if (l != NULL
2903 && !value_check_lock(l->lv_lock,
2904 (char_u *)N_("reverse() argument"), TRUE))
2905 {
2906 if (l->lv_first == &range_list_item)
2907 {
2908 varnumber_T new_start = l->lv_u.nonmat.lv_start
2909 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2910 l->lv_u.nonmat.lv_end = new_start
2911 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2912 l->lv_u.nonmat.lv_start = new_start;
2913 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
2914 return;
2915 }
2916 li = l->lv_u.mat.lv_last;
2917 l->lv_first = l->lv_u.mat.lv_last = NULL;
2918 l->lv_len = 0;
2919 while (li != NULL)
2920 {
2921 ni = li->li_prev;
2922 list_append(l, li);
2923 li = ni;
2924 }
2925 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
2926 }
2927}
2928
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002929/*
2930 * "reverse({list})" function
2931 */
2932 void
2933f_reverse(typval_T *argvars, typval_T *rettv)
2934{
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002935 if (in_vim9script() && check_for_list_or_blob_arg(argvars, 0) == FAIL)
2936 return;
2937
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002938 if (argvars[0].v_type == VAR_BLOB)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002939 blob_reverse(argvars[0].vval.v_blob, rettv);
2940 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002941 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaaref982572021-08-12 19:27:57 +02002942 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002943 list_reverse(argvars[0].vval.v_list, rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002944}
2945
Bram Moolenaar85629982020-06-01 18:39:20 +02002946/*
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00002947 * reduce() List argvars[0] using the function 'funcname' with arguments in
2948 * 'funcexe' starting with the initial value argvars[2] and return the result
2949 * in 'rettv'.
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002950 */
2951 static void
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00002952list_reduce(
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002953 typval_T *argvars,
2954 char_u *func_name,
2955 funcexe_T *funcexe,
2956 typval_T *rettv)
2957{
2958 list_T *l = argvars[0].vval.v_list;
2959 listitem_T *li = NULL;
2960 typval_T initial;
2961 typval_T argv[3];
2962 int r;
2963 int called_emsg_start = called_emsg;
2964 int prev_locked;
2965
2966 if (l != NULL)
2967 CHECK_LIST_MATERIALIZE(l);
2968 if (argvars[2].v_type == VAR_UNKNOWN)
2969 {
2970 if (l == NULL || l->lv_first == NULL)
2971 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002972 semsg(_(e_reduce_of_an_empty_str_with_no_initial_value), "List");
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00002973 return;
2974 }
2975 initial = l->lv_first->li_tv;
2976 li = l->lv_first->li_next;
2977 }
2978 else
2979 {
2980 initial = argvars[2];
2981 if (l != NULL)
2982 li = l->lv_first;
2983 }
2984 copy_tv(&initial, rettv);
2985
2986 if (l == NULL)
2987 return;
2988
2989 prev_locked = l->lv_lock;
2990
2991 l->lv_lock = VAR_FIXED; // disallow the list changing here
2992 for ( ; li != NULL; li = li->li_next)
2993 {
2994 argv[0] = *rettv;
2995 argv[1] = li->li_tv;
2996 rettv->v_type = VAR_UNKNOWN;
2997 r = call_func(func_name, -1, rettv, 2, argv, funcexe);
2998 clear_tv(&argv[0]);
2999 if (r == FAIL || called_emsg != called_emsg_start)
3000 break;
3001 }
3002 l->lv_lock = prev_locked;
3003}
3004
3005/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003006 * "reduce(list, { accumulator, element -> value } [, initial])" function
Yegappan Lakshmanand92813a2021-12-21 13:19:42 +00003007 * "reduce(blob, { accumulator, element -> value } [, initial])"
3008 * "reduce(string, { accumulator, element -> value } [, initial])"
Bram Moolenaar85629982020-06-01 18:39:20 +02003009 */
3010 void
3011f_reduce(typval_T *argvars, typval_T *rettv)
3012{
Bram Moolenaar85629982020-06-01 18:39:20 +02003013 char_u *func_name;
3014 partial_T *partial = NULL;
3015 funcexe_T funcexe;
Bram Moolenaar85629982020-06-01 18:39:20 +02003016
rbtnn0ccb5842021-12-18 18:33:46 +00003017 if (in_vim9script()
3018 && check_for_string_or_list_or_blob_arg(argvars, 0) == FAIL)
Bram Moolenaar85629982020-06-01 18:39:20 +02003019 return;
rbtnn0ccb5842021-12-18 18:33:46 +00003020
3021 if (argvars[0].v_type != VAR_STRING
3022 && argvars[0].v_type != VAR_LIST
3023 && argvars[0].v_type != VAR_BLOB)
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003024 {
rbtnn0ccb5842021-12-18 18:33:46 +00003025 semsg(_(e_string_list_or_blob_required), "reduce()");
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00003026 return;
3027 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003028
3029 if (argvars[1].v_type == VAR_FUNC)
3030 func_name = argvars[1].vval.v_string;
3031 else if (argvars[1].v_type == VAR_PARTIAL)
3032 {
3033 partial = argvars[1].vval.v_partial;
3034 func_name = partial_name(partial);
3035 }
3036 else
3037 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01003038 if (func_name == NULL || *func_name == NUL)
3039 {
3040 emsg(_(e_missing_function_argument));
3041 return;
3042 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003043
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003044 CLEAR_FIELD(funcexe);
3045 funcexe.fe_evaluate = TRUE;
3046 funcexe.fe_partial = partial;
Bram Moolenaar85629982020-06-01 18:39:20 +02003047
3048 if (argvars[0].v_type == VAR_LIST)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003049 list_reduce(argvars, func_name, &funcexe, rettv);
rbtnn0ccb5842021-12-18 18:33:46 +00003050 else if (argvars[0].v_type == VAR_STRING)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003051 string_reduce(argvars, func_name, &funcexe, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003052 else
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +00003053 blob_reduce(argvars, func_name, &funcexe, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003054}
3055
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02003056#endif // defined(FEAT_EVAL)