blob: 9aec823eef2d3617ff243061d8fca385b41eb582 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarda861d62016-07-17 15:46:27 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +020011 * list.c: List support and container (List, Dict, Blob) functions.
Bram Moolenaarda861d62016-07-17 15:46:27 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar08c308a2019-09-04 17:48:15 +020018static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
19
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010020// List heads for garbage collection.
21static list_T *first_list = NULL; // list of all lists
Bram Moolenaarda861d62016-07-17 15:46:27 +020022
Bram Moolenaar00d253e2020-04-06 22:13:01 +020023#define FOR_ALL_WATCHERS(l, lw) \
24 for ((lw) = (l)->lv_watch; (lw) != NULL; (lw) = (lw)->lw_next)
25
Bram Moolenaarbdff0122020-04-05 18:56:05 +020026static void list_free_item(list_T *l, listitem_T *item);
27
Bram Moolenaarda861d62016-07-17 15:46:27 +020028/*
29 * Add a watcher to a list.
30 */
31 void
32list_add_watch(list_T *l, listwatch_T *lw)
33{
34 lw->lw_next = l->lv_watch;
35 l->lv_watch = lw;
36}
37
38/*
39 * Remove a watcher from a list.
40 * No warning when it isn't found...
41 */
42 void
43list_rem_watch(list_T *l, listwatch_T *lwrem)
44{
45 listwatch_T *lw, **lwp;
46
47 lwp = &l->lv_watch;
Bram Moolenaar00d253e2020-04-06 22:13:01 +020048 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020049 {
50 if (lw == lwrem)
51 {
52 *lwp = lw->lw_next;
53 break;
54 }
55 lwp = &lw->lw_next;
56 }
57}
58
59/*
60 * Just before removing an item from a list: advance watchers to the next
61 * item.
62 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020063 static void
Bram Moolenaarda861d62016-07-17 15:46:27 +020064list_fix_watch(list_T *l, listitem_T *item)
65{
66 listwatch_T *lw;
67
Bram Moolenaar00d253e2020-04-06 22:13:01 +020068 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020069 if (lw->lw_item == item)
70 lw->lw_item = item->li_next;
71}
72
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073 static void
74list_init(list_T *l)
75{
76 // Prepend the list to the list of lists for garbage collection.
77 if (first_list != NULL)
78 first_list->lv_used_prev = l;
79 l->lv_used_prev = NULL;
80 l->lv_used_next = first_list;
81 first_list = l;
82}
83
Bram Moolenaarda861d62016-07-17 15:46:27 +020084/*
85 * Allocate an empty header for a list.
86 * Caller should take care of the reference count.
87 */
88 list_T *
89list_alloc(void)
90{
91 list_T *l;
92
Bram Moolenaarc799fe22019-05-28 23:08:19 +020093 l = ALLOC_CLEAR_ONE(list_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +020094 if (l != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095 list_init(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +020096 return l;
97}
98
99/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100100 * list_alloc() with an ID for alloc_fail().
101 */
102 list_T *
103list_alloc_id(alloc_id_T id UNUSED)
104{
105#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200106 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100107 return NULL;
108#endif
109 return (list_alloc());
110}
111
112/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 * Allocate space for a list, plus "count" items.
114 * Next list_set_item() must be called for each item.
115 */
116 list_T *
117list_alloc_with_items(int count)
118{
119 list_T *l;
120
121 l = (list_T *)alloc_clear(sizeof(list_T) + count * sizeof(listitem_T));
122 if (l != NULL)
123 {
124 list_init(l);
125
126 if (count > 0)
127 {
128 listitem_T *li = (listitem_T *)(l + 1);
129 int i;
130
131 l->lv_len = count;
132 l->lv_with_items = count;
133 l->lv_first = li;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100134 l->lv_u.mat.lv_last = li + count - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 for (i = 0; i < count; ++i)
136 {
137 if (i == 0)
138 li->li_prev = NULL;
139 else
140 li->li_prev = li - 1;
141 if (i == count - 1)
142 li->li_next = NULL;
143 else
144 li->li_next = li + 1;
145 ++li;
146 }
147 }
148 }
149 return l;
150}
151
152/*
153 * Set item "idx" for a list previously allocated with list_alloc_with_items().
154 * The contents of "tv" is moved into the list item.
155 * Each item must be set exactly once.
156 */
157 void
158list_set_item(list_T *l, int idx, typval_T *tv)
159{
160 listitem_T *li = (listitem_T *)(l + 1) + idx;
161
162 li->li_tv = *tv;
163}
164
165/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200166 * Allocate an empty list for a return value, with reference count set.
167 * Returns OK or FAIL.
168 */
169 int
170rettv_list_alloc(typval_T *rettv)
171{
172 list_T *l = list_alloc();
173
174 if (l == NULL)
175 return FAIL;
176
Bram Moolenaarda861d62016-07-17 15:46:27 +0200177 rettv->v_lock = 0;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200178 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200179 return OK;
180}
181
182/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100183 * Same as rettv_list_alloc() but uses an allocation id for testing.
184 */
185 int
186rettv_list_alloc_id(typval_T *rettv, alloc_id_T id UNUSED)
187{
188#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200189 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaar162b7142018-12-21 15:17:36 +0100190 return FAIL;
191#endif
192 return rettv_list_alloc(rettv);
193}
194
195
196/*
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200197 * Set a list as the return value. Increments the reference count.
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200198 */
199 void
200rettv_list_set(typval_T *rettv, list_T *l)
201{
202 rettv->v_type = VAR_LIST;
203 rettv->vval.v_list = l;
204 if (l != NULL)
205 ++l->lv_refcount;
206}
207
208/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200209 * Unreference a list: decrement the reference count and free it when it
210 * becomes zero.
211 */
212 void
213list_unref(list_T *l)
214{
215 if (l != NULL && --l->lv_refcount <= 0)
216 list_free(l);
217}
218
219/*
220 * Free a list, including all non-container items it points to.
221 * Ignores the reference count.
222 */
223 static void
224list_free_contents(list_T *l)
225{
226 listitem_T *item;
227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 if (l->lv_first != &range_list_item)
229 for (item = l->lv_first; item != NULL; item = l->lv_first)
230 {
231 // Remove the item before deleting it.
232 l->lv_first = item->li_next;
233 clear_tv(&item->li_tv);
234 list_free_item(l, item);
235 }
Bram Moolenaarda861d62016-07-17 15:46:27 +0200236}
237
238/*
239 * Go through the list of lists and free items without the copyID.
240 * But don't free a list that has a watcher (used in a for loop), these
241 * are not referenced anywhere.
242 */
243 int
244list_free_nonref(int copyID)
245{
246 list_T *ll;
247 int did_free = FALSE;
248
249 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
250 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
251 && ll->lv_watch == NULL)
252 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100253 // Free the List and ordinary items it contains, but don't recurse
254 // into Lists and Dictionaries, they will be in the list of dicts
255 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200256 list_free_contents(ll);
257 did_free = TRUE;
258 }
259 return did_free;
260}
261
262 static void
263list_free_list(list_T *l)
264{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100265 // Remove the list from the list of lists for garbage collection.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200266 if (l->lv_used_prev == NULL)
267 first_list = l->lv_used_next;
268 else
269 l->lv_used_prev->lv_used_next = l->lv_used_next;
270 if (l->lv_used_next != NULL)
271 l->lv_used_next->lv_used_prev = l->lv_used_prev;
272
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100273 free_type(l->lv_type);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200274 vim_free(l);
275}
276
277 void
278list_free_items(int copyID)
279{
280 list_T *ll, *ll_next;
281
282 for (ll = first_list; ll != NULL; ll = ll_next)
283 {
284 ll_next = ll->lv_used_next;
285 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
286 && ll->lv_watch == NULL)
287 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100288 // Free the List and ordinary items it contains, but don't recurse
289 // into Lists and Dictionaries, they will be in the list of dicts
290 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200291 list_free_list(ll);
292 }
293 }
294}
295
296 void
297list_free(list_T *l)
298{
299 if (!in_free_unref_items)
300 {
301 list_free_contents(l);
302 list_free_list(l);
303 }
304}
305
306/*
307 * Allocate a list item.
308 * It is not initialized, don't forget to set v_lock.
309 */
310 listitem_T *
311listitem_alloc(void)
312{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200313 return ALLOC_ONE(listitem_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200314}
315
316/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100317 * Free a list item, unless it was allocated together with the list itself.
318 * Does not clear the value. Does not notify watchers.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200319 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200320 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100321list_free_item(list_T *l, listitem_T *item)
322{
323 if (l->lv_with_items == 0 || item < (listitem_T *)l
324 || item >= (listitem_T *)(l + 1) + l->lv_with_items)
325 vim_free(item);
326}
327
328/*
329 * Free a list item, unless it was allocated together with the list itself.
330 * Also clears the value. Does not notify watchers.
331 */
332 void
333listitem_free(list_T *l, listitem_T *item)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200334{
335 clear_tv(&item->li_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 list_free_item(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200337}
338
339/*
340 * Remove a list item from a List and free it. Also clears the value.
341 */
342 void
343listitem_remove(list_T *l, listitem_T *item)
344{
345 vimlist_remove(l, item, item);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 listitem_free(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200347}
348
349/*
350 * Get the number of items in a list.
351 */
352 long
353list_len(list_T *l)
354{
355 if (l == NULL)
356 return 0L;
357 return l->lv_len;
358}
359
360/*
361 * Return TRUE when two lists have exactly the same values.
362 */
363 int
364list_equal(
365 list_T *l1,
366 list_T *l2,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100367 int ic, // ignore case for strings
368 int recursive) // TRUE when used recursively
Bram Moolenaarda861d62016-07-17 15:46:27 +0200369{
370 listitem_T *item1, *item2;
371
Bram Moolenaarda861d62016-07-17 15:46:27 +0200372 if (l1 == l2)
373 return TRUE;
374 if (list_len(l1) != list_len(l2))
375 return FALSE;
Bram Moolenaar7b293c72020-04-09 21:33:22 +0200376 if (list_len(l1) == 0)
377 // empty and NULL list are considered equal
378 return TRUE;
379 if (l1 == NULL || l2 == NULL)
380 return FALSE;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200381
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200382 CHECK_LIST_MATERIALIZE(l1);
383 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384
Bram Moolenaarda861d62016-07-17 15:46:27 +0200385 for (item1 = l1->lv_first, item2 = l2->lv_first;
386 item1 != NULL && item2 != NULL;
387 item1 = item1->li_next, item2 = item2->li_next)
388 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
389 return FALSE;
390 return item1 == NULL && item2 == NULL;
391}
392
393/*
394 * Locate item with index "n" in list "l" and return it.
395 * A negative index is counted from the end; -1 is the last item.
396 * Returns NULL when "n" is out of range.
397 */
398 listitem_T *
399list_find(list_T *l, long n)
400{
401 listitem_T *item;
402 long idx;
403
404 if (l == NULL)
405 return NULL;
406
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100407 // Negative index is relative to the end.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200408 if (n < 0)
409 n = l->lv_len + n;
410
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100411 // Check for index out of range.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200412 if (n < 0 || n >= l->lv_len)
413 return NULL;
414
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200415 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100417 // When there is a cached index may start search from there.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100418 if (l->lv_u.mat.lv_idx_item != NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200419 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100420 if (n < l->lv_u.mat.lv_idx / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200421 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100422 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200423 item = l->lv_first;
424 idx = 0;
425 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100426 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200427 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100428 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100429 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200430 idx = l->lv_len - 1;
431 }
432 else
433 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100434 // closest to the cached index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100435 item = l->lv_u.mat.lv_idx_item;
436 idx = l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200437 }
438 }
439 else
440 {
441 if (n < l->lv_len / 2)
442 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100443 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200444 item = l->lv_first;
445 idx = 0;
446 }
447 else
448 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100449 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100450 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200451 idx = l->lv_len - 1;
452 }
453 }
454
455 while (n > idx)
456 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100457 // search forward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200458 item = item->li_next;
459 ++idx;
460 }
461 while (n < idx)
462 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100463 // search backward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200464 item = item->li_prev;
465 --idx;
466 }
467
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100468 // cache the used index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100469 l->lv_u.mat.lv_idx = idx;
470 l->lv_u.mat.lv_idx_item = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200471
472 return item;
473}
474
475/*
476 * Get list item "l[idx]" as a number.
477 */
478 long
479list_find_nr(
480 list_T *l,
481 long idx,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100482 int *errorp) // set to TRUE when something wrong
Bram Moolenaarda861d62016-07-17 15:46:27 +0200483{
484 listitem_T *li;
485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486 if (l != NULL && l->lv_first == &range_list_item)
487 {
488 long n = idx;
489
490 // not materialized range() list: compute the value.
491 // Negative index is relative to the end.
492 if (n < 0)
493 n = l->lv_len + n;
494
495 // Check for index out of range.
496 if (n < 0 || n >= l->lv_len)
497 {
498 if (errorp != NULL)
499 *errorp = TRUE;
500 return -1L;
501 }
502
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100503 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 }
505
Bram Moolenaarda861d62016-07-17 15:46:27 +0200506 li = list_find(l, idx);
507 if (li == NULL)
508 {
509 if (errorp != NULL)
510 *errorp = TRUE;
511 return -1L;
512 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100513 return (long)tv_get_number_chk(&li->li_tv, errorp);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200514}
515
516/*
517 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
518 */
519 char_u *
520list_find_str(list_T *l, long idx)
521{
522 listitem_T *li;
523
524 li = list_find(l, idx - 1);
525 if (li == NULL)
526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 semsg(_(e_listidx), idx);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200528 return NULL;
529 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100530 return tv_get_string(&li->li_tv);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200531}
532
533/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100534 * Like list_find() but when a negative index is used that is not found use
535 * zero and set "idx" to zero. Used for first index of a range.
536 */
537 listitem_T *
538list_find_index(list_T *l, long *idx)
539{
540 listitem_T *li = list_find(l, *idx);
541
542 if (li == NULL)
543 {
544 if (*idx < 0)
545 {
546 *idx = 0;
547 li = list_find(l, *idx);
548 }
549 }
550 return li;
551}
552
553/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200554 * Locate "item" list "l" and return its index.
555 * Returns -1 when "item" is not in the list.
556 */
557 long
558list_idx_of_item(list_T *l, listitem_T *item)
559{
560 long idx = 0;
561 listitem_T *li;
562
563 if (l == NULL)
564 return -1;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200565 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200566 idx = 0;
567 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
568 ++idx;
569 if (li == NULL)
570 return -1;
571 return idx;
572}
573
574/*
575 * Append item "item" to the end of list "l".
576 */
577 void
578list_append(list_T *l, listitem_T *item)
579{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200580 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100581 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200582 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100583 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200584 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100585 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200586 item->li_prev = NULL;
587 }
588 else
589 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100590 l->lv_u.mat.lv_last->li_next = item;
591 item->li_prev = l->lv_u.mat.lv_last;
592 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200593 }
594 ++l->lv_len;
595 item->li_next = NULL;
596}
597
598/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200600 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200601 */
602 int
603list_append_tv(list_T *l, typval_T *tv)
604{
605 listitem_T *li = listitem_alloc();
606
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200607 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
608 && check_typval_arg_type(l->lv_type->tt_member, tv, 0) == FAIL)
609 return FAIL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200610 if (li == NULL)
611 return FAIL;
612 copy_tv(tv, &li->li_tv);
613 list_append(l, li);
614 return OK;
615}
616
617/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 * As list_append_tv() but move the value instead of copying it.
619 * Return FAIL when out of memory.
620 */
621 int
622list_append_tv_move(list_T *l, typval_T *tv)
623{
624 listitem_T *li = listitem_alloc();
625
626 if (li == NULL)
627 return FAIL;
628 li->li_tv = *tv;
629 list_append(l, li);
630 return OK;
631}
632
633/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200634 * Add a dictionary to a list. Used by getqflist().
635 * Return FAIL when out of memory.
636 */
637 int
638list_append_dict(list_T *list, dict_T *dict)
639{
640 listitem_T *li = listitem_alloc();
641
642 if (li == NULL)
643 return FAIL;
644 li->li_tv.v_type = VAR_DICT;
645 li->li_tv.v_lock = 0;
646 li->li_tv.vval.v_dict = dict;
647 list_append(list, li);
648 ++dict->dv_refcount;
649 return OK;
650}
651
652/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100653 * Append list2 to list1.
654 * Return FAIL when out of memory.
655 */
656 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200657list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100658{
659 listitem_T *li = listitem_alloc();
660
661 if (li == NULL)
662 return FAIL;
663 li->li_tv.v_type = VAR_LIST;
664 li->li_tv.v_lock = 0;
665 li->li_tv.vval.v_list = list2;
666 list_append(list1, li);
667 ++list2->lv_refcount;
668 return OK;
669}
670
671/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200672 * Make a copy of "str" and append it as an item to list "l".
673 * When "len" >= 0 use "str[len]".
674 * Returns FAIL when out of memory.
675 */
676 int
677list_append_string(list_T *l, char_u *str, int len)
678{
679 listitem_T *li = listitem_alloc();
680
681 if (li == NULL)
682 return FAIL;
683 list_append(l, li);
684 li->li_tv.v_type = VAR_STRING;
685 li->li_tv.v_lock = 0;
686 if (str == NULL)
687 li->li_tv.vval.v_string = NULL;
688 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
689 : vim_strsave(str))) == NULL)
690 return FAIL;
691 return OK;
692}
693
694/*
695 * Append "n" to list "l".
696 * Returns FAIL when out of memory.
697 */
698 int
699list_append_number(list_T *l, varnumber_T n)
700{
701 listitem_T *li;
702
703 li = listitem_alloc();
704 if (li == NULL)
705 return FAIL;
706 li->li_tv.v_type = VAR_NUMBER;
707 li->li_tv.v_lock = 0;
708 li->li_tv.vval.v_number = n;
709 list_append(l, li);
710 return OK;
711}
712
713/*
714 * Insert typval_T "tv" in list "l" before "item".
715 * If "item" is NULL append at the end.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100716 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200717 */
718 int
719list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
720{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100721 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200722
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100723 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100724 && check_typval_arg_type(l->lv_type->tt_member, tv, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100725 return FAIL;
726 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200727 if (ni == NULL)
728 return FAIL;
729 copy_tv(tv, &ni->li_tv);
730 list_insert(l, ni, item);
731 return OK;
732}
733
734 void
735list_insert(list_T *l, listitem_T *ni, listitem_T *item)
736{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200737 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200738 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100739 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200740 list_append(l, ni);
741 else
742 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100743 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200744 ni->li_prev = item->li_prev;
745 ni->li_next = item;
746 if (item->li_prev == NULL)
747 {
748 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100749 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200750 }
751 else
752 {
753 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100754 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200755 }
756 item->li_prev = ni;
757 ++l->lv_len;
758 }
759}
760
761/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200762 * Flatten "list" to depth "maxdepth".
763 * It does nothing if "maxdepth" is 0.
764 * Returns FAIL when out of memory.
765 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100766 static void
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200767list_flatten(list_T *list, long maxdepth)
768{
769 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200770 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200771 int n;
772
773 if (maxdepth == 0)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100774 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200775 CHECK_LIST_MATERIALIZE(list);
776
777 n = 0;
778 item = list->lv_first;
779 while (item != NULL)
780 {
781 fast_breakcheck();
782 if (got_int)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100783 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200784
785 if (item->li_tv.v_type == VAR_LIST)
786 {
787 listitem_T *next = item->li_next;
788
789 vimlist_remove(list, item, item);
790 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100791 return;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200792 clear_tv(&item->li_tv);
793 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200794
795 if (item->li_prev == NULL)
796 item = list->lv_first;
797 else
798 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200799 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200800
801 if (++n >= maxdepth)
802 {
803 n = 0;
804 item = next;
805 }
806 }
807 else
808 {
809 n = 0;
810 item = item->li_next;
811 }
812 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200813}
814
815/*
Bram Moolenaar3b690062021-02-01 20:14:51 +0100816 * "flatten()" and "flattennew()" functions
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200817 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100818 static void
819flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200820{
821 list_T *l;
822 long maxdepth;
823 int error = FALSE;
824
825 if (argvars[0].v_type != VAR_LIST)
826 {
827 semsg(_(e_listarg), "flatten()");
828 return;
829 }
830
831 if (argvars[1].v_type == VAR_UNKNOWN)
832 maxdepth = 999999;
833 else
834 {
835 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
836 if (error)
837 return;
838 if (maxdepth < 0)
839 {
840 emsg(_("E900: maxdepth must be non-negative number"));
841 return;
842 }
843 }
844
845 l = argvars[0].vval.v_list;
Bram Moolenaar3b690062021-02-01 20:14:51 +0100846 rettv->v_type = VAR_LIST;
847 rettv->vval.v_list = l;
848 if (l == NULL)
849 return;
850
851 if (make_copy)
852 {
853 l = list_copy(l, TRUE, get_copyID());
854 rettv->vval.v_list = l;
855 if (l == NULL)
856 return;
857 }
858 else
859 {
860 if (value_check_lock(l->lv_lock,
861 (char_u *)N_("flatten() argument"), TRUE))
862 return;
863 ++l->lv_refcount;
864 }
865
866 list_flatten(l, maxdepth);
867}
868
869/*
870 * "flatten(list[, {maxdepth}])" function
871 */
872 void
873f_flatten(typval_T *argvars, typval_T *rettv)
874{
875 if (in_vim9script())
876 emsg(_(e_cannot_use_flatten_in_vim9_script));
877 else
878 flatten_common(argvars, rettv, FALSE);
879}
880
881/*
882 * "flattennew(list[, {maxdepth}])" function
883 */
884 void
885f_flattennew(typval_T *argvars, typval_T *rettv)
886{
887 flatten_common(argvars, rettv, TRUE);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200888}
889
890/*
Bram Moolenaar1a739232020-10-10 15:37:58 +0200891 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200892 * If "bef" is NULL append at the end, otherwise insert before this item.
893 * Returns FAIL when out of memory.
894 */
895 int
896list_extend(list_T *l1, list_T *l2, listitem_T *bef)
897{
898 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200899 int todo;
Bram Moolenaardcae51f2021-04-08 20:10:10 +0200900 listitem_T *bef_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200901
Bram Moolenaar1a739232020-10-10 15:37:58 +0200902 // NULL list is equivalent to an empty list: nothing to do.
903 if (l2 == NULL || l2->lv_len == 0)
904 return OK;
905
906 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200907 CHECK_LIST_MATERIALIZE(l1);
908 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909
Bram Moolenaardcae51f2021-04-08 20:10:10 +0200910 // When exending a list with itself, at some point we run into the item
911 // that was before "bef" and need to skip over the already inserted items
912 // to "bef".
913 bef_prev = bef == NULL ? NULL : bef->li_prev;
914
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100915 // We also quit the loop when we have inserted the original item count of
916 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaardcae51f2021-04-08 20:10:10 +0200917 for (item = l2->lv_first; item != NULL && --todo >= 0;
918 item = item == bef_prev ? bef : item->li_next)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200919 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
920 return FAIL;
921 return OK;
922}
923
924/*
925 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
926 * Return FAIL when out of memory.
927 */
928 int
929list_concat(list_T *l1, list_T *l2, typval_T *tv)
930{
931 list_T *l;
932
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100933 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +0200934 if (l1 == NULL)
935 l = list_alloc();
936 else
937 l = list_copy(l1, FALSE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200938 if (l == NULL)
939 return FAIL;
940 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +0100941 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200942 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200943 if (l1 == NULL)
944 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200945
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100946 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200947 return list_extend(l, l2, NULL);
948}
949
Bram Moolenaar9af78762020-06-16 11:34:42 +0200950 list_T *
951list_slice(list_T *ol, long n1, long n2)
952{
953 listitem_T *item;
954 list_T *l = list_alloc();
955
956 if (l == NULL)
957 return NULL;
958 for (item = list_find(ol, n1); n1 <= n2; ++n1)
959 {
960 if (list_append_tv(l, &item->li_tv) == FAIL)
961 {
962 list_free(l);
963 return NULL;
964 }
965 item = item->li_next;
966 }
967 return l;
968}
969
Bram Moolenaared591872020-08-15 22:14:53 +0200970 int
971list_slice_or_index(
972 list_T *list,
973 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +0100974 varnumber_T n1_arg,
975 varnumber_T n2_arg,
976 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +0200977 typval_T *rettv,
978 int verbose)
979{
980 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +0100981 varnumber_T n1 = n1_arg;
982 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +0200983 typval_T var1;
984
985 if (n1 < 0)
986 n1 = len + n1;
987 if (n1 < 0 || n1 >= len)
988 {
989 // For a range we allow invalid values and return an empty
990 // list. A list index out of range is an error.
991 if (!range)
992 {
993 if (verbose)
Bram Moolenaar239f8d92021-01-17 13:21:20 +0100994 semsg(_(e_listidx), (long)n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +0200995 return FAIL;
996 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +0200997 n1 = n1 < 0 ? 0 : len;
Bram Moolenaared591872020-08-15 22:14:53 +0200998 }
999 if (range)
1000 {
1001 list_T *l;
1002
1003 if (n2 < 0)
1004 n2 = len + n2;
1005 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01001006 n2 = len - (exclusive ? 0 : 1);
1007 if (exclusive)
1008 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +02001009 if (n2 < 0 || n2 + 1 < n1)
1010 n2 = -1;
1011 l = list_slice(list, n1, n2);
1012 if (l == NULL)
1013 return FAIL;
1014 clear_tv(rettv);
1015 rettv_list_set(rettv, l);
1016 }
1017 else
1018 {
1019 // copy the item to "var1" to avoid that freeing the list makes it
1020 // invalid.
1021 copy_tv(&list_find(list, n1)->li_tv, &var1);
1022 clear_tv(rettv);
1023 *rettv = var1;
1024 }
1025 return OK;
1026}
1027
Bram Moolenaarda861d62016-07-17 15:46:27 +02001028/*
1029 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1030 * The refcount of the new list is set to 1.
1031 * See item_copy() for "copyID".
1032 * Returns NULL when out of memory.
1033 */
1034 list_T *
1035list_copy(list_T *orig, int deep, int copyID)
1036{
1037 list_T *copy;
1038 listitem_T *item;
1039 listitem_T *ni;
1040
1041 if (orig == NULL)
1042 return NULL;
1043
1044 copy = list_alloc();
1045 if (copy != NULL)
1046 {
1047 if (copyID != 0)
1048 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001049 // Do this before adding the items, because one of the items may
1050 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001051 orig->lv_copyID = copyID;
1052 orig->lv_copylist = copy;
1053 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001054 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001055 for (item = orig->lv_first; item != NULL && !got_int;
1056 item = item->li_next)
1057 {
1058 ni = listitem_alloc();
1059 if (ni == NULL)
1060 break;
1061 if (deep)
1062 {
1063 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
1064 {
1065 vim_free(ni);
1066 break;
1067 }
1068 }
1069 else
1070 copy_tv(&item->li_tv, &ni->li_tv);
1071 list_append(copy, ni);
1072 }
1073 ++copy->lv_refcount;
1074 if (item != NULL)
1075 {
1076 list_unref(copy);
1077 copy = NULL;
1078 }
1079 }
1080
1081 return copy;
1082}
1083
1084/*
1085 * Remove items "item" to "item2" from list "l".
1086 * Does not free the listitem or the value!
1087 * This used to be called list_remove, but that conflicts with a Sun header
1088 * file.
1089 */
1090 void
1091vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1092{
1093 listitem_T *ip;
1094
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001095 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001097 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001098 for (ip = item; ip != NULL; ip = ip->li_next)
1099 {
1100 --l->lv_len;
1101 list_fix_watch(l, ip);
1102 if (ip == item2)
1103 break;
1104 }
1105
1106 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001107 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001108 else
1109 item2->li_next->li_prev = item->li_prev;
1110 if (item->li_prev == NULL)
1111 l->lv_first = item2->li_next;
1112 else
1113 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001114 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001115}
1116
1117/*
1118 * Return an allocated string with the string representation of a list.
1119 * May return NULL.
1120 */
1121 char_u *
1122list2string(typval_T *tv, int copyID, int restore_copyID)
1123{
1124 garray_T ga;
1125
1126 if (tv->vval.v_list == NULL)
1127 return NULL;
1128 ga_init2(&ga, (int)sizeof(char), 80);
1129 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001130 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001131 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1132 FALSE, restore_copyID, copyID) == FAIL)
1133 {
1134 vim_free(ga.ga_data);
1135 return NULL;
1136 }
1137 ga_append(&ga, ']');
1138 ga_append(&ga, NUL);
1139 return (char_u *)ga.ga_data;
1140}
1141
1142typedef struct join_S {
1143 char_u *s;
1144 char_u *tofree;
1145} join_T;
1146
1147 static int
1148list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001149 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001150 list_T *l,
1151 char_u *sep,
1152 int echo_style,
1153 int restore_copyID,
1154 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001155 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001156{
1157 int i;
1158 join_T *p;
1159 int len;
1160 int sumlen = 0;
1161 int first = TRUE;
1162 char_u *tofree;
1163 char_u numbuf[NUMBUFLEN];
1164 listitem_T *item;
1165 char_u *s;
1166
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001167 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001168 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001169 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1170 {
1171 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001172 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001173 if (s == NULL)
1174 return FAIL;
1175
1176 len = (int)STRLEN(s);
1177 sumlen += len;
1178
1179 (void)ga_grow(join_gap, 1);
1180 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1181 if (tofree != NULL || s != numbuf)
1182 {
1183 p->s = s;
1184 p->tofree = tofree;
1185 }
1186 else
1187 {
1188 p->s = vim_strnsave(s, len);
1189 p->tofree = p->s;
1190 }
1191
1192 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001193 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001194 break;
1195 }
1196
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001197 // Allocate result buffer with its total size, avoid re-allocation and
1198 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001199 if (join_gap->ga_len >= 2)
1200 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1201 if (ga_grow(gap, sumlen + 2) == FAIL)
1202 return FAIL;
1203
1204 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1205 {
1206 if (first)
1207 first = FALSE;
1208 else
1209 ga_concat(gap, sep);
1210 p = ((join_T *)join_gap->ga_data) + i;
1211
1212 if (p->s != NULL)
1213 ga_concat(gap, p->s);
1214 line_breakcheck();
1215 }
1216
1217 return OK;
1218}
1219
1220/*
1221 * Join list "l" into a string in "*gap", using separator "sep".
1222 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1223 * Return FAIL or OK.
1224 */
1225 int
1226list_join(
1227 garray_T *gap,
1228 list_T *l,
1229 char_u *sep,
1230 int echo_style,
1231 int restore_copyID,
1232 int copyID)
1233{
1234 garray_T join_ga;
1235 int retval;
1236 join_T *p;
1237 int i;
1238
1239 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001240 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001241 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1242 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1243 copyID, &join_ga);
1244
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001245 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001246 if (join_ga.ga_data != NULL)
1247 {
1248 p = (join_T *)join_ga.ga_data;
1249 for (i = 0; i < join_ga.ga_len; ++i)
1250 {
1251 vim_free(p->tofree);
1252 ++p;
1253 }
1254 ga_clear(&join_ga);
1255 }
1256
1257 return retval;
1258}
1259
1260/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001261 * "join()" function
1262 */
1263 void
1264f_join(typval_T *argvars, typval_T *rettv)
1265{
1266 garray_T ga;
1267 char_u *sep;
1268
1269 if (argvars[0].v_type != VAR_LIST)
1270 {
1271 emsg(_(e_listreq));
1272 return;
1273 }
1274 if (argvars[0].vval.v_list == NULL)
1275 return;
1276 if (argvars[1].v_type == VAR_UNKNOWN)
1277 sep = (char_u *)" ";
1278 else
1279 sep = tv_get_string_chk(&argvars[1]);
1280
1281 rettv->v_type = VAR_STRING;
1282
1283 if (sep != NULL)
1284 {
1285 ga_init2(&ga, (int)sizeof(char), 80);
1286 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1287 ga_append(&ga, NUL);
1288 rettv->vval.v_string = (char_u *)ga.ga_data;
1289 }
1290 else
1291 rettv->vval.v_string = NULL;
1292}
1293
1294/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001295 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001296 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001297 * Return OK or FAIL.
1298 */
1299 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001300eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001301{
Bram Moolenaar71478202020-06-26 22:46:27 +02001302 int evaluate = evalarg == NULL ? FALSE
1303 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001304 list_T *l = NULL;
1305 typval_T tv;
1306 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001307 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001308 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001309
1310 if (evaluate)
1311 {
1312 l = list_alloc();
1313 if (l == NULL)
1314 return FAIL;
1315 }
1316
Bram Moolenaar962d7212020-07-04 14:15:00 +02001317 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001318 while (**arg != ']' && **arg != NUL)
1319 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001320 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001321 goto failret;
1322 if (evaluate)
1323 {
1324 item = listitem_alloc();
1325 if (item != NULL)
1326 {
1327 item->li_tv = tv;
1328 item->li_tv.v_lock = 0;
1329 list_append(l, item);
1330 }
1331 else
1332 clear_tv(&tv);
1333 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001334 // Legacy Vim script allowed a space before the comma.
1335 if (!vim9script)
1336 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001337
Bram Moolenaare6e03172020-06-27 16:36:05 +02001338 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001339 had_comma = **arg == ',';
1340 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001341 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001342 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001343 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001344 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001345 goto failret;
1346 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001347 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001348 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001349
Bram Moolenaare6b53242020-07-01 17:28:33 +02001350 // The "]" can be on the next line. But a double quoted string may
1351 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001352 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001353 if (**arg == ']')
1354 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001355
1356 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001357 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001359 {
1360 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001361 semsg(_(e_no_white_space_allowed_before_str_str),
1362 ",", *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001363 else
1364 semsg(_("E696: Missing comma in List: %s"), *arg);
1365 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001366 goto failret;
1367 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001368 }
1369
1370 if (**arg != ']')
1371 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001373 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001374failret:
1375 if (evaluate)
1376 list_free(l);
1377 return FAIL;
1378 }
1379
Bram Moolenaar9d489562020-07-30 20:08:50 +02001380 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001381 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001382 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001383
1384 return OK;
1385}
1386
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001387/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001388 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001389 */
1390 int
1391write_list(FILE *fd, list_T *list, int binary)
1392{
1393 listitem_T *li;
1394 int c;
1395 int ret = OK;
1396 char_u *s;
1397
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001398 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001399 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001400 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001401 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001402 {
1403 if (*s == '\n')
1404 c = putc(NUL, fd);
1405 else
1406 c = putc(*s, fd);
1407 if (c == EOF)
1408 {
1409 ret = FAIL;
1410 break;
1411 }
1412 }
1413 if (!binary || li->li_next != NULL)
1414 if (putc('\n', fd) == EOF)
1415 {
1416 ret = FAIL;
1417 break;
1418 }
1419 if (ret == FAIL)
1420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001421 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001422 break;
1423 }
1424 }
1425 return ret;
1426}
1427
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001428/*
1429 * Initialize a static list with 10 items.
1430 */
1431 void
1432init_static_list(staticList10_T *sl)
1433{
1434 list_T *l = &sl->sl_list;
1435 int i;
1436
1437 memset(sl, 0, sizeof(staticList10_T));
1438 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001439 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001440 l->lv_refcount = DO_NOT_FREE_CNT;
1441 l->lv_lock = VAR_FIXED;
1442 sl->sl_list.lv_len = 10;
1443
1444 for (i = 0; i < 10; ++i)
1445 {
1446 listitem_T *li = &sl->sl_items[i];
1447
1448 if (i == 0)
1449 li->li_prev = NULL;
1450 else
1451 li->li_prev = li - 1;
1452 if (i == 9)
1453 li->li_next = NULL;
1454 else
1455 li->li_next = li + 1;
1456 }
1457}
1458
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001459/*
1460 * "list2str()" function
1461 */
1462 void
1463f_list2str(typval_T *argvars, typval_T *rettv)
1464{
1465 list_T *l;
1466 listitem_T *li;
1467 garray_T ga;
1468 int utf8 = FALSE;
1469
1470 rettv->v_type = VAR_STRING;
1471 rettv->vval.v_string = NULL;
1472 if (argvars[0].v_type != VAR_LIST)
1473 {
1474 emsg(_(e_invarg));
1475 return;
1476 }
1477
1478 l = argvars[0].vval.v_list;
1479 if (l == NULL)
1480 return; // empty list results in empty string
1481
1482 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001483 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001484
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001485 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001486 ga_init2(&ga, 1, 80);
1487 if (has_mbyte || utf8)
1488 {
1489 char_u buf[MB_MAXBYTES + 1];
1490 int (*char2bytes)(int, char_u *);
1491
1492 if (utf8 || enc_utf8)
1493 char2bytes = utf_char2bytes;
1494 else
1495 char2bytes = mb_char2bytes;
1496
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001497 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001498 {
1499 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1500 ga_concat(&ga, buf);
1501 }
1502 ga_append(&ga, NUL);
1503 }
1504 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1505 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001506 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001507 ga_append(&ga, tv_get_number(&li->li_tv));
1508 ga_append(&ga, NUL);
1509 }
1510
1511 rettv->v_type = VAR_STRING;
1512 rettv->vval.v_string = ga.ga_data;
1513}
1514
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001515 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001516list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1517{
1518 list_T *l;
1519 listitem_T *item, *item2;
1520 listitem_T *li;
1521 int error = FALSE;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001522 long idx;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001523
1524 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001525 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001526 return;
1527
1528 idx = (long)tv_get_number_chk(&argvars[1], &error);
1529 if (error)
1530 ; // type error: do nothing, errmsg already given
1531 else if ((item = list_find(l, idx)) == NULL)
1532 semsg(_(e_listidx), idx);
1533 else
1534 {
1535 if (argvars[2].v_type == VAR_UNKNOWN)
1536 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001537 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001538 vimlist_remove(l, item, item);
1539 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001541 }
1542 else
1543 {
1544 // Remove range of items, return list with values.
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001545 long end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001546
1547 if (error)
1548 ; // type error: do nothing
1549 else if ((item2 = list_find(l, end)) == NULL)
1550 semsg(_(e_listidx), end);
1551 else
1552 {
1553 int cnt = 0;
1554
1555 for (li = item; li != NULL; li = li->li_next)
1556 {
1557 ++cnt;
1558 if (li == item2)
1559 break;
1560 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001561 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar108010a2021-06-27 22:03:33 +02001562 emsg(_(e_invalid_range));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001563 else
1564 {
1565 vimlist_remove(l, item, item2);
1566 if (rettv_list_alloc(rettv) == OK)
1567 {
1568 l = rettv->vval.v_list;
1569 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001570 l->lv_u.mat.lv_last = item2;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001571 item->li_prev = NULL;
1572 item2->li_next = NULL;
1573 l->lv_len = cnt;
1574 }
1575 }
1576 }
1577 }
1578 }
1579}
1580
1581static int item_compare(const void *s1, const void *s2);
1582static int item_compare2(const void *s1, const void *s2);
1583
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001584// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001585typedef struct
1586{
1587 listitem_T *item;
1588 int idx;
1589} sortItem_T;
1590
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001591// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001592typedef struct
1593{
1594 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001595 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001596 int item_compare_numeric;
1597 int item_compare_numbers;
1598#ifdef FEAT_FLOAT
1599 int item_compare_float;
1600#endif
1601 char_u *item_compare_func;
1602 partial_T *item_compare_partial;
1603 dict_T *item_compare_selfdict;
1604 int item_compare_func_err;
1605 int item_compare_keep_zero;
1606} sortinfo_T;
1607static sortinfo_T *sortinfo = NULL;
1608#define ITEM_COMPARE_FAIL 999
1609
1610/*
1611 * Compare functions for f_sort() and f_uniq() below.
1612 */
1613 static int
1614item_compare(const void *s1, const void *s2)
1615{
1616 sortItem_T *si1, *si2;
1617 typval_T *tv1, *tv2;
1618 char_u *p1, *p2;
1619 char_u *tofree1 = NULL, *tofree2 = NULL;
1620 int res;
1621 char_u numbuf1[NUMBUFLEN];
1622 char_u numbuf2[NUMBUFLEN];
1623
1624 si1 = (sortItem_T *)s1;
1625 si2 = (sortItem_T *)s2;
1626 tv1 = &si1->item->li_tv;
1627 tv2 = &si2->item->li_tv;
1628
1629 if (sortinfo->item_compare_numbers)
1630 {
1631 varnumber_T v1 = tv_get_number(tv1);
1632 varnumber_T v2 = tv_get_number(tv2);
1633
1634 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1635 }
1636
1637#ifdef FEAT_FLOAT
1638 if (sortinfo->item_compare_float)
1639 {
1640 float_T v1 = tv_get_float(tv1);
1641 float_T v2 = tv_get_float(tv2);
1642
1643 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1644 }
1645#endif
1646
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001647 // tv2string() puts quotes around a string and allocates memory. Don't do
1648 // that for string variables. Use a single quote when comparing with a
1649 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001650 if (tv1->v_type == VAR_STRING)
1651 {
1652 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1653 p1 = (char_u *)"'";
1654 else
1655 p1 = tv1->vval.v_string;
1656 }
1657 else
1658 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1659 if (tv2->v_type == VAR_STRING)
1660 {
1661 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1662 p2 = (char_u *)"'";
1663 else
1664 p2 = tv2->vval.v_string;
1665 }
1666 else
1667 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1668 if (p1 == NULL)
1669 p1 = (char_u *)"";
1670 if (p2 == NULL)
1671 p2 = (char_u *)"";
1672 if (!sortinfo->item_compare_numeric)
1673 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001674 if (sortinfo->item_compare_lc)
1675 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001676 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001677 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001678 }
1679 else
1680 {
1681 double n1, n2;
1682 n1 = strtod((char *)p1, (char **)&p1);
1683 n2 = strtod((char *)p2, (char **)&p2);
1684 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1685 }
1686
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001687 // When the result would be zero, compare the item indexes. Makes the
1688 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001689 if (res == 0 && !sortinfo->item_compare_keep_zero)
1690 res = si1->idx > si2->idx ? 1 : -1;
1691
1692 vim_free(tofree1);
1693 vim_free(tofree2);
1694 return res;
1695}
1696
1697 static int
1698item_compare2(const void *s1, const void *s2)
1699{
1700 sortItem_T *si1, *si2;
1701 int res;
1702 typval_T rettv;
1703 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001704 char_u *func_name;
1705 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001706 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001707
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001708 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001709 if (sortinfo->item_compare_func_err)
1710 return 0;
1711
1712 si1 = (sortItem_T *)s1;
1713 si2 = (sortItem_T *)s2;
1714
1715 if (partial == NULL)
1716 func_name = sortinfo->item_compare_func;
1717 else
1718 func_name = partial_name(partial);
1719
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001720 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1721 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001722 copy_tv(&si1->item->li_tv, &argv[0]);
1723 copy_tv(&si2->item->li_tv, &argv[1]);
1724
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001725 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001726 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001727 funcexe.evaluate = TRUE;
1728 funcexe.partial = partial;
1729 funcexe.selfdict = sortinfo->item_compare_selfdict;
1730 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001731 clear_tv(&argv[0]);
1732 clear_tv(&argv[1]);
1733
1734 if (res == FAIL)
1735 res = ITEM_COMPARE_FAIL;
1736 else
1737 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1738 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001739 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001740 clear_tv(&rettv);
1741
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001742 // When the result would be zero, compare the pointers themselves. Makes
1743 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001744 if (res == 0 && !sortinfo->item_compare_keep_zero)
1745 res = si1->idx > si2->idx ? 1 : -1;
1746
1747 return res;
1748}
1749
1750/*
1751 * "sort()" or "uniq()" function
1752 */
1753 static void
1754do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1755{
1756 list_T *l;
1757 listitem_T *li;
1758 sortItem_T *ptrs;
1759 sortinfo_T *old_sortinfo;
1760 sortinfo_T info;
1761 long len;
1762 long i;
1763
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001764 // Pointer to current info struct used in compare function. Save and
1765 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001766 old_sortinfo = sortinfo;
1767 sortinfo = &info;
1768
1769 if (argvars[0].v_type != VAR_LIST)
1770 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1771 else
1772 {
1773 l = argvars[0].vval.v_list;
Bram Moolenaara187c432020-09-16 21:08:28 +02001774 if (l == NULL || value_check_lock(l->lv_lock,
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001775 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1776 TRUE))
1777 goto theend;
1778 rettv_list_set(rettv, l);
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001779 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001780
1781 len = list_len(l);
1782 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001783 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001784
1785 info.item_compare_ic = FALSE;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001786 info.item_compare_lc = FALSE;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001787 info.item_compare_numeric = FALSE;
1788 info.item_compare_numbers = FALSE;
1789#ifdef FEAT_FLOAT
1790 info.item_compare_float = FALSE;
1791#endif
1792 info.item_compare_func = NULL;
1793 info.item_compare_partial = NULL;
1794 info.item_compare_selfdict = NULL;
1795 if (argvars[1].v_type != VAR_UNKNOWN)
1796 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001797 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001798 if (argvars[1].v_type == VAR_FUNC)
1799 info.item_compare_func = argvars[1].vval.v_string;
1800 else if (argvars[1].v_type == VAR_PARTIAL)
1801 info.item_compare_partial = argvars[1].vval.v_partial;
1802 else
1803 {
1804 int error = FALSE;
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001805 int nr = 0;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001806
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001807 if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001808 {
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001809 nr = tv_get_number_chk(&argvars[1], &error);
1810 if (error)
1811 goto theend; // type error; errmsg already given
1812 if (nr == 1)
1813 info.item_compare_ic = TRUE;
1814 }
1815 if (nr != 1)
1816 {
1817 if (argvars[1].v_type != VAR_NUMBER)
1818 info.item_compare_func = tv_get_string(&argvars[1]);
1819 else if (nr != 0)
1820 {
1821 emsg(_(e_invarg));
1822 goto theend;
1823 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001824 }
1825 if (info.item_compare_func != NULL)
1826 {
1827 if (*info.item_compare_func == NUL)
1828 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001829 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001830 info.item_compare_func = NULL;
1831 }
1832 else if (STRCMP(info.item_compare_func, "n") == 0)
1833 {
1834 info.item_compare_func = NULL;
1835 info.item_compare_numeric = TRUE;
1836 }
1837 else if (STRCMP(info.item_compare_func, "N") == 0)
1838 {
1839 info.item_compare_func = NULL;
1840 info.item_compare_numbers = TRUE;
1841 }
1842#ifdef FEAT_FLOAT
1843 else if (STRCMP(info.item_compare_func, "f") == 0)
1844 {
1845 info.item_compare_func = NULL;
1846 info.item_compare_float = TRUE;
1847 }
1848#endif
1849 else if (STRCMP(info.item_compare_func, "i") == 0)
1850 {
1851 info.item_compare_func = NULL;
1852 info.item_compare_ic = TRUE;
1853 }
Bram Moolenaar55e29612020-11-01 13:57:44 +01001854 else if (STRCMP(info.item_compare_func, "l") == 0)
1855 {
1856 info.item_compare_func = NULL;
1857 info.item_compare_lc = TRUE;
1858 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001859 }
1860 }
1861
1862 if (argvars[2].v_type != VAR_UNKNOWN)
1863 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001864 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001865 if (argvars[2].v_type != VAR_DICT)
1866 {
1867 emsg(_(e_dictreq));
1868 goto theend;
1869 }
1870 info.item_compare_selfdict = argvars[2].vval.v_dict;
1871 }
1872 }
1873
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001874 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001875 ptrs = ALLOC_MULT(sortItem_T, len);
1876 if (ptrs == NULL)
1877 goto theend;
1878
1879 i = 0;
1880 if (sort)
1881 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001882 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001883 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001884 {
1885 ptrs[i].item = li;
1886 ptrs[i].idx = i;
1887 ++i;
1888 }
1889
1890 info.item_compare_func_err = FALSE;
1891 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001892 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001893 if ((info.item_compare_func != NULL
1894 || info.item_compare_partial != NULL)
1895 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
1896 == ITEM_COMPARE_FAIL)
1897 emsg(_("E702: Sort compare function failed"));
1898 else
1899 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001900 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001901 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
1902 info.item_compare_func == NULL
1903 && info.item_compare_partial == NULL
1904 ? item_compare : item_compare2);
1905
1906 if (!info.item_compare_func_err)
1907 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001908 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001909 l->lv_first = l->lv_u.mat.lv_last
1910 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001911 l->lv_len = 0;
1912 for (i = 0; i < len; ++i)
1913 list_append(l, ptrs[i].item);
1914 }
1915 }
1916 }
1917 else
1918 {
1919 int (*item_compare_func_ptr)(const void *, const void *);
1920
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001921 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001922 info.item_compare_func_err = FALSE;
1923 info.item_compare_keep_zero = TRUE;
1924 item_compare_func_ptr = info.item_compare_func != NULL
1925 || info.item_compare_partial != NULL
1926 ? item_compare2 : item_compare;
1927
1928 for (li = l->lv_first; li != NULL && li->li_next != NULL;
1929 li = li->li_next)
1930 {
1931 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
1932 == 0)
1933 ptrs[i++].item = li;
1934 if (info.item_compare_func_err)
1935 {
1936 emsg(_("E882: Uniq compare function failed"));
1937 break;
1938 }
1939 }
1940
1941 if (!info.item_compare_func_err)
1942 {
1943 while (--i >= 0)
1944 {
1945 li = ptrs[i].item->li_next;
1946 ptrs[i].item->li_next = li->li_next;
1947 if (li->li_next != NULL)
1948 li->li_next->li_prev = ptrs[i].item;
1949 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001950 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001951 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001953 l->lv_len--;
1954 }
1955 }
1956 }
1957
1958 vim_free(ptrs);
1959 }
1960theend:
1961 sortinfo = old_sortinfo;
1962}
1963
1964/*
1965 * "sort({list})" function
1966 */
1967 void
1968f_sort(typval_T *argvars, typval_T *rettv)
1969{
1970 do_sort_uniq(argvars, rettv, TRUE);
1971}
1972
1973/*
1974 * "uniq({list})" function
1975 */
1976 void
1977f_uniq(typval_T *argvars, typval_T *rettv)
1978{
1979 do_sort_uniq(argvars, rettv, FALSE);
1980}
1981
Bram Moolenaarea696852020-11-09 18:31:39 +01001982typedef enum {
1983 FILTERMAP_FILTER,
1984 FILTERMAP_MAP,
1985 FILTERMAP_MAPNEW
1986} filtermap_T;
1987
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001988/*
1989 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01001990 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001991 */
1992 static int
Bram Moolenaarea696852020-11-09 18:31:39 +01001993filter_map_one(
1994 typval_T *tv, // original value
1995 typval_T *expr, // callback
1996 filtermap_T filtermap,
1997 typval_T *newtv, // for map() and mapnew(): new value
1998 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001999{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002000 typval_T argv[3];
2001 int retval = FAIL;
2002
2003 copy_tv(tv, get_vim_var_tv(VV_VAL));
2004 argv[0] = *get_vim_var_tv(VV_KEY);
2005 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01002006 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002007 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002008 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002009 {
2010 int error = FALSE;
2011
2012 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02002013 if (in_vim9script())
Bram Moolenaarea696852020-11-09 18:31:39 +01002014 *remp = !tv2bool(newtv);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002015 else
Bram Moolenaarea696852020-11-09 18:31:39 +01002016 *remp = (tv_get_number_chk(newtv, &error) == 0);
2017 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002018 // On type error, nothing has been removed; return FAIL to stop the
2019 // loop. The error message was given by tv_get_number_chk().
2020 if (error)
2021 goto theend;
2022 }
2023 retval = OK;
2024theend:
2025 clear_tv(get_vim_var_tv(VV_VAL));
2026 return retval;
2027}
2028
2029/*
2030 * Implementation of map() and filter().
2031 */
2032 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01002033filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002034{
2035 typval_T *expr;
2036 listitem_T *li, *nli;
2037 list_T *l = NULL;
2038 dictitem_T *di;
2039 hashtab_T *ht;
2040 hashitem_T *hi;
2041 dict_T *d = NULL;
2042 blob_T *b = NULL;
2043 int rem;
2044 int todo;
Bram Moolenaarea696852020-11-09 18:31:39 +01002045 char_u *ermsg = (char_u *)(filtermap == FILTERMAP_MAP ? "map()"
2046 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
2047 : "filter()");
2048 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2049 ? N_("map() argument")
2050 : filtermap == FILTERMAP_MAPNEW
2051 ? N_("mapnew() argument")
2052 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002053 int save_did_emsg;
2054 int idx = 0;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002055 type_T *type = NULL;
2056 garray_T type_list;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002057
Bram Moolenaarea696852020-11-09 18:31:39 +01002058 // map() and filter() return the first argument, also on failure.
2059 if (filtermap != FILTERMAP_MAPNEW)
2060 copy_tv(&argvars[0], rettv);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002061 if (filtermap == FILTERMAP_MAP && in_vim9script())
2062 {
2063 // Check that map() does not change the type of the dict.
2064 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaarf2253962021-04-13 20:53:13 +02002065 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002066 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002067
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002068 if (argvars[0].v_type == VAR_BLOB)
2069 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002070 if (filtermap == FILTERMAP_MAPNEW)
2071 {
2072 rettv->v_type = VAR_BLOB;
2073 rettv->vval.v_blob = NULL;
2074 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002075 if ((b = argvars[0].vval.v_blob) == NULL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002076 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002077 }
2078 else if (argvars[0].v_type == VAR_LIST)
2079 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002080 if (filtermap == FILTERMAP_MAPNEW)
2081 {
2082 rettv->v_type = VAR_LIST;
2083 rettv->vval.v_list = NULL;
2084 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002085 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002086 || (filtermap == FILTERMAP_FILTER
2087 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002088 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002089 }
2090 else if (argvars[0].v_type == VAR_DICT)
2091 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002092 if (filtermap == FILTERMAP_MAPNEW)
2093 {
2094 rettv->v_type = VAR_DICT;
2095 rettv->vval.v_dict = NULL;
2096 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002097 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002098 || (filtermap == FILTERMAP_FILTER
2099 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002100 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002101 }
2102 else
2103 {
Bram Moolenaarfcb0b612020-05-26 20:22:01 +02002104 semsg(_(e_listdictblobarg), ermsg);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002105 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002106 }
2107
2108 expr = &argvars[1];
2109 // On type errors, the preceding call has already displayed an error
2110 // message. Avoid a misleading error message for an empty string that
2111 // was not passed as argument.
2112 if (expr->v_type != VAR_UNKNOWN)
2113 {
2114 typval_T save_val;
2115 typval_T save_key;
2116
2117 prepare_vimvar(VV_VAL, &save_val);
2118 prepare_vimvar(VV_KEY, &save_key);
2119
2120 // We reset "did_emsg" to be able to detect whether an error
2121 // occurred during evaluation of the expression.
2122 save_did_emsg = did_emsg;
2123 did_emsg = FALSE;
2124
2125 if (argvars[0].v_type == VAR_DICT)
2126 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002127 int prev_lock = d->dv_lock;
Bram Moolenaarea696852020-11-09 18:31:39 +01002128 dict_T *d_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002129
Bram Moolenaarea696852020-11-09 18:31:39 +01002130 if (filtermap == FILTERMAP_MAPNEW)
2131 {
2132 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002133 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002134 d_ret = rettv->vval.v_dict;
2135 }
2136
2137 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002138 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002139 ht = &d->dv_hashtab;
2140 hash_lock(ht);
2141 todo = (int)ht->ht_used;
2142 for (hi = ht->ht_array; todo > 0; ++hi)
2143 {
2144 if (!HASHITEM_EMPTY(hi))
2145 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002146 int r;
2147 typval_T newtv;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002148
2149 --todo;
2150 di = HI2DI(hi);
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002151 if (filtermap == FILTERMAP_MAP
Bram Moolenaarea696852020-11-09 18:31:39 +01002152 && (value_check_lock(di->di_tv.v_lock,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002153 arg_errmsg, TRUE)
2154 || var_check_ro(di->di_flags,
2155 arg_errmsg, TRUE)))
2156 break;
2157 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaar027c4ab2021-02-21 16:20:18 +01002158 newtv.v_type = VAR_UNKNOWN;
Bram Moolenaarea696852020-11-09 18:31:39 +01002159 r = filter_map_one(&di->di_tv, expr, filtermap,
2160 &newtv, &rem);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002161 clear_tv(get_vim_var_tv(VV_KEY));
2162 if (r == FAIL || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002163 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002164 clear_tv(&newtv);
2165 break;
2166 }
2167 if (filtermap == FILTERMAP_MAP)
2168 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002169 if (type != NULL && check_typval_arg_type(
2170 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002171 {
2172 clear_tv(&newtv);
2173 break;
2174 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002175 // map(): replace the dict item value
2176 clear_tv(&di->di_tv);
2177 newtv.v_lock = 0;
2178 di->di_tv = newtv;
2179 }
2180 else if (filtermap == FILTERMAP_MAPNEW)
2181 {
2182 // mapnew(): add the item value to the new dict
2183 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
2184 clear_tv(&newtv);
2185 if (r == FAIL)
2186 break;
2187 }
2188 else if (filtermap == FILTERMAP_FILTER && rem)
2189 {
2190 // filter(false): remove the item from the dict
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002191 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2192 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2193 break;
2194 dictitem_remove(d, di);
2195 }
2196 }
2197 }
2198 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002199 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002200 }
2201 else if (argvars[0].v_type == VAR_BLOB)
2202 {
2203 int i;
2204 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002205 varnumber_T val;
Bram Moolenaarea696852020-11-09 18:31:39 +01002206 blob_T *b_ret = b;
2207
2208 if (filtermap == FILTERMAP_MAPNEW)
2209 {
2210 if (blob_copy(b, rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002211 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002212 b_ret = rettv->vval.v_blob;
2213 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002214
2215 // set_vim_var_nr() doesn't set the type
2216 set_vim_var_type(VV_KEY, VAR_NUMBER);
2217
2218 for (i = 0; i < b->bv_ga.ga_len; i++)
2219 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002220 typval_T newtv;
2221
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002222 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002223 val = blob_get(b, i);
2224 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002225 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaarea696852020-11-09 18:31:39 +01002226 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
2227 || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002228 break;
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002229 if (newtv.v_type != VAR_NUMBER && newtv.v_type != VAR_BOOL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002230 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002231 clear_tv(&newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002232 emsg(_(e_invalblob));
2233 break;
2234 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002235 if (filtermap != FILTERMAP_FILTER)
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002236 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002237 if (newtv.vval.v_number != val)
2238 blob_set(b_ret, i, newtv.vval.v_number);
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002239 }
2240 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002241 {
2242 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2243
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002244 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002245 (size_t)b->bv_ga.ga_len - i - 1);
2246 --b->bv_ga.ga_len;
2247 --i;
2248 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002249 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002250 }
2251 }
2252 else // argvars[0].v_type == VAR_LIST
2253 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002254 int prev_lock = l->lv_lock;
2255 list_T *l_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002256
Bram Moolenaarea696852020-11-09 18:31:39 +01002257 if (filtermap == FILTERMAP_MAPNEW)
2258 {
2259 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002260 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002261 l_ret = rettv->vval.v_list;
2262 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002263 // set_vim_var_nr() doesn't set the type
2264 set_vim_var_type(VV_KEY, VAR_NUMBER);
2265
Bram Moolenaarea696852020-11-09 18:31:39 +01002266 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002267 l->lv_lock = VAR_LOCKED;
Bram Moolenaarea696852020-11-09 18:31:39 +01002268
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002269 if (l->lv_first == &range_list_item)
2270 {
2271 varnumber_T val = l->lv_u.nonmat.lv_start;
2272 int len = l->lv_len;
2273 int stride = l->lv_u.nonmat.lv_stride;
2274
2275 // List from range(): loop over the numbers
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002276 if (filtermap != FILTERMAP_MAPNEW)
2277 {
2278 l->lv_first = NULL;
2279 l->lv_u.mat.lv_last = NULL;
2280 l->lv_len = 0;
2281 l->lv_u.mat.lv_idx_item = NULL;
2282 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002283
2284 for (idx = 0; idx < len; ++idx)
Bram Moolenaarc56936e2020-11-10 11:43:56 +01002285 {
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002286 typval_T tv;
2287 typval_T newtv;
2288
2289 tv.v_type = VAR_NUMBER;
2290 tv.v_lock = 0;
2291 tv.vval.v_number = val;
2292 set_vim_var_nr(VV_KEY, idx);
2293 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2294 == FAIL)
Bram Moolenaarea696852020-11-09 18:31:39 +01002295 break;
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002296 if (did_emsg)
2297 {
2298 clear_tv(&newtv);
2299 break;
2300 }
2301 if (filtermap != FILTERMAP_FILTER)
2302 {
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002303 if (filtermap == FILTERMAP_MAP && type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002304 && check_typval_arg_type(
2305 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002306 {
2307 clear_tv(&newtv);
2308 break;
2309 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002310 // map(), mapnew(): always append the new value to the
2311 // list
2312 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2313 ? l : l_ret, &newtv) == FAIL)
2314 break;
2315 }
2316 else if (!rem)
2317 {
2318 // filter(): append the list item value when not rem
2319 if (list_append_tv_move(l, &tv) == FAIL)
2320 break;
2321 }
2322
2323 val += stride;
Bram Moolenaarea696852020-11-09 18:31:39 +01002324 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002325 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002326 else
2327 {
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002328 // Materialized list: loop over the items
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002329 for (li = l->lv_first; li != NULL; li = nli)
2330 {
2331 typval_T newtv;
2332
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002333 if (filtermap == FILTERMAP_MAP && value_check_lock(
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002334 li->li_tv.v_lock, arg_errmsg, TRUE))
2335 break;
2336 nli = li->li_next;
2337 set_vim_var_nr(VV_KEY, idx);
2338 if (filter_map_one(&li->li_tv, expr, filtermap,
2339 &newtv, &rem) == FAIL)
2340 break;
2341 if (did_emsg)
2342 {
2343 clear_tv(&newtv);
2344 break;
2345 }
2346 if (filtermap == FILTERMAP_MAP)
2347 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002348 if (type != NULL && check_typval_arg_type(
2349 type->tt_member, &newtv, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002350 {
2351 clear_tv(&newtv);
2352 break;
2353 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002354 // map(): replace the list item value
2355 clear_tv(&li->li_tv);
2356 newtv.v_lock = 0;
2357 li->li_tv = newtv;
2358 }
2359 else if (filtermap == FILTERMAP_MAPNEW)
2360 {
2361 // mapnew(): append the list item value
2362 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2363 break;
2364 }
2365 else if (filtermap == FILTERMAP_FILTER && rem)
2366 listitem_remove(l, li);
2367 ++idx;
2368 }
2369 }
2370
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002371 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002372 }
2373
2374 restore_vimvar(VV_KEY, &save_key);
2375 restore_vimvar(VV_VAL, &save_val);
2376
2377 did_emsg |= save_did_emsg;
2378 }
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002379
2380theend:
2381 if (type != NULL)
2382 clear_type_list(&type_list);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002383}
2384
2385/*
2386 * "filter()" function
2387 */
2388 void
2389f_filter(typval_T *argvars, typval_T *rettv)
2390{
Bram Moolenaarea696852020-11-09 18:31:39 +01002391 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002392}
2393
2394/*
2395 * "map()" function
2396 */
2397 void
2398f_map(typval_T *argvars, typval_T *rettv)
2399{
Bram Moolenaarea696852020-11-09 18:31:39 +01002400 filter_map(argvars, rettv, FILTERMAP_MAP);
2401}
2402
2403/*
2404 * "mapnew()" function
2405 */
2406 void
2407f_mapnew(typval_T *argvars, typval_T *rettv)
2408{
2409 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002410}
2411
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002412/*
2413 * "add(list, item)" function
2414 */
2415 void
2416f_add(typval_T *argvars, typval_T *rettv)
2417{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002418 rettv->vval.v_number = 1; // Default: Failed
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002419 if (argvars[0].v_type == VAR_LIST)
2420 {
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002421 list_T *l = argvars[0].vval.v_list;
2422
2423 if (l == NULL)
2424 {
2425 if (in_vim9script())
2426 emsg(_(e_cannot_add_to_null_list));
2427 }
2428 else if (!value_check_lock(l->lv_lock,
2429 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002430 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002431 {
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002432 copy_tv(&argvars[0], rettv);
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002433 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002434 }
2435 else if (argvars[0].v_type == VAR_BLOB)
2436 {
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002437 blob_T *b = argvars[0].vval.v_blob;
2438
2439 if (b == NULL)
2440 {
2441 if (in_vim9script())
2442 emsg(_(e_cannot_add_to_null_blob));
2443 }
2444 else if (!value_check_lock(b->bv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002445 (char_u *)N_("add() argument"), TRUE))
2446 {
2447 int error = FALSE;
2448 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2449
2450 if (!error)
2451 {
2452 ga_append(&b->bv_ga, (int)n);
2453 copy_tv(&argvars[0], rettv);
2454 }
2455 }
2456 }
2457 else
2458 emsg(_(e_listblobreq));
2459}
2460
2461/*
2462 * "count()" function
2463 */
2464 void
2465f_count(typval_T *argvars, typval_T *rettv)
2466{
2467 long n = 0;
2468 int ic = FALSE;
2469 int error = FALSE;
2470
2471 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002472 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002473
2474 if (argvars[0].v_type == VAR_STRING)
2475 {
2476 char_u *expr = tv_get_string_chk(&argvars[1]);
2477 char_u *p = argvars[0].vval.v_string;
2478 char_u *next;
2479
2480 if (!error && expr != NULL && *expr != NUL && p != NULL)
2481 {
2482 if (ic)
2483 {
2484 size_t len = STRLEN(expr);
2485
2486 while (*p != NUL)
2487 {
2488 if (MB_STRNICMP(p, expr, len) == 0)
2489 {
2490 ++n;
2491 p += len;
2492 }
2493 else
2494 MB_PTR_ADV(p);
2495 }
2496 }
2497 else
2498 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2499 != NULL)
2500 {
2501 ++n;
2502 p = next + STRLEN(expr);
2503 }
2504 }
2505
2506 }
2507 else if (argvars[0].v_type == VAR_LIST)
2508 {
2509 listitem_T *li;
2510 list_T *l;
2511 long idx;
2512
2513 if ((l = argvars[0].vval.v_list) != NULL)
2514 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002515 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002516 li = l->lv_first;
2517 if (argvars[2].v_type != VAR_UNKNOWN)
2518 {
2519 if (argvars[3].v_type != VAR_UNKNOWN)
2520 {
2521 idx = (long)tv_get_number_chk(&argvars[3], &error);
2522 if (!error)
2523 {
2524 li = list_find(l, idx);
2525 if (li == NULL)
2526 semsg(_(e_listidx), idx);
2527 }
2528 }
2529 if (error)
2530 li = NULL;
2531 }
2532
2533 for ( ; li != NULL; li = li->li_next)
2534 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2535 ++n;
2536 }
2537 }
2538 else if (argvars[0].v_type == VAR_DICT)
2539 {
2540 int todo;
2541 dict_T *d;
2542 hashitem_T *hi;
2543
2544 if ((d = argvars[0].vval.v_dict) != NULL)
2545 {
2546 if (argvars[2].v_type != VAR_UNKNOWN)
2547 {
2548 if (argvars[3].v_type != VAR_UNKNOWN)
2549 emsg(_(e_invarg));
2550 }
2551
2552 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2553 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2554 {
2555 if (!HASHITEM_EMPTY(hi))
2556 {
2557 --todo;
2558 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2559 ++n;
2560 }
2561 }
2562 }
2563 }
2564 else
2565 semsg(_(e_listdictarg), "count()");
2566 rettv->vval.v_number = n;
2567}
2568
2569/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002570 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002571 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002572 static void
2573extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002574{
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002575 type_T *type = NULL;
2576 garray_T type_list;
2577
2578 if (!is_new && in_vim9script())
2579 {
2580 // Check that map() does not change the type of the dict.
2581 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaarf2253962021-04-13 20:53:13 +02002582 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002583 }
2584
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002585 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2586 {
2587 list_T *l1, *l2;
2588 listitem_T *item;
2589 long before;
2590 int error = FALSE;
2591
2592 l1 = argvars[0].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002593 if (l1 == NULL)
2594 {
2595 emsg(_(e_cannot_extend_null_list));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002596 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002597 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002598 l2 = argvars[1].vval.v_list;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002599 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
2600 && l2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002601 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002602 if (is_new)
2603 {
2604 l1 = list_copy(l1, FALSE, get_copyID());
2605 if (l1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002606 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002607 }
2608
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002609 if (argvars[2].v_type != VAR_UNKNOWN)
2610 {
2611 before = (long)tv_get_number_chk(&argvars[2], &error);
2612 if (error)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002613 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002614
2615 if (before == l1->lv_len)
2616 item = NULL;
2617 else
2618 {
2619 item = list_find(l1, before);
2620 if (item == NULL)
2621 {
2622 semsg(_(e_listidx), before);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002623 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002624 }
2625 }
2626 }
2627 else
2628 item = NULL;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002629 if (type != NULL && check_typval_arg_type(
2630 type, &argvars[1], 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002631 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002632 list_extend(l1, l2, item);
2633
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002634 if (is_new)
2635 {
2636 rettv->v_type = VAR_LIST;
2637 rettv->vval.v_list = l1;
2638 rettv->v_lock = FALSE;
2639 }
2640 else
2641 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002642 }
2643 }
2644 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2645 {
2646 dict_T *d1, *d2;
2647 char_u *action;
2648 int i;
2649
2650 d1 = argvars[0].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002651 if (d1 == NULL)
2652 {
2653 emsg(_(e_cannot_extend_null_dict));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002654 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002655 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002656 d2 = argvars[1].vval.v_dict;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002657 if ((is_new || !value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
2658 && d2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002659 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002660 if (is_new)
2661 {
2662 d1 = dict_copy(d1, FALSE, get_copyID());
2663 if (d1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002664 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002665 }
2666
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002667 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002668 if (argvars[2].v_type != VAR_UNKNOWN)
2669 {
2670 static char *(av[]) = {"keep", "force", "error"};
2671
2672 action = tv_get_string_chk(&argvars[2]);
2673 if (action == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002674 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002675 for (i = 0; i < 3; ++i)
2676 if (STRCMP(action, av[i]) == 0)
2677 break;
2678 if (i == 3)
2679 {
2680 semsg(_(e_invarg2), action);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002681 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002682 }
2683 }
2684 else
2685 action = (char_u *)"force";
2686
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002687 if (type != NULL && check_typval_arg_type(
2688 type, &argvars[1], 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002689 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002690 dict_extend(d1, d2, action);
2691
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002692 if (is_new)
2693 {
2694 rettv->v_type = VAR_DICT;
2695 rettv->vval.v_dict = d1;
2696 rettv->v_lock = FALSE;
2697 }
2698 else
2699 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002700 }
2701 }
2702 else
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002703 semsg(_(e_listdictarg), is_new ? "extendnew()" : "extend()");
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002704
2705theend:
2706 if (type != NULL)
2707 clear_type_list(&type_list);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002708}
2709
2710/*
2711 * "extend(list, list [, idx])" function
2712 * "extend(dict, dict [, action])" function
2713 */
2714 void
2715f_extend(typval_T *argvars, typval_T *rettv)
2716{
2717 char_u *errmsg = (char_u *)N_("extend() argument");
2718
2719 extend(argvars, rettv, errmsg, FALSE);
2720}
2721
2722/*
2723 * "extendnew(list, list [, idx])" function
2724 * "extendnew(dict, dict [, action])" function
2725 */
2726 void
2727f_extendnew(typval_T *argvars, typval_T *rettv)
2728{
2729 char_u *errmsg = (char_u *)N_("extendnew() argument");
2730
2731 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002732}
2733
2734/*
2735 * "insert()" function
2736 */
2737 void
2738f_insert(typval_T *argvars, typval_T *rettv)
2739{
2740 long before = 0;
2741 listitem_T *item;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002742 int error = FALSE;
2743
2744 if (argvars[0].v_type == VAR_BLOB)
2745 {
2746 int val, len;
2747 char_u *p;
2748
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002749 if (argvars[0].vval.v_blob == NULL)
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002750 {
2751 if (in_vim9script())
2752 emsg(_(e_cannot_add_to_null_blob));
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002753 return;
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002754 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002755
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002756 len = blob_len(argvars[0].vval.v_blob);
2757 if (argvars[2].v_type != VAR_UNKNOWN)
2758 {
2759 before = (long)tv_get_number_chk(&argvars[2], &error);
2760 if (error)
2761 return; // type error; errmsg already given
2762 if (before < 0 || before > len)
2763 {
2764 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2765 return;
2766 }
2767 }
2768 val = tv_get_number_chk(&argvars[1], &error);
2769 if (error)
2770 return;
2771 if (val < 0 || val > 255)
2772 {
2773 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2774 return;
2775 }
2776
2777 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
2778 return;
2779 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2780 mch_memmove(p + before + 1, p + before, (size_t)len - before);
2781 *(p + before) = val;
2782 ++argvars[0].vval.v_blob->bv_ga.ga_len;
2783
2784 copy_tv(&argvars[0], rettv);
2785 }
2786 else if (argvars[0].v_type != VAR_LIST)
2787 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002788 else
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002789 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002790 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002791
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002792 if (l == NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002793 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002794 if (in_vim9script())
2795 emsg(_(e_cannot_add_to_null_list));
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002796 }
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002797 else if (!value_check_lock(l->lv_lock,
2798 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002799 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002800 if (argvars[2].v_type != VAR_UNKNOWN)
2801 before = (long)tv_get_number_chk(&argvars[2], &error);
2802 if (error)
2803 return; // type error; errmsg already given
2804
2805 if (before == l->lv_len)
2806 item = NULL;
2807 else
2808 {
2809 item = list_find(l, before);
2810 if (item == NULL)
2811 {
2812 semsg(_(e_listidx), before);
2813 l = NULL;
2814 }
2815 }
2816 if (l != NULL)
2817 {
2818 (void)list_insert_tv(l, &argvars[1], item);
2819 copy_tv(&argvars[0], rettv);
2820 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002821 }
2822 }
2823}
2824
2825/*
2826 * "remove()" function
2827 */
2828 void
2829f_remove(typval_T *argvars, typval_T *rettv)
2830{
2831 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2832
2833 if (argvars[0].v_type == VAR_DICT)
2834 dict_remove(argvars, rettv, arg_errmsg);
2835 else if (argvars[0].v_type == VAR_BLOB)
2836 blob_remove(argvars, rettv);
2837 else if (argvars[0].v_type == VAR_LIST)
2838 list_remove(argvars, rettv, arg_errmsg);
2839 else
2840 semsg(_(e_listdictblobarg), "remove()");
2841}
2842
2843/*
2844 * "reverse({list})" function
2845 */
2846 void
2847f_reverse(typval_T *argvars, typval_T *rettv)
2848{
2849 list_T *l;
2850 listitem_T *li, *ni;
2851
2852 if (argvars[0].v_type == VAR_BLOB)
2853 {
2854 blob_T *b = argvars[0].vval.v_blob;
2855 int i, len = blob_len(b);
2856
2857 for (i = 0; i < len / 2; i++)
2858 {
2859 int tmp = blob_get(b, i);
2860
2861 blob_set(b, i, blob_get(b, len - i - 1));
2862 blob_set(b, len - i - 1, tmp);
2863 }
2864 rettv_blob_set(rettv, b);
2865 return;
2866 }
2867
2868 if (argvars[0].v_type != VAR_LIST)
2869 semsg(_(e_listblobarg), "reverse()");
2870 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002871 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002872 (char_u *)N_("reverse() argument"), TRUE))
2873 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002874 if (l->lv_first == &range_list_item)
2875 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002876 varnumber_T new_start = l->lv_u.nonmat.lv_start
2877 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2878 l->lv_u.nonmat.lv_end = new_start
2879 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2880 l->lv_u.nonmat.lv_start = new_start;
2881 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002882 rettv_list_set(rettv, l);
2883 return;
2884 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002885 li = l->lv_u.mat.lv_last;
2886 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002887 l->lv_len = 0;
2888 while (li != NULL)
2889 {
2890 ni = li->li_prev;
2891 list_append(l, li);
2892 li = ni;
2893 }
2894 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002895 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002896 }
2897}
2898
Bram Moolenaar85629982020-06-01 18:39:20 +02002899/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002900 * "reduce(list, { accumulator, element -> value } [, initial])" function
Bram Moolenaar85629982020-06-01 18:39:20 +02002901 */
2902 void
2903f_reduce(typval_T *argvars, typval_T *rettv)
2904{
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002905 typval_T initial;
Bram Moolenaar85629982020-06-01 18:39:20 +02002906 char_u *func_name;
2907 partial_T *partial = NULL;
2908 funcexe_T funcexe;
2909 typval_T argv[3];
2910
2911 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
2912 {
2913 emsg(_(e_listblobreq));
2914 return;
2915 }
2916
2917 if (argvars[1].v_type == VAR_FUNC)
2918 func_name = argvars[1].vval.v_string;
2919 else if (argvars[1].v_type == VAR_PARTIAL)
2920 {
2921 partial = argvars[1].vval.v_partial;
2922 func_name = partial_name(partial);
2923 }
2924 else
2925 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01002926 if (func_name == NULL || *func_name == NUL)
2927 {
2928 emsg(_(e_missing_function_argument));
2929 return;
2930 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002931
2932 vim_memset(&funcexe, 0, sizeof(funcexe));
2933 funcexe.evaluate = TRUE;
2934 funcexe.partial = partial;
2935
2936 if (argvars[0].v_type == VAR_LIST)
2937 {
2938 list_T *l = argvars[0].vval.v_list;
2939 listitem_T *li = NULL;
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002940 int r;
Bram Moolenaarca275a02020-06-24 22:07:46 +02002941 int called_emsg_start = called_emsg;
Bram Moolenaar85629982020-06-01 18:39:20 +02002942
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002943 if (l != NULL)
2944 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar85629982020-06-01 18:39:20 +02002945 if (argvars[2].v_type == VAR_UNKNOWN)
2946 {
2947 if (l == NULL || l->lv_first == NULL)
2948 {
2949 semsg(_(e_reduceempty), "List");
2950 return;
2951 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002952 initial = l->lv_first->li_tv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002953 li = l->lv_first->li_next;
2954 }
2955 else
2956 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002957 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002958 if (l != NULL)
2959 li = l->lv_first;
2960 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002961 copy_tv(&initial, rettv);
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002962
2963 if (l != NULL)
Bram Moolenaar85629982020-06-01 18:39:20 +02002964 {
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002965 int prev_locked = l->lv_lock;
2966
2967 l->lv_lock = VAR_FIXED; // disallow the list changing here
2968 for ( ; li != NULL; li = li->li_next)
2969 {
2970 argv[0] = *rettv;
2971 argv[1] = li->li_tv;
2972 rettv->v_type = VAR_UNKNOWN;
2973 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
2974 clear_tv(&argv[0]);
2975 if (r == FAIL || called_emsg != called_emsg_start)
2976 break;
2977 }
2978 l->lv_lock = prev_locked;
Bram Moolenaar85629982020-06-01 18:39:20 +02002979 }
2980 }
2981 else
2982 {
2983 blob_T *b = argvars[0].vval.v_blob;
2984 int i;
2985
2986 if (argvars[2].v_type == VAR_UNKNOWN)
2987 {
2988 if (b == NULL || b->bv_ga.ga_len == 0)
2989 {
2990 semsg(_(e_reduceempty), "Blob");
2991 return;
2992 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002993 initial.v_type = VAR_NUMBER;
2994 initial.vval.v_number = blob_get(b, 0);
Bram Moolenaar85629982020-06-01 18:39:20 +02002995 i = 1;
2996 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002997 else if (argvars[2].v_type != VAR_NUMBER)
2998 {
2999 emsg(_(e_number_exp));
3000 return;
3001 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003002 else
3003 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003004 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02003005 i = 0;
3006 }
3007
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003008 copy_tv(&initial, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003009 if (b != NULL)
3010 {
3011 for ( ; i < b->bv_ga.ga_len; i++)
3012 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003013 argv[0] = *rettv;
Bram Moolenaar85629982020-06-01 18:39:20 +02003014 argv[1].v_type = VAR_NUMBER;
3015 argv[1].vval.v_number = blob_get(b, i);
3016 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
3017 return;
Bram Moolenaar85629982020-06-01 18:39:20 +02003018 }
3019 }
3020 }
3021}
3022
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02003023#endif // defined(FEAT_EVAL)