blob: 484ce74c985581c6c1fc81e6e6524ff3e3710a23 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarda861d62016-07-17 15:46:27 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +020011 * list.c: List support and container (List, Dict, Blob) functions.
Bram Moolenaarda861d62016-07-17 15:46:27 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar08c308a2019-09-04 17:48:15 +020018static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
19
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010020// List heads for garbage collection.
21static list_T *first_list = NULL; // list of all lists
Bram Moolenaarda861d62016-07-17 15:46:27 +020022
Bram Moolenaar00d253e2020-04-06 22:13:01 +020023#define FOR_ALL_WATCHERS(l, lw) \
24 for ((lw) = (l)->lv_watch; (lw) != NULL; (lw) = (lw)->lw_next)
25
Bram Moolenaarbdff0122020-04-05 18:56:05 +020026static void list_free_item(list_T *l, listitem_T *item);
27
Bram Moolenaarda861d62016-07-17 15:46:27 +020028/*
29 * Add a watcher to a list.
30 */
31 void
32list_add_watch(list_T *l, listwatch_T *lw)
33{
34 lw->lw_next = l->lv_watch;
35 l->lv_watch = lw;
36}
37
38/*
39 * Remove a watcher from a list.
40 * No warning when it isn't found...
41 */
42 void
43list_rem_watch(list_T *l, listwatch_T *lwrem)
44{
45 listwatch_T *lw, **lwp;
46
47 lwp = &l->lv_watch;
Bram Moolenaar00d253e2020-04-06 22:13:01 +020048 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020049 {
50 if (lw == lwrem)
51 {
52 *lwp = lw->lw_next;
53 break;
54 }
55 lwp = &lw->lw_next;
56 }
57}
58
59/*
60 * Just before removing an item from a list: advance watchers to the next
61 * item.
62 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020063 static void
Bram Moolenaarda861d62016-07-17 15:46:27 +020064list_fix_watch(list_T *l, listitem_T *item)
65{
66 listwatch_T *lw;
67
Bram Moolenaar00d253e2020-04-06 22:13:01 +020068 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020069 if (lw->lw_item == item)
70 lw->lw_item = item->li_next;
71}
72
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073 static void
74list_init(list_T *l)
75{
76 // Prepend the list to the list of lists for garbage collection.
77 if (first_list != NULL)
78 first_list->lv_used_prev = l;
79 l->lv_used_prev = NULL;
80 l->lv_used_next = first_list;
81 first_list = l;
82}
83
Bram Moolenaarda861d62016-07-17 15:46:27 +020084/*
85 * Allocate an empty header for a list.
86 * Caller should take care of the reference count.
87 */
88 list_T *
89list_alloc(void)
90{
91 list_T *l;
92
Bram Moolenaarc799fe22019-05-28 23:08:19 +020093 l = ALLOC_CLEAR_ONE(list_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +020094 if (l != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095 list_init(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +020096 return l;
97}
98
99/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100100 * list_alloc() with an ID for alloc_fail().
101 */
102 list_T *
103list_alloc_id(alloc_id_T id UNUSED)
104{
105#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200106 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100107 return NULL;
108#endif
109 return (list_alloc());
110}
111
112/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 * Allocate space for a list, plus "count" items.
114 * Next list_set_item() must be called for each item.
115 */
116 list_T *
117list_alloc_with_items(int count)
118{
119 list_T *l;
120
121 l = (list_T *)alloc_clear(sizeof(list_T) + count * sizeof(listitem_T));
122 if (l != NULL)
123 {
124 list_init(l);
125
126 if (count > 0)
127 {
128 listitem_T *li = (listitem_T *)(l + 1);
129 int i;
130
131 l->lv_len = count;
132 l->lv_with_items = count;
133 l->lv_first = li;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100134 l->lv_u.mat.lv_last = li + count - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 for (i = 0; i < count; ++i)
136 {
137 if (i == 0)
138 li->li_prev = NULL;
139 else
140 li->li_prev = li - 1;
141 if (i == count - 1)
142 li->li_next = NULL;
143 else
144 li->li_next = li + 1;
145 ++li;
146 }
147 }
148 }
149 return l;
150}
151
152/*
153 * Set item "idx" for a list previously allocated with list_alloc_with_items().
154 * The contents of "tv" is moved into the list item.
155 * Each item must be set exactly once.
156 */
157 void
158list_set_item(list_T *l, int idx, typval_T *tv)
159{
160 listitem_T *li = (listitem_T *)(l + 1) + idx;
161
162 li->li_tv = *tv;
163}
164
165/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200166 * Allocate an empty list for a return value, with reference count set.
167 * Returns OK or FAIL.
168 */
169 int
170rettv_list_alloc(typval_T *rettv)
171{
172 list_T *l = list_alloc();
173
174 if (l == NULL)
175 return FAIL;
176
Bram Moolenaarda861d62016-07-17 15:46:27 +0200177 rettv->v_lock = 0;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200178 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200179 return OK;
180}
181
182/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100183 * Same as rettv_list_alloc() but uses an allocation id for testing.
184 */
185 int
186rettv_list_alloc_id(typval_T *rettv, alloc_id_T id UNUSED)
187{
188#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200189 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaar162b7142018-12-21 15:17:36 +0100190 return FAIL;
191#endif
192 return rettv_list_alloc(rettv);
193}
194
195
196/*
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200197 * Set a list as the return value. Increments the reference count.
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200198 */
199 void
200rettv_list_set(typval_T *rettv, list_T *l)
201{
202 rettv->v_type = VAR_LIST;
203 rettv->vval.v_list = l;
204 if (l != NULL)
205 ++l->lv_refcount;
206}
207
208/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200209 * Unreference a list: decrement the reference count and free it when it
210 * becomes zero.
211 */
212 void
213list_unref(list_T *l)
214{
215 if (l != NULL && --l->lv_refcount <= 0)
216 list_free(l);
217}
218
219/*
220 * Free a list, including all non-container items it points to.
221 * Ignores the reference count.
222 */
223 static void
224list_free_contents(list_T *l)
225{
226 listitem_T *item;
227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 if (l->lv_first != &range_list_item)
229 for (item = l->lv_first; item != NULL; item = l->lv_first)
230 {
231 // Remove the item before deleting it.
232 l->lv_first = item->li_next;
233 clear_tv(&item->li_tv);
234 list_free_item(l, item);
235 }
Bram Moolenaarda861d62016-07-17 15:46:27 +0200236}
237
238/*
239 * Go through the list of lists and free items without the copyID.
240 * But don't free a list that has a watcher (used in a for loop), these
241 * are not referenced anywhere.
242 */
243 int
244list_free_nonref(int copyID)
245{
246 list_T *ll;
247 int did_free = FALSE;
248
249 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
250 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
251 && ll->lv_watch == NULL)
252 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100253 // Free the List and ordinary items it contains, but don't recurse
254 // into Lists and Dictionaries, they will be in the list of dicts
255 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200256 list_free_contents(ll);
257 did_free = TRUE;
258 }
259 return did_free;
260}
261
262 static void
263list_free_list(list_T *l)
264{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100265 // Remove the list from the list of lists for garbage collection.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200266 if (l->lv_used_prev == NULL)
267 first_list = l->lv_used_next;
268 else
269 l->lv_used_prev->lv_used_next = l->lv_used_next;
270 if (l->lv_used_next != NULL)
271 l->lv_used_next->lv_used_prev = l->lv_used_prev;
272
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100273 free_type(l->lv_type);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200274 vim_free(l);
275}
276
277 void
278list_free_items(int copyID)
279{
280 list_T *ll, *ll_next;
281
282 for (ll = first_list; ll != NULL; ll = ll_next)
283 {
284 ll_next = ll->lv_used_next;
285 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
286 && ll->lv_watch == NULL)
287 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100288 // Free the List and ordinary items it contains, but don't recurse
289 // into Lists and Dictionaries, they will be in the list of dicts
290 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200291 list_free_list(ll);
292 }
293 }
294}
295
296 void
297list_free(list_T *l)
298{
299 if (!in_free_unref_items)
300 {
301 list_free_contents(l);
302 list_free_list(l);
303 }
304}
305
306/*
307 * Allocate a list item.
308 * It is not initialized, don't forget to set v_lock.
309 */
310 listitem_T *
311listitem_alloc(void)
312{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200313 return ALLOC_ONE(listitem_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200314}
315
316/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100317 * Free a list item, unless it was allocated together with the list itself.
318 * Does not clear the value. Does not notify watchers.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200319 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200320 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100321list_free_item(list_T *l, listitem_T *item)
322{
323 if (l->lv_with_items == 0 || item < (listitem_T *)l
324 || item >= (listitem_T *)(l + 1) + l->lv_with_items)
325 vim_free(item);
326}
327
328/*
329 * Free a list item, unless it was allocated together with the list itself.
330 * Also clears the value. Does not notify watchers.
331 */
332 void
333listitem_free(list_T *l, listitem_T *item)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200334{
335 clear_tv(&item->li_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 list_free_item(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200337}
338
339/*
340 * Remove a list item from a List and free it. Also clears the value.
341 */
342 void
343listitem_remove(list_T *l, listitem_T *item)
344{
345 vimlist_remove(l, item, item);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 listitem_free(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200347}
348
349/*
350 * Get the number of items in a list.
351 */
352 long
353list_len(list_T *l)
354{
355 if (l == NULL)
356 return 0L;
357 return l->lv_len;
358}
359
360/*
361 * Return TRUE when two lists have exactly the same values.
362 */
363 int
364list_equal(
365 list_T *l1,
366 list_T *l2,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100367 int ic, // ignore case for strings
368 int recursive) // TRUE when used recursively
Bram Moolenaarda861d62016-07-17 15:46:27 +0200369{
370 listitem_T *item1, *item2;
371
Bram Moolenaarda861d62016-07-17 15:46:27 +0200372 if (l1 == l2)
373 return TRUE;
374 if (list_len(l1) != list_len(l2))
375 return FALSE;
Bram Moolenaar7b293c72020-04-09 21:33:22 +0200376 if (list_len(l1) == 0)
377 // empty and NULL list are considered equal
378 return TRUE;
379 if (l1 == NULL || l2 == NULL)
380 return FALSE;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200381
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200382 CHECK_LIST_MATERIALIZE(l1);
383 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384
Bram Moolenaarda861d62016-07-17 15:46:27 +0200385 for (item1 = l1->lv_first, item2 = l2->lv_first;
386 item1 != NULL && item2 != NULL;
387 item1 = item1->li_next, item2 = item2->li_next)
388 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
389 return FALSE;
390 return item1 == NULL && item2 == NULL;
391}
392
393/*
394 * Locate item with index "n" in list "l" and return it.
395 * A negative index is counted from the end; -1 is the last item.
396 * Returns NULL when "n" is out of range.
397 */
398 listitem_T *
399list_find(list_T *l, long n)
400{
401 listitem_T *item;
402 long idx;
403
404 if (l == NULL)
405 return NULL;
406
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100407 // Negative index is relative to the end.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200408 if (n < 0)
409 n = l->lv_len + n;
410
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100411 // Check for index out of range.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200412 if (n < 0 || n >= l->lv_len)
413 return NULL;
414
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200415 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100417 // When there is a cached index may start search from there.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100418 if (l->lv_u.mat.lv_idx_item != NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200419 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100420 if (n < l->lv_u.mat.lv_idx / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200421 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100422 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200423 item = l->lv_first;
424 idx = 0;
425 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100426 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200427 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100428 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100429 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200430 idx = l->lv_len - 1;
431 }
432 else
433 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100434 // closest to the cached index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100435 item = l->lv_u.mat.lv_idx_item;
436 idx = l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200437 }
438 }
439 else
440 {
441 if (n < l->lv_len / 2)
442 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100443 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200444 item = l->lv_first;
445 idx = 0;
446 }
447 else
448 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100449 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100450 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200451 idx = l->lv_len - 1;
452 }
453 }
454
455 while (n > idx)
456 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100457 // search forward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200458 item = item->li_next;
459 ++idx;
460 }
461 while (n < idx)
462 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100463 // search backward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200464 item = item->li_prev;
465 --idx;
466 }
467
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100468 // cache the used index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100469 l->lv_u.mat.lv_idx = idx;
470 l->lv_u.mat.lv_idx_item = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200471
472 return item;
473}
474
475/*
476 * Get list item "l[idx]" as a number.
477 */
478 long
479list_find_nr(
480 list_T *l,
481 long idx,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100482 int *errorp) // set to TRUE when something wrong
Bram Moolenaarda861d62016-07-17 15:46:27 +0200483{
484 listitem_T *li;
485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486 if (l != NULL && l->lv_first == &range_list_item)
487 {
488 long n = idx;
489
490 // not materialized range() list: compute the value.
491 // Negative index is relative to the end.
492 if (n < 0)
493 n = l->lv_len + n;
494
495 // Check for index out of range.
496 if (n < 0 || n >= l->lv_len)
497 {
498 if (errorp != NULL)
499 *errorp = TRUE;
500 return -1L;
501 }
502
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100503 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 }
505
Bram Moolenaarda861d62016-07-17 15:46:27 +0200506 li = list_find(l, idx);
507 if (li == NULL)
508 {
509 if (errorp != NULL)
510 *errorp = TRUE;
511 return -1L;
512 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100513 return (long)tv_get_number_chk(&li->li_tv, errorp);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200514}
515
516/*
517 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
518 */
519 char_u *
520list_find_str(list_T *l, long idx)
521{
522 listitem_T *li;
523
524 li = list_find(l, idx - 1);
525 if (li == NULL)
526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 semsg(_(e_listidx), idx);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200528 return NULL;
529 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100530 return tv_get_string(&li->li_tv);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200531}
532
533/*
534 * Locate "item" list "l" and return its index.
535 * Returns -1 when "item" is not in the list.
536 */
537 long
538list_idx_of_item(list_T *l, listitem_T *item)
539{
540 long idx = 0;
541 listitem_T *li;
542
543 if (l == NULL)
544 return -1;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200545 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200546 idx = 0;
547 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
548 ++idx;
549 if (li == NULL)
550 return -1;
551 return idx;
552}
553
554/*
555 * Append item "item" to the end of list "l".
556 */
557 void
558list_append(list_T *l, listitem_T *item)
559{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200560 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100561 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200562 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100563 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200564 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100565 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200566 item->li_prev = NULL;
567 }
568 else
569 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100570 l->lv_u.mat.lv_last->li_next = item;
571 item->li_prev = l->lv_u.mat.lv_last;
572 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200573 }
574 ++l->lv_len;
575 item->li_next = NULL;
576}
577
578/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200580 * Return FAIL when out of memory.
581 */
582 int
583list_append_tv(list_T *l, typval_T *tv)
584{
585 listitem_T *li = listitem_alloc();
586
587 if (li == NULL)
588 return FAIL;
589 copy_tv(tv, &li->li_tv);
590 list_append(l, li);
591 return OK;
592}
593
594/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595 * As list_append_tv() but move the value instead of copying it.
596 * Return FAIL when out of memory.
597 */
598 int
599list_append_tv_move(list_T *l, typval_T *tv)
600{
601 listitem_T *li = listitem_alloc();
602
603 if (li == NULL)
604 return FAIL;
605 li->li_tv = *tv;
606 list_append(l, li);
607 return OK;
608}
609
610/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200611 * Add a dictionary to a list. Used by getqflist().
612 * Return FAIL when out of memory.
613 */
614 int
615list_append_dict(list_T *list, dict_T *dict)
616{
617 listitem_T *li = listitem_alloc();
618
619 if (li == NULL)
620 return FAIL;
621 li->li_tv.v_type = VAR_DICT;
622 li->li_tv.v_lock = 0;
623 li->li_tv.vval.v_dict = dict;
624 list_append(list, li);
625 ++dict->dv_refcount;
626 return OK;
627}
628
629/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100630 * Append list2 to list1.
631 * Return FAIL when out of memory.
632 */
633 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200634list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100635{
636 listitem_T *li = listitem_alloc();
637
638 if (li == NULL)
639 return FAIL;
640 li->li_tv.v_type = VAR_LIST;
641 li->li_tv.v_lock = 0;
642 li->li_tv.vval.v_list = list2;
643 list_append(list1, li);
644 ++list2->lv_refcount;
645 return OK;
646}
647
648/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200649 * Make a copy of "str" and append it as an item to list "l".
650 * When "len" >= 0 use "str[len]".
651 * Returns FAIL when out of memory.
652 */
653 int
654list_append_string(list_T *l, char_u *str, int len)
655{
656 listitem_T *li = listitem_alloc();
657
658 if (li == NULL)
659 return FAIL;
660 list_append(l, li);
661 li->li_tv.v_type = VAR_STRING;
662 li->li_tv.v_lock = 0;
663 if (str == NULL)
664 li->li_tv.vval.v_string = NULL;
665 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
666 : vim_strsave(str))) == NULL)
667 return FAIL;
668 return OK;
669}
670
671/*
672 * Append "n" to list "l".
673 * Returns FAIL when out of memory.
674 */
675 int
676list_append_number(list_T *l, varnumber_T n)
677{
678 listitem_T *li;
679
680 li = listitem_alloc();
681 if (li == NULL)
682 return FAIL;
683 li->li_tv.v_type = VAR_NUMBER;
684 li->li_tv.v_lock = 0;
685 li->li_tv.vval.v_number = n;
686 list_append(l, li);
687 return OK;
688}
689
690/*
691 * Insert typval_T "tv" in list "l" before "item".
692 * If "item" is NULL append at the end.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100693 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200694 */
695 int
696list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
697{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100698 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200699
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100700 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100701 && check_typval_arg_type(l->lv_type->tt_member, tv, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100702 return FAIL;
703 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200704 if (ni == NULL)
705 return FAIL;
706 copy_tv(tv, &ni->li_tv);
707 list_insert(l, ni, item);
708 return OK;
709}
710
711 void
712list_insert(list_T *l, listitem_T *ni, listitem_T *item)
713{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200714 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200715 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100716 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200717 list_append(l, ni);
718 else
719 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100720 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200721 ni->li_prev = item->li_prev;
722 ni->li_next = item;
723 if (item->li_prev == NULL)
724 {
725 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100726 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200727 }
728 else
729 {
730 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100731 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200732 }
733 item->li_prev = ni;
734 ++l->lv_len;
735 }
736}
737
738/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200739 * Flatten "list" to depth "maxdepth".
740 * It does nothing if "maxdepth" is 0.
741 * Returns FAIL when out of memory.
742 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100743 static void
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200744list_flatten(list_T *list, long maxdepth)
745{
746 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200747 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200748 int n;
749
750 if (maxdepth == 0)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100751 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200752 CHECK_LIST_MATERIALIZE(list);
753
754 n = 0;
755 item = list->lv_first;
756 while (item != NULL)
757 {
758 fast_breakcheck();
759 if (got_int)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100760 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200761
762 if (item->li_tv.v_type == VAR_LIST)
763 {
764 listitem_T *next = item->li_next;
765
766 vimlist_remove(list, item, item);
767 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100768 return;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200769 clear_tv(&item->li_tv);
770 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200771
772 if (item->li_prev == NULL)
773 item = list->lv_first;
774 else
775 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200776 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200777
778 if (++n >= maxdepth)
779 {
780 n = 0;
781 item = next;
782 }
783 }
784 else
785 {
786 n = 0;
787 item = item->li_next;
788 }
789 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200790}
791
792/*
Bram Moolenaar3b690062021-02-01 20:14:51 +0100793 * "flatten()" and "flattennew()" functions
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200794 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100795 static void
796flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200797{
798 list_T *l;
799 long maxdepth;
800 int error = FALSE;
801
802 if (argvars[0].v_type != VAR_LIST)
803 {
804 semsg(_(e_listarg), "flatten()");
805 return;
806 }
807
808 if (argvars[1].v_type == VAR_UNKNOWN)
809 maxdepth = 999999;
810 else
811 {
812 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
813 if (error)
814 return;
815 if (maxdepth < 0)
816 {
817 emsg(_("E900: maxdepth must be non-negative number"));
818 return;
819 }
820 }
821
822 l = argvars[0].vval.v_list;
Bram Moolenaar3b690062021-02-01 20:14:51 +0100823 rettv->v_type = VAR_LIST;
824 rettv->vval.v_list = l;
825 if (l == NULL)
826 return;
827
828 if (make_copy)
829 {
830 l = list_copy(l, TRUE, get_copyID());
831 rettv->vval.v_list = l;
832 if (l == NULL)
833 return;
834 }
835 else
836 {
837 if (value_check_lock(l->lv_lock,
838 (char_u *)N_("flatten() argument"), TRUE))
839 return;
840 ++l->lv_refcount;
841 }
842
843 list_flatten(l, maxdepth);
844}
845
846/*
847 * "flatten(list[, {maxdepth}])" function
848 */
849 void
850f_flatten(typval_T *argvars, typval_T *rettv)
851{
852 if (in_vim9script())
853 emsg(_(e_cannot_use_flatten_in_vim9_script));
854 else
855 flatten_common(argvars, rettv, FALSE);
856}
857
858/*
859 * "flattennew(list[, {maxdepth}])" function
860 */
861 void
862f_flattennew(typval_T *argvars, typval_T *rettv)
863{
864 flatten_common(argvars, rettv, TRUE);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200865}
866
867/*
Bram Moolenaar1a739232020-10-10 15:37:58 +0200868 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200869 * If "bef" is NULL append at the end, otherwise insert before this item.
870 * Returns FAIL when out of memory.
871 */
872 int
873list_extend(list_T *l1, list_T *l2, listitem_T *bef)
874{
875 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200876 int todo;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200877
Bram Moolenaar1a739232020-10-10 15:37:58 +0200878 // NULL list is equivalent to an empty list: nothing to do.
879 if (l2 == NULL || l2->lv_len == 0)
880 return OK;
881
882 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200883 CHECK_LIST_MATERIALIZE(l1);
884 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100886 // We also quit the loop when we have inserted the original item count of
887 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200888 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
889 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
890 return FAIL;
891 return OK;
892}
893
894/*
895 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
896 * Return FAIL when out of memory.
897 */
898 int
899list_concat(list_T *l1, list_T *l2, typval_T *tv)
900{
901 list_T *l;
902
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100903 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +0200904 if (l1 == NULL)
905 l = list_alloc();
906 else
907 l = list_copy(l1, FALSE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200908 if (l == NULL)
909 return FAIL;
910 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +0100911 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200912 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200913 if (l1 == NULL)
914 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200915
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100916 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200917 return list_extend(l, l2, NULL);
918}
919
Bram Moolenaar9af78762020-06-16 11:34:42 +0200920 list_T *
921list_slice(list_T *ol, long n1, long n2)
922{
923 listitem_T *item;
924 list_T *l = list_alloc();
925
926 if (l == NULL)
927 return NULL;
928 for (item = list_find(ol, n1); n1 <= n2; ++n1)
929 {
930 if (list_append_tv(l, &item->li_tv) == FAIL)
931 {
932 list_free(l);
933 return NULL;
934 }
935 item = item->li_next;
936 }
937 return l;
938}
939
Bram Moolenaared591872020-08-15 22:14:53 +0200940 int
941list_slice_or_index(
942 list_T *list,
943 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +0100944 varnumber_T n1_arg,
945 varnumber_T n2_arg,
946 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +0200947 typval_T *rettv,
948 int verbose)
949{
950 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +0100951 varnumber_T n1 = n1_arg;
952 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +0200953 typval_T var1;
954
955 if (n1 < 0)
956 n1 = len + n1;
957 if (n1 < 0 || n1 >= len)
958 {
959 // For a range we allow invalid values and return an empty
960 // list. A list index out of range is an error.
961 if (!range)
962 {
963 if (verbose)
Bram Moolenaar239f8d92021-01-17 13:21:20 +0100964 semsg(_(e_listidx), (long)n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +0200965 return FAIL;
966 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200967 n1 = n1 < 0 ? 0 : len;
Bram Moolenaared591872020-08-15 22:14:53 +0200968 }
969 if (range)
970 {
971 list_T *l;
972
973 if (n2 < 0)
974 n2 = len + n2;
975 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +0100976 n2 = len - (exclusive ? 0 : 1);
977 if (exclusive)
978 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +0200979 if (n2 < 0 || n2 + 1 < n1)
980 n2 = -1;
981 l = list_slice(list, n1, n2);
982 if (l == NULL)
983 return FAIL;
984 clear_tv(rettv);
985 rettv_list_set(rettv, l);
986 }
987 else
988 {
989 // copy the item to "var1" to avoid that freeing the list makes it
990 // invalid.
991 copy_tv(&list_find(list, n1)->li_tv, &var1);
992 clear_tv(rettv);
993 *rettv = var1;
994 }
995 return OK;
996}
997
Bram Moolenaarda861d62016-07-17 15:46:27 +0200998/*
999 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1000 * The refcount of the new list is set to 1.
1001 * See item_copy() for "copyID".
1002 * Returns NULL when out of memory.
1003 */
1004 list_T *
1005list_copy(list_T *orig, int deep, int copyID)
1006{
1007 list_T *copy;
1008 listitem_T *item;
1009 listitem_T *ni;
1010
1011 if (orig == NULL)
1012 return NULL;
1013
1014 copy = list_alloc();
1015 if (copy != NULL)
1016 {
1017 if (copyID != 0)
1018 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001019 // Do this before adding the items, because one of the items may
1020 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001021 orig->lv_copyID = copyID;
1022 orig->lv_copylist = copy;
1023 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001024 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001025 for (item = orig->lv_first; item != NULL && !got_int;
1026 item = item->li_next)
1027 {
1028 ni = listitem_alloc();
1029 if (ni == NULL)
1030 break;
1031 if (deep)
1032 {
1033 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
1034 {
1035 vim_free(ni);
1036 break;
1037 }
1038 }
1039 else
1040 copy_tv(&item->li_tv, &ni->li_tv);
1041 list_append(copy, ni);
1042 }
1043 ++copy->lv_refcount;
1044 if (item != NULL)
1045 {
1046 list_unref(copy);
1047 copy = NULL;
1048 }
1049 }
1050
1051 return copy;
1052}
1053
1054/*
1055 * Remove items "item" to "item2" from list "l".
1056 * Does not free the listitem or the value!
1057 * This used to be called list_remove, but that conflicts with a Sun header
1058 * file.
1059 */
1060 void
1061vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1062{
1063 listitem_T *ip;
1064
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001065 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001067 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001068 for (ip = item; ip != NULL; ip = ip->li_next)
1069 {
1070 --l->lv_len;
1071 list_fix_watch(l, ip);
1072 if (ip == item2)
1073 break;
1074 }
1075
1076 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001077 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001078 else
1079 item2->li_next->li_prev = item->li_prev;
1080 if (item->li_prev == NULL)
1081 l->lv_first = item2->li_next;
1082 else
1083 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001084 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001085}
1086
1087/*
1088 * Return an allocated string with the string representation of a list.
1089 * May return NULL.
1090 */
1091 char_u *
1092list2string(typval_T *tv, int copyID, int restore_copyID)
1093{
1094 garray_T ga;
1095
1096 if (tv->vval.v_list == NULL)
1097 return NULL;
1098 ga_init2(&ga, (int)sizeof(char), 80);
1099 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001100 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001101 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1102 FALSE, restore_copyID, copyID) == FAIL)
1103 {
1104 vim_free(ga.ga_data);
1105 return NULL;
1106 }
1107 ga_append(&ga, ']');
1108 ga_append(&ga, NUL);
1109 return (char_u *)ga.ga_data;
1110}
1111
1112typedef struct join_S {
1113 char_u *s;
1114 char_u *tofree;
1115} join_T;
1116
1117 static int
1118list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001119 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001120 list_T *l,
1121 char_u *sep,
1122 int echo_style,
1123 int restore_copyID,
1124 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001125 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001126{
1127 int i;
1128 join_T *p;
1129 int len;
1130 int sumlen = 0;
1131 int first = TRUE;
1132 char_u *tofree;
1133 char_u numbuf[NUMBUFLEN];
1134 listitem_T *item;
1135 char_u *s;
1136
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001137 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001138 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001139 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1140 {
1141 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001142 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001143 if (s == NULL)
1144 return FAIL;
1145
1146 len = (int)STRLEN(s);
1147 sumlen += len;
1148
1149 (void)ga_grow(join_gap, 1);
1150 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1151 if (tofree != NULL || s != numbuf)
1152 {
1153 p->s = s;
1154 p->tofree = tofree;
1155 }
1156 else
1157 {
1158 p->s = vim_strnsave(s, len);
1159 p->tofree = p->s;
1160 }
1161
1162 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001163 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001164 break;
1165 }
1166
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001167 // Allocate result buffer with its total size, avoid re-allocation and
1168 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001169 if (join_gap->ga_len >= 2)
1170 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1171 if (ga_grow(gap, sumlen + 2) == FAIL)
1172 return FAIL;
1173
1174 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1175 {
1176 if (first)
1177 first = FALSE;
1178 else
1179 ga_concat(gap, sep);
1180 p = ((join_T *)join_gap->ga_data) + i;
1181
1182 if (p->s != NULL)
1183 ga_concat(gap, p->s);
1184 line_breakcheck();
1185 }
1186
1187 return OK;
1188}
1189
1190/*
1191 * Join list "l" into a string in "*gap", using separator "sep".
1192 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1193 * Return FAIL or OK.
1194 */
1195 int
1196list_join(
1197 garray_T *gap,
1198 list_T *l,
1199 char_u *sep,
1200 int echo_style,
1201 int restore_copyID,
1202 int copyID)
1203{
1204 garray_T join_ga;
1205 int retval;
1206 join_T *p;
1207 int i;
1208
1209 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001210 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001211 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1212 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1213 copyID, &join_ga);
1214
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001215 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001216 if (join_ga.ga_data != NULL)
1217 {
1218 p = (join_T *)join_ga.ga_data;
1219 for (i = 0; i < join_ga.ga_len; ++i)
1220 {
1221 vim_free(p->tofree);
1222 ++p;
1223 }
1224 ga_clear(&join_ga);
1225 }
1226
1227 return retval;
1228}
1229
1230/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001231 * "join()" function
1232 */
1233 void
1234f_join(typval_T *argvars, typval_T *rettv)
1235{
1236 garray_T ga;
1237 char_u *sep;
1238
1239 if (argvars[0].v_type != VAR_LIST)
1240 {
1241 emsg(_(e_listreq));
1242 return;
1243 }
1244 if (argvars[0].vval.v_list == NULL)
1245 return;
1246 if (argvars[1].v_type == VAR_UNKNOWN)
1247 sep = (char_u *)" ";
1248 else
1249 sep = tv_get_string_chk(&argvars[1]);
1250
1251 rettv->v_type = VAR_STRING;
1252
1253 if (sep != NULL)
1254 {
1255 ga_init2(&ga, (int)sizeof(char), 80);
1256 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1257 ga_append(&ga, NUL);
1258 rettv->vval.v_string = (char_u *)ga.ga_data;
1259 }
1260 else
1261 rettv->vval.v_string = NULL;
1262}
1263
1264/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001265 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001266 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001267 * Return OK or FAIL.
1268 */
1269 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001270eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001271{
Bram Moolenaar71478202020-06-26 22:46:27 +02001272 int evaluate = evalarg == NULL ? FALSE
1273 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001274 list_T *l = NULL;
1275 typval_T tv;
1276 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001277 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001278 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001279
1280 if (evaluate)
1281 {
1282 l = list_alloc();
1283 if (l == NULL)
1284 return FAIL;
1285 }
1286
Bram Moolenaar962d7212020-07-04 14:15:00 +02001287 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001288 while (**arg != ']' && **arg != NUL)
1289 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001290 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001291 goto failret;
1292 if (evaluate)
1293 {
1294 item = listitem_alloc();
1295 if (item != NULL)
1296 {
1297 item->li_tv = tv;
1298 item->li_tv.v_lock = 0;
1299 list_append(l, item);
1300 }
1301 else
1302 clear_tv(&tv);
1303 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001304 // Legacy Vim script allowed a space before the comma.
1305 if (!vim9script)
1306 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001307
Bram Moolenaare6e03172020-06-27 16:36:05 +02001308 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001309 had_comma = **arg == ',';
1310 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001311 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001312 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001313 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001314 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001315 goto failret;
1316 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001317 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001318 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001319
Bram Moolenaare6b53242020-07-01 17:28:33 +02001320 // The "]" can be on the next line. But a double quoted string may
1321 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001322 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001323 if (**arg == ']')
1324 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001325
1326 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001327 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001329 {
1330 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001331 semsg(_(e_no_white_space_allowed_before_str_str),
1332 ",", *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001333 else
1334 semsg(_("E696: Missing comma in List: %s"), *arg);
1335 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001336 goto failret;
1337 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001338 }
1339
1340 if (**arg != ']')
1341 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001343 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001344failret:
1345 if (evaluate)
1346 list_free(l);
1347 return FAIL;
1348 }
1349
Bram Moolenaar9d489562020-07-30 20:08:50 +02001350 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001351 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001352 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001353
1354 return OK;
1355}
1356
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001357/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001358 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001359 */
1360 int
1361write_list(FILE *fd, list_T *list, int binary)
1362{
1363 listitem_T *li;
1364 int c;
1365 int ret = OK;
1366 char_u *s;
1367
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001368 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001369 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001370 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001371 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372 {
1373 if (*s == '\n')
1374 c = putc(NUL, fd);
1375 else
1376 c = putc(*s, fd);
1377 if (c == EOF)
1378 {
1379 ret = FAIL;
1380 break;
1381 }
1382 }
1383 if (!binary || li->li_next != NULL)
1384 if (putc('\n', fd) == EOF)
1385 {
1386 ret = FAIL;
1387 break;
1388 }
1389 if (ret == FAIL)
1390 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001391 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001392 break;
1393 }
1394 }
1395 return ret;
1396}
1397
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001398/*
1399 * Initialize a static list with 10 items.
1400 */
1401 void
1402init_static_list(staticList10_T *sl)
1403{
1404 list_T *l = &sl->sl_list;
1405 int i;
1406
1407 memset(sl, 0, sizeof(staticList10_T));
1408 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001409 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001410 l->lv_refcount = DO_NOT_FREE_CNT;
1411 l->lv_lock = VAR_FIXED;
1412 sl->sl_list.lv_len = 10;
1413
1414 for (i = 0; i < 10; ++i)
1415 {
1416 listitem_T *li = &sl->sl_items[i];
1417
1418 if (i == 0)
1419 li->li_prev = NULL;
1420 else
1421 li->li_prev = li - 1;
1422 if (i == 9)
1423 li->li_next = NULL;
1424 else
1425 li->li_next = li + 1;
1426 }
1427}
1428
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001429/*
1430 * "list2str()" function
1431 */
1432 void
1433f_list2str(typval_T *argvars, typval_T *rettv)
1434{
1435 list_T *l;
1436 listitem_T *li;
1437 garray_T ga;
1438 int utf8 = FALSE;
1439
1440 rettv->v_type = VAR_STRING;
1441 rettv->vval.v_string = NULL;
1442 if (argvars[0].v_type != VAR_LIST)
1443 {
1444 emsg(_(e_invarg));
1445 return;
1446 }
1447
1448 l = argvars[0].vval.v_list;
1449 if (l == NULL)
1450 return; // empty list results in empty string
1451
1452 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001453 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001454
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001455 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001456 ga_init2(&ga, 1, 80);
1457 if (has_mbyte || utf8)
1458 {
1459 char_u buf[MB_MAXBYTES + 1];
1460 int (*char2bytes)(int, char_u *);
1461
1462 if (utf8 || enc_utf8)
1463 char2bytes = utf_char2bytes;
1464 else
1465 char2bytes = mb_char2bytes;
1466
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001467 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001468 {
1469 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1470 ga_concat(&ga, buf);
1471 }
1472 ga_append(&ga, NUL);
1473 }
1474 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1475 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001476 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001477 ga_append(&ga, tv_get_number(&li->li_tv));
1478 ga_append(&ga, NUL);
1479 }
1480
1481 rettv->v_type = VAR_STRING;
1482 rettv->vval.v_string = ga.ga_data;
1483}
1484
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001485 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001486list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1487{
1488 list_T *l;
1489 listitem_T *item, *item2;
1490 listitem_T *li;
1491 int error = FALSE;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001492 long idx;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001493
1494 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001495 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001496 return;
1497
1498 idx = (long)tv_get_number_chk(&argvars[1], &error);
1499 if (error)
1500 ; // type error: do nothing, errmsg already given
1501 else if ((item = list_find(l, idx)) == NULL)
1502 semsg(_(e_listidx), idx);
1503 else
1504 {
1505 if (argvars[2].v_type == VAR_UNKNOWN)
1506 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001507 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001508 vimlist_remove(l, item, item);
1509 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001511 }
1512 else
1513 {
1514 // Remove range of items, return list with values.
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001515 long end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001516
1517 if (error)
1518 ; // type error: do nothing
1519 else if ((item2 = list_find(l, end)) == NULL)
1520 semsg(_(e_listidx), end);
1521 else
1522 {
1523 int cnt = 0;
1524
1525 for (li = item; li != NULL; li = li->li_next)
1526 {
1527 ++cnt;
1528 if (li == item2)
1529 break;
1530 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001531 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001532 emsg(_(e_invrange));
1533 else
1534 {
1535 vimlist_remove(l, item, item2);
1536 if (rettv_list_alloc(rettv) == OK)
1537 {
1538 l = rettv->vval.v_list;
1539 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001540 l->lv_u.mat.lv_last = item2;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001541 item->li_prev = NULL;
1542 item2->li_next = NULL;
1543 l->lv_len = cnt;
1544 }
1545 }
1546 }
1547 }
1548 }
1549}
1550
1551static int item_compare(const void *s1, const void *s2);
1552static int item_compare2(const void *s1, const void *s2);
1553
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001554// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001555typedef struct
1556{
1557 listitem_T *item;
1558 int idx;
1559} sortItem_T;
1560
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001561// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001562typedef struct
1563{
1564 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001565 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001566 int item_compare_numeric;
1567 int item_compare_numbers;
1568#ifdef FEAT_FLOAT
1569 int item_compare_float;
1570#endif
1571 char_u *item_compare_func;
1572 partial_T *item_compare_partial;
1573 dict_T *item_compare_selfdict;
1574 int item_compare_func_err;
1575 int item_compare_keep_zero;
1576} sortinfo_T;
1577static sortinfo_T *sortinfo = NULL;
1578#define ITEM_COMPARE_FAIL 999
1579
1580/*
1581 * Compare functions for f_sort() and f_uniq() below.
1582 */
1583 static int
1584item_compare(const void *s1, const void *s2)
1585{
1586 sortItem_T *si1, *si2;
1587 typval_T *tv1, *tv2;
1588 char_u *p1, *p2;
1589 char_u *tofree1 = NULL, *tofree2 = NULL;
1590 int res;
1591 char_u numbuf1[NUMBUFLEN];
1592 char_u numbuf2[NUMBUFLEN];
1593
1594 si1 = (sortItem_T *)s1;
1595 si2 = (sortItem_T *)s2;
1596 tv1 = &si1->item->li_tv;
1597 tv2 = &si2->item->li_tv;
1598
1599 if (sortinfo->item_compare_numbers)
1600 {
1601 varnumber_T v1 = tv_get_number(tv1);
1602 varnumber_T v2 = tv_get_number(tv2);
1603
1604 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1605 }
1606
1607#ifdef FEAT_FLOAT
1608 if (sortinfo->item_compare_float)
1609 {
1610 float_T v1 = tv_get_float(tv1);
1611 float_T v2 = tv_get_float(tv2);
1612
1613 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1614 }
1615#endif
1616
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001617 // tv2string() puts quotes around a string and allocates memory. Don't do
1618 // that for string variables. Use a single quote when comparing with a
1619 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001620 if (tv1->v_type == VAR_STRING)
1621 {
1622 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1623 p1 = (char_u *)"'";
1624 else
1625 p1 = tv1->vval.v_string;
1626 }
1627 else
1628 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1629 if (tv2->v_type == VAR_STRING)
1630 {
1631 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1632 p2 = (char_u *)"'";
1633 else
1634 p2 = tv2->vval.v_string;
1635 }
1636 else
1637 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1638 if (p1 == NULL)
1639 p1 = (char_u *)"";
1640 if (p2 == NULL)
1641 p2 = (char_u *)"";
1642 if (!sortinfo->item_compare_numeric)
1643 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001644 if (sortinfo->item_compare_lc)
1645 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001646 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001647 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001648 }
1649 else
1650 {
1651 double n1, n2;
1652 n1 = strtod((char *)p1, (char **)&p1);
1653 n2 = strtod((char *)p2, (char **)&p2);
1654 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1655 }
1656
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001657 // When the result would be zero, compare the item indexes. Makes the
1658 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001659 if (res == 0 && !sortinfo->item_compare_keep_zero)
1660 res = si1->idx > si2->idx ? 1 : -1;
1661
1662 vim_free(tofree1);
1663 vim_free(tofree2);
1664 return res;
1665}
1666
1667 static int
1668item_compare2(const void *s1, const void *s2)
1669{
1670 sortItem_T *si1, *si2;
1671 int res;
1672 typval_T rettv;
1673 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001674 char_u *func_name;
1675 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001676 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001677
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001678 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001679 if (sortinfo->item_compare_func_err)
1680 return 0;
1681
1682 si1 = (sortItem_T *)s1;
1683 si2 = (sortItem_T *)s2;
1684
1685 if (partial == NULL)
1686 func_name = sortinfo->item_compare_func;
1687 else
1688 func_name = partial_name(partial);
1689
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001690 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1691 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001692 copy_tv(&si1->item->li_tv, &argv[0]);
1693 copy_tv(&si2->item->li_tv, &argv[1]);
1694
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001695 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001696 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001697 funcexe.evaluate = TRUE;
1698 funcexe.partial = partial;
1699 funcexe.selfdict = sortinfo->item_compare_selfdict;
1700 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001701 clear_tv(&argv[0]);
1702 clear_tv(&argv[1]);
1703
1704 if (res == FAIL)
1705 res = ITEM_COMPARE_FAIL;
1706 else
1707 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1708 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001709 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001710 clear_tv(&rettv);
1711
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001712 // When the result would be zero, compare the pointers themselves. Makes
1713 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001714 if (res == 0 && !sortinfo->item_compare_keep_zero)
1715 res = si1->idx > si2->idx ? 1 : -1;
1716
1717 return res;
1718}
1719
1720/*
1721 * "sort()" or "uniq()" function
1722 */
1723 static void
1724do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1725{
1726 list_T *l;
1727 listitem_T *li;
1728 sortItem_T *ptrs;
1729 sortinfo_T *old_sortinfo;
1730 sortinfo_T info;
1731 long len;
1732 long i;
1733
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001734 // Pointer to current info struct used in compare function. Save and
1735 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001736 old_sortinfo = sortinfo;
1737 sortinfo = &info;
1738
1739 if (argvars[0].v_type != VAR_LIST)
1740 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1741 else
1742 {
1743 l = argvars[0].vval.v_list;
Bram Moolenaara187c432020-09-16 21:08:28 +02001744 if (l == NULL || value_check_lock(l->lv_lock,
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001745 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1746 TRUE))
1747 goto theend;
1748 rettv_list_set(rettv, l);
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001749 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001750
1751 len = list_len(l);
1752 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001753 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001754
1755 info.item_compare_ic = FALSE;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001756 info.item_compare_lc = FALSE;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001757 info.item_compare_numeric = FALSE;
1758 info.item_compare_numbers = FALSE;
1759#ifdef FEAT_FLOAT
1760 info.item_compare_float = FALSE;
1761#endif
1762 info.item_compare_func = NULL;
1763 info.item_compare_partial = NULL;
1764 info.item_compare_selfdict = NULL;
1765 if (argvars[1].v_type != VAR_UNKNOWN)
1766 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001767 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001768 if (argvars[1].v_type == VAR_FUNC)
1769 info.item_compare_func = argvars[1].vval.v_string;
1770 else if (argvars[1].v_type == VAR_PARTIAL)
1771 info.item_compare_partial = argvars[1].vval.v_partial;
1772 else
1773 {
1774 int error = FALSE;
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001775 int nr = 0;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001776
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001777 if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001778 {
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001779 nr = tv_get_number_chk(&argvars[1], &error);
1780 if (error)
1781 goto theend; // type error; errmsg already given
1782 if (nr == 1)
1783 info.item_compare_ic = TRUE;
1784 }
1785 if (nr != 1)
1786 {
1787 if (argvars[1].v_type != VAR_NUMBER)
1788 info.item_compare_func = tv_get_string(&argvars[1]);
1789 else if (nr != 0)
1790 {
1791 emsg(_(e_invarg));
1792 goto theend;
1793 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001794 }
1795 if (info.item_compare_func != NULL)
1796 {
1797 if (*info.item_compare_func == NUL)
1798 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001799 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001800 info.item_compare_func = NULL;
1801 }
1802 else if (STRCMP(info.item_compare_func, "n") == 0)
1803 {
1804 info.item_compare_func = NULL;
1805 info.item_compare_numeric = TRUE;
1806 }
1807 else if (STRCMP(info.item_compare_func, "N") == 0)
1808 {
1809 info.item_compare_func = NULL;
1810 info.item_compare_numbers = TRUE;
1811 }
1812#ifdef FEAT_FLOAT
1813 else if (STRCMP(info.item_compare_func, "f") == 0)
1814 {
1815 info.item_compare_func = NULL;
1816 info.item_compare_float = TRUE;
1817 }
1818#endif
1819 else if (STRCMP(info.item_compare_func, "i") == 0)
1820 {
1821 info.item_compare_func = NULL;
1822 info.item_compare_ic = TRUE;
1823 }
Bram Moolenaar55e29612020-11-01 13:57:44 +01001824 else if (STRCMP(info.item_compare_func, "l") == 0)
1825 {
1826 info.item_compare_func = NULL;
1827 info.item_compare_lc = TRUE;
1828 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001829 }
1830 }
1831
1832 if (argvars[2].v_type != VAR_UNKNOWN)
1833 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001834 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001835 if (argvars[2].v_type != VAR_DICT)
1836 {
1837 emsg(_(e_dictreq));
1838 goto theend;
1839 }
1840 info.item_compare_selfdict = argvars[2].vval.v_dict;
1841 }
1842 }
1843
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001844 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001845 ptrs = ALLOC_MULT(sortItem_T, len);
1846 if (ptrs == NULL)
1847 goto theend;
1848
1849 i = 0;
1850 if (sort)
1851 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001852 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001853 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001854 {
1855 ptrs[i].item = li;
1856 ptrs[i].idx = i;
1857 ++i;
1858 }
1859
1860 info.item_compare_func_err = FALSE;
1861 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001862 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001863 if ((info.item_compare_func != NULL
1864 || info.item_compare_partial != NULL)
1865 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
1866 == ITEM_COMPARE_FAIL)
1867 emsg(_("E702: Sort compare function failed"));
1868 else
1869 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001870 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001871 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
1872 info.item_compare_func == NULL
1873 && info.item_compare_partial == NULL
1874 ? item_compare : item_compare2);
1875
1876 if (!info.item_compare_func_err)
1877 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001878 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001879 l->lv_first = l->lv_u.mat.lv_last
1880 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001881 l->lv_len = 0;
1882 for (i = 0; i < len; ++i)
1883 list_append(l, ptrs[i].item);
1884 }
1885 }
1886 }
1887 else
1888 {
1889 int (*item_compare_func_ptr)(const void *, const void *);
1890
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001891 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001892 info.item_compare_func_err = FALSE;
1893 info.item_compare_keep_zero = TRUE;
1894 item_compare_func_ptr = info.item_compare_func != NULL
1895 || info.item_compare_partial != NULL
1896 ? item_compare2 : item_compare;
1897
1898 for (li = l->lv_first; li != NULL && li->li_next != NULL;
1899 li = li->li_next)
1900 {
1901 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
1902 == 0)
1903 ptrs[i++].item = li;
1904 if (info.item_compare_func_err)
1905 {
1906 emsg(_("E882: Uniq compare function failed"));
1907 break;
1908 }
1909 }
1910
1911 if (!info.item_compare_func_err)
1912 {
1913 while (--i >= 0)
1914 {
1915 li = ptrs[i].item->li_next;
1916 ptrs[i].item->li_next = li->li_next;
1917 if (li->li_next != NULL)
1918 li->li_next->li_prev = ptrs[i].item;
1919 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001920 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001921 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001923 l->lv_len--;
1924 }
1925 }
1926 }
1927
1928 vim_free(ptrs);
1929 }
1930theend:
1931 sortinfo = old_sortinfo;
1932}
1933
1934/*
1935 * "sort({list})" function
1936 */
1937 void
1938f_sort(typval_T *argvars, typval_T *rettv)
1939{
1940 do_sort_uniq(argvars, rettv, TRUE);
1941}
1942
1943/*
1944 * "uniq({list})" function
1945 */
1946 void
1947f_uniq(typval_T *argvars, typval_T *rettv)
1948{
1949 do_sort_uniq(argvars, rettv, FALSE);
1950}
1951
Bram Moolenaarea696852020-11-09 18:31:39 +01001952typedef enum {
1953 FILTERMAP_FILTER,
1954 FILTERMAP_MAP,
1955 FILTERMAP_MAPNEW
1956} filtermap_T;
1957
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001958/*
1959 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01001960 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001961 */
1962 static int
Bram Moolenaarea696852020-11-09 18:31:39 +01001963filter_map_one(
1964 typval_T *tv, // original value
1965 typval_T *expr, // callback
1966 filtermap_T filtermap,
1967 typval_T *newtv, // for map() and mapnew(): new value
1968 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001969{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001970 typval_T argv[3];
1971 int retval = FAIL;
1972
1973 copy_tv(tv, get_vim_var_tv(VV_VAL));
1974 argv[0] = *get_vim_var_tv(VV_KEY);
1975 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01001976 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001977 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01001978 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001979 {
1980 int error = FALSE;
1981
1982 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02001983 if (in_vim9script())
Bram Moolenaarea696852020-11-09 18:31:39 +01001984 *remp = !tv2bool(newtv);
Bram Moolenaar56acb092020-08-16 14:48:19 +02001985 else
Bram Moolenaarea696852020-11-09 18:31:39 +01001986 *remp = (tv_get_number_chk(newtv, &error) == 0);
1987 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001988 // On type error, nothing has been removed; return FAIL to stop the
1989 // loop. The error message was given by tv_get_number_chk().
1990 if (error)
1991 goto theend;
1992 }
1993 retval = OK;
1994theend:
1995 clear_tv(get_vim_var_tv(VV_VAL));
1996 return retval;
1997}
1998
1999/*
2000 * Implementation of map() and filter().
2001 */
2002 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01002003filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002004{
2005 typval_T *expr;
2006 listitem_T *li, *nli;
2007 list_T *l = NULL;
2008 dictitem_T *di;
2009 hashtab_T *ht;
2010 hashitem_T *hi;
2011 dict_T *d = NULL;
2012 blob_T *b = NULL;
2013 int rem;
2014 int todo;
Bram Moolenaarea696852020-11-09 18:31:39 +01002015 char_u *ermsg = (char_u *)(filtermap == FILTERMAP_MAP ? "map()"
2016 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
2017 : "filter()");
2018 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2019 ? N_("map() argument")
2020 : filtermap == FILTERMAP_MAPNEW
2021 ? N_("mapnew() argument")
2022 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002023 int save_did_emsg;
2024 int idx = 0;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002025 type_T *type = NULL;
2026 garray_T type_list;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002027
Bram Moolenaarea696852020-11-09 18:31:39 +01002028 // map() and filter() return the first argument, also on failure.
2029 if (filtermap != FILTERMAP_MAPNEW)
2030 copy_tv(&argvars[0], rettv);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002031 if (filtermap == FILTERMAP_MAP && in_vim9script())
2032 {
2033 // Check that map() does not change the type of the dict.
2034 ga_init2(&type_list, sizeof(type_T *), 10);
2035 type = typval2type(argvars, &type_list);
2036 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002037
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002038 if (argvars[0].v_type == VAR_BLOB)
2039 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002040 if (filtermap == FILTERMAP_MAPNEW)
2041 {
2042 rettv->v_type = VAR_BLOB;
2043 rettv->vval.v_blob = NULL;
2044 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002045 if ((b = argvars[0].vval.v_blob) == NULL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002046 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002047 }
2048 else if (argvars[0].v_type == VAR_LIST)
2049 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002050 if (filtermap == FILTERMAP_MAPNEW)
2051 {
2052 rettv->v_type = VAR_LIST;
2053 rettv->vval.v_list = NULL;
2054 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002055 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002056 || (filtermap == FILTERMAP_FILTER
2057 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002058 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002059 }
2060 else if (argvars[0].v_type == VAR_DICT)
2061 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002062 if (filtermap == FILTERMAP_MAPNEW)
2063 {
2064 rettv->v_type = VAR_DICT;
2065 rettv->vval.v_dict = NULL;
2066 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002067 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002068 || (filtermap == FILTERMAP_FILTER
2069 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002070 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002071 }
2072 else
2073 {
Bram Moolenaarfcb0b612020-05-26 20:22:01 +02002074 semsg(_(e_listdictblobarg), ermsg);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002075 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002076 }
2077
2078 expr = &argvars[1];
2079 // On type errors, the preceding call has already displayed an error
2080 // message. Avoid a misleading error message for an empty string that
2081 // was not passed as argument.
2082 if (expr->v_type != VAR_UNKNOWN)
2083 {
2084 typval_T save_val;
2085 typval_T save_key;
2086
2087 prepare_vimvar(VV_VAL, &save_val);
2088 prepare_vimvar(VV_KEY, &save_key);
2089
2090 // We reset "did_emsg" to be able to detect whether an error
2091 // occurred during evaluation of the expression.
2092 save_did_emsg = did_emsg;
2093 did_emsg = FALSE;
2094
2095 if (argvars[0].v_type == VAR_DICT)
2096 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002097 int prev_lock = d->dv_lock;
Bram Moolenaarea696852020-11-09 18:31:39 +01002098 dict_T *d_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002099
Bram Moolenaarea696852020-11-09 18:31:39 +01002100 if (filtermap == FILTERMAP_MAPNEW)
2101 {
2102 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002103 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002104 d_ret = rettv->vval.v_dict;
2105 }
2106
2107 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002108 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002109 ht = &d->dv_hashtab;
2110 hash_lock(ht);
2111 todo = (int)ht->ht_used;
2112 for (hi = ht->ht_array; todo > 0; ++hi)
2113 {
2114 if (!HASHITEM_EMPTY(hi))
2115 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002116 int r;
2117 typval_T newtv;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002118
2119 --todo;
2120 di = HI2DI(hi);
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002121 if (filtermap == FILTERMAP_MAP
Bram Moolenaarea696852020-11-09 18:31:39 +01002122 && (value_check_lock(di->di_tv.v_lock,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002123 arg_errmsg, TRUE)
2124 || var_check_ro(di->di_flags,
2125 arg_errmsg, TRUE)))
2126 break;
2127 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaarea696852020-11-09 18:31:39 +01002128 r = filter_map_one(&di->di_tv, expr, filtermap,
2129 &newtv, &rem);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002130 clear_tv(get_vim_var_tv(VV_KEY));
2131 if (r == FAIL || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002132 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002133 clear_tv(&newtv);
2134 break;
2135 }
2136 if (filtermap == FILTERMAP_MAP)
2137 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002138 if (type != NULL && check_typval_arg_type(
2139 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002140 {
2141 clear_tv(&newtv);
2142 break;
2143 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002144 // map(): replace the dict item value
2145 clear_tv(&di->di_tv);
2146 newtv.v_lock = 0;
2147 di->di_tv = newtv;
2148 }
2149 else if (filtermap == FILTERMAP_MAPNEW)
2150 {
2151 // mapnew(): add the item value to the new dict
2152 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
2153 clear_tv(&newtv);
2154 if (r == FAIL)
2155 break;
2156 }
2157 else if (filtermap == FILTERMAP_FILTER && rem)
2158 {
2159 // filter(false): remove the item from the dict
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002160 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2161 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2162 break;
2163 dictitem_remove(d, di);
2164 }
2165 }
2166 }
2167 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002168 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002169 }
2170 else if (argvars[0].v_type == VAR_BLOB)
2171 {
2172 int i;
2173 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002174 varnumber_T val;
Bram Moolenaarea696852020-11-09 18:31:39 +01002175 blob_T *b_ret = b;
2176
2177 if (filtermap == FILTERMAP_MAPNEW)
2178 {
2179 if (blob_copy(b, rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002180 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002181 b_ret = rettv->vval.v_blob;
2182 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002183
2184 // set_vim_var_nr() doesn't set the type
2185 set_vim_var_type(VV_KEY, VAR_NUMBER);
2186
2187 for (i = 0; i < b->bv_ga.ga_len; i++)
2188 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002189 typval_T newtv;
2190
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002191 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002192 val = blob_get(b, i);
2193 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002194 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaarea696852020-11-09 18:31:39 +01002195 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
2196 || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002197 break;
Bram Moolenaarea696852020-11-09 18:31:39 +01002198 if (newtv.v_type != VAR_NUMBER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002199 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002200 clear_tv(&newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002201 emsg(_(e_invalblob));
2202 break;
2203 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002204 if (filtermap != FILTERMAP_FILTER)
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002205 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002206 if (newtv.vval.v_number != val)
2207 blob_set(b_ret, i, newtv.vval.v_number);
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002208 }
2209 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002210 {
2211 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2212
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002213 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002214 (size_t)b->bv_ga.ga_len - i - 1);
2215 --b->bv_ga.ga_len;
2216 --i;
2217 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002218 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002219 }
2220 }
2221 else // argvars[0].v_type == VAR_LIST
2222 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002223 int prev_lock = l->lv_lock;
2224 list_T *l_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002225
Bram Moolenaarea696852020-11-09 18:31:39 +01002226 if (filtermap == FILTERMAP_MAPNEW)
2227 {
2228 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002229 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002230 l_ret = rettv->vval.v_list;
2231 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002232 // set_vim_var_nr() doesn't set the type
2233 set_vim_var_type(VV_KEY, VAR_NUMBER);
2234
Bram Moolenaarea696852020-11-09 18:31:39 +01002235 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002236 l->lv_lock = VAR_LOCKED;
Bram Moolenaarea696852020-11-09 18:31:39 +01002237
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002238 if (l->lv_first == &range_list_item)
2239 {
2240 varnumber_T val = l->lv_u.nonmat.lv_start;
2241 int len = l->lv_len;
2242 int stride = l->lv_u.nonmat.lv_stride;
2243
2244 // List from range(): loop over the numbers
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002245 if (filtermap != FILTERMAP_MAPNEW)
2246 {
2247 l->lv_first = NULL;
2248 l->lv_u.mat.lv_last = NULL;
2249 l->lv_len = 0;
2250 l->lv_u.mat.lv_idx_item = NULL;
2251 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002252
2253 for (idx = 0; idx < len; ++idx)
Bram Moolenaarc56936e2020-11-10 11:43:56 +01002254 {
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002255 typval_T tv;
2256 typval_T newtv;
2257
2258 tv.v_type = VAR_NUMBER;
2259 tv.v_lock = 0;
2260 tv.vval.v_number = val;
2261 set_vim_var_nr(VV_KEY, idx);
2262 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2263 == FAIL)
Bram Moolenaarea696852020-11-09 18:31:39 +01002264 break;
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002265 if (did_emsg)
2266 {
2267 clear_tv(&newtv);
2268 break;
2269 }
2270 if (filtermap != FILTERMAP_FILTER)
2271 {
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002272 if (filtermap == FILTERMAP_MAP && type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002273 && check_typval_arg_type(
2274 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002275 {
2276 clear_tv(&newtv);
2277 break;
2278 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002279 // map(), mapnew(): always append the new value to the
2280 // list
2281 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2282 ? l : l_ret, &newtv) == FAIL)
2283 break;
2284 }
2285 else if (!rem)
2286 {
2287 // filter(): append the list item value when not rem
2288 if (list_append_tv_move(l, &tv) == FAIL)
2289 break;
2290 }
2291
2292 val += stride;
Bram Moolenaarea696852020-11-09 18:31:39 +01002293 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002294 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002295 else
2296 {
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002297 // Materialized list: loop over the items
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002298 for (li = l->lv_first; li != NULL; li = nli)
2299 {
2300 typval_T newtv;
2301
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002302 if (filtermap == FILTERMAP_MAP && value_check_lock(
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002303 li->li_tv.v_lock, arg_errmsg, TRUE))
2304 break;
2305 nli = li->li_next;
2306 set_vim_var_nr(VV_KEY, idx);
2307 if (filter_map_one(&li->li_tv, expr, filtermap,
2308 &newtv, &rem) == FAIL)
2309 break;
2310 if (did_emsg)
2311 {
2312 clear_tv(&newtv);
2313 break;
2314 }
2315 if (filtermap == FILTERMAP_MAP)
2316 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002317 if (type != NULL && check_typval_arg_type(
2318 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002319 {
2320 clear_tv(&newtv);
2321 break;
2322 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002323 // map(): replace the list item value
2324 clear_tv(&li->li_tv);
2325 newtv.v_lock = 0;
2326 li->li_tv = newtv;
2327 }
2328 else if (filtermap == FILTERMAP_MAPNEW)
2329 {
2330 // mapnew(): append the list item value
2331 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2332 break;
2333 }
2334 else if (filtermap == FILTERMAP_FILTER && rem)
2335 listitem_remove(l, li);
2336 ++idx;
2337 }
2338 }
2339
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002340 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002341 }
2342
2343 restore_vimvar(VV_KEY, &save_key);
2344 restore_vimvar(VV_VAL, &save_val);
2345
2346 did_emsg |= save_did_emsg;
2347 }
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002348
2349theend:
2350 if (type != NULL)
2351 clear_type_list(&type_list);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002352}
2353
2354/*
2355 * "filter()" function
2356 */
2357 void
2358f_filter(typval_T *argvars, typval_T *rettv)
2359{
Bram Moolenaarea696852020-11-09 18:31:39 +01002360 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002361}
2362
2363/*
2364 * "map()" function
2365 */
2366 void
2367f_map(typval_T *argvars, typval_T *rettv)
2368{
Bram Moolenaarea696852020-11-09 18:31:39 +01002369 filter_map(argvars, rettv, FILTERMAP_MAP);
2370}
2371
2372/*
2373 * "mapnew()" function
2374 */
2375 void
2376f_mapnew(typval_T *argvars, typval_T *rettv)
2377{
2378 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002379}
2380
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002381/*
2382 * "add(list, item)" function
2383 */
2384 void
2385f_add(typval_T *argvars, typval_T *rettv)
2386{
2387 list_T *l;
2388 blob_T *b;
2389
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002390 rettv->vval.v_number = 1; // Default: Failed
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002391 if (argvars[0].v_type == VAR_LIST)
2392 {
2393 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002394 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002395 (char_u *)N_("add() argument"), TRUE)
2396 && list_append_tv(l, &argvars[1]) == OK)
2397 copy_tv(&argvars[0], rettv);
2398 }
2399 else if (argvars[0].v_type == VAR_BLOB)
2400 {
2401 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002402 && !value_check_lock(b->bv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002403 (char_u *)N_("add() argument"), TRUE))
2404 {
2405 int error = FALSE;
2406 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2407
2408 if (!error)
2409 {
2410 ga_append(&b->bv_ga, (int)n);
2411 copy_tv(&argvars[0], rettv);
2412 }
2413 }
2414 }
2415 else
2416 emsg(_(e_listblobreq));
2417}
2418
2419/*
2420 * "count()" function
2421 */
2422 void
2423f_count(typval_T *argvars, typval_T *rettv)
2424{
2425 long n = 0;
2426 int ic = FALSE;
2427 int error = FALSE;
2428
2429 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002430 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002431
2432 if (argvars[0].v_type == VAR_STRING)
2433 {
2434 char_u *expr = tv_get_string_chk(&argvars[1]);
2435 char_u *p = argvars[0].vval.v_string;
2436 char_u *next;
2437
2438 if (!error && expr != NULL && *expr != NUL && p != NULL)
2439 {
2440 if (ic)
2441 {
2442 size_t len = STRLEN(expr);
2443
2444 while (*p != NUL)
2445 {
2446 if (MB_STRNICMP(p, expr, len) == 0)
2447 {
2448 ++n;
2449 p += len;
2450 }
2451 else
2452 MB_PTR_ADV(p);
2453 }
2454 }
2455 else
2456 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2457 != NULL)
2458 {
2459 ++n;
2460 p = next + STRLEN(expr);
2461 }
2462 }
2463
2464 }
2465 else if (argvars[0].v_type == VAR_LIST)
2466 {
2467 listitem_T *li;
2468 list_T *l;
2469 long idx;
2470
2471 if ((l = argvars[0].vval.v_list) != NULL)
2472 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002473 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002474 li = l->lv_first;
2475 if (argvars[2].v_type != VAR_UNKNOWN)
2476 {
2477 if (argvars[3].v_type != VAR_UNKNOWN)
2478 {
2479 idx = (long)tv_get_number_chk(&argvars[3], &error);
2480 if (!error)
2481 {
2482 li = list_find(l, idx);
2483 if (li == NULL)
2484 semsg(_(e_listidx), idx);
2485 }
2486 }
2487 if (error)
2488 li = NULL;
2489 }
2490
2491 for ( ; li != NULL; li = li->li_next)
2492 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2493 ++n;
2494 }
2495 }
2496 else if (argvars[0].v_type == VAR_DICT)
2497 {
2498 int todo;
2499 dict_T *d;
2500 hashitem_T *hi;
2501
2502 if ((d = argvars[0].vval.v_dict) != NULL)
2503 {
2504 if (argvars[2].v_type != VAR_UNKNOWN)
2505 {
2506 if (argvars[3].v_type != VAR_UNKNOWN)
2507 emsg(_(e_invarg));
2508 }
2509
2510 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2511 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2512 {
2513 if (!HASHITEM_EMPTY(hi))
2514 {
2515 --todo;
2516 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2517 ++n;
2518 }
2519 }
2520 }
2521 }
2522 else
2523 semsg(_(e_listdictarg), "count()");
2524 rettv->vval.v_number = n;
2525}
2526
2527/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002528 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002529 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002530 static void
2531extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002532{
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002533 type_T *type = NULL;
2534 garray_T type_list;
2535
2536 if (!is_new && in_vim9script())
2537 {
2538 // Check that map() does not change the type of the dict.
2539 ga_init2(&type_list, sizeof(type_T *), 10);
2540 type = typval2type(argvars, &type_list);
2541 }
2542
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002543 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2544 {
2545 list_T *l1, *l2;
2546 listitem_T *item;
2547 long before;
2548 int error = FALSE;
2549
2550 l1 = argvars[0].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002551 if (l1 == NULL)
2552 {
2553 emsg(_(e_cannot_extend_null_list));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002554 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002555 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002556 l2 = argvars[1].vval.v_list;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002557 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
2558 && l2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002559 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002560 if (is_new)
2561 {
2562 l1 = list_copy(l1, FALSE, get_copyID());
2563 if (l1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002564 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002565 }
2566
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002567 if (argvars[2].v_type != VAR_UNKNOWN)
2568 {
2569 before = (long)tv_get_number_chk(&argvars[2], &error);
2570 if (error)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002571 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002572
2573 if (before == l1->lv_len)
2574 item = NULL;
2575 else
2576 {
2577 item = list_find(l1, before);
2578 if (item == NULL)
2579 {
2580 semsg(_(e_listidx), before);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002581 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002582 }
2583 }
2584 }
2585 else
2586 item = NULL;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002587 if (type != NULL && check_typval_arg_type(
2588 type, &argvars[1], 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002589 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002590 list_extend(l1, l2, item);
2591
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002592 if (is_new)
2593 {
2594 rettv->v_type = VAR_LIST;
2595 rettv->vval.v_list = l1;
2596 rettv->v_lock = FALSE;
2597 }
2598 else
2599 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002600 }
2601 }
2602 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2603 {
2604 dict_T *d1, *d2;
2605 char_u *action;
2606 int i;
2607
2608 d1 = argvars[0].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002609 if (d1 == NULL)
2610 {
2611 emsg(_(e_cannot_extend_null_dict));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002612 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002613 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002614 d2 = argvars[1].vval.v_dict;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002615 if ((is_new || !value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
2616 && d2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002617 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002618 if (is_new)
2619 {
2620 d1 = dict_copy(d1, FALSE, get_copyID());
2621 if (d1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002622 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002623 }
2624
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002625 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002626 if (argvars[2].v_type != VAR_UNKNOWN)
2627 {
2628 static char *(av[]) = {"keep", "force", "error"};
2629
2630 action = tv_get_string_chk(&argvars[2]);
2631 if (action == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002632 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002633 for (i = 0; i < 3; ++i)
2634 if (STRCMP(action, av[i]) == 0)
2635 break;
2636 if (i == 3)
2637 {
2638 semsg(_(e_invarg2), action);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002639 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002640 }
2641 }
2642 else
2643 action = (char_u *)"force";
2644
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002645 if (type != NULL && check_typval_arg_type(
2646 type, &argvars[1], 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002647 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002648 dict_extend(d1, d2, action);
2649
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002650 if (is_new)
2651 {
2652 rettv->v_type = VAR_DICT;
2653 rettv->vval.v_dict = d1;
2654 rettv->v_lock = FALSE;
2655 }
2656 else
2657 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002658 }
2659 }
2660 else
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002661 semsg(_(e_listdictarg), is_new ? "extendnew()" : "extend()");
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002662
2663theend:
2664 if (type != NULL)
2665 clear_type_list(&type_list);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002666}
2667
2668/*
2669 * "extend(list, list [, idx])" function
2670 * "extend(dict, dict [, action])" function
2671 */
2672 void
2673f_extend(typval_T *argvars, typval_T *rettv)
2674{
2675 char_u *errmsg = (char_u *)N_("extend() argument");
2676
2677 extend(argvars, rettv, errmsg, FALSE);
2678}
2679
2680/*
2681 * "extendnew(list, list [, idx])" function
2682 * "extendnew(dict, dict [, action])" function
2683 */
2684 void
2685f_extendnew(typval_T *argvars, typval_T *rettv)
2686{
2687 char_u *errmsg = (char_u *)N_("extendnew() argument");
2688
2689 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002690}
2691
2692/*
2693 * "insert()" function
2694 */
2695 void
2696f_insert(typval_T *argvars, typval_T *rettv)
2697{
2698 long before = 0;
2699 listitem_T *item;
2700 list_T *l;
2701 int error = FALSE;
2702
2703 if (argvars[0].v_type == VAR_BLOB)
2704 {
2705 int val, len;
2706 char_u *p;
2707
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002708 if (argvars[0].vval.v_blob == NULL)
2709 return;
2710
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002711 len = blob_len(argvars[0].vval.v_blob);
2712 if (argvars[2].v_type != VAR_UNKNOWN)
2713 {
2714 before = (long)tv_get_number_chk(&argvars[2], &error);
2715 if (error)
2716 return; // type error; errmsg already given
2717 if (before < 0 || before > len)
2718 {
2719 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2720 return;
2721 }
2722 }
2723 val = tv_get_number_chk(&argvars[1], &error);
2724 if (error)
2725 return;
2726 if (val < 0 || val > 255)
2727 {
2728 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2729 return;
2730 }
2731
2732 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
2733 return;
2734 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2735 mch_memmove(p + before + 1, p + before, (size_t)len - before);
2736 *(p + before) = val;
2737 ++argvars[0].vval.v_blob->bv_ga.ga_len;
2738
2739 copy_tv(&argvars[0], rettv);
2740 }
2741 else if (argvars[0].v_type != VAR_LIST)
2742 semsg(_(e_listblobarg), "insert()");
2743 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002744 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002745 (char_u *)N_("insert() argument"), TRUE))
2746 {
2747 if (argvars[2].v_type != VAR_UNKNOWN)
2748 before = (long)tv_get_number_chk(&argvars[2], &error);
2749 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002750 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002751
2752 if (before == l->lv_len)
2753 item = NULL;
2754 else
2755 {
2756 item = list_find(l, before);
2757 if (item == NULL)
2758 {
2759 semsg(_(e_listidx), before);
2760 l = NULL;
2761 }
2762 }
2763 if (l != NULL)
2764 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02002765 (void)list_insert_tv(l, &argvars[1], item);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002766 copy_tv(&argvars[0], rettv);
2767 }
2768 }
2769}
2770
2771/*
2772 * "remove()" function
2773 */
2774 void
2775f_remove(typval_T *argvars, typval_T *rettv)
2776{
2777 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2778
2779 if (argvars[0].v_type == VAR_DICT)
2780 dict_remove(argvars, rettv, arg_errmsg);
2781 else if (argvars[0].v_type == VAR_BLOB)
2782 blob_remove(argvars, rettv);
2783 else if (argvars[0].v_type == VAR_LIST)
2784 list_remove(argvars, rettv, arg_errmsg);
2785 else
2786 semsg(_(e_listdictblobarg), "remove()");
2787}
2788
2789/*
2790 * "reverse({list})" function
2791 */
2792 void
2793f_reverse(typval_T *argvars, typval_T *rettv)
2794{
2795 list_T *l;
2796 listitem_T *li, *ni;
2797
2798 if (argvars[0].v_type == VAR_BLOB)
2799 {
2800 blob_T *b = argvars[0].vval.v_blob;
2801 int i, len = blob_len(b);
2802
2803 for (i = 0; i < len / 2; i++)
2804 {
2805 int tmp = blob_get(b, i);
2806
2807 blob_set(b, i, blob_get(b, len - i - 1));
2808 blob_set(b, len - i - 1, tmp);
2809 }
2810 rettv_blob_set(rettv, b);
2811 return;
2812 }
2813
2814 if (argvars[0].v_type != VAR_LIST)
2815 semsg(_(e_listblobarg), "reverse()");
2816 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002817 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002818 (char_u *)N_("reverse() argument"), TRUE))
2819 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002820 if (l->lv_first == &range_list_item)
2821 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002822 varnumber_T new_start = l->lv_u.nonmat.lv_start
2823 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2824 l->lv_u.nonmat.lv_end = new_start
2825 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2826 l->lv_u.nonmat.lv_start = new_start;
2827 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002828 rettv_list_set(rettv, l);
2829 return;
2830 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002831 li = l->lv_u.mat.lv_last;
2832 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002833 l->lv_len = 0;
2834 while (li != NULL)
2835 {
2836 ni = li->li_prev;
2837 list_append(l, li);
2838 li = ni;
2839 }
2840 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002841 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002842 }
2843}
2844
Bram Moolenaar85629982020-06-01 18:39:20 +02002845/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002846 * "reduce(list, { accumulator, element -> value } [, initial])" function
Bram Moolenaar85629982020-06-01 18:39:20 +02002847 */
2848 void
2849f_reduce(typval_T *argvars, typval_T *rettv)
2850{
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002851 typval_T initial;
Bram Moolenaar85629982020-06-01 18:39:20 +02002852 char_u *func_name;
2853 partial_T *partial = NULL;
2854 funcexe_T funcexe;
2855 typval_T argv[3];
2856
2857 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
2858 {
2859 emsg(_(e_listblobreq));
2860 return;
2861 }
2862
2863 if (argvars[1].v_type == VAR_FUNC)
2864 func_name = argvars[1].vval.v_string;
2865 else if (argvars[1].v_type == VAR_PARTIAL)
2866 {
2867 partial = argvars[1].vval.v_partial;
2868 func_name = partial_name(partial);
2869 }
2870 else
2871 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01002872 if (func_name == NULL || *func_name == NUL)
2873 {
2874 emsg(_(e_missing_function_argument));
2875 return;
2876 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002877
2878 vim_memset(&funcexe, 0, sizeof(funcexe));
2879 funcexe.evaluate = TRUE;
2880 funcexe.partial = partial;
2881
2882 if (argvars[0].v_type == VAR_LIST)
2883 {
2884 list_T *l = argvars[0].vval.v_list;
2885 listitem_T *li = NULL;
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002886 int r;
Bram Moolenaarca275a02020-06-24 22:07:46 +02002887 int called_emsg_start = called_emsg;
Bram Moolenaar85629982020-06-01 18:39:20 +02002888
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002889 if (l != NULL)
2890 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar85629982020-06-01 18:39:20 +02002891 if (argvars[2].v_type == VAR_UNKNOWN)
2892 {
2893 if (l == NULL || l->lv_first == NULL)
2894 {
2895 semsg(_(e_reduceempty), "List");
2896 return;
2897 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002898 initial = l->lv_first->li_tv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002899 li = l->lv_first->li_next;
2900 }
2901 else
2902 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002903 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002904 if (l != NULL)
2905 li = l->lv_first;
2906 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002907 copy_tv(&initial, rettv);
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002908
2909 if (l != NULL)
Bram Moolenaar85629982020-06-01 18:39:20 +02002910 {
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002911 int prev_locked = l->lv_lock;
2912
2913 l->lv_lock = VAR_FIXED; // disallow the list changing here
2914 for ( ; li != NULL; li = li->li_next)
2915 {
2916 argv[0] = *rettv;
2917 argv[1] = li->li_tv;
2918 rettv->v_type = VAR_UNKNOWN;
2919 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
2920 clear_tv(&argv[0]);
2921 if (r == FAIL || called_emsg != called_emsg_start)
2922 break;
2923 }
2924 l->lv_lock = prev_locked;
Bram Moolenaar85629982020-06-01 18:39:20 +02002925 }
2926 }
2927 else
2928 {
2929 blob_T *b = argvars[0].vval.v_blob;
2930 int i;
2931
2932 if (argvars[2].v_type == VAR_UNKNOWN)
2933 {
2934 if (b == NULL || b->bv_ga.ga_len == 0)
2935 {
2936 semsg(_(e_reduceempty), "Blob");
2937 return;
2938 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002939 initial.v_type = VAR_NUMBER;
2940 initial.vval.v_number = blob_get(b, 0);
Bram Moolenaar85629982020-06-01 18:39:20 +02002941 i = 1;
2942 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002943 else if (argvars[2].v_type != VAR_NUMBER)
2944 {
2945 emsg(_(e_number_exp));
2946 return;
2947 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002948 else
2949 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002950 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002951 i = 0;
2952 }
2953
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002954 copy_tv(&initial, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02002955 if (b != NULL)
2956 {
2957 for ( ; i < b->bv_ga.ga_len; i++)
2958 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002959 argv[0] = *rettv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002960 argv[1].v_type = VAR_NUMBER;
2961 argv[1].vval.v_number = blob_get(b, i);
2962 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
2963 return;
Bram Moolenaar85629982020-06-01 18:39:20 +02002964 }
2965 }
2966 }
2967}
2968
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002969#endif // defined(FEAT_EVAL)