blob: 988b2d982c4bca43bad88e8f5670eb14f32f9ea8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarda861d62016-07-17 15:46:27 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +020011 * list.c: List support and container (List, Dict, Blob) functions.
Bram Moolenaarda861d62016-07-17 15:46:27 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar08c308a2019-09-04 17:48:15 +020018static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
19
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010020// List heads for garbage collection.
21static list_T *first_list = NULL; // list of all lists
Bram Moolenaarda861d62016-07-17 15:46:27 +020022
Bram Moolenaar00d253e2020-04-06 22:13:01 +020023#define FOR_ALL_WATCHERS(l, lw) \
24 for ((lw) = (l)->lv_watch; (lw) != NULL; (lw) = (lw)->lw_next)
25
Bram Moolenaarbdff0122020-04-05 18:56:05 +020026static void list_free_item(list_T *l, listitem_T *item);
27
Bram Moolenaarda861d62016-07-17 15:46:27 +020028/*
29 * Add a watcher to a list.
30 */
31 void
32list_add_watch(list_T *l, listwatch_T *lw)
33{
34 lw->lw_next = l->lv_watch;
35 l->lv_watch = lw;
36}
37
38/*
39 * Remove a watcher from a list.
40 * No warning when it isn't found...
41 */
42 void
43list_rem_watch(list_T *l, listwatch_T *lwrem)
44{
45 listwatch_T *lw, **lwp;
46
47 lwp = &l->lv_watch;
Bram Moolenaar00d253e2020-04-06 22:13:01 +020048 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020049 {
50 if (lw == lwrem)
51 {
52 *lwp = lw->lw_next;
53 break;
54 }
55 lwp = &lw->lw_next;
56 }
57}
58
59/*
60 * Just before removing an item from a list: advance watchers to the next
61 * item.
62 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020063 static void
Bram Moolenaarda861d62016-07-17 15:46:27 +020064list_fix_watch(list_T *l, listitem_T *item)
65{
66 listwatch_T *lw;
67
Bram Moolenaar00d253e2020-04-06 22:13:01 +020068 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020069 if (lw->lw_item == item)
70 lw->lw_item = item->li_next;
71}
72
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073 static void
74list_init(list_T *l)
75{
76 // Prepend the list to the list of lists for garbage collection.
77 if (first_list != NULL)
78 first_list->lv_used_prev = l;
79 l->lv_used_prev = NULL;
80 l->lv_used_next = first_list;
81 first_list = l;
82}
83
Bram Moolenaarda861d62016-07-17 15:46:27 +020084/*
85 * Allocate an empty header for a list.
86 * Caller should take care of the reference count.
87 */
88 list_T *
89list_alloc(void)
90{
91 list_T *l;
92
Bram Moolenaarc799fe22019-05-28 23:08:19 +020093 l = ALLOC_CLEAR_ONE(list_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +020094 if (l != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095 list_init(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +020096 return l;
97}
98
99/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100100 * list_alloc() with an ID for alloc_fail().
101 */
102 list_T *
103list_alloc_id(alloc_id_T id UNUSED)
104{
105#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200106 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100107 return NULL;
108#endif
109 return (list_alloc());
110}
111
112/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 * Allocate space for a list, plus "count" items.
114 * Next list_set_item() must be called for each item.
115 */
116 list_T *
117list_alloc_with_items(int count)
118{
119 list_T *l;
120
121 l = (list_T *)alloc_clear(sizeof(list_T) + count * sizeof(listitem_T));
122 if (l != NULL)
123 {
124 list_init(l);
125
126 if (count > 0)
127 {
128 listitem_T *li = (listitem_T *)(l + 1);
129 int i;
130
131 l->lv_len = count;
132 l->lv_with_items = count;
133 l->lv_first = li;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100134 l->lv_u.mat.lv_last = li + count - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 for (i = 0; i < count; ++i)
136 {
137 if (i == 0)
138 li->li_prev = NULL;
139 else
140 li->li_prev = li - 1;
141 if (i == count - 1)
142 li->li_next = NULL;
143 else
144 li->li_next = li + 1;
145 ++li;
146 }
147 }
148 }
149 return l;
150}
151
152/*
153 * Set item "idx" for a list previously allocated with list_alloc_with_items().
154 * The contents of "tv" is moved into the list item.
155 * Each item must be set exactly once.
156 */
157 void
158list_set_item(list_T *l, int idx, typval_T *tv)
159{
160 listitem_T *li = (listitem_T *)(l + 1) + idx;
161
162 li->li_tv = *tv;
163}
164
165/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200166 * Allocate an empty list for a return value, with reference count set.
167 * Returns OK or FAIL.
168 */
169 int
170rettv_list_alloc(typval_T *rettv)
171{
172 list_T *l = list_alloc();
173
174 if (l == NULL)
175 return FAIL;
176
Bram Moolenaarda861d62016-07-17 15:46:27 +0200177 rettv->v_lock = 0;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200178 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200179 return OK;
180}
181
182/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100183 * Same as rettv_list_alloc() but uses an allocation id for testing.
184 */
185 int
186rettv_list_alloc_id(typval_T *rettv, alloc_id_T id UNUSED)
187{
188#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200189 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaar162b7142018-12-21 15:17:36 +0100190 return FAIL;
191#endif
192 return rettv_list_alloc(rettv);
193}
194
195
196/*
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200197 * Set a list as the return value. Increments the reference count.
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200198 */
199 void
200rettv_list_set(typval_T *rettv, list_T *l)
201{
202 rettv->v_type = VAR_LIST;
203 rettv->vval.v_list = l;
204 if (l != NULL)
205 ++l->lv_refcount;
206}
207
208/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200209 * Unreference a list: decrement the reference count and free it when it
210 * becomes zero.
211 */
212 void
213list_unref(list_T *l)
214{
215 if (l != NULL && --l->lv_refcount <= 0)
216 list_free(l);
217}
218
219/*
220 * Free a list, including all non-container items it points to.
221 * Ignores the reference count.
222 */
223 static void
224list_free_contents(list_T *l)
225{
226 listitem_T *item;
227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 if (l->lv_first != &range_list_item)
229 for (item = l->lv_first; item != NULL; item = l->lv_first)
230 {
231 // Remove the item before deleting it.
232 l->lv_first = item->li_next;
233 clear_tv(&item->li_tv);
234 list_free_item(l, item);
235 }
Bram Moolenaarda861d62016-07-17 15:46:27 +0200236}
237
238/*
239 * Go through the list of lists and free items without the copyID.
240 * But don't free a list that has a watcher (used in a for loop), these
241 * are not referenced anywhere.
242 */
243 int
244list_free_nonref(int copyID)
245{
246 list_T *ll;
247 int did_free = FALSE;
248
249 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
250 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
251 && ll->lv_watch == NULL)
252 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100253 // Free the List and ordinary items it contains, but don't recurse
254 // into Lists and Dictionaries, they will be in the list of dicts
255 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200256 list_free_contents(ll);
257 did_free = TRUE;
258 }
259 return did_free;
260}
261
262 static void
263list_free_list(list_T *l)
264{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100265 // Remove the list from the list of lists for garbage collection.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200266 if (l->lv_used_prev == NULL)
267 first_list = l->lv_used_next;
268 else
269 l->lv_used_prev->lv_used_next = l->lv_used_next;
270 if (l->lv_used_next != NULL)
271 l->lv_used_next->lv_used_prev = l->lv_used_prev;
272
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100273 free_type(l->lv_type);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200274 vim_free(l);
275}
276
277 void
278list_free_items(int copyID)
279{
280 list_T *ll, *ll_next;
281
282 for (ll = first_list; ll != NULL; ll = ll_next)
283 {
284 ll_next = ll->lv_used_next;
285 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
286 && ll->lv_watch == NULL)
287 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100288 // Free the List and ordinary items it contains, but don't recurse
289 // into Lists and Dictionaries, they will be in the list of dicts
290 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200291 list_free_list(ll);
292 }
293 }
294}
295
296 void
297list_free(list_T *l)
298{
299 if (!in_free_unref_items)
300 {
301 list_free_contents(l);
302 list_free_list(l);
303 }
304}
305
306/*
307 * Allocate a list item.
308 * It is not initialized, don't forget to set v_lock.
309 */
310 listitem_T *
311listitem_alloc(void)
312{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200313 return ALLOC_ONE(listitem_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200314}
315
316/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100317 * Free a list item, unless it was allocated together with the list itself.
318 * Does not clear the value. Does not notify watchers.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200319 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200320 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100321list_free_item(list_T *l, listitem_T *item)
322{
323 if (l->lv_with_items == 0 || item < (listitem_T *)l
324 || item >= (listitem_T *)(l + 1) + l->lv_with_items)
325 vim_free(item);
326}
327
328/*
329 * Free a list item, unless it was allocated together with the list itself.
330 * Also clears the value. Does not notify watchers.
331 */
332 void
333listitem_free(list_T *l, listitem_T *item)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200334{
335 clear_tv(&item->li_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 list_free_item(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200337}
338
339/*
340 * Remove a list item from a List and free it. Also clears the value.
341 */
342 void
343listitem_remove(list_T *l, listitem_T *item)
344{
345 vimlist_remove(l, item, item);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 listitem_free(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200347}
348
349/*
350 * Get the number of items in a list.
351 */
352 long
353list_len(list_T *l)
354{
355 if (l == NULL)
356 return 0L;
357 return l->lv_len;
358}
359
360/*
361 * Return TRUE when two lists have exactly the same values.
362 */
363 int
364list_equal(
365 list_T *l1,
366 list_T *l2,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100367 int ic, // ignore case for strings
368 int recursive) // TRUE when used recursively
Bram Moolenaarda861d62016-07-17 15:46:27 +0200369{
370 listitem_T *item1, *item2;
371
Bram Moolenaarda861d62016-07-17 15:46:27 +0200372 if (l1 == l2)
373 return TRUE;
374 if (list_len(l1) != list_len(l2))
375 return FALSE;
Bram Moolenaar7b293c72020-04-09 21:33:22 +0200376 if (list_len(l1) == 0)
377 // empty and NULL list are considered equal
378 return TRUE;
379 if (l1 == NULL || l2 == NULL)
380 return FALSE;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200381
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200382 CHECK_LIST_MATERIALIZE(l1);
383 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384
Bram Moolenaarda861d62016-07-17 15:46:27 +0200385 for (item1 = l1->lv_first, item2 = l2->lv_first;
386 item1 != NULL && item2 != NULL;
387 item1 = item1->li_next, item2 = item2->li_next)
388 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
389 return FALSE;
390 return item1 == NULL && item2 == NULL;
391}
392
393/*
394 * Locate item with index "n" in list "l" and return it.
395 * A negative index is counted from the end; -1 is the last item.
396 * Returns NULL when "n" is out of range.
397 */
398 listitem_T *
399list_find(list_T *l, long n)
400{
401 listitem_T *item;
402 long idx;
403
404 if (l == NULL)
405 return NULL;
406
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100407 // Negative index is relative to the end.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200408 if (n < 0)
409 n = l->lv_len + n;
410
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100411 // Check for index out of range.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200412 if (n < 0 || n >= l->lv_len)
413 return NULL;
414
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200415 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100417 // When there is a cached index may start search from there.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100418 if (l->lv_u.mat.lv_idx_item != NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200419 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100420 if (n < l->lv_u.mat.lv_idx / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200421 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100422 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200423 item = l->lv_first;
424 idx = 0;
425 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100426 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200427 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100428 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100429 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200430 idx = l->lv_len - 1;
431 }
432 else
433 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100434 // closest to the cached index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100435 item = l->lv_u.mat.lv_idx_item;
436 idx = l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200437 }
438 }
439 else
440 {
441 if (n < l->lv_len / 2)
442 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100443 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200444 item = l->lv_first;
445 idx = 0;
446 }
447 else
448 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100449 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100450 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200451 idx = l->lv_len - 1;
452 }
453 }
454
455 while (n > idx)
456 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100457 // search forward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200458 item = item->li_next;
459 ++idx;
460 }
461 while (n < idx)
462 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100463 // search backward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200464 item = item->li_prev;
465 --idx;
466 }
467
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100468 // cache the used index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100469 l->lv_u.mat.lv_idx = idx;
470 l->lv_u.mat.lv_idx_item = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200471
472 return item;
473}
474
475/*
476 * Get list item "l[idx]" as a number.
477 */
478 long
479list_find_nr(
480 list_T *l,
481 long idx,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100482 int *errorp) // set to TRUE when something wrong
Bram Moolenaarda861d62016-07-17 15:46:27 +0200483{
484 listitem_T *li;
485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486 if (l != NULL && l->lv_first == &range_list_item)
487 {
488 long n = idx;
489
490 // not materialized range() list: compute the value.
491 // Negative index is relative to the end.
492 if (n < 0)
493 n = l->lv_len + n;
494
495 // Check for index out of range.
496 if (n < 0 || n >= l->lv_len)
497 {
498 if (errorp != NULL)
499 *errorp = TRUE;
500 return -1L;
501 }
502
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100503 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 }
505
Bram Moolenaarda861d62016-07-17 15:46:27 +0200506 li = list_find(l, idx);
507 if (li == NULL)
508 {
509 if (errorp != NULL)
510 *errorp = TRUE;
511 return -1L;
512 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100513 return (long)tv_get_number_chk(&li->li_tv, errorp);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200514}
515
516/*
517 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
518 */
519 char_u *
520list_find_str(list_T *l, long idx)
521{
522 listitem_T *li;
523
524 li = list_find(l, idx - 1);
525 if (li == NULL)
526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 semsg(_(e_listidx), idx);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200528 return NULL;
529 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100530 return tv_get_string(&li->li_tv);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200531}
532
533/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100534 * Like list_find() but when a negative index is used that is not found use
535 * zero and set "idx" to zero. Used for first index of a range.
536 */
537 listitem_T *
538list_find_index(list_T *l, long *idx)
539{
540 listitem_T *li = list_find(l, *idx);
541
542 if (li == NULL)
543 {
544 if (*idx < 0)
545 {
546 *idx = 0;
547 li = list_find(l, *idx);
548 }
549 }
550 return li;
551}
552
553/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200554 * Locate "item" list "l" and return its index.
555 * Returns -1 when "item" is not in the list.
556 */
557 long
558list_idx_of_item(list_T *l, listitem_T *item)
559{
560 long idx = 0;
561 listitem_T *li;
562
563 if (l == NULL)
564 return -1;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200565 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200566 idx = 0;
567 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
568 ++idx;
569 if (li == NULL)
570 return -1;
571 return idx;
572}
573
574/*
575 * Append item "item" to the end of list "l".
576 */
577 void
578list_append(list_T *l, listitem_T *item)
579{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200580 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100581 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200582 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100583 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200584 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100585 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200586 item->li_prev = NULL;
587 }
588 else
589 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100590 l->lv_u.mat.lv_last->li_next = item;
591 item->li_prev = l->lv_u.mat.lv_last;
592 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200593 }
594 ++l->lv_len;
595 item->li_next = NULL;
596}
597
598/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200600 * Return FAIL when out of memory.
601 */
602 int
603list_append_tv(list_T *l, typval_T *tv)
604{
605 listitem_T *li = listitem_alloc();
606
607 if (li == NULL)
608 return FAIL;
609 copy_tv(tv, &li->li_tv);
610 list_append(l, li);
611 return OK;
612}
613
614/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 * As list_append_tv() but move the value instead of copying it.
616 * Return FAIL when out of memory.
617 */
618 int
619list_append_tv_move(list_T *l, typval_T *tv)
620{
621 listitem_T *li = listitem_alloc();
622
623 if (li == NULL)
624 return FAIL;
625 li->li_tv = *tv;
626 list_append(l, li);
627 return OK;
628}
629
630/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200631 * Add a dictionary to a list. Used by getqflist().
632 * Return FAIL when out of memory.
633 */
634 int
635list_append_dict(list_T *list, dict_T *dict)
636{
637 listitem_T *li = listitem_alloc();
638
639 if (li == NULL)
640 return FAIL;
641 li->li_tv.v_type = VAR_DICT;
642 li->li_tv.v_lock = 0;
643 li->li_tv.vval.v_dict = dict;
644 list_append(list, li);
645 ++dict->dv_refcount;
646 return OK;
647}
648
649/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100650 * Append list2 to list1.
651 * Return FAIL when out of memory.
652 */
653 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200654list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100655{
656 listitem_T *li = listitem_alloc();
657
658 if (li == NULL)
659 return FAIL;
660 li->li_tv.v_type = VAR_LIST;
661 li->li_tv.v_lock = 0;
662 li->li_tv.vval.v_list = list2;
663 list_append(list1, li);
664 ++list2->lv_refcount;
665 return OK;
666}
667
668/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200669 * Make a copy of "str" and append it as an item to list "l".
670 * When "len" >= 0 use "str[len]".
671 * Returns FAIL when out of memory.
672 */
673 int
674list_append_string(list_T *l, char_u *str, int len)
675{
676 listitem_T *li = listitem_alloc();
677
678 if (li == NULL)
679 return FAIL;
680 list_append(l, li);
681 li->li_tv.v_type = VAR_STRING;
682 li->li_tv.v_lock = 0;
683 if (str == NULL)
684 li->li_tv.vval.v_string = NULL;
685 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
686 : vim_strsave(str))) == NULL)
687 return FAIL;
688 return OK;
689}
690
691/*
692 * Append "n" to list "l".
693 * Returns FAIL when out of memory.
694 */
695 int
696list_append_number(list_T *l, varnumber_T n)
697{
698 listitem_T *li;
699
700 li = listitem_alloc();
701 if (li == NULL)
702 return FAIL;
703 li->li_tv.v_type = VAR_NUMBER;
704 li->li_tv.v_lock = 0;
705 li->li_tv.vval.v_number = n;
706 list_append(l, li);
707 return OK;
708}
709
710/*
711 * Insert typval_T "tv" in list "l" before "item".
712 * If "item" is NULL append at the end.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100713 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200714 */
715 int
716list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
717{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100718 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200719
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100720 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100721 && check_typval_arg_type(l->lv_type->tt_member, tv, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100722 return FAIL;
723 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200724 if (ni == NULL)
725 return FAIL;
726 copy_tv(tv, &ni->li_tv);
727 list_insert(l, ni, item);
728 return OK;
729}
730
731 void
732list_insert(list_T *l, listitem_T *ni, listitem_T *item)
733{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200734 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200735 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100736 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200737 list_append(l, ni);
738 else
739 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100740 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200741 ni->li_prev = item->li_prev;
742 ni->li_next = item;
743 if (item->li_prev == NULL)
744 {
745 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100746 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200747 }
748 else
749 {
750 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100751 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200752 }
753 item->li_prev = ni;
754 ++l->lv_len;
755 }
756}
757
758/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200759 * Flatten "list" to depth "maxdepth".
760 * It does nothing if "maxdepth" is 0.
761 * Returns FAIL when out of memory.
762 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100763 static void
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200764list_flatten(list_T *list, long maxdepth)
765{
766 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200767 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200768 int n;
769
770 if (maxdepth == 0)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100771 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200772 CHECK_LIST_MATERIALIZE(list);
773
774 n = 0;
775 item = list->lv_first;
776 while (item != NULL)
777 {
778 fast_breakcheck();
779 if (got_int)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100780 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200781
782 if (item->li_tv.v_type == VAR_LIST)
783 {
784 listitem_T *next = item->li_next;
785
786 vimlist_remove(list, item, item);
787 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100788 return;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200789 clear_tv(&item->li_tv);
790 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200791
792 if (item->li_prev == NULL)
793 item = list->lv_first;
794 else
795 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200796 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200797
798 if (++n >= maxdepth)
799 {
800 n = 0;
801 item = next;
802 }
803 }
804 else
805 {
806 n = 0;
807 item = item->li_next;
808 }
809 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200810}
811
812/*
Bram Moolenaar3b690062021-02-01 20:14:51 +0100813 * "flatten()" and "flattennew()" functions
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200814 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100815 static void
816flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200817{
818 list_T *l;
819 long maxdepth;
820 int error = FALSE;
821
822 if (argvars[0].v_type != VAR_LIST)
823 {
824 semsg(_(e_listarg), "flatten()");
825 return;
826 }
827
828 if (argvars[1].v_type == VAR_UNKNOWN)
829 maxdepth = 999999;
830 else
831 {
832 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
833 if (error)
834 return;
835 if (maxdepth < 0)
836 {
837 emsg(_("E900: maxdepth must be non-negative number"));
838 return;
839 }
840 }
841
842 l = argvars[0].vval.v_list;
Bram Moolenaar3b690062021-02-01 20:14:51 +0100843 rettv->v_type = VAR_LIST;
844 rettv->vval.v_list = l;
845 if (l == NULL)
846 return;
847
848 if (make_copy)
849 {
850 l = list_copy(l, TRUE, get_copyID());
851 rettv->vval.v_list = l;
852 if (l == NULL)
853 return;
854 }
855 else
856 {
857 if (value_check_lock(l->lv_lock,
858 (char_u *)N_("flatten() argument"), TRUE))
859 return;
860 ++l->lv_refcount;
861 }
862
863 list_flatten(l, maxdepth);
864}
865
866/*
867 * "flatten(list[, {maxdepth}])" function
868 */
869 void
870f_flatten(typval_T *argvars, typval_T *rettv)
871{
872 if (in_vim9script())
873 emsg(_(e_cannot_use_flatten_in_vim9_script));
874 else
875 flatten_common(argvars, rettv, FALSE);
876}
877
878/*
879 * "flattennew(list[, {maxdepth}])" function
880 */
881 void
882f_flattennew(typval_T *argvars, typval_T *rettv)
883{
884 flatten_common(argvars, rettv, TRUE);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200885}
886
887/*
Bram Moolenaar1a739232020-10-10 15:37:58 +0200888 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200889 * If "bef" is NULL append at the end, otherwise insert before this item.
890 * Returns FAIL when out of memory.
891 */
892 int
893list_extend(list_T *l1, list_T *l2, listitem_T *bef)
894{
895 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200896 int todo;
Bram Moolenaardcae51f2021-04-08 20:10:10 +0200897 listitem_T *bef_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200898
Bram Moolenaar1a739232020-10-10 15:37:58 +0200899 // NULL list is equivalent to an empty list: nothing to do.
900 if (l2 == NULL || l2->lv_len == 0)
901 return OK;
902
903 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200904 CHECK_LIST_MATERIALIZE(l1);
905 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906
Bram Moolenaardcae51f2021-04-08 20:10:10 +0200907 // When exending a list with itself, at some point we run into the item
908 // that was before "bef" and need to skip over the already inserted items
909 // to "bef".
910 bef_prev = bef == NULL ? NULL : bef->li_prev;
911
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100912 // We also quit the loop when we have inserted the original item count of
913 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaardcae51f2021-04-08 20:10:10 +0200914 for (item = l2->lv_first; item != NULL && --todo >= 0;
915 item = item == bef_prev ? bef : item->li_next)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200916 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
917 return FAIL;
918 return OK;
919}
920
921/*
922 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
923 * Return FAIL when out of memory.
924 */
925 int
926list_concat(list_T *l1, list_T *l2, typval_T *tv)
927{
928 list_T *l;
929
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100930 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +0200931 if (l1 == NULL)
932 l = list_alloc();
933 else
934 l = list_copy(l1, FALSE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200935 if (l == NULL)
936 return FAIL;
937 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +0100938 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200939 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200940 if (l1 == NULL)
941 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200942
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100943 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200944 return list_extend(l, l2, NULL);
945}
946
Bram Moolenaar9af78762020-06-16 11:34:42 +0200947 list_T *
948list_slice(list_T *ol, long n1, long n2)
949{
950 listitem_T *item;
951 list_T *l = list_alloc();
952
953 if (l == NULL)
954 return NULL;
955 for (item = list_find(ol, n1); n1 <= n2; ++n1)
956 {
957 if (list_append_tv(l, &item->li_tv) == FAIL)
958 {
959 list_free(l);
960 return NULL;
961 }
962 item = item->li_next;
963 }
964 return l;
965}
966
Bram Moolenaared591872020-08-15 22:14:53 +0200967 int
968list_slice_or_index(
969 list_T *list,
970 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +0100971 varnumber_T n1_arg,
972 varnumber_T n2_arg,
973 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +0200974 typval_T *rettv,
975 int verbose)
976{
977 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +0100978 varnumber_T n1 = n1_arg;
979 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +0200980 typval_T var1;
981
982 if (n1 < 0)
983 n1 = len + n1;
984 if (n1 < 0 || n1 >= len)
985 {
986 // For a range we allow invalid values and return an empty
987 // list. A list index out of range is an error.
988 if (!range)
989 {
990 if (verbose)
Bram Moolenaar239f8d92021-01-17 13:21:20 +0100991 semsg(_(e_listidx), (long)n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +0200992 return FAIL;
993 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200994 n1 = n1 < 0 ? 0 : len;
Bram Moolenaared591872020-08-15 22:14:53 +0200995 }
996 if (range)
997 {
998 list_T *l;
999
1000 if (n2 < 0)
1001 n2 = len + n2;
1002 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01001003 n2 = len - (exclusive ? 0 : 1);
1004 if (exclusive)
1005 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +02001006 if (n2 < 0 || n2 + 1 < n1)
1007 n2 = -1;
1008 l = list_slice(list, n1, n2);
1009 if (l == NULL)
1010 return FAIL;
1011 clear_tv(rettv);
1012 rettv_list_set(rettv, l);
1013 }
1014 else
1015 {
1016 // copy the item to "var1" to avoid that freeing the list makes it
1017 // invalid.
1018 copy_tv(&list_find(list, n1)->li_tv, &var1);
1019 clear_tv(rettv);
1020 *rettv = var1;
1021 }
1022 return OK;
1023}
1024
Bram Moolenaarda861d62016-07-17 15:46:27 +02001025/*
1026 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1027 * The refcount of the new list is set to 1.
1028 * See item_copy() for "copyID".
1029 * Returns NULL when out of memory.
1030 */
1031 list_T *
1032list_copy(list_T *orig, int deep, int copyID)
1033{
1034 list_T *copy;
1035 listitem_T *item;
1036 listitem_T *ni;
1037
1038 if (orig == NULL)
1039 return NULL;
1040
1041 copy = list_alloc();
1042 if (copy != NULL)
1043 {
1044 if (copyID != 0)
1045 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001046 // Do this before adding the items, because one of the items may
1047 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001048 orig->lv_copyID = copyID;
1049 orig->lv_copylist = copy;
1050 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001051 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001052 for (item = orig->lv_first; item != NULL && !got_int;
1053 item = item->li_next)
1054 {
1055 ni = listitem_alloc();
1056 if (ni == NULL)
1057 break;
1058 if (deep)
1059 {
1060 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
1061 {
1062 vim_free(ni);
1063 break;
1064 }
1065 }
1066 else
1067 copy_tv(&item->li_tv, &ni->li_tv);
1068 list_append(copy, ni);
1069 }
1070 ++copy->lv_refcount;
1071 if (item != NULL)
1072 {
1073 list_unref(copy);
1074 copy = NULL;
1075 }
1076 }
1077
1078 return copy;
1079}
1080
1081/*
1082 * Remove items "item" to "item2" from list "l".
1083 * Does not free the listitem or the value!
1084 * This used to be called list_remove, but that conflicts with a Sun header
1085 * file.
1086 */
1087 void
1088vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1089{
1090 listitem_T *ip;
1091
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001092 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001094 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001095 for (ip = item; ip != NULL; ip = ip->li_next)
1096 {
1097 --l->lv_len;
1098 list_fix_watch(l, ip);
1099 if (ip == item2)
1100 break;
1101 }
1102
1103 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001104 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001105 else
1106 item2->li_next->li_prev = item->li_prev;
1107 if (item->li_prev == NULL)
1108 l->lv_first = item2->li_next;
1109 else
1110 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001111 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001112}
1113
1114/*
1115 * Return an allocated string with the string representation of a list.
1116 * May return NULL.
1117 */
1118 char_u *
1119list2string(typval_T *tv, int copyID, int restore_copyID)
1120{
1121 garray_T ga;
1122
1123 if (tv->vval.v_list == NULL)
1124 return NULL;
1125 ga_init2(&ga, (int)sizeof(char), 80);
1126 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001127 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001128 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1129 FALSE, restore_copyID, copyID) == FAIL)
1130 {
1131 vim_free(ga.ga_data);
1132 return NULL;
1133 }
1134 ga_append(&ga, ']');
1135 ga_append(&ga, NUL);
1136 return (char_u *)ga.ga_data;
1137}
1138
1139typedef struct join_S {
1140 char_u *s;
1141 char_u *tofree;
1142} join_T;
1143
1144 static int
1145list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001146 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001147 list_T *l,
1148 char_u *sep,
1149 int echo_style,
1150 int restore_copyID,
1151 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001152 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001153{
1154 int i;
1155 join_T *p;
1156 int len;
1157 int sumlen = 0;
1158 int first = TRUE;
1159 char_u *tofree;
1160 char_u numbuf[NUMBUFLEN];
1161 listitem_T *item;
1162 char_u *s;
1163
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001164 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001165 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001166 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1167 {
1168 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001169 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001170 if (s == NULL)
1171 return FAIL;
1172
1173 len = (int)STRLEN(s);
1174 sumlen += len;
1175
1176 (void)ga_grow(join_gap, 1);
1177 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1178 if (tofree != NULL || s != numbuf)
1179 {
1180 p->s = s;
1181 p->tofree = tofree;
1182 }
1183 else
1184 {
1185 p->s = vim_strnsave(s, len);
1186 p->tofree = p->s;
1187 }
1188
1189 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001190 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001191 break;
1192 }
1193
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001194 // Allocate result buffer with its total size, avoid re-allocation and
1195 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001196 if (join_gap->ga_len >= 2)
1197 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1198 if (ga_grow(gap, sumlen + 2) == FAIL)
1199 return FAIL;
1200
1201 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1202 {
1203 if (first)
1204 first = FALSE;
1205 else
1206 ga_concat(gap, sep);
1207 p = ((join_T *)join_gap->ga_data) + i;
1208
1209 if (p->s != NULL)
1210 ga_concat(gap, p->s);
1211 line_breakcheck();
1212 }
1213
1214 return OK;
1215}
1216
1217/*
1218 * Join list "l" into a string in "*gap", using separator "sep".
1219 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1220 * Return FAIL or OK.
1221 */
1222 int
1223list_join(
1224 garray_T *gap,
1225 list_T *l,
1226 char_u *sep,
1227 int echo_style,
1228 int restore_copyID,
1229 int copyID)
1230{
1231 garray_T join_ga;
1232 int retval;
1233 join_T *p;
1234 int i;
1235
1236 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001237 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001238 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1239 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1240 copyID, &join_ga);
1241
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001242 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001243 if (join_ga.ga_data != NULL)
1244 {
1245 p = (join_T *)join_ga.ga_data;
1246 for (i = 0; i < join_ga.ga_len; ++i)
1247 {
1248 vim_free(p->tofree);
1249 ++p;
1250 }
1251 ga_clear(&join_ga);
1252 }
1253
1254 return retval;
1255}
1256
1257/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001258 * "join()" function
1259 */
1260 void
1261f_join(typval_T *argvars, typval_T *rettv)
1262{
1263 garray_T ga;
1264 char_u *sep;
1265
1266 if (argvars[0].v_type != VAR_LIST)
1267 {
1268 emsg(_(e_listreq));
1269 return;
1270 }
1271 if (argvars[0].vval.v_list == NULL)
1272 return;
1273 if (argvars[1].v_type == VAR_UNKNOWN)
1274 sep = (char_u *)" ";
1275 else
1276 sep = tv_get_string_chk(&argvars[1]);
1277
1278 rettv->v_type = VAR_STRING;
1279
1280 if (sep != NULL)
1281 {
1282 ga_init2(&ga, (int)sizeof(char), 80);
1283 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1284 ga_append(&ga, NUL);
1285 rettv->vval.v_string = (char_u *)ga.ga_data;
1286 }
1287 else
1288 rettv->vval.v_string = NULL;
1289}
1290
1291/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001292 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001293 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001294 * Return OK or FAIL.
1295 */
1296 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001297eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001298{
Bram Moolenaar71478202020-06-26 22:46:27 +02001299 int evaluate = evalarg == NULL ? FALSE
1300 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001301 list_T *l = NULL;
1302 typval_T tv;
1303 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001304 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001305 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001306
1307 if (evaluate)
1308 {
1309 l = list_alloc();
1310 if (l == NULL)
1311 return FAIL;
1312 }
1313
Bram Moolenaar962d7212020-07-04 14:15:00 +02001314 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001315 while (**arg != ']' && **arg != NUL)
1316 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001317 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001318 goto failret;
1319 if (evaluate)
1320 {
1321 item = listitem_alloc();
1322 if (item != NULL)
1323 {
1324 item->li_tv = tv;
1325 item->li_tv.v_lock = 0;
1326 list_append(l, item);
1327 }
1328 else
1329 clear_tv(&tv);
1330 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001331 // Legacy Vim script allowed a space before the comma.
1332 if (!vim9script)
1333 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001334
Bram Moolenaare6e03172020-06-27 16:36:05 +02001335 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001336 had_comma = **arg == ',';
1337 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001338 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001339 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001340 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001341 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001342 goto failret;
1343 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001344 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001345 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001346
Bram Moolenaare6b53242020-07-01 17:28:33 +02001347 // The "]" can be on the next line. But a double quoted string may
1348 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001349 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001350 if (**arg == ']')
1351 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001352
1353 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001354 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001356 {
1357 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001358 semsg(_(e_no_white_space_allowed_before_str_str),
1359 ",", *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001360 else
1361 semsg(_("E696: Missing comma in List: %s"), *arg);
1362 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001363 goto failret;
1364 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001365 }
1366
1367 if (**arg != ']')
1368 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001370 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001371failret:
1372 if (evaluate)
1373 list_free(l);
1374 return FAIL;
1375 }
1376
Bram Moolenaar9d489562020-07-30 20:08:50 +02001377 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001378 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001379 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001380
1381 return OK;
1382}
1383
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001384/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001385 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001386 */
1387 int
1388write_list(FILE *fd, list_T *list, int binary)
1389{
1390 listitem_T *li;
1391 int c;
1392 int ret = OK;
1393 char_u *s;
1394
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001395 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001396 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001397 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001398 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001399 {
1400 if (*s == '\n')
1401 c = putc(NUL, fd);
1402 else
1403 c = putc(*s, fd);
1404 if (c == EOF)
1405 {
1406 ret = FAIL;
1407 break;
1408 }
1409 }
1410 if (!binary || li->li_next != NULL)
1411 if (putc('\n', fd) == EOF)
1412 {
1413 ret = FAIL;
1414 break;
1415 }
1416 if (ret == FAIL)
1417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001418 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001419 break;
1420 }
1421 }
1422 return ret;
1423}
1424
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001425/*
1426 * Initialize a static list with 10 items.
1427 */
1428 void
1429init_static_list(staticList10_T *sl)
1430{
1431 list_T *l = &sl->sl_list;
1432 int i;
1433
1434 memset(sl, 0, sizeof(staticList10_T));
1435 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001436 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001437 l->lv_refcount = DO_NOT_FREE_CNT;
1438 l->lv_lock = VAR_FIXED;
1439 sl->sl_list.lv_len = 10;
1440
1441 for (i = 0; i < 10; ++i)
1442 {
1443 listitem_T *li = &sl->sl_items[i];
1444
1445 if (i == 0)
1446 li->li_prev = NULL;
1447 else
1448 li->li_prev = li - 1;
1449 if (i == 9)
1450 li->li_next = NULL;
1451 else
1452 li->li_next = li + 1;
1453 }
1454}
1455
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001456/*
1457 * "list2str()" function
1458 */
1459 void
1460f_list2str(typval_T *argvars, typval_T *rettv)
1461{
1462 list_T *l;
1463 listitem_T *li;
1464 garray_T ga;
1465 int utf8 = FALSE;
1466
1467 rettv->v_type = VAR_STRING;
1468 rettv->vval.v_string = NULL;
1469 if (argvars[0].v_type != VAR_LIST)
1470 {
1471 emsg(_(e_invarg));
1472 return;
1473 }
1474
1475 l = argvars[0].vval.v_list;
1476 if (l == NULL)
1477 return; // empty list results in empty string
1478
1479 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001480 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001481
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001482 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001483 ga_init2(&ga, 1, 80);
1484 if (has_mbyte || utf8)
1485 {
1486 char_u buf[MB_MAXBYTES + 1];
1487 int (*char2bytes)(int, char_u *);
1488
1489 if (utf8 || enc_utf8)
1490 char2bytes = utf_char2bytes;
1491 else
1492 char2bytes = mb_char2bytes;
1493
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001494 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001495 {
1496 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1497 ga_concat(&ga, buf);
1498 }
1499 ga_append(&ga, NUL);
1500 }
1501 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1502 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001503 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001504 ga_append(&ga, tv_get_number(&li->li_tv));
1505 ga_append(&ga, NUL);
1506 }
1507
1508 rettv->v_type = VAR_STRING;
1509 rettv->vval.v_string = ga.ga_data;
1510}
1511
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001512 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001513list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1514{
1515 list_T *l;
1516 listitem_T *item, *item2;
1517 listitem_T *li;
1518 int error = FALSE;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001519 long idx;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001520
1521 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001522 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001523 return;
1524
1525 idx = (long)tv_get_number_chk(&argvars[1], &error);
1526 if (error)
1527 ; // type error: do nothing, errmsg already given
1528 else if ((item = list_find(l, idx)) == NULL)
1529 semsg(_(e_listidx), idx);
1530 else
1531 {
1532 if (argvars[2].v_type == VAR_UNKNOWN)
1533 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001534 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001535 vimlist_remove(l, item, item);
1536 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001538 }
1539 else
1540 {
1541 // Remove range of items, return list with values.
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001542 long end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001543
1544 if (error)
1545 ; // type error: do nothing
1546 else if ((item2 = list_find(l, end)) == NULL)
1547 semsg(_(e_listidx), end);
1548 else
1549 {
1550 int cnt = 0;
1551
1552 for (li = item; li != NULL; li = li->li_next)
1553 {
1554 ++cnt;
1555 if (li == item2)
1556 break;
1557 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001558 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001559 emsg(_(e_invrange));
1560 else
1561 {
1562 vimlist_remove(l, item, item2);
1563 if (rettv_list_alloc(rettv) == OK)
1564 {
1565 l = rettv->vval.v_list;
1566 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001567 l->lv_u.mat.lv_last = item2;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001568 item->li_prev = NULL;
1569 item2->li_next = NULL;
1570 l->lv_len = cnt;
1571 }
1572 }
1573 }
1574 }
1575 }
1576}
1577
1578static int item_compare(const void *s1, const void *s2);
1579static int item_compare2(const void *s1, const void *s2);
1580
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001581// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001582typedef struct
1583{
1584 listitem_T *item;
1585 int idx;
1586} sortItem_T;
1587
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001588// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001589typedef struct
1590{
1591 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001592 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001593 int item_compare_numeric;
1594 int item_compare_numbers;
1595#ifdef FEAT_FLOAT
1596 int item_compare_float;
1597#endif
1598 char_u *item_compare_func;
1599 partial_T *item_compare_partial;
1600 dict_T *item_compare_selfdict;
1601 int item_compare_func_err;
1602 int item_compare_keep_zero;
1603} sortinfo_T;
1604static sortinfo_T *sortinfo = NULL;
1605#define ITEM_COMPARE_FAIL 999
1606
1607/*
1608 * Compare functions for f_sort() and f_uniq() below.
1609 */
1610 static int
1611item_compare(const void *s1, const void *s2)
1612{
1613 sortItem_T *si1, *si2;
1614 typval_T *tv1, *tv2;
1615 char_u *p1, *p2;
1616 char_u *tofree1 = NULL, *tofree2 = NULL;
1617 int res;
1618 char_u numbuf1[NUMBUFLEN];
1619 char_u numbuf2[NUMBUFLEN];
1620
1621 si1 = (sortItem_T *)s1;
1622 si2 = (sortItem_T *)s2;
1623 tv1 = &si1->item->li_tv;
1624 tv2 = &si2->item->li_tv;
1625
1626 if (sortinfo->item_compare_numbers)
1627 {
1628 varnumber_T v1 = tv_get_number(tv1);
1629 varnumber_T v2 = tv_get_number(tv2);
1630
1631 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1632 }
1633
1634#ifdef FEAT_FLOAT
1635 if (sortinfo->item_compare_float)
1636 {
1637 float_T v1 = tv_get_float(tv1);
1638 float_T v2 = tv_get_float(tv2);
1639
1640 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1641 }
1642#endif
1643
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001644 // tv2string() puts quotes around a string and allocates memory. Don't do
1645 // that for string variables. Use a single quote when comparing with a
1646 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001647 if (tv1->v_type == VAR_STRING)
1648 {
1649 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1650 p1 = (char_u *)"'";
1651 else
1652 p1 = tv1->vval.v_string;
1653 }
1654 else
1655 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1656 if (tv2->v_type == VAR_STRING)
1657 {
1658 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1659 p2 = (char_u *)"'";
1660 else
1661 p2 = tv2->vval.v_string;
1662 }
1663 else
1664 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1665 if (p1 == NULL)
1666 p1 = (char_u *)"";
1667 if (p2 == NULL)
1668 p2 = (char_u *)"";
1669 if (!sortinfo->item_compare_numeric)
1670 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001671 if (sortinfo->item_compare_lc)
1672 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001673 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001674 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001675 }
1676 else
1677 {
1678 double n1, n2;
1679 n1 = strtod((char *)p1, (char **)&p1);
1680 n2 = strtod((char *)p2, (char **)&p2);
1681 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1682 }
1683
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001684 // When the result would be zero, compare the item indexes. Makes the
1685 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001686 if (res == 0 && !sortinfo->item_compare_keep_zero)
1687 res = si1->idx > si2->idx ? 1 : -1;
1688
1689 vim_free(tofree1);
1690 vim_free(tofree2);
1691 return res;
1692}
1693
1694 static int
1695item_compare2(const void *s1, const void *s2)
1696{
1697 sortItem_T *si1, *si2;
1698 int res;
1699 typval_T rettv;
1700 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001701 char_u *func_name;
1702 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001703 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001704
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001705 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001706 if (sortinfo->item_compare_func_err)
1707 return 0;
1708
1709 si1 = (sortItem_T *)s1;
1710 si2 = (sortItem_T *)s2;
1711
1712 if (partial == NULL)
1713 func_name = sortinfo->item_compare_func;
1714 else
1715 func_name = partial_name(partial);
1716
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001717 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1718 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001719 copy_tv(&si1->item->li_tv, &argv[0]);
1720 copy_tv(&si2->item->li_tv, &argv[1]);
1721
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001722 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001723 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001724 funcexe.evaluate = TRUE;
1725 funcexe.partial = partial;
1726 funcexe.selfdict = sortinfo->item_compare_selfdict;
1727 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001728 clear_tv(&argv[0]);
1729 clear_tv(&argv[1]);
1730
1731 if (res == FAIL)
1732 res = ITEM_COMPARE_FAIL;
1733 else
1734 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1735 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001736 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001737 clear_tv(&rettv);
1738
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001739 // When the result would be zero, compare the pointers themselves. Makes
1740 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001741 if (res == 0 && !sortinfo->item_compare_keep_zero)
1742 res = si1->idx > si2->idx ? 1 : -1;
1743
1744 return res;
1745}
1746
1747/*
1748 * "sort()" or "uniq()" function
1749 */
1750 static void
1751do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1752{
1753 list_T *l;
1754 listitem_T *li;
1755 sortItem_T *ptrs;
1756 sortinfo_T *old_sortinfo;
1757 sortinfo_T info;
1758 long len;
1759 long i;
1760
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001761 // Pointer to current info struct used in compare function. Save and
1762 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001763 old_sortinfo = sortinfo;
1764 sortinfo = &info;
1765
1766 if (argvars[0].v_type != VAR_LIST)
1767 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1768 else
1769 {
1770 l = argvars[0].vval.v_list;
Bram Moolenaara187c432020-09-16 21:08:28 +02001771 if (l == NULL || value_check_lock(l->lv_lock,
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001772 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1773 TRUE))
1774 goto theend;
1775 rettv_list_set(rettv, l);
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001776 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001777
1778 len = list_len(l);
1779 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001780 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001781
1782 info.item_compare_ic = FALSE;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001783 info.item_compare_lc = FALSE;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001784 info.item_compare_numeric = FALSE;
1785 info.item_compare_numbers = FALSE;
1786#ifdef FEAT_FLOAT
1787 info.item_compare_float = FALSE;
1788#endif
1789 info.item_compare_func = NULL;
1790 info.item_compare_partial = NULL;
1791 info.item_compare_selfdict = NULL;
1792 if (argvars[1].v_type != VAR_UNKNOWN)
1793 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001794 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001795 if (argvars[1].v_type == VAR_FUNC)
1796 info.item_compare_func = argvars[1].vval.v_string;
1797 else if (argvars[1].v_type == VAR_PARTIAL)
1798 info.item_compare_partial = argvars[1].vval.v_partial;
1799 else
1800 {
1801 int error = FALSE;
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001802 int nr = 0;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001803
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001804 if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001805 {
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001806 nr = tv_get_number_chk(&argvars[1], &error);
1807 if (error)
1808 goto theend; // type error; errmsg already given
1809 if (nr == 1)
1810 info.item_compare_ic = TRUE;
1811 }
1812 if (nr != 1)
1813 {
1814 if (argvars[1].v_type != VAR_NUMBER)
1815 info.item_compare_func = tv_get_string(&argvars[1]);
1816 else if (nr != 0)
1817 {
1818 emsg(_(e_invarg));
1819 goto theend;
1820 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001821 }
1822 if (info.item_compare_func != NULL)
1823 {
1824 if (*info.item_compare_func == NUL)
1825 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001826 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001827 info.item_compare_func = NULL;
1828 }
1829 else if (STRCMP(info.item_compare_func, "n") == 0)
1830 {
1831 info.item_compare_func = NULL;
1832 info.item_compare_numeric = TRUE;
1833 }
1834 else if (STRCMP(info.item_compare_func, "N") == 0)
1835 {
1836 info.item_compare_func = NULL;
1837 info.item_compare_numbers = TRUE;
1838 }
1839#ifdef FEAT_FLOAT
1840 else if (STRCMP(info.item_compare_func, "f") == 0)
1841 {
1842 info.item_compare_func = NULL;
1843 info.item_compare_float = TRUE;
1844 }
1845#endif
1846 else if (STRCMP(info.item_compare_func, "i") == 0)
1847 {
1848 info.item_compare_func = NULL;
1849 info.item_compare_ic = TRUE;
1850 }
Bram Moolenaar55e29612020-11-01 13:57:44 +01001851 else if (STRCMP(info.item_compare_func, "l") == 0)
1852 {
1853 info.item_compare_func = NULL;
1854 info.item_compare_lc = TRUE;
1855 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001856 }
1857 }
1858
1859 if (argvars[2].v_type != VAR_UNKNOWN)
1860 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001861 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001862 if (argvars[2].v_type != VAR_DICT)
1863 {
1864 emsg(_(e_dictreq));
1865 goto theend;
1866 }
1867 info.item_compare_selfdict = argvars[2].vval.v_dict;
1868 }
1869 }
1870
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001871 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001872 ptrs = ALLOC_MULT(sortItem_T, len);
1873 if (ptrs == NULL)
1874 goto theend;
1875
1876 i = 0;
1877 if (sort)
1878 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001879 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001880 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001881 {
1882 ptrs[i].item = li;
1883 ptrs[i].idx = i;
1884 ++i;
1885 }
1886
1887 info.item_compare_func_err = FALSE;
1888 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001889 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001890 if ((info.item_compare_func != NULL
1891 || info.item_compare_partial != NULL)
1892 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
1893 == ITEM_COMPARE_FAIL)
1894 emsg(_("E702: Sort compare function failed"));
1895 else
1896 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001897 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001898 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
1899 info.item_compare_func == NULL
1900 && info.item_compare_partial == NULL
1901 ? item_compare : item_compare2);
1902
1903 if (!info.item_compare_func_err)
1904 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001905 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001906 l->lv_first = l->lv_u.mat.lv_last
1907 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001908 l->lv_len = 0;
1909 for (i = 0; i < len; ++i)
1910 list_append(l, ptrs[i].item);
1911 }
1912 }
1913 }
1914 else
1915 {
1916 int (*item_compare_func_ptr)(const void *, const void *);
1917
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001918 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001919 info.item_compare_func_err = FALSE;
1920 info.item_compare_keep_zero = TRUE;
1921 item_compare_func_ptr = info.item_compare_func != NULL
1922 || info.item_compare_partial != NULL
1923 ? item_compare2 : item_compare;
1924
1925 for (li = l->lv_first; li != NULL && li->li_next != NULL;
1926 li = li->li_next)
1927 {
1928 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
1929 == 0)
1930 ptrs[i++].item = li;
1931 if (info.item_compare_func_err)
1932 {
1933 emsg(_("E882: Uniq compare function failed"));
1934 break;
1935 }
1936 }
1937
1938 if (!info.item_compare_func_err)
1939 {
1940 while (--i >= 0)
1941 {
1942 li = ptrs[i].item->li_next;
1943 ptrs[i].item->li_next = li->li_next;
1944 if (li->li_next != NULL)
1945 li->li_next->li_prev = ptrs[i].item;
1946 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001947 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001948 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001950 l->lv_len--;
1951 }
1952 }
1953 }
1954
1955 vim_free(ptrs);
1956 }
1957theend:
1958 sortinfo = old_sortinfo;
1959}
1960
1961/*
1962 * "sort({list})" function
1963 */
1964 void
1965f_sort(typval_T *argvars, typval_T *rettv)
1966{
1967 do_sort_uniq(argvars, rettv, TRUE);
1968}
1969
1970/*
1971 * "uniq({list})" function
1972 */
1973 void
1974f_uniq(typval_T *argvars, typval_T *rettv)
1975{
1976 do_sort_uniq(argvars, rettv, FALSE);
1977}
1978
Bram Moolenaarea696852020-11-09 18:31:39 +01001979typedef enum {
1980 FILTERMAP_FILTER,
1981 FILTERMAP_MAP,
1982 FILTERMAP_MAPNEW
1983} filtermap_T;
1984
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001985/*
1986 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01001987 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001988 */
1989 static int
Bram Moolenaarea696852020-11-09 18:31:39 +01001990filter_map_one(
1991 typval_T *tv, // original value
1992 typval_T *expr, // callback
1993 filtermap_T filtermap,
1994 typval_T *newtv, // for map() and mapnew(): new value
1995 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001996{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001997 typval_T argv[3];
1998 int retval = FAIL;
1999
2000 copy_tv(tv, get_vim_var_tv(VV_VAL));
2001 argv[0] = *get_vim_var_tv(VV_KEY);
2002 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01002003 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002004 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002005 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002006 {
2007 int error = FALSE;
2008
2009 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02002010 if (in_vim9script())
Bram Moolenaarea696852020-11-09 18:31:39 +01002011 *remp = !tv2bool(newtv);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002012 else
Bram Moolenaarea696852020-11-09 18:31:39 +01002013 *remp = (tv_get_number_chk(newtv, &error) == 0);
2014 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002015 // On type error, nothing has been removed; return FAIL to stop the
2016 // loop. The error message was given by tv_get_number_chk().
2017 if (error)
2018 goto theend;
2019 }
2020 retval = OK;
2021theend:
2022 clear_tv(get_vim_var_tv(VV_VAL));
2023 return retval;
2024}
2025
2026/*
2027 * Implementation of map() and filter().
2028 */
2029 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01002030filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002031{
2032 typval_T *expr;
2033 listitem_T *li, *nli;
2034 list_T *l = NULL;
2035 dictitem_T *di;
2036 hashtab_T *ht;
2037 hashitem_T *hi;
2038 dict_T *d = NULL;
2039 blob_T *b = NULL;
2040 int rem;
2041 int todo;
Bram Moolenaarea696852020-11-09 18:31:39 +01002042 char_u *ermsg = (char_u *)(filtermap == FILTERMAP_MAP ? "map()"
2043 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
2044 : "filter()");
2045 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2046 ? N_("map() argument")
2047 : filtermap == FILTERMAP_MAPNEW
2048 ? N_("mapnew() argument")
2049 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002050 int save_did_emsg;
2051 int idx = 0;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002052 type_T *type = NULL;
2053 garray_T type_list;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002054
Bram Moolenaarea696852020-11-09 18:31:39 +01002055 // map() and filter() return the first argument, also on failure.
2056 if (filtermap != FILTERMAP_MAPNEW)
2057 copy_tv(&argvars[0], rettv);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002058 if (filtermap == FILTERMAP_MAP && in_vim9script())
2059 {
2060 // Check that map() does not change the type of the dict.
2061 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaarf2253962021-04-13 20:53:13 +02002062 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002063 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002064
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002065 if (argvars[0].v_type == VAR_BLOB)
2066 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002067 if (filtermap == FILTERMAP_MAPNEW)
2068 {
2069 rettv->v_type = VAR_BLOB;
2070 rettv->vval.v_blob = NULL;
2071 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002072 if ((b = argvars[0].vval.v_blob) == NULL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002073 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002074 }
2075 else if (argvars[0].v_type == VAR_LIST)
2076 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002077 if (filtermap == FILTERMAP_MAPNEW)
2078 {
2079 rettv->v_type = VAR_LIST;
2080 rettv->vval.v_list = NULL;
2081 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002082 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002083 || (filtermap == FILTERMAP_FILTER
2084 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002085 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002086 }
2087 else if (argvars[0].v_type == VAR_DICT)
2088 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002089 if (filtermap == FILTERMAP_MAPNEW)
2090 {
2091 rettv->v_type = VAR_DICT;
2092 rettv->vval.v_dict = NULL;
2093 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002094 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002095 || (filtermap == FILTERMAP_FILTER
2096 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002097 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002098 }
2099 else
2100 {
Bram Moolenaarfcb0b612020-05-26 20:22:01 +02002101 semsg(_(e_listdictblobarg), ermsg);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002102 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002103 }
2104
2105 expr = &argvars[1];
2106 // On type errors, the preceding call has already displayed an error
2107 // message. Avoid a misleading error message for an empty string that
2108 // was not passed as argument.
2109 if (expr->v_type != VAR_UNKNOWN)
2110 {
2111 typval_T save_val;
2112 typval_T save_key;
2113
2114 prepare_vimvar(VV_VAL, &save_val);
2115 prepare_vimvar(VV_KEY, &save_key);
2116
2117 // We reset "did_emsg" to be able to detect whether an error
2118 // occurred during evaluation of the expression.
2119 save_did_emsg = did_emsg;
2120 did_emsg = FALSE;
2121
2122 if (argvars[0].v_type == VAR_DICT)
2123 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002124 int prev_lock = d->dv_lock;
Bram Moolenaarea696852020-11-09 18:31:39 +01002125 dict_T *d_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002126
Bram Moolenaarea696852020-11-09 18:31:39 +01002127 if (filtermap == FILTERMAP_MAPNEW)
2128 {
2129 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002130 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002131 d_ret = rettv->vval.v_dict;
2132 }
2133
2134 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002135 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002136 ht = &d->dv_hashtab;
2137 hash_lock(ht);
2138 todo = (int)ht->ht_used;
2139 for (hi = ht->ht_array; todo > 0; ++hi)
2140 {
2141 if (!HASHITEM_EMPTY(hi))
2142 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002143 int r;
2144 typval_T newtv;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002145
2146 --todo;
2147 di = HI2DI(hi);
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002148 if (filtermap == FILTERMAP_MAP
Bram Moolenaarea696852020-11-09 18:31:39 +01002149 && (value_check_lock(di->di_tv.v_lock,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002150 arg_errmsg, TRUE)
2151 || var_check_ro(di->di_flags,
2152 arg_errmsg, TRUE)))
2153 break;
2154 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaar027c4ab2021-02-21 16:20:18 +01002155 newtv.v_type = VAR_UNKNOWN;
Bram Moolenaarea696852020-11-09 18:31:39 +01002156 r = filter_map_one(&di->di_tv, expr, filtermap,
2157 &newtv, &rem);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002158 clear_tv(get_vim_var_tv(VV_KEY));
2159 if (r == FAIL || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002160 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002161 clear_tv(&newtv);
2162 break;
2163 }
2164 if (filtermap == FILTERMAP_MAP)
2165 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002166 if (type != NULL && check_typval_arg_type(
2167 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002168 {
2169 clear_tv(&newtv);
2170 break;
2171 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002172 // map(): replace the dict item value
2173 clear_tv(&di->di_tv);
2174 newtv.v_lock = 0;
2175 di->di_tv = newtv;
2176 }
2177 else if (filtermap == FILTERMAP_MAPNEW)
2178 {
2179 // mapnew(): add the item value to the new dict
2180 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
2181 clear_tv(&newtv);
2182 if (r == FAIL)
2183 break;
2184 }
2185 else if (filtermap == FILTERMAP_FILTER && rem)
2186 {
2187 // filter(false): remove the item from the dict
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002188 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2189 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2190 break;
2191 dictitem_remove(d, di);
2192 }
2193 }
2194 }
2195 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002196 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002197 }
2198 else if (argvars[0].v_type == VAR_BLOB)
2199 {
2200 int i;
2201 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002202 varnumber_T val;
Bram Moolenaarea696852020-11-09 18:31:39 +01002203 blob_T *b_ret = b;
2204
2205 if (filtermap == FILTERMAP_MAPNEW)
2206 {
2207 if (blob_copy(b, rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002208 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002209 b_ret = rettv->vval.v_blob;
2210 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002211
2212 // set_vim_var_nr() doesn't set the type
2213 set_vim_var_type(VV_KEY, VAR_NUMBER);
2214
2215 for (i = 0; i < b->bv_ga.ga_len; i++)
2216 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002217 typval_T newtv;
2218
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002219 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002220 val = blob_get(b, i);
2221 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002222 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaarea696852020-11-09 18:31:39 +01002223 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
2224 || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002225 break;
Bram Moolenaarea696852020-11-09 18:31:39 +01002226 if (newtv.v_type != VAR_NUMBER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002227 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002228 clear_tv(&newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002229 emsg(_(e_invalblob));
2230 break;
2231 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002232 if (filtermap != FILTERMAP_FILTER)
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002233 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002234 if (newtv.vval.v_number != val)
2235 blob_set(b_ret, i, newtv.vval.v_number);
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002236 }
2237 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002238 {
2239 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2240
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002241 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002242 (size_t)b->bv_ga.ga_len - i - 1);
2243 --b->bv_ga.ga_len;
2244 --i;
2245 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002246 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002247 }
2248 }
2249 else // argvars[0].v_type == VAR_LIST
2250 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002251 int prev_lock = l->lv_lock;
2252 list_T *l_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002253
Bram Moolenaarea696852020-11-09 18:31:39 +01002254 if (filtermap == FILTERMAP_MAPNEW)
2255 {
2256 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002257 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002258 l_ret = rettv->vval.v_list;
2259 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002260 // set_vim_var_nr() doesn't set the type
2261 set_vim_var_type(VV_KEY, VAR_NUMBER);
2262
Bram Moolenaarea696852020-11-09 18:31:39 +01002263 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002264 l->lv_lock = VAR_LOCKED;
Bram Moolenaarea696852020-11-09 18:31:39 +01002265
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002266 if (l->lv_first == &range_list_item)
2267 {
2268 varnumber_T val = l->lv_u.nonmat.lv_start;
2269 int len = l->lv_len;
2270 int stride = l->lv_u.nonmat.lv_stride;
2271
2272 // List from range(): loop over the numbers
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002273 if (filtermap != FILTERMAP_MAPNEW)
2274 {
2275 l->lv_first = NULL;
2276 l->lv_u.mat.lv_last = NULL;
2277 l->lv_len = 0;
2278 l->lv_u.mat.lv_idx_item = NULL;
2279 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002280
2281 for (idx = 0; idx < len; ++idx)
Bram Moolenaarc56936e2020-11-10 11:43:56 +01002282 {
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002283 typval_T tv;
2284 typval_T newtv;
2285
2286 tv.v_type = VAR_NUMBER;
2287 tv.v_lock = 0;
2288 tv.vval.v_number = val;
2289 set_vim_var_nr(VV_KEY, idx);
2290 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2291 == FAIL)
Bram Moolenaarea696852020-11-09 18:31:39 +01002292 break;
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002293 if (did_emsg)
2294 {
2295 clear_tv(&newtv);
2296 break;
2297 }
2298 if (filtermap != FILTERMAP_FILTER)
2299 {
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002300 if (filtermap == FILTERMAP_MAP && type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002301 && check_typval_arg_type(
2302 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002303 {
2304 clear_tv(&newtv);
2305 break;
2306 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002307 // map(), mapnew(): always append the new value to the
2308 // list
2309 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2310 ? l : l_ret, &newtv) == FAIL)
2311 break;
2312 }
2313 else if (!rem)
2314 {
2315 // filter(): append the list item value when not rem
2316 if (list_append_tv_move(l, &tv) == FAIL)
2317 break;
2318 }
2319
2320 val += stride;
Bram Moolenaarea696852020-11-09 18:31:39 +01002321 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002322 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002323 else
2324 {
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002325 // Materialized list: loop over the items
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002326 for (li = l->lv_first; li != NULL; li = nli)
2327 {
2328 typval_T newtv;
2329
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002330 if (filtermap == FILTERMAP_MAP && value_check_lock(
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002331 li->li_tv.v_lock, arg_errmsg, TRUE))
2332 break;
2333 nli = li->li_next;
2334 set_vim_var_nr(VV_KEY, idx);
2335 if (filter_map_one(&li->li_tv, expr, filtermap,
2336 &newtv, &rem) == FAIL)
2337 break;
2338 if (did_emsg)
2339 {
2340 clear_tv(&newtv);
2341 break;
2342 }
2343 if (filtermap == FILTERMAP_MAP)
2344 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002345 if (type != NULL && check_typval_arg_type(
2346 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002347 {
2348 clear_tv(&newtv);
2349 break;
2350 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002351 // map(): replace the list item value
2352 clear_tv(&li->li_tv);
2353 newtv.v_lock = 0;
2354 li->li_tv = newtv;
2355 }
2356 else if (filtermap == FILTERMAP_MAPNEW)
2357 {
2358 // mapnew(): append the list item value
2359 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2360 break;
2361 }
2362 else if (filtermap == FILTERMAP_FILTER && rem)
2363 listitem_remove(l, li);
2364 ++idx;
2365 }
2366 }
2367
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002368 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002369 }
2370
2371 restore_vimvar(VV_KEY, &save_key);
2372 restore_vimvar(VV_VAL, &save_val);
2373
2374 did_emsg |= save_did_emsg;
2375 }
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002376
2377theend:
2378 if (type != NULL)
2379 clear_type_list(&type_list);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002380}
2381
2382/*
2383 * "filter()" function
2384 */
2385 void
2386f_filter(typval_T *argvars, typval_T *rettv)
2387{
Bram Moolenaarea696852020-11-09 18:31:39 +01002388 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002389}
2390
2391/*
2392 * "map()" function
2393 */
2394 void
2395f_map(typval_T *argvars, typval_T *rettv)
2396{
Bram Moolenaarea696852020-11-09 18:31:39 +01002397 filter_map(argvars, rettv, FILTERMAP_MAP);
2398}
2399
2400/*
2401 * "mapnew()" function
2402 */
2403 void
2404f_mapnew(typval_T *argvars, typval_T *rettv)
2405{
2406 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002407}
2408
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002409/*
2410 * "add(list, item)" function
2411 */
2412 void
2413f_add(typval_T *argvars, typval_T *rettv)
2414{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002415 rettv->vval.v_number = 1; // Default: Failed
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002416 if (argvars[0].v_type == VAR_LIST)
2417 {
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002418 list_T *l = argvars[0].vval.v_list;
2419
2420 if (l == NULL)
2421 {
2422 if (in_vim9script())
2423 emsg(_(e_cannot_add_to_null_list));
2424 }
2425 else if (!value_check_lock(l->lv_lock,
2426 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002427 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002428 {
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002429 copy_tv(&argvars[0], rettv);
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002430 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002431 }
2432 else if (argvars[0].v_type == VAR_BLOB)
2433 {
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002434 blob_T *b = argvars[0].vval.v_blob;
2435
2436 if (b == NULL)
2437 {
2438 if (in_vim9script())
2439 emsg(_(e_cannot_add_to_null_blob));
2440 }
2441 else if (!value_check_lock(b->bv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002442 (char_u *)N_("add() argument"), TRUE))
2443 {
2444 int error = FALSE;
2445 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2446
2447 if (!error)
2448 {
2449 ga_append(&b->bv_ga, (int)n);
2450 copy_tv(&argvars[0], rettv);
2451 }
2452 }
2453 }
2454 else
2455 emsg(_(e_listblobreq));
2456}
2457
2458/*
2459 * "count()" function
2460 */
2461 void
2462f_count(typval_T *argvars, typval_T *rettv)
2463{
2464 long n = 0;
2465 int ic = FALSE;
2466 int error = FALSE;
2467
2468 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002469 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002470
2471 if (argvars[0].v_type == VAR_STRING)
2472 {
2473 char_u *expr = tv_get_string_chk(&argvars[1]);
2474 char_u *p = argvars[0].vval.v_string;
2475 char_u *next;
2476
2477 if (!error && expr != NULL && *expr != NUL && p != NULL)
2478 {
2479 if (ic)
2480 {
2481 size_t len = STRLEN(expr);
2482
2483 while (*p != NUL)
2484 {
2485 if (MB_STRNICMP(p, expr, len) == 0)
2486 {
2487 ++n;
2488 p += len;
2489 }
2490 else
2491 MB_PTR_ADV(p);
2492 }
2493 }
2494 else
2495 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2496 != NULL)
2497 {
2498 ++n;
2499 p = next + STRLEN(expr);
2500 }
2501 }
2502
2503 }
2504 else if (argvars[0].v_type == VAR_LIST)
2505 {
2506 listitem_T *li;
2507 list_T *l;
2508 long idx;
2509
2510 if ((l = argvars[0].vval.v_list) != NULL)
2511 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002512 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002513 li = l->lv_first;
2514 if (argvars[2].v_type != VAR_UNKNOWN)
2515 {
2516 if (argvars[3].v_type != VAR_UNKNOWN)
2517 {
2518 idx = (long)tv_get_number_chk(&argvars[3], &error);
2519 if (!error)
2520 {
2521 li = list_find(l, idx);
2522 if (li == NULL)
2523 semsg(_(e_listidx), idx);
2524 }
2525 }
2526 if (error)
2527 li = NULL;
2528 }
2529
2530 for ( ; li != NULL; li = li->li_next)
2531 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2532 ++n;
2533 }
2534 }
2535 else if (argvars[0].v_type == VAR_DICT)
2536 {
2537 int todo;
2538 dict_T *d;
2539 hashitem_T *hi;
2540
2541 if ((d = argvars[0].vval.v_dict) != NULL)
2542 {
2543 if (argvars[2].v_type != VAR_UNKNOWN)
2544 {
2545 if (argvars[3].v_type != VAR_UNKNOWN)
2546 emsg(_(e_invarg));
2547 }
2548
2549 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2550 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2551 {
2552 if (!HASHITEM_EMPTY(hi))
2553 {
2554 --todo;
2555 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2556 ++n;
2557 }
2558 }
2559 }
2560 }
2561 else
2562 semsg(_(e_listdictarg), "count()");
2563 rettv->vval.v_number = n;
2564}
2565
2566/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002567 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002568 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002569 static void
2570extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002571{
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002572 type_T *type = NULL;
2573 garray_T type_list;
2574
2575 if (!is_new && in_vim9script())
2576 {
2577 // Check that map() does not change the type of the dict.
2578 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaarf2253962021-04-13 20:53:13 +02002579 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002580 }
2581
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002582 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2583 {
2584 list_T *l1, *l2;
2585 listitem_T *item;
2586 long before;
2587 int error = FALSE;
2588
2589 l1 = argvars[0].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002590 if (l1 == NULL)
2591 {
2592 emsg(_(e_cannot_extend_null_list));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002593 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002594 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002595 l2 = argvars[1].vval.v_list;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002596 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
2597 && l2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002598 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002599 if (is_new)
2600 {
2601 l1 = list_copy(l1, FALSE, get_copyID());
2602 if (l1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002603 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002604 }
2605
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002606 if (argvars[2].v_type != VAR_UNKNOWN)
2607 {
2608 before = (long)tv_get_number_chk(&argvars[2], &error);
2609 if (error)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002610 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002611
2612 if (before == l1->lv_len)
2613 item = NULL;
2614 else
2615 {
2616 item = list_find(l1, before);
2617 if (item == NULL)
2618 {
2619 semsg(_(e_listidx), before);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002620 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002621 }
2622 }
2623 }
2624 else
2625 item = NULL;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002626 if (type != NULL && check_typval_arg_type(
2627 type, &argvars[1], 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002628 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002629 list_extend(l1, l2, item);
2630
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002631 if (is_new)
2632 {
2633 rettv->v_type = VAR_LIST;
2634 rettv->vval.v_list = l1;
2635 rettv->v_lock = FALSE;
2636 }
2637 else
2638 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002639 }
2640 }
2641 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2642 {
2643 dict_T *d1, *d2;
2644 char_u *action;
2645 int i;
2646
2647 d1 = argvars[0].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002648 if (d1 == NULL)
2649 {
2650 emsg(_(e_cannot_extend_null_dict));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002651 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002652 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002653 d2 = argvars[1].vval.v_dict;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002654 if ((is_new || !value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
2655 && d2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002656 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002657 if (is_new)
2658 {
2659 d1 = dict_copy(d1, FALSE, get_copyID());
2660 if (d1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002661 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002662 }
2663
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002664 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002665 if (argvars[2].v_type != VAR_UNKNOWN)
2666 {
2667 static char *(av[]) = {"keep", "force", "error"};
2668
2669 action = tv_get_string_chk(&argvars[2]);
2670 if (action == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002671 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002672 for (i = 0; i < 3; ++i)
2673 if (STRCMP(action, av[i]) == 0)
2674 break;
2675 if (i == 3)
2676 {
2677 semsg(_(e_invarg2), action);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002678 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002679 }
2680 }
2681 else
2682 action = (char_u *)"force";
2683
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002684 if (type != NULL && check_typval_arg_type(
2685 type, &argvars[1], 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002686 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002687 dict_extend(d1, d2, action);
2688
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002689 if (is_new)
2690 {
2691 rettv->v_type = VAR_DICT;
2692 rettv->vval.v_dict = d1;
2693 rettv->v_lock = FALSE;
2694 }
2695 else
2696 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002697 }
2698 }
2699 else
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002700 semsg(_(e_listdictarg), is_new ? "extendnew()" : "extend()");
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002701
2702theend:
2703 if (type != NULL)
2704 clear_type_list(&type_list);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002705}
2706
2707/*
2708 * "extend(list, list [, idx])" function
2709 * "extend(dict, dict [, action])" function
2710 */
2711 void
2712f_extend(typval_T *argvars, typval_T *rettv)
2713{
2714 char_u *errmsg = (char_u *)N_("extend() argument");
2715
2716 extend(argvars, rettv, errmsg, FALSE);
2717}
2718
2719/*
2720 * "extendnew(list, list [, idx])" function
2721 * "extendnew(dict, dict [, action])" function
2722 */
2723 void
2724f_extendnew(typval_T *argvars, typval_T *rettv)
2725{
2726 char_u *errmsg = (char_u *)N_("extendnew() argument");
2727
2728 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002729}
2730
2731/*
2732 * "insert()" function
2733 */
2734 void
2735f_insert(typval_T *argvars, typval_T *rettv)
2736{
2737 long before = 0;
2738 listitem_T *item;
2739 list_T *l;
2740 int error = FALSE;
2741
2742 if (argvars[0].v_type == VAR_BLOB)
2743 {
2744 int val, len;
2745 char_u *p;
2746
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002747 if (argvars[0].vval.v_blob == NULL)
2748 return;
2749
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002750 len = blob_len(argvars[0].vval.v_blob);
2751 if (argvars[2].v_type != VAR_UNKNOWN)
2752 {
2753 before = (long)tv_get_number_chk(&argvars[2], &error);
2754 if (error)
2755 return; // type error; errmsg already given
2756 if (before < 0 || before > len)
2757 {
2758 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2759 return;
2760 }
2761 }
2762 val = tv_get_number_chk(&argvars[1], &error);
2763 if (error)
2764 return;
2765 if (val < 0 || val > 255)
2766 {
2767 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2768 return;
2769 }
2770
2771 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
2772 return;
2773 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2774 mch_memmove(p + before + 1, p + before, (size_t)len - before);
2775 *(p + before) = val;
2776 ++argvars[0].vval.v_blob->bv_ga.ga_len;
2777
2778 copy_tv(&argvars[0], rettv);
2779 }
2780 else if (argvars[0].v_type != VAR_LIST)
2781 semsg(_(e_listblobarg), "insert()");
2782 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002783 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002784 (char_u *)N_("insert() argument"), TRUE))
2785 {
2786 if (argvars[2].v_type != VAR_UNKNOWN)
2787 before = (long)tv_get_number_chk(&argvars[2], &error);
2788 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002789 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002790
2791 if (before == l->lv_len)
2792 item = NULL;
2793 else
2794 {
2795 item = list_find(l, before);
2796 if (item == NULL)
2797 {
2798 semsg(_(e_listidx), before);
2799 l = NULL;
2800 }
2801 }
2802 if (l != NULL)
2803 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02002804 (void)list_insert_tv(l, &argvars[1], item);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002805 copy_tv(&argvars[0], rettv);
2806 }
2807 }
2808}
2809
2810/*
2811 * "remove()" function
2812 */
2813 void
2814f_remove(typval_T *argvars, typval_T *rettv)
2815{
2816 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2817
2818 if (argvars[0].v_type == VAR_DICT)
2819 dict_remove(argvars, rettv, arg_errmsg);
2820 else if (argvars[0].v_type == VAR_BLOB)
2821 blob_remove(argvars, rettv);
2822 else if (argvars[0].v_type == VAR_LIST)
2823 list_remove(argvars, rettv, arg_errmsg);
2824 else
2825 semsg(_(e_listdictblobarg), "remove()");
2826}
2827
2828/*
2829 * "reverse({list})" function
2830 */
2831 void
2832f_reverse(typval_T *argvars, typval_T *rettv)
2833{
2834 list_T *l;
2835 listitem_T *li, *ni;
2836
2837 if (argvars[0].v_type == VAR_BLOB)
2838 {
2839 blob_T *b = argvars[0].vval.v_blob;
2840 int i, len = blob_len(b);
2841
2842 for (i = 0; i < len / 2; i++)
2843 {
2844 int tmp = blob_get(b, i);
2845
2846 blob_set(b, i, blob_get(b, len - i - 1));
2847 blob_set(b, len - i - 1, tmp);
2848 }
2849 rettv_blob_set(rettv, b);
2850 return;
2851 }
2852
2853 if (argvars[0].v_type != VAR_LIST)
2854 semsg(_(e_listblobarg), "reverse()");
2855 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002856 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002857 (char_u *)N_("reverse() argument"), TRUE))
2858 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002859 if (l->lv_first == &range_list_item)
2860 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002861 varnumber_T new_start = l->lv_u.nonmat.lv_start
2862 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2863 l->lv_u.nonmat.lv_end = new_start
2864 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2865 l->lv_u.nonmat.lv_start = new_start;
2866 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002867 rettv_list_set(rettv, l);
2868 return;
2869 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002870 li = l->lv_u.mat.lv_last;
2871 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002872 l->lv_len = 0;
2873 while (li != NULL)
2874 {
2875 ni = li->li_prev;
2876 list_append(l, li);
2877 li = ni;
2878 }
2879 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002880 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002881 }
2882}
2883
Bram Moolenaar85629982020-06-01 18:39:20 +02002884/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002885 * "reduce(list, { accumulator, element -> value } [, initial])" function
Bram Moolenaar85629982020-06-01 18:39:20 +02002886 */
2887 void
2888f_reduce(typval_T *argvars, typval_T *rettv)
2889{
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002890 typval_T initial;
Bram Moolenaar85629982020-06-01 18:39:20 +02002891 char_u *func_name;
2892 partial_T *partial = NULL;
2893 funcexe_T funcexe;
2894 typval_T argv[3];
2895
2896 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
2897 {
2898 emsg(_(e_listblobreq));
2899 return;
2900 }
2901
2902 if (argvars[1].v_type == VAR_FUNC)
2903 func_name = argvars[1].vval.v_string;
2904 else if (argvars[1].v_type == VAR_PARTIAL)
2905 {
2906 partial = argvars[1].vval.v_partial;
2907 func_name = partial_name(partial);
2908 }
2909 else
2910 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01002911 if (func_name == NULL || *func_name == NUL)
2912 {
2913 emsg(_(e_missing_function_argument));
2914 return;
2915 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002916
2917 vim_memset(&funcexe, 0, sizeof(funcexe));
2918 funcexe.evaluate = TRUE;
2919 funcexe.partial = partial;
2920
2921 if (argvars[0].v_type == VAR_LIST)
2922 {
2923 list_T *l = argvars[0].vval.v_list;
2924 listitem_T *li = NULL;
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002925 int r;
Bram Moolenaarca275a02020-06-24 22:07:46 +02002926 int called_emsg_start = called_emsg;
Bram Moolenaar85629982020-06-01 18:39:20 +02002927
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002928 if (l != NULL)
2929 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar85629982020-06-01 18:39:20 +02002930 if (argvars[2].v_type == VAR_UNKNOWN)
2931 {
2932 if (l == NULL || l->lv_first == NULL)
2933 {
2934 semsg(_(e_reduceempty), "List");
2935 return;
2936 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002937 initial = l->lv_first->li_tv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002938 li = l->lv_first->li_next;
2939 }
2940 else
2941 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002942 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002943 if (l != NULL)
2944 li = l->lv_first;
2945 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002946 copy_tv(&initial, rettv);
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002947
2948 if (l != NULL)
Bram Moolenaar85629982020-06-01 18:39:20 +02002949 {
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002950 int prev_locked = l->lv_lock;
2951
2952 l->lv_lock = VAR_FIXED; // disallow the list changing here
2953 for ( ; li != NULL; li = li->li_next)
2954 {
2955 argv[0] = *rettv;
2956 argv[1] = li->li_tv;
2957 rettv->v_type = VAR_UNKNOWN;
2958 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
2959 clear_tv(&argv[0]);
2960 if (r == FAIL || called_emsg != called_emsg_start)
2961 break;
2962 }
2963 l->lv_lock = prev_locked;
Bram Moolenaar85629982020-06-01 18:39:20 +02002964 }
2965 }
2966 else
2967 {
2968 blob_T *b = argvars[0].vval.v_blob;
2969 int i;
2970
2971 if (argvars[2].v_type == VAR_UNKNOWN)
2972 {
2973 if (b == NULL || b->bv_ga.ga_len == 0)
2974 {
2975 semsg(_(e_reduceempty), "Blob");
2976 return;
2977 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002978 initial.v_type = VAR_NUMBER;
2979 initial.vval.v_number = blob_get(b, 0);
Bram Moolenaar85629982020-06-01 18:39:20 +02002980 i = 1;
2981 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002982 else if (argvars[2].v_type != VAR_NUMBER)
2983 {
2984 emsg(_(e_number_exp));
2985 return;
2986 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002987 else
2988 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002989 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002990 i = 0;
2991 }
2992
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002993 copy_tv(&initial, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02002994 if (b != NULL)
2995 {
2996 for ( ; i < b->bv_ga.ga_len; i++)
2997 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002998 argv[0] = *rettv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002999 argv[1].v_type = VAR_NUMBER;
3000 argv[1].vval.v_number = blob_get(b, i);
3001 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
3002 return;
Bram Moolenaar85629982020-06-01 18:39:20 +02003003 }
3004 }
3005 }
3006}
3007
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02003008#endif // defined(FEAT_EVAL)