blob: afbc7d395824fad62d6a59ad81561c1295ea4775 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarda861d62016-07-17 15:46:27 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar08c308a2019-09-04 17:48:15 +020011 * list.c: List support and container (List, Dict, Blob) functions.
Bram Moolenaarda861d62016-07-17 15:46:27 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar08c308a2019-09-04 17:48:15 +020018static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
19
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010020// List heads for garbage collection.
21static list_T *first_list = NULL; // list of all lists
Bram Moolenaarda861d62016-07-17 15:46:27 +020022
Bram Moolenaar00d253e2020-04-06 22:13:01 +020023#define FOR_ALL_WATCHERS(l, lw) \
24 for ((lw) = (l)->lv_watch; (lw) != NULL; (lw) = (lw)->lw_next)
25
Bram Moolenaarbdff0122020-04-05 18:56:05 +020026static void list_free_item(list_T *l, listitem_T *item);
27
Bram Moolenaarda861d62016-07-17 15:46:27 +020028/*
29 * Add a watcher to a list.
30 */
31 void
32list_add_watch(list_T *l, listwatch_T *lw)
33{
34 lw->lw_next = l->lv_watch;
35 l->lv_watch = lw;
36}
37
38/*
39 * Remove a watcher from a list.
40 * No warning when it isn't found...
41 */
42 void
43list_rem_watch(list_T *l, listwatch_T *lwrem)
44{
45 listwatch_T *lw, **lwp;
46
47 lwp = &l->lv_watch;
Bram Moolenaar00d253e2020-04-06 22:13:01 +020048 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020049 {
50 if (lw == lwrem)
51 {
52 *lwp = lw->lw_next;
53 break;
54 }
55 lwp = &lw->lw_next;
56 }
57}
58
59/*
60 * Just before removing an item from a list: advance watchers to the next
61 * item.
62 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020063 static void
Bram Moolenaarda861d62016-07-17 15:46:27 +020064list_fix_watch(list_T *l, listitem_T *item)
65{
66 listwatch_T *lw;
67
Bram Moolenaar00d253e2020-04-06 22:13:01 +020068 FOR_ALL_WATCHERS(l, lw)
Bram Moolenaarda861d62016-07-17 15:46:27 +020069 if (lw->lw_item == item)
70 lw->lw_item = item->li_next;
71}
72
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073 static void
74list_init(list_T *l)
75{
76 // Prepend the list to the list of lists for garbage collection.
77 if (first_list != NULL)
78 first_list->lv_used_prev = l;
79 l->lv_used_prev = NULL;
80 l->lv_used_next = first_list;
81 first_list = l;
82}
83
Bram Moolenaarda861d62016-07-17 15:46:27 +020084/*
85 * Allocate an empty header for a list.
86 * Caller should take care of the reference count.
87 */
88 list_T *
89list_alloc(void)
90{
91 list_T *l;
92
Bram Moolenaarc799fe22019-05-28 23:08:19 +020093 l = ALLOC_CLEAR_ONE(list_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +020094 if (l != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095 list_init(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +020096 return l;
97}
98
99/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100100 * list_alloc() with an ID for alloc_fail().
101 */
102 list_T *
103list_alloc_id(alloc_id_T id UNUSED)
104{
105#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200106 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100107 return NULL;
108#endif
109 return (list_alloc());
110}
111
112/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 * Allocate space for a list, plus "count" items.
114 * Next list_set_item() must be called for each item.
115 */
116 list_T *
117list_alloc_with_items(int count)
118{
119 list_T *l;
120
121 l = (list_T *)alloc_clear(sizeof(list_T) + count * sizeof(listitem_T));
122 if (l != NULL)
123 {
124 list_init(l);
125
126 if (count > 0)
127 {
128 listitem_T *li = (listitem_T *)(l + 1);
129 int i;
130
131 l->lv_len = count;
132 l->lv_with_items = count;
133 l->lv_first = li;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100134 l->lv_u.mat.lv_last = li + count - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100135 for (i = 0; i < count; ++i)
136 {
137 if (i == 0)
138 li->li_prev = NULL;
139 else
140 li->li_prev = li - 1;
141 if (i == count - 1)
142 li->li_next = NULL;
143 else
144 li->li_next = li + 1;
145 ++li;
146 }
147 }
148 }
149 return l;
150}
151
152/*
153 * Set item "idx" for a list previously allocated with list_alloc_with_items().
154 * The contents of "tv" is moved into the list item.
155 * Each item must be set exactly once.
156 */
157 void
158list_set_item(list_T *l, int idx, typval_T *tv)
159{
160 listitem_T *li = (listitem_T *)(l + 1) + idx;
161
162 li->li_tv = *tv;
163}
164
165/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200166 * Allocate an empty list for a return value, with reference count set.
167 * Returns OK or FAIL.
168 */
169 int
170rettv_list_alloc(typval_T *rettv)
171{
172 list_T *l = list_alloc();
173
174 if (l == NULL)
175 return FAIL;
176
Bram Moolenaarda861d62016-07-17 15:46:27 +0200177 rettv->v_lock = 0;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200178 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200179 return OK;
180}
181
182/*
Bram Moolenaar162b7142018-12-21 15:17:36 +0100183 * Same as rettv_list_alloc() but uses an allocation id for testing.
184 */
185 int
186rettv_list_alloc_id(typval_T *rettv, alloc_id_T id UNUSED)
187{
188#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +0200189 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaar162b7142018-12-21 15:17:36 +0100190 return FAIL;
191#endif
192 return rettv_list_alloc(rettv);
193}
194
195
196/*
Bram Moolenaare809a4e2019-07-04 17:35:05 +0200197 * Set a list as the return value. Increments the reference count.
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200198 */
199 void
200rettv_list_set(typval_T *rettv, list_T *l)
201{
202 rettv->v_type = VAR_LIST;
203 rettv->vval.v_list = l;
204 if (l != NULL)
205 ++l->lv_refcount;
206}
207
208/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200209 * Unreference a list: decrement the reference count and free it when it
210 * becomes zero.
211 */
212 void
213list_unref(list_T *l)
214{
215 if (l != NULL && --l->lv_refcount <= 0)
216 list_free(l);
217}
218
219/*
220 * Free a list, including all non-container items it points to.
221 * Ignores the reference count.
222 */
223 static void
224list_free_contents(list_T *l)
225{
226 listitem_T *item;
227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 if (l->lv_first != &range_list_item)
229 for (item = l->lv_first; item != NULL; item = l->lv_first)
230 {
231 // Remove the item before deleting it.
232 l->lv_first = item->li_next;
233 clear_tv(&item->li_tv);
234 list_free_item(l, item);
235 }
Bram Moolenaarda861d62016-07-17 15:46:27 +0200236}
237
238/*
239 * Go through the list of lists and free items without the copyID.
240 * But don't free a list that has a watcher (used in a for loop), these
241 * are not referenced anywhere.
242 */
243 int
244list_free_nonref(int copyID)
245{
246 list_T *ll;
247 int did_free = FALSE;
248
249 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
250 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
251 && ll->lv_watch == NULL)
252 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100253 // Free the List and ordinary items it contains, but don't recurse
254 // into Lists and Dictionaries, they will be in the list of dicts
255 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200256 list_free_contents(ll);
257 did_free = TRUE;
258 }
259 return did_free;
260}
261
262 static void
263list_free_list(list_T *l)
264{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100265 // Remove the list from the list of lists for garbage collection.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200266 if (l->lv_used_prev == NULL)
267 first_list = l->lv_used_next;
268 else
269 l->lv_used_prev->lv_used_next = l->lv_used_next;
270 if (l->lv_used_next != NULL)
271 l->lv_used_next->lv_used_prev = l->lv_used_prev;
272
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100273 free_type(l->lv_type);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200274 vim_free(l);
275}
276
277 void
278list_free_items(int copyID)
279{
280 list_T *ll, *ll_next;
281
282 for (ll = first_list; ll != NULL; ll = ll_next)
283 {
284 ll_next = ll->lv_used_next;
285 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
286 && ll->lv_watch == NULL)
287 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100288 // Free the List and ordinary items it contains, but don't recurse
289 // into Lists and Dictionaries, they will be in the list of dicts
290 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200291 list_free_list(ll);
292 }
293 }
294}
295
296 void
297list_free(list_T *l)
298{
299 if (!in_free_unref_items)
300 {
301 list_free_contents(l);
302 list_free_list(l);
303 }
304}
305
306/*
307 * Allocate a list item.
308 * It is not initialized, don't forget to set v_lock.
309 */
310 listitem_T *
311listitem_alloc(void)
312{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200313 return ALLOC_ONE(listitem_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200314}
315
316/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100317 * Free a list item, unless it was allocated together with the list itself.
318 * Does not clear the value. Does not notify watchers.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200319 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200320 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100321list_free_item(list_T *l, listitem_T *item)
322{
323 if (l->lv_with_items == 0 || item < (listitem_T *)l
324 || item >= (listitem_T *)(l + 1) + l->lv_with_items)
325 vim_free(item);
326}
327
328/*
329 * Free a list item, unless it was allocated together with the list itself.
330 * Also clears the value. Does not notify watchers.
331 */
332 void
333listitem_free(list_T *l, listitem_T *item)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200334{
335 clear_tv(&item->li_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 list_free_item(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200337}
338
339/*
340 * Remove a list item from a List and free it. Also clears the value.
341 */
342 void
343listitem_remove(list_T *l, listitem_T *item)
344{
345 vimlist_remove(l, item, item);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346 listitem_free(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200347}
348
349/*
350 * Get the number of items in a list.
351 */
352 long
353list_len(list_T *l)
354{
355 if (l == NULL)
356 return 0L;
357 return l->lv_len;
358}
359
360/*
361 * Return TRUE when two lists have exactly the same values.
362 */
363 int
364list_equal(
365 list_T *l1,
366 list_T *l2,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100367 int ic, // ignore case for strings
368 int recursive) // TRUE when used recursively
Bram Moolenaarda861d62016-07-17 15:46:27 +0200369{
370 listitem_T *item1, *item2;
371
Bram Moolenaarda861d62016-07-17 15:46:27 +0200372 if (l1 == l2)
373 return TRUE;
374 if (list_len(l1) != list_len(l2))
375 return FALSE;
Bram Moolenaar7b293c72020-04-09 21:33:22 +0200376 if (list_len(l1) == 0)
377 // empty and NULL list are considered equal
378 return TRUE;
379 if (l1 == NULL || l2 == NULL)
380 return FALSE;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200381
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200382 CHECK_LIST_MATERIALIZE(l1);
383 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384
Bram Moolenaarda861d62016-07-17 15:46:27 +0200385 for (item1 = l1->lv_first, item2 = l2->lv_first;
386 item1 != NULL && item2 != NULL;
387 item1 = item1->li_next, item2 = item2->li_next)
388 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
389 return FALSE;
390 return item1 == NULL && item2 == NULL;
391}
392
393/*
394 * Locate item with index "n" in list "l" and return it.
395 * A negative index is counted from the end; -1 is the last item.
396 * Returns NULL when "n" is out of range.
397 */
398 listitem_T *
399list_find(list_T *l, long n)
400{
401 listitem_T *item;
402 long idx;
403
404 if (l == NULL)
405 return NULL;
406
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100407 // Negative index is relative to the end.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200408 if (n < 0)
409 n = l->lv_len + n;
410
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100411 // Check for index out of range.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200412 if (n < 0 || n >= l->lv_len)
413 return NULL;
414
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200415 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100416
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100417 // When there is a cached index may start search from there.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100418 if (l->lv_u.mat.lv_idx_item != NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200419 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100420 if (n < l->lv_u.mat.lv_idx / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200421 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100422 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200423 item = l->lv_first;
424 idx = 0;
425 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100426 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200427 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100428 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100429 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200430 idx = l->lv_len - 1;
431 }
432 else
433 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100434 // closest to the cached index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100435 item = l->lv_u.mat.lv_idx_item;
436 idx = l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200437 }
438 }
439 else
440 {
441 if (n < l->lv_len / 2)
442 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100443 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200444 item = l->lv_first;
445 idx = 0;
446 }
447 else
448 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100449 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100450 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200451 idx = l->lv_len - 1;
452 }
453 }
454
455 while (n > idx)
456 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100457 // search forward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200458 item = item->li_next;
459 ++idx;
460 }
461 while (n < idx)
462 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100463 // search backward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200464 item = item->li_prev;
465 --idx;
466 }
467
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100468 // cache the used index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100469 l->lv_u.mat.lv_idx = idx;
470 l->lv_u.mat.lv_idx_item = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200471
472 return item;
473}
474
475/*
476 * Get list item "l[idx]" as a number.
477 */
478 long
479list_find_nr(
480 list_T *l,
481 long idx,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100482 int *errorp) // set to TRUE when something wrong
Bram Moolenaarda861d62016-07-17 15:46:27 +0200483{
484 listitem_T *li;
485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486 if (l != NULL && l->lv_first == &range_list_item)
487 {
488 long n = idx;
489
490 // not materialized range() list: compute the value.
491 // Negative index is relative to the end.
492 if (n < 0)
493 n = l->lv_len + n;
494
495 // Check for index out of range.
496 if (n < 0 || n >= l->lv_len)
497 {
498 if (errorp != NULL)
499 *errorp = TRUE;
500 return -1L;
501 }
502
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100503 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 }
505
Bram Moolenaarda861d62016-07-17 15:46:27 +0200506 li = list_find(l, idx);
507 if (li == NULL)
508 {
509 if (errorp != NULL)
510 *errorp = TRUE;
511 return -1L;
512 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100513 return (long)tv_get_number_chk(&li->li_tv, errorp);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200514}
515
516/*
517 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
518 */
519 char_u *
520list_find_str(list_T *l, long idx)
521{
522 listitem_T *li;
523
524 li = list_find(l, idx - 1);
525 if (li == NULL)
526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 semsg(_(e_listidx), idx);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200528 return NULL;
529 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100530 return tv_get_string(&li->li_tv);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200531}
532
533/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100534 * Like list_find() but when a negative index is used that is not found use
535 * zero and set "idx" to zero. Used for first index of a range.
536 */
537 listitem_T *
538list_find_index(list_T *l, long *idx)
539{
540 listitem_T *li = list_find(l, *idx);
541
542 if (li == NULL)
543 {
544 if (*idx < 0)
545 {
546 *idx = 0;
547 li = list_find(l, *idx);
548 }
549 }
550 return li;
551}
552
553/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200554 * Locate "item" list "l" and return its index.
555 * Returns -1 when "item" is not in the list.
556 */
557 long
558list_idx_of_item(list_T *l, listitem_T *item)
559{
560 long idx = 0;
561 listitem_T *li;
562
563 if (l == NULL)
564 return -1;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200565 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200566 idx = 0;
567 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
568 ++idx;
569 if (li == NULL)
570 return -1;
571 return idx;
572}
573
574/*
575 * Append item "item" to the end of list "l".
576 */
577 void
578list_append(list_T *l, listitem_T *item)
579{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200580 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100581 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200582 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100583 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200584 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100585 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200586 item->li_prev = NULL;
587 }
588 else
589 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100590 l->lv_u.mat.lv_last->li_next = item;
591 item->li_prev = l->lv_u.mat.lv_last;
592 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200593 }
594 ++l->lv_len;
595 item->li_next = NULL;
596}
597
598/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200600 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200601 */
602 int
603list_append_tv(list_T *l, typval_T *tv)
604{
Bram Moolenaar90fba562021-07-09 19:17:55 +0200605 listitem_T *li;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200606
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200607 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200608 && check_typval_arg_type(l->lv_type->tt_member, tv,
609 NULL, 0) == FAIL)
Bram Moolenaarf32f0992021-07-08 20:53:40 +0200610 return FAIL;
Bram Moolenaar90fba562021-07-09 19:17:55 +0200611 li = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200612 if (li == NULL)
613 return FAIL;
614 copy_tv(tv, &li->li_tv);
615 list_append(l, li);
616 return OK;
617}
618
619/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620 * As list_append_tv() but move the value instead of copying it.
621 * Return FAIL when out of memory.
622 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200623 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624list_append_tv_move(list_T *l, typval_T *tv)
625{
626 listitem_T *li = listitem_alloc();
627
628 if (li == NULL)
629 return FAIL;
630 li->li_tv = *tv;
631 list_append(l, li);
632 return OK;
633}
634
635/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200636 * Add a dictionary to a list. Used by getqflist().
637 * Return FAIL when out of memory.
638 */
639 int
640list_append_dict(list_T *list, dict_T *dict)
641{
642 listitem_T *li = listitem_alloc();
643
644 if (li == NULL)
645 return FAIL;
646 li->li_tv.v_type = VAR_DICT;
647 li->li_tv.v_lock = 0;
648 li->li_tv.vval.v_dict = dict;
649 list_append(list, li);
650 ++dict->dv_refcount;
651 return OK;
652}
653
654/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100655 * Append list2 to list1.
656 * Return FAIL when out of memory.
657 */
658 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200659list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100660{
661 listitem_T *li = listitem_alloc();
662
663 if (li == NULL)
664 return FAIL;
665 li->li_tv.v_type = VAR_LIST;
666 li->li_tv.v_lock = 0;
667 li->li_tv.vval.v_list = list2;
668 list_append(list1, li);
669 ++list2->lv_refcount;
670 return OK;
671}
672
673/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200674 * Make a copy of "str" and append it as an item to list "l".
675 * When "len" >= 0 use "str[len]".
676 * Returns FAIL when out of memory.
677 */
678 int
679list_append_string(list_T *l, char_u *str, int len)
680{
681 listitem_T *li = listitem_alloc();
682
683 if (li == NULL)
684 return FAIL;
685 list_append(l, li);
686 li->li_tv.v_type = VAR_STRING;
687 li->li_tv.v_lock = 0;
688 if (str == NULL)
689 li->li_tv.vval.v_string = NULL;
690 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
691 : vim_strsave(str))) == NULL)
692 return FAIL;
693 return OK;
694}
695
696/*
697 * Append "n" to list "l".
698 * Returns FAIL when out of memory.
699 */
700 int
701list_append_number(list_T *l, varnumber_T n)
702{
703 listitem_T *li;
704
705 li = listitem_alloc();
706 if (li == NULL)
707 return FAIL;
708 li->li_tv.v_type = VAR_NUMBER;
709 li->li_tv.v_lock = 0;
710 li->li_tv.vval.v_number = n;
711 list_append(l, li);
712 return OK;
713}
714
715/*
716 * Insert typval_T "tv" in list "l" before "item".
717 * If "item" is NULL append at the end.
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100718 * Return FAIL when out of memory or the type is wrong.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200719 */
720 int
721list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
722{
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100723 listitem_T *ni;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200724
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100725 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200726 && check_typval_arg_type(l->lv_type->tt_member, tv,
727 NULL, 0) == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100728 return FAIL;
729 ni = listitem_alloc();
Bram Moolenaarda861d62016-07-17 15:46:27 +0200730 if (ni == NULL)
731 return FAIL;
732 copy_tv(tv, &ni->li_tv);
733 list_insert(l, ni, item);
734 return OK;
735}
736
737 void
738list_insert(list_T *l, listitem_T *ni, listitem_T *item)
739{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200740 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200741 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100742 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200743 list_append(l, ni);
744 else
745 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100746 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200747 ni->li_prev = item->li_prev;
748 ni->li_next = item;
749 if (item->li_prev == NULL)
750 {
751 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100752 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200753 }
754 else
755 {
756 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100757 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200758 }
759 item->li_prev = ni;
760 ++l->lv_len;
761 }
762}
763
764/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200765 * Flatten "list" to depth "maxdepth".
766 * It does nothing if "maxdepth" is 0.
767 * Returns FAIL when out of memory.
768 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100769 static void
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200770list_flatten(list_T *list, long maxdepth)
771{
772 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200773 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200774 int n;
775
776 if (maxdepth == 0)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100777 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200778 CHECK_LIST_MATERIALIZE(list);
779
780 n = 0;
781 item = list->lv_first;
782 while (item != NULL)
783 {
784 fast_breakcheck();
785 if (got_int)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100786 return;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200787
788 if (item->li_tv.v_type == VAR_LIST)
789 {
790 listitem_T *next = item->li_next;
791
792 vimlist_remove(list, item, item);
793 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
Bram Moolenaar3b690062021-02-01 20:14:51 +0100794 return;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200795 clear_tv(&item->li_tv);
796 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200797
798 if (item->li_prev == NULL)
799 item = list->lv_first;
800 else
801 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200802 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200803
804 if (++n >= maxdepth)
805 {
806 n = 0;
807 item = next;
808 }
809 }
810 else
811 {
812 n = 0;
813 item = item->li_next;
814 }
815 }
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200816}
817
818/*
Bram Moolenaar3b690062021-02-01 20:14:51 +0100819 * "flatten()" and "flattennew()" functions
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200820 */
Bram Moolenaar3b690062021-02-01 20:14:51 +0100821 static void
822flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200823{
824 list_T *l;
825 long maxdepth;
826 int error = FALSE;
827
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200828 if (in_vim9script()
829 && (check_for_list_arg(argvars, 0) == FAIL
830 || check_for_opt_number_arg(argvars, 1) == FAIL))
831 return;
832
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200833 if (argvars[0].v_type != VAR_LIST)
834 {
835 semsg(_(e_listarg), "flatten()");
836 return;
837 }
838
839 if (argvars[1].v_type == VAR_UNKNOWN)
840 maxdepth = 999999;
841 else
842 {
843 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
844 if (error)
845 return;
846 if (maxdepth < 0)
847 {
848 emsg(_("E900: maxdepth must be non-negative number"));
849 return;
850 }
851 }
852
853 l = argvars[0].vval.v_list;
Bram Moolenaar3b690062021-02-01 20:14:51 +0100854 rettv->v_type = VAR_LIST;
855 rettv->vval.v_list = l;
856 if (l == NULL)
857 return;
858
859 if (make_copy)
860 {
861 l = list_copy(l, TRUE, get_copyID());
862 rettv->vval.v_list = l;
863 if (l == NULL)
864 return;
865 }
866 else
867 {
868 if (value_check_lock(l->lv_lock,
869 (char_u *)N_("flatten() argument"), TRUE))
870 return;
871 ++l->lv_refcount;
872 }
873
874 list_flatten(l, maxdepth);
875}
876
877/*
878 * "flatten(list[, {maxdepth}])" function
879 */
880 void
881f_flatten(typval_T *argvars, typval_T *rettv)
882{
883 if (in_vim9script())
884 emsg(_(e_cannot_use_flatten_in_vim9_script));
885 else
886 flatten_common(argvars, rettv, FALSE);
887}
888
889/*
890 * "flattennew(list[, {maxdepth}])" function
891 */
892 void
893f_flattennew(typval_T *argvars, typval_T *rettv)
894{
895 flatten_common(argvars, rettv, TRUE);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200896}
897
898/*
Bram Moolenaar1a739232020-10-10 15:37:58 +0200899 * Extend "l1" with "l2". "l1" must not be NULL.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200900 * If "bef" is NULL append at the end, otherwise insert before this item.
901 * Returns FAIL when out of memory.
902 */
903 int
904list_extend(list_T *l1, list_T *l2, listitem_T *bef)
905{
906 listitem_T *item;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200907 int todo;
Bram Moolenaardcae51f2021-04-08 20:10:10 +0200908 listitem_T *bef_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200909
Bram Moolenaar1a739232020-10-10 15:37:58 +0200910 // NULL list is equivalent to an empty list: nothing to do.
911 if (l2 == NULL || l2->lv_len == 0)
912 return OK;
913
914 todo = l2->lv_len;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200915 CHECK_LIST_MATERIALIZE(l1);
916 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917
Bram Moolenaardcae51f2021-04-08 20:10:10 +0200918 // When exending a list with itself, at some point we run into the item
919 // that was before "bef" and need to skip over the already inserted items
920 // to "bef".
921 bef_prev = bef == NULL ? NULL : bef->li_prev;
922
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100923 // We also quit the loop when we have inserted the original item count of
924 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaardcae51f2021-04-08 20:10:10 +0200925 for (item = l2->lv_first; item != NULL && --todo >= 0;
926 item = item == bef_prev ? bef : item->li_next)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200927 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
928 return FAIL;
929 return OK;
930}
931
932/*
933 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
934 * Return FAIL when out of memory.
935 */
936 int
937list_concat(list_T *l1, list_T *l2, typval_T *tv)
938{
939 list_T *l;
940
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100941 // make a copy of the first list.
Bram Moolenaar1a739232020-10-10 15:37:58 +0200942 if (l1 == NULL)
943 l = list_alloc();
944 else
945 l = list_copy(l1, FALSE, 0);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200946 if (l == NULL)
947 return FAIL;
948 tv->v_type = VAR_LIST;
Bram Moolenaar9681f712020-11-21 13:58:50 +0100949 tv->v_lock = 0;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200950 tv->vval.v_list = l;
Bram Moolenaar1a739232020-10-10 15:37:58 +0200951 if (l1 == NULL)
952 ++l->lv_refcount;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200953
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100954 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200955 return list_extend(l, l2, NULL);
956}
957
Bram Moolenaar9af78762020-06-16 11:34:42 +0200958 list_T *
959list_slice(list_T *ol, long n1, long n2)
960{
961 listitem_T *item;
962 list_T *l = list_alloc();
963
964 if (l == NULL)
965 return NULL;
966 for (item = list_find(ol, n1); n1 <= n2; ++n1)
967 {
968 if (list_append_tv(l, &item->li_tv) == FAIL)
969 {
970 list_free(l);
971 return NULL;
972 }
973 item = item->li_next;
974 }
975 return l;
976}
977
Bram Moolenaared591872020-08-15 22:14:53 +0200978 int
979list_slice_or_index(
980 list_T *list,
981 int range,
Bram Moolenaar6601b622021-01-13 21:47:15 +0100982 varnumber_T n1_arg,
983 varnumber_T n2_arg,
984 int exclusive,
Bram Moolenaared591872020-08-15 22:14:53 +0200985 typval_T *rettv,
986 int verbose)
987{
988 long len = list_len(list);
Bram Moolenaar6601b622021-01-13 21:47:15 +0100989 varnumber_T n1 = n1_arg;
990 varnumber_T n2 = n2_arg;
Bram Moolenaared591872020-08-15 22:14:53 +0200991 typval_T var1;
992
993 if (n1 < 0)
994 n1 = len + n1;
995 if (n1 < 0 || n1 >= len)
996 {
997 // For a range we allow invalid values and return an empty
998 // list. A list index out of range is an error.
999 if (!range)
1000 {
1001 if (verbose)
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001002 semsg(_(e_listidx), (long)n1_arg);
Bram Moolenaared591872020-08-15 22:14:53 +02001003 return FAIL;
1004 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001005 n1 = n1 < 0 ? 0 : len;
Bram Moolenaared591872020-08-15 22:14:53 +02001006 }
1007 if (range)
1008 {
1009 list_T *l;
1010
1011 if (n2 < 0)
1012 n2 = len + n2;
1013 else if (n2 >= len)
Bram Moolenaar6601b622021-01-13 21:47:15 +01001014 n2 = len - (exclusive ? 0 : 1);
1015 if (exclusive)
1016 --n2;
Bram Moolenaared591872020-08-15 22:14:53 +02001017 if (n2 < 0 || n2 + 1 < n1)
1018 n2 = -1;
1019 l = list_slice(list, n1, n2);
1020 if (l == NULL)
1021 return FAIL;
1022 clear_tv(rettv);
1023 rettv_list_set(rettv, l);
1024 }
1025 else
1026 {
1027 // copy the item to "var1" to avoid that freeing the list makes it
1028 // invalid.
1029 copy_tv(&list_find(list, n1)->li_tv, &var1);
1030 clear_tv(rettv);
1031 *rettv = var1;
1032 }
1033 return OK;
1034}
1035
Bram Moolenaarda861d62016-07-17 15:46:27 +02001036/*
1037 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1038 * The refcount of the new list is set to 1.
1039 * See item_copy() for "copyID".
1040 * Returns NULL when out of memory.
1041 */
1042 list_T *
1043list_copy(list_T *orig, int deep, int copyID)
1044{
1045 list_T *copy;
1046 listitem_T *item;
1047 listitem_T *ni;
1048
1049 if (orig == NULL)
1050 return NULL;
1051
1052 copy = list_alloc();
1053 if (copy != NULL)
1054 {
1055 if (copyID != 0)
1056 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001057 // Do this before adding the items, because one of the items may
1058 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001059 orig->lv_copyID = copyID;
1060 orig->lv_copylist = copy;
1061 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001062 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001063 for (item = orig->lv_first; item != NULL && !got_int;
1064 item = item->li_next)
1065 {
1066 ni = listitem_alloc();
1067 if (ni == NULL)
1068 break;
1069 if (deep)
1070 {
1071 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
1072 {
1073 vim_free(ni);
1074 break;
1075 }
1076 }
1077 else
1078 copy_tv(&item->li_tv, &ni->li_tv);
1079 list_append(copy, ni);
1080 }
1081 ++copy->lv_refcount;
1082 if (item != NULL)
1083 {
1084 list_unref(copy);
1085 copy = NULL;
1086 }
1087 }
1088
1089 return copy;
1090}
1091
1092/*
1093 * Remove items "item" to "item2" from list "l".
1094 * Does not free the listitem or the value!
1095 * This used to be called list_remove, but that conflicts with a Sun header
1096 * file.
1097 */
1098 void
1099vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1100{
1101 listitem_T *ip;
1102
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001103 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001105 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001106 for (ip = item; ip != NULL; ip = ip->li_next)
1107 {
1108 --l->lv_len;
1109 list_fix_watch(l, ip);
1110 if (ip == item2)
1111 break;
1112 }
1113
1114 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001115 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001116 else
1117 item2->li_next->li_prev = item->li_prev;
1118 if (item->li_prev == NULL)
1119 l->lv_first = item2->li_next;
1120 else
1121 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001122 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001123}
1124
1125/*
1126 * Return an allocated string with the string representation of a list.
1127 * May return NULL.
1128 */
1129 char_u *
1130list2string(typval_T *tv, int copyID, int restore_copyID)
1131{
1132 garray_T ga;
1133
1134 if (tv->vval.v_list == NULL)
1135 return NULL;
1136 ga_init2(&ga, (int)sizeof(char), 80);
1137 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001138 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001139 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1140 FALSE, restore_copyID, copyID) == FAIL)
1141 {
1142 vim_free(ga.ga_data);
1143 return NULL;
1144 }
1145 ga_append(&ga, ']');
1146 ga_append(&ga, NUL);
1147 return (char_u *)ga.ga_data;
1148}
1149
1150typedef struct join_S {
1151 char_u *s;
1152 char_u *tofree;
1153} join_T;
1154
1155 static int
1156list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001157 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001158 list_T *l,
1159 char_u *sep,
1160 int echo_style,
1161 int restore_copyID,
1162 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001163 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001164{
1165 int i;
1166 join_T *p;
1167 int len;
1168 int sumlen = 0;
1169 int first = TRUE;
1170 char_u *tofree;
1171 char_u numbuf[NUMBUFLEN];
1172 listitem_T *item;
1173 char_u *s;
1174
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001175 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001176 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001177 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1178 {
1179 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001180 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001181 if (s == NULL)
1182 return FAIL;
1183
1184 len = (int)STRLEN(s);
1185 sumlen += len;
1186
1187 (void)ga_grow(join_gap, 1);
1188 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1189 if (tofree != NULL || s != numbuf)
1190 {
1191 p->s = s;
1192 p->tofree = tofree;
1193 }
1194 else
1195 {
1196 p->s = vim_strnsave(s, len);
1197 p->tofree = p->s;
1198 }
1199
1200 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001201 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001202 break;
1203 }
1204
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001205 // Allocate result buffer with its total size, avoid re-allocation and
1206 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001207 if (join_gap->ga_len >= 2)
1208 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1209 if (ga_grow(gap, sumlen + 2) == FAIL)
1210 return FAIL;
1211
1212 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1213 {
1214 if (first)
1215 first = FALSE;
1216 else
1217 ga_concat(gap, sep);
1218 p = ((join_T *)join_gap->ga_data) + i;
1219
1220 if (p->s != NULL)
1221 ga_concat(gap, p->s);
1222 line_breakcheck();
1223 }
1224
1225 return OK;
1226}
1227
1228/*
1229 * Join list "l" into a string in "*gap", using separator "sep".
1230 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1231 * Return FAIL or OK.
1232 */
1233 int
1234list_join(
1235 garray_T *gap,
1236 list_T *l,
1237 char_u *sep,
1238 int echo_style,
1239 int restore_copyID,
1240 int copyID)
1241{
1242 garray_T join_ga;
1243 int retval;
1244 join_T *p;
1245 int i;
1246
1247 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001248 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001249 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1250 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1251 copyID, &join_ga);
1252
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001253 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001254 if (join_ga.ga_data != NULL)
1255 {
1256 p = (join_T *)join_ga.ga_data;
1257 for (i = 0; i < join_ga.ga_len; ++i)
1258 {
1259 vim_free(p->tofree);
1260 ++p;
1261 }
1262 ga_clear(&join_ga);
1263 }
1264
1265 return retval;
1266}
1267
1268/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001269 * "join()" function
1270 */
1271 void
1272f_join(typval_T *argvars, typval_T *rettv)
1273{
1274 garray_T ga;
1275 char_u *sep;
1276
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001277 if (in_vim9script()
1278 && (check_for_list_arg(argvars, 0) == FAIL
1279 || check_for_opt_string_arg(argvars, 1) == FAIL))
1280 return;
1281
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001282 if (argvars[0].v_type != VAR_LIST)
1283 {
1284 emsg(_(e_listreq));
1285 return;
1286 }
1287 if (argvars[0].vval.v_list == NULL)
1288 return;
1289 if (argvars[1].v_type == VAR_UNKNOWN)
1290 sep = (char_u *)" ";
1291 else
1292 sep = tv_get_string_chk(&argvars[1]);
1293
1294 rettv->v_type = VAR_STRING;
1295
1296 if (sep != NULL)
1297 {
1298 ga_init2(&ga, (int)sizeof(char), 80);
1299 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1300 ga_append(&ga, NUL);
1301 rettv->vval.v_string = (char_u *)ga.ga_data;
1302 }
1303 else
1304 rettv->vval.v_string = NULL;
1305}
1306
1307/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001308 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001309 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001310 * Return OK or FAIL.
1311 */
1312 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001313eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001314{
Bram Moolenaar71478202020-06-26 22:46:27 +02001315 int evaluate = evalarg == NULL ? FALSE
1316 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001317 list_T *l = NULL;
1318 typval_T tv;
1319 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001320 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001321 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001322
1323 if (evaluate)
1324 {
1325 l = list_alloc();
1326 if (l == NULL)
1327 return FAIL;
1328 }
1329
Bram Moolenaar962d7212020-07-04 14:15:00 +02001330 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001331 while (**arg != ']' && **arg != NUL)
1332 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001333 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001334 goto failret;
1335 if (evaluate)
1336 {
1337 item = listitem_alloc();
1338 if (item != NULL)
1339 {
1340 item->li_tv = tv;
1341 item->li_tv.v_lock = 0;
1342 list_append(l, item);
1343 }
1344 else
1345 clear_tv(&tv);
1346 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001347 // Legacy Vim script allowed a space before the comma.
1348 if (!vim9script)
1349 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001350
Bram Moolenaare6e03172020-06-27 16:36:05 +02001351 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001352 had_comma = **arg == ',';
1353 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001354 {
Bram Moolenaar659bb222020-11-12 20:16:39 +01001355 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
Bram Moolenaare6e03172020-06-27 16:36:05 +02001356 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001357 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001358 goto failret;
1359 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001360 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001361 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001362
Bram Moolenaare6b53242020-07-01 17:28:33 +02001363 // The "]" can be on the next line. But a double quoted string may
1364 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001365 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001366 if (**arg == ']')
1367 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001368
1369 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001370 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001372 {
1373 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001374 semsg(_(e_no_white_space_allowed_before_str_str),
1375 ",", *arg);
Bram Moolenaardb199212020-08-12 18:01:53 +02001376 else
1377 semsg(_("E696: Missing comma in List: %s"), *arg);
1378 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001379 goto failret;
1380 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001381 }
1382
1383 if (**arg != ']')
1384 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001386 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001387failret:
1388 if (evaluate)
1389 list_free(l);
1390 return FAIL;
1391 }
1392
Bram Moolenaar9d489562020-07-30 20:08:50 +02001393 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001394 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001395 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001396
1397 return OK;
1398}
1399
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001400/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001401 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001402 */
1403 int
1404write_list(FILE *fd, list_T *list, int binary)
1405{
1406 listitem_T *li;
1407 int c;
1408 int ret = OK;
1409 char_u *s;
1410
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001411 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001412 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001413 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001414 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001415 {
1416 if (*s == '\n')
1417 c = putc(NUL, fd);
1418 else
1419 c = putc(*s, fd);
1420 if (c == EOF)
1421 {
1422 ret = FAIL;
1423 break;
1424 }
1425 }
1426 if (!binary || li->li_next != NULL)
1427 if (putc('\n', fd) == EOF)
1428 {
1429 ret = FAIL;
1430 break;
1431 }
1432 if (ret == FAIL)
1433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001434 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001435 break;
1436 }
1437 }
1438 return ret;
1439}
1440
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001441/*
1442 * Initialize a static list with 10 items.
1443 */
1444 void
1445init_static_list(staticList10_T *sl)
1446{
1447 list_T *l = &sl->sl_list;
1448 int i;
1449
1450 memset(sl, 0, sizeof(staticList10_T));
1451 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001452 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001453 l->lv_refcount = DO_NOT_FREE_CNT;
1454 l->lv_lock = VAR_FIXED;
1455 sl->sl_list.lv_len = 10;
1456
1457 for (i = 0; i < 10; ++i)
1458 {
1459 listitem_T *li = &sl->sl_items[i];
1460
1461 if (i == 0)
1462 li->li_prev = NULL;
1463 else
1464 li->li_prev = li - 1;
1465 if (i == 9)
1466 li->li_next = NULL;
1467 else
1468 li->li_next = li + 1;
1469 }
1470}
1471
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001472/*
1473 * "list2str()" function
1474 */
1475 void
1476f_list2str(typval_T *argvars, typval_T *rettv)
1477{
1478 list_T *l;
1479 listitem_T *li;
1480 garray_T ga;
1481 int utf8 = FALSE;
1482
1483 rettv->v_type = VAR_STRING;
1484 rettv->vval.v_string = NULL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001485
1486 if (in_vim9script()
1487 && (check_for_list_arg(argvars, 0) == FAIL
1488 || check_for_opt_bool_arg(argvars, 1) == FAIL))
1489 return;
1490
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001491 if (argvars[0].v_type != VAR_LIST)
1492 {
1493 emsg(_(e_invarg));
1494 return;
1495 }
1496
1497 l = argvars[0].vval.v_list;
1498 if (l == NULL)
1499 return; // empty list results in empty string
1500
1501 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaara48f7862020-09-05 20:16:57 +02001502 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001503
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001504 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001505 ga_init2(&ga, 1, 80);
1506 if (has_mbyte || utf8)
1507 {
1508 char_u buf[MB_MAXBYTES + 1];
1509 int (*char2bytes)(int, char_u *);
1510
1511 if (utf8 || enc_utf8)
1512 char2bytes = utf_char2bytes;
1513 else
1514 char2bytes = mb_char2bytes;
1515
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001516 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001517 {
1518 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1519 ga_concat(&ga, buf);
1520 }
1521 ga_append(&ga, NUL);
1522 }
1523 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1524 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001525 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001526 ga_append(&ga, tv_get_number(&li->li_tv));
1527 ga_append(&ga, NUL);
1528 }
1529
1530 rettv->v_type = VAR_STRING;
1531 rettv->vval.v_string = ga.ga_data;
1532}
1533
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001534 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001535list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1536{
1537 list_T *l;
1538 listitem_T *item, *item2;
1539 listitem_T *li;
1540 int error = FALSE;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001541 long idx;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001542
1543 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001544 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001545 return;
1546
1547 idx = (long)tv_get_number_chk(&argvars[1], &error);
1548 if (error)
1549 ; // type error: do nothing, errmsg already given
1550 else if ((item = list_find(l, idx)) == NULL)
1551 semsg(_(e_listidx), idx);
1552 else
1553 {
1554 if (argvars[2].v_type == VAR_UNKNOWN)
1555 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001556 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001557 vimlist_remove(l, item, item);
1558 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001560 }
1561 else
1562 {
1563 // Remove range of items, return list with values.
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001564 long end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001565
1566 if (error)
1567 ; // type error: do nothing
1568 else if ((item2 = list_find(l, end)) == NULL)
1569 semsg(_(e_listidx), end);
1570 else
1571 {
1572 int cnt = 0;
1573
1574 for (li = item; li != NULL; li = li->li_next)
1575 {
1576 ++cnt;
1577 if (li == item2)
1578 break;
1579 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001580 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar108010a2021-06-27 22:03:33 +02001581 emsg(_(e_invalid_range));
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001582 else
1583 {
1584 vimlist_remove(l, item, item2);
1585 if (rettv_list_alloc(rettv) == OK)
1586 {
Bram Moolenaar885971e2021-07-18 22:25:29 +02001587 list_T *rl = rettv->vval.v_list;
1588
1589 if (l->lv_with_items > 0)
1590 {
1591 // need to copy the list items and move the value
1592 while (item != NULL)
1593 {
1594 li = listitem_alloc();
1595 if (li == NULL)
1596 return;
1597 li->li_tv = item->li_tv;
1598 init_tv(&item->li_tv);
1599 list_append(rl, li);
1600 if (item == item2)
1601 break;
1602 item = item->li_next;
1603 }
1604 }
1605 else
1606 {
1607 rl->lv_first = item;
1608 rl->lv_u.mat.lv_last = item2;
1609 item->li_prev = NULL;
1610 item2->li_next = NULL;
1611 rl->lv_len = cnt;
1612 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001613 }
1614 }
1615 }
1616 }
1617 }
1618}
1619
1620static int item_compare(const void *s1, const void *s2);
1621static int item_compare2(const void *s1, const void *s2);
1622
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001623// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001624typedef struct
1625{
1626 listitem_T *item;
1627 int idx;
1628} sortItem_T;
1629
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001630// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001631typedef struct
1632{
1633 int item_compare_ic;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001634 int item_compare_lc;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001635 int item_compare_numeric;
1636 int item_compare_numbers;
1637#ifdef FEAT_FLOAT
1638 int item_compare_float;
1639#endif
1640 char_u *item_compare_func;
1641 partial_T *item_compare_partial;
1642 dict_T *item_compare_selfdict;
1643 int item_compare_func_err;
1644 int item_compare_keep_zero;
1645} sortinfo_T;
1646static sortinfo_T *sortinfo = NULL;
1647#define ITEM_COMPARE_FAIL 999
1648
1649/*
1650 * Compare functions for f_sort() and f_uniq() below.
1651 */
1652 static int
1653item_compare(const void *s1, const void *s2)
1654{
1655 sortItem_T *si1, *si2;
1656 typval_T *tv1, *tv2;
1657 char_u *p1, *p2;
1658 char_u *tofree1 = NULL, *tofree2 = NULL;
1659 int res;
1660 char_u numbuf1[NUMBUFLEN];
1661 char_u numbuf2[NUMBUFLEN];
1662
1663 si1 = (sortItem_T *)s1;
1664 si2 = (sortItem_T *)s2;
1665 tv1 = &si1->item->li_tv;
1666 tv2 = &si2->item->li_tv;
1667
1668 if (sortinfo->item_compare_numbers)
1669 {
1670 varnumber_T v1 = tv_get_number(tv1);
1671 varnumber_T v2 = tv_get_number(tv2);
1672
1673 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1674 }
1675
1676#ifdef FEAT_FLOAT
1677 if (sortinfo->item_compare_float)
1678 {
1679 float_T v1 = tv_get_float(tv1);
1680 float_T v2 = tv_get_float(tv2);
1681
1682 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1683 }
1684#endif
1685
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001686 // tv2string() puts quotes around a string and allocates memory. Don't do
1687 // that for string variables. Use a single quote when comparing with a
1688 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001689 if (tv1->v_type == VAR_STRING)
1690 {
1691 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1692 p1 = (char_u *)"'";
1693 else
1694 p1 = tv1->vval.v_string;
1695 }
1696 else
1697 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1698 if (tv2->v_type == VAR_STRING)
1699 {
1700 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1701 p2 = (char_u *)"'";
1702 else
1703 p2 = tv2->vval.v_string;
1704 }
1705 else
1706 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1707 if (p1 == NULL)
1708 p1 = (char_u *)"";
1709 if (p2 == NULL)
1710 p2 = (char_u *)"";
1711 if (!sortinfo->item_compare_numeric)
1712 {
Bram Moolenaar55e29612020-11-01 13:57:44 +01001713 if (sortinfo->item_compare_lc)
1714 res = strcoll((char *)p1, (char *)p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001715 else
Bram Moolenaar55e29612020-11-01 13:57:44 +01001716 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001717 }
1718 else
1719 {
1720 double n1, n2;
1721 n1 = strtod((char *)p1, (char **)&p1);
1722 n2 = strtod((char *)p2, (char **)&p2);
1723 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1724 }
1725
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001726 // When the result would be zero, compare the item indexes. Makes the
1727 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001728 if (res == 0 && !sortinfo->item_compare_keep_zero)
1729 res = si1->idx > si2->idx ? 1 : -1;
1730
1731 vim_free(tofree1);
1732 vim_free(tofree2);
1733 return res;
1734}
1735
1736 static int
1737item_compare2(const void *s1, const void *s2)
1738{
1739 sortItem_T *si1, *si2;
1740 int res;
1741 typval_T rettv;
1742 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001743 char_u *func_name;
1744 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001745 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001746
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001747 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001748 if (sortinfo->item_compare_func_err)
1749 return 0;
1750
1751 si1 = (sortItem_T *)s1;
1752 si2 = (sortItem_T *)s2;
1753
1754 if (partial == NULL)
1755 func_name = sortinfo->item_compare_func;
1756 else
1757 func_name = partial_name(partial);
1758
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001759 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1760 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001761 copy_tv(&si1->item->li_tv, &argv[0]);
1762 copy_tv(&si2->item->li_tv, &argv[1]);
1763
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001764 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001765 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001766 funcexe.evaluate = TRUE;
1767 funcexe.partial = partial;
1768 funcexe.selfdict = sortinfo->item_compare_selfdict;
1769 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001770 clear_tv(&argv[0]);
1771 clear_tv(&argv[1]);
1772
1773 if (res == FAIL)
1774 res = ITEM_COMPARE_FAIL;
1775 else
1776 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1777 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001778 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001779 clear_tv(&rettv);
1780
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001781 // When the result would be zero, compare the pointers themselves. Makes
1782 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001783 if (res == 0 && !sortinfo->item_compare_keep_zero)
1784 res = si1->idx > si2->idx ? 1 : -1;
1785
1786 return res;
1787}
1788
1789/*
1790 * "sort()" or "uniq()" function
1791 */
1792 static void
1793do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1794{
1795 list_T *l;
1796 listitem_T *li;
1797 sortItem_T *ptrs;
1798 sortinfo_T *old_sortinfo;
1799 sortinfo_T info;
1800 long len;
1801 long i;
1802
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001803 if (in_vim9script()
1804 && (check_for_list_arg(argvars, 0) == FAIL
1805 || (argvars[1].v_type != VAR_UNKNOWN
1806 && check_for_opt_dict_arg(argvars, 2) == FAIL)))
1807 return;
1808
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001809 // Pointer to current info struct used in compare function. Save and
1810 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001811 old_sortinfo = sortinfo;
1812 sortinfo = &info;
1813
1814 if (argvars[0].v_type != VAR_LIST)
1815 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1816 else
1817 {
1818 l = argvars[0].vval.v_list;
Bram Moolenaara187c432020-09-16 21:08:28 +02001819 if (l == NULL || value_check_lock(l->lv_lock,
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001820 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1821 TRUE))
1822 goto theend;
1823 rettv_list_set(rettv, l);
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001824 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001825
1826 len = list_len(l);
1827 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001828 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001829
1830 info.item_compare_ic = FALSE;
Bram Moolenaar55e29612020-11-01 13:57:44 +01001831 info.item_compare_lc = FALSE;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001832 info.item_compare_numeric = FALSE;
1833 info.item_compare_numbers = FALSE;
1834#ifdef FEAT_FLOAT
1835 info.item_compare_float = FALSE;
1836#endif
1837 info.item_compare_func = NULL;
1838 info.item_compare_partial = NULL;
1839 info.item_compare_selfdict = NULL;
1840 if (argvars[1].v_type != VAR_UNKNOWN)
1841 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001842 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001843 if (argvars[1].v_type == VAR_FUNC)
1844 info.item_compare_func = argvars[1].vval.v_string;
1845 else if (argvars[1].v_type == VAR_PARTIAL)
1846 info.item_compare_partial = argvars[1].vval.v_partial;
1847 else
1848 {
1849 int error = FALSE;
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001850 int nr = 0;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001851
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001852 if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001853 {
Bram Moolenaar08e51f42020-09-16 23:23:36 +02001854 nr = tv_get_number_chk(&argvars[1], &error);
1855 if (error)
1856 goto theend; // type error; errmsg already given
1857 if (nr == 1)
1858 info.item_compare_ic = TRUE;
1859 }
1860 if (nr != 1)
1861 {
1862 if (argvars[1].v_type != VAR_NUMBER)
1863 info.item_compare_func = tv_get_string(&argvars[1]);
1864 else if (nr != 0)
1865 {
1866 emsg(_(e_invarg));
1867 goto theend;
1868 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001869 }
1870 if (info.item_compare_func != NULL)
1871 {
1872 if (*info.item_compare_func == NUL)
1873 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001874 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001875 info.item_compare_func = NULL;
1876 }
1877 else if (STRCMP(info.item_compare_func, "n") == 0)
1878 {
1879 info.item_compare_func = NULL;
1880 info.item_compare_numeric = TRUE;
1881 }
1882 else if (STRCMP(info.item_compare_func, "N") == 0)
1883 {
1884 info.item_compare_func = NULL;
1885 info.item_compare_numbers = TRUE;
1886 }
1887#ifdef FEAT_FLOAT
1888 else if (STRCMP(info.item_compare_func, "f") == 0)
1889 {
1890 info.item_compare_func = NULL;
1891 info.item_compare_float = TRUE;
1892 }
1893#endif
1894 else if (STRCMP(info.item_compare_func, "i") == 0)
1895 {
1896 info.item_compare_func = NULL;
1897 info.item_compare_ic = TRUE;
1898 }
Bram Moolenaar55e29612020-11-01 13:57:44 +01001899 else if (STRCMP(info.item_compare_func, "l") == 0)
1900 {
1901 info.item_compare_func = NULL;
1902 info.item_compare_lc = TRUE;
1903 }
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001904 }
1905 }
1906
1907 if (argvars[2].v_type != VAR_UNKNOWN)
1908 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001909 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001910 if (argvars[2].v_type != VAR_DICT)
1911 {
1912 emsg(_(e_dictreq));
1913 goto theend;
1914 }
1915 info.item_compare_selfdict = argvars[2].vval.v_dict;
1916 }
1917 }
1918
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001919 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001920 ptrs = ALLOC_MULT(sortItem_T, len);
1921 if (ptrs == NULL)
1922 goto theend;
1923
1924 i = 0;
1925 if (sort)
1926 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001927 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001928 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001929 {
1930 ptrs[i].item = li;
1931 ptrs[i].idx = i;
1932 ++i;
1933 }
1934
1935 info.item_compare_func_err = FALSE;
1936 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001937 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001938 if ((info.item_compare_func != NULL
1939 || info.item_compare_partial != NULL)
1940 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
1941 == ITEM_COMPARE_FAIL)
1942 emsg(_("E702: Sort compare function failed"));
1943 else
1944 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001945 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001946 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
1947 info.item_compare_func == NULL
1948 && info.item_compare_partial == NULL
1949 ? item_compare : item_compare2);
1950
1951 if (!info.item_compare_func_err)
1952 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001953 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001954 l->lv_first = l->lv_u.mat.lv_last
1955 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001956 l->lv_len = 0;
1957 for (i = 0; i < len; ++i)
1958 list_append(l, ptrs[i].item);
1959 }
1960 }
1961 }
1962 else
1963 {
1964 int (*item_compare_func_ptr)(const void *, const void *);
1965
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001966 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001967 info.item_compare_func_err = FALSE;
1968 info.item_compare_keep_zero = TRUE;
1969 item_compare_func_ptr = info.item_compare_func != NULL
1970 || info.item_compare_partial != NULL
1971 ? item_compare2 : item_compare;
1972
1973 for (li = l->lv_first; li != NULL && li->li_next != NULL;
1974 li = li->li_next)
1975 {
1976 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
1977 == 0)
1978 ptrs[i++].item = li;
1979 if (info.item_compare_func_err)
1980 {
1981 emsg(_("E882: Uniq compare function failed"));
1982 break;
1983 }
1984 }
1985
1986 if (!info.item_compare_func_err)
1987 {
1988 while (--i >= 0)
1989 {
1990 li = ptrs[i].item->li_next;
1991 ptrs[i].item->li_next = li->li_next;
1992 if (li->li_next != NULL)
1993 li->li_next->li_prev = ptrs[i].item;
1994 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001995 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001996 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001998 l->lv_len--;
1999 }
2000 }
2001 }
2002
2003 vim_free(ptrs);
2004 }
2005theend:
2006 sortinfo = old_sortinfo;
2007}
2008
2009/*
2010 * "sort({list})" function
2011 */
2012 void
2013f_sort(typval_T *argvars, typval_T *rettv)
2014{
2015 do_sort_uniq(argvars, rettv, TRUE);
2016}
2017
2018/*
2019 * "uniq({list})" function
2020 */
2021 void
2022f_uniq(typval_T *argvars, typval_T *rettv)
2023{
2024 do_sort_uniq(argvars, rettv, FALSE);
2025}
2026
Bram Moolenaarea696852020-11-09 18:31:39 +01002027typedef enum {
2028 FILTERMAP_FILTER,
2029 FILTERMAP_MAP,
2030 FILTERMAP_MAPNEW
2031} filtermap_T;
2032
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002033/*
2034 * Handle one item for map() and filter().
Bram Moolenaarea696852020-11-09 18:31:39 +01002035 * Sets v:val to "tv". Caller must set v:key.
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002036 */
2037 static int
Bram Moolenaarea696852020-11-09 18:31:39 +01002038filter_map_one(
2039 typval_T *tv, // original value
2040 typval_T *expr, // callback
2041 filtermap_T filtermap,
2042 typval_T *newtv, // for map() and mapnew(): new value
2043 int *remp) // for filter(): remove flag
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002044{
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002045 typval_T argv[3];
2046 int retval = FAIL;
2047
2048 copy_tv(tv, get_vim_var_tv(VV_VAL));
2049 argv[0] = *get_vim_var_tv(VV_KEY);
2050 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaarea696852020-11-09 18:31:39 +01002051 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002052 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002053 if (filtermap == FILTERMAP_FILTER)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002054 {
2055 int error = FALSE;
2056
2057 // filter(): when expr is zero remove the item
Bram Moolenaar56acb092020-08-16 14:48:19 +02002058 if (in_vim9script())
Bram Moolenaarea696852020-11-09 18:31:39 +01002059 *remp = !tv2bool(newtv);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002060 else
Bram Moolenaarea696852020-11-09 18:31:39 +01002061 *remp = (tv_get_number_chk(newtv, &error) == 0);
2062 clear_tv(newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002063 // On type error, nothing has been removed; return FAIL to stop the
2064 // loop. The error message was given by tv_get_number_chk().
2065 if (error)
2066 goto theend;
2067 }
2068 retval = OK;
2069theend:
2070 clear_tv(get_vim_var_tv(VV_VAL));
2071 return retval;
2072}
2073
2074/*
2075 * Implementation of map() and filter().
2076 */
2077 static void
Bram Moolenaarea696852020-11-09 18:31:39 +01002078filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002079{
2080 typval_T *expr;
2081 listitem_T *li, *nli;
2082 list_T *l = NULL;
2083 dictitem_T *di;
2084 hashtab_T *ht;
2085 hashitem_T *hi;
2086 dict_T *d = NULL;
2087 blob_T *b = NULL;
2088 int rem;
2089 int todo;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002090 char *func_name = filtermap == FILTERMAP_MAP ? "map()"
Bram Moolenaarea696852020-11-09 18:31:39 +01002091 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002092 : "filter()";
Bram Moolenaarea696852020-11-09 18:31:39 +01002093 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
2094 ? N_("map() argument")
2095 : filtermap == FILTERMAP_MAPNEW
2096 ? N_("mapnew() argument")
2097 : N_("filter() argument"));
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002098 int save_did_emsg;
2099 int idx = 0;
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002100 type_T *type = NULL;
2101 garray_T type_list;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002102
Bram Moolenaarea696852020-11-09 18:31:39 +01002103 // map() and filter() return the first argument, also on failure.
2104 if (filtermap != FILTERMAP_MAPNEW)
2105 copy_tv(&argvars[0], rettv);
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002106
2107 if (in_vim9script()
2108 && (check_for_list_or_dict_or_blob_arg(argvars, 0) == FAIL))
2109 return;
2110
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002111 if (filtermap == FILTERMAP_MAP && in_vim9script())
2112 {
2113 // Check that map() does not change the type of the dict.
2114 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaarf2253962021-04-13 20:53:13 +02002115 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002116 }
Bram Moolenaarffdf8ad2020-10-15 22:29:17 +02002117
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002118 if (argvars[0].v_type == VAR_BLOB)
2119 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002120 if (filtermap == FILTERMAP_MAPNEW)
2121 {
2122 rettv->v_type = VAR_BLOB;
2123 rettv->vval.v_blob = NULL;
2124 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002125 if ((b = argvars[0].vval.v_blob) == NULL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002126 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002127 }
2128 else if (argvars[0].v_type == VAR_LIST)
2129 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002130 if (filtermap == FILTERMAP_MAPNEW)
2131 {
2132 rettv->v_type = VAR_LIST;
2133 rettv->vval.v_list = NULL;
2134 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002135 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002136 || (filtermap == FILTERMAP_FILTER
2137 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002138 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002139 }
2140 else if (argvars[0].v_type == VAR_DICT)
2141 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002142 if (filtermap == FILTERMAP_MAPNEW)
2143 {
2144 rettv->v_type = VAR_DICT;
2145 rettv->vval.v_dict = NULL;
2146 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002147 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaarea696852020-11-09 18:31:39 +01002148 || (filtermap == FILTERMAP_FILTER
2149 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002150 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002151 }
2152 else
2153 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002154 semsg(_(e_listdictblobarg), func_name);
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002155 goto theend;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002156 }
2157
2158 expr = &argvars[1];
2159 // On type errors, the preceding call has already displayed an error
2160 // message. Avoid a misleading error message for an empty string that
2161 // was not passed as argument.
2162 if (expr->v_type != VAR_UNKNOWN)
2163 {
2164 typval_T save_val;
2165 typval_T save_key;
2166
2167 prepare_vimvar(VV_VAL, &save_val);
2168 prepare_vimvar(VV_KEY, &save_key);
2169
2170 // We reset "did_emsg" to be able to detect whether an error
2171 // occurred during evaluation of the expression.
2172 save_did_emsg = did_emsg;
2173 did_emsg = FALSE;
2174
2175 if (argvars[0].v_type == VAR_DICT)
2176 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002177 int prev_lock = d->dv_lock;
Bram Moolenaarea696852020-11-09 18:31:39 +01002178 dict_T *d_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002179
Bram Moolenaarea696852020-11-09 18:31:39 +01002180 if (filtermap == FILTERMAP_MAPNEW)
2181 {
2182 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002183 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002184 d_ret = rettv->vval.v_dict;
2185 }
2186
2187 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002188 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002189 ht = &d->dv_hashtab;
2190 hash_lock(ht);
2191 todo = (int)ht->ht_used;
2192 for (hi = ht->ht_array; todo > 0; ++hi)
2193 {
2194 if (!HASHITEM_EMPTY(hi))
2195 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002196 int r;
2197 typval_T newtv;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002198
2199 --todo;
2200 di = HI2DI(hi);
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002201 if (filtermap == FILTERMAP_MAP
Bram Moolenaarea696852020-11-09 18:31:39 +01002202 && (value_check_lock(di->di_tv.v_lock,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002203 arg_errmsg, TRUE)
2204 || var_check_ro(di->di_flags,
2205 arg_errmsg, TRUE)))
2206 break;
2207 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaar027c4ab2021-02-21 16:20:18 +01002208 newtv.v_type = VAR_UNKNOWN;
Bram Moolenaarea696852020-11-09 18:31:39 +01002209 r = filter_map_one(&di->di_tv, expr, filtermap,
2210 &newtv, &rem);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002211 clear_tv(get_vim_var_tv(VV_KEY));
2212 if (r == FAIL || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002213 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002214 clear_tv(&newtv);
2215 break;
2216 }
2217 if (filtermap == FILTERMAP_MAP)
2218 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002219 if (type != NULL && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002220 type->tt_member, &newtv,
2221 func_name, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002222 {
2223 clear_tv(&newtv);
2224 break;
2225 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002226 // map(): replace the dict item value
2227 clear_tv(&di->di_tv);
2228 newtv.v_lock = 0;
2229 di->di_tv = newtv;
2230 }
2231 else if (filtermap == FILTERMAP_MAPNEW)
2232 {
2233 // mapnew(): add the item value to the new dict
2234 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
2235 clear_tv(&newtv);
2236 if (r == FAIL)
2237 break;
2238 }
2239 else if (filtermap == FILTERMAP_FILTER && rem)
2240 {
2241 // filter(false): remove the item from the dict
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002242 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2243 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2244 break;
2245 dictitem_remove(d, di);
2246 }
2247 }
2248 }
2249 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002250 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002251 }
2252 else if (argvars[0].v_type == VAR_BLOB)
2253 {
2254 int i;
2255 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002256 varnumber_T val;
Bram Moolenaarea696852020-11-09 18:31:39 +01002257 blob_T *b_ret = b;
2258
2259 if (filtermap == FILTERMAP_MAPNEW)
2260 {
2261 if (blob_copy(b, rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002262 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002263 b_ret = rettv->vval.v_blob;
2264 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002265
2266 // set_vim_var_nr() doesn't set the type
2267 set_vim_var_type(VV_KEY, VAR_NUMBER);
2268
2269 for (i = 0; i < b->bv_ga.ga_len; i++)
2270 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002271 typval_T newtv;
2272
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002273 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002274 val = blob_get(b, i);
2275 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002276 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaarea696852020-11-09 18:31:39 +01002277 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
2278 || did_emsg)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002279 break;
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002280 if (newtv.v_type != VAR_NUMBER && newtv.v_type != VAR_BOOL)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002281 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002282 clear_tv(&newtv);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002283 emsg(_(e_invalblob));
2284 break;
2285 }
Bram Moolenaarea696852020-11-09 18:31:39 +01002286 if (filtermap != FILTERMAP_FILTER)
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002287 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002288 if (newtv.vval.v_number != val)
2289 blob_set(b_ret, i, newtv.vval.v_number);
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002290 }
2291 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002292 {
2293 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2294
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002295 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002296 (size_t)b->bv_ga.ga_len - i - 1);
2297 --b->bv_ga.ga_len;
2298 --i;
2299 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002300 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002301 }
2302 }
2303 else // argvars[0].v_type == VAR_LIST
2304 {
Bram Moolenaarea696852020-11-09 18:31:39 +01002305 int prev_lock = l->lv_lock;
2306 list_T *l_ret = NULL;
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002307
Bram Moolenaarea696852020-11-09 18:31:39 +01002308 if (filtermap == FILTERMAP_MAPNEW)
2309 {
2310 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002311 goto theend;
Bram Moolenaarea696852020-11-09 18:31:39 +01002312 l_ret = rettv->vval.v_list;
2313 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002314 // set_vim_var_nr() doesn't set the type
2315 set_vim_var_type(VV_KEY, VAR_NUMBER);
2316
Bram Moolenaarea696852020-11-09 18:31:39 +01002317 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002318 l->lv_lock = VAR_LOCKED;
Bram Moolenaarea696852020-11-09 18:31:39 +01002319
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002320 if (l->lv_first == &range_list_item)
2321 {
2322 varnumber_T val = l->lv_u.nonmat.lv_start;
2323 int len = l->lv_len;
2324 int stride = l->lv_u.nonmat.lv_stride;
2325
2326 // List from range(): loop over the numbers
Bram Moolenaar75ab91f2021-01-10 22:42:50 +01002327 if (filtermap != FILTERMAP_MAPNEW)
2328 {
2329 l->lv_first = NULL;
2330 l->lv_u.mat.lv_last = NULL;
2331 l->lv_len = 0;
2332 l->lv_u.mat.lv_idx_item = NULL;
2333 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002334
2335 for (idx = 0; idx < len; ++idx)
Bram Moolenaarc56936e2020-11-10 11:43:56 +01002336 {
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002337 typval_T tv;
2338 typval_T newtv;
2339
2340 tv.v_type = VAR_NUMBER;
2341 tv.v_lock = 0;
2342 tv.vval.v_number = val;
2343 set_vim_var_nr(VV_KEY, idx);
2344 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
2345 == FAIL)
Bram Moolenaarea696852020-11-09 18:31:39 +01002346 break;
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002347 if (did_emsg)
2348 {
2349 clear_tv(&newtv);
2350 break;
2351 }
2352 if (filtermap != FILTERMAP_FILTER)
2353 {
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002354 if (filtermap == FILTERMAP_MAP && type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002355 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002356 type->tt_member, &newtv,
2357 func_name, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002358 {
2359 clear_tv(&newtv);
2360 break;
2361 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002362 // map(), mapnew(): always append the new value to the
2363 // list
2364 if (list_append_tv_move(filtermap == FILTERMAP_MAP
2365 ? l : l_ret, &newtv) == FAIL)
2366 break;
2367 }
2368 else if (!rem)
2369 {
2370 // filter(): append the list item value when not rem
2371 if (list_append_tv_move(l, &tv) == FAIL)
2372 break;
2373 }
2374
2375 val += stride;
Bram Moolenaarea696852020-11-09 18:31:39 +01002376 }
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002377 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002378 else
2379 {
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002380 // Materialized list: loop over the items
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002381 for (li = l->lv_first; li != NULL; li = nli)
2382 {
2383 typval_T newtv;
2384
Bram Moolenaar57cf4972020-12-01 21:08:05 +01002385 if (filtermap == FILTERMAP_MAP && value_check_lock(
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002386 li->li_tv.v_lock, arg_errmsg, TRUE))
2387 break;
2388 nli = li->li_next;
2389 set_vim_var_nr(VV_KEY, idx);
2390 if (filter_map_one(&li->li_tv, expr, filtermap,
2391 &newtv, &rem) == FAIL)
2392 break;
2393 if (did_emsg)
2394 {
2395 clear_tv(&newtv);
2396 break;
2397 }
2398 if (filtermap == FILTERMAP_MAP)
2399 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002400 if (type != NULL && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002401 type->tt_member, &newtv, func_name, 0) == FAIL)
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002402 {
2403 clear_tv(&newtv);
2404 break;
2405 }
Bram Moolenaarf8ca03b2020-11-28 20:32:29 +01002406 // map(): replace the list item value
2407 clear_tv(&li->li_tv);
2408 newtv.v_lock = 0;
2409 li->li_tv = newtv;
2410 }
2411 else if (filtermap == FILTERMAP_MAPNEW)
2412 {
2413 // mapnew(): append the list item value
2414 if (list_append_tv_move(l_ret, &newtv) == FAIL)
2415 break;
2416 }
2417 else if (filtermap == FILTERMAP_FILTER && rem)
2418 listitem_remove(l, li);
2419 ++idx;
2420 }
2421 }
2422
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002423 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002424 }
2425
2426 restore_vimvar(VV_KEY, &save_key);
2427 restore_vimvar(VV_VAL, &save_val);
2428
2429 did_emsg |= save_did_emsg;
2430 }
Bram Moolenaar70250fb2021-01-16 19:01:53 +01002431
2432theend:
2433 if (type != NULL)
2434 clear_type_list(&type_list);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002435}
2436
2437/*
2438 * "filter()" function
2439 */
2440 void
2441f_filter(typval_T *argvars, typval_T *rettv)
2442{
Bram Moolenaarea696852020-11-09 18:31:39 +01002443 filter_map(argvars, rettv, FILTERMAP_FILTER);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002444}
2445
2446/*
2447 * "map()" function
2448 */
2449 void
2450f_map(typval_T *argvars, typval_T *rettv)
2451{
Bram Moolenaarea696852020-11-09 18:31:39 +01002452 filter_map(argvars, rettv, FILTERMAP_MAP);
2453}
2454
2455/*
2456 * "mapnew()" function
2457 */
2458 void
2459f_mapnew(typval_T *argvars, typval_T *rettv)
2460{
2461 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002462}
2463
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002464/*
2465 * "add(list, item)" function
2466 */
2467 void
2468f_add(typval_T *argvars, typval_T *rettv)
2469{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002470 rettv->vval.v_number = 1; // Default: Failed
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002471
2472 if (in_vim9script()
2473 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2474 || (argvars[0].v_type == VAR_BLOB
2475 && check_for_number_arg(argvars, 1) == FAIL)))
2476 return;
2477
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002478 if (argvars[0].v_type == VAR_LIST)
2479 {
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002480 list_T *l = argvars[0].vval.v_list;
2481
2482 if (l == NULL)
2483 {
2484 if (in_vim9script())
2485 emsg(_(e_cannot_add_to_null_list));
2486 }
2487 else if (!value_check_lock(l->lv_lock,
2488 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002489 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002490 {
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002491 copy_tv(&argvars[0], rettv);
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002492 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002493 }
2494 else if (argvars[0].v_type == VAR_BLOB)
2495 {
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002496 blob_T *b = argvars[0].vval.v_blob;
2497
2498 if (b == NULL)
2499 {
2500 if (in_vim9script())
2501 emsg(_(e_cannot_add_to_null_blob));
2502 }
2503 else if (!value_check_lock(b->bv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002504 (char_u *)N_("add() argument"), TRUE))
2505 {
2506 int error = FALSE;
2507 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2508
2509 if (!error)
2510 {
2511 ga_append(&b->bv_ga, (int)n);
2512 copy_tv(&argvars[0], rettv);
2513 }
2514 }
2515 }
2516 else
2517 emsg(_(e_listblobreq));
2518}
2519
2520/*
2521 * "count()" function
2522 */
2523 void
2524f_count(typval_T *argvars, typval_T *rettv)
2525{
2526 long n = 0;
2527 int ic = FALSE;
2528 int error = FALSE;
2529
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002530 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02002531 && (check_for_string_or_list_or_dict_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02002532 || check_for_opt_bool_arg(argvars, 2) == FAIL
2533 || (argvars[2].v_type != VAR_UNKNOWN
2534 && check_for_opt_number_arg(argvars, 3) == FAIL)))
2535 return;
2536
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002537 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar119f5572020-09-02 21:31:22 +02002538 ic = (int)tv_get_bool_chk(&argvars[2], &error);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002539
2540 if (argvars[0].v_type == VAR_STRING)
2541 {
2542 char_u *expr = tv_get_string_chk(&argvars[1]);
2543 char_u *p = argvars[0].vval.v_string;
2544 char_u *next;
2545
2546 if (!error && expr != NULL && *expr != NUL && p != NULL)
2547 {
2548 if (ic)
2549 {
2550 size_t len = STRLEN(expr);
2551
2552 while (*p != NUL)
2553 {
2554 if (MB_STRNICMP(p, expr, len) == 0)
2555 {
2556 ++n;
2557 p += len;
2558 }
2559 else
2560 MB_PTR_ADV(p);
2561 }
2562 }
2563 else
2564 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2565 != NULL)
2566 {
2567 ++n;
2568 p = next + STRLEN(expr);
2569 }
2570 }
2571
2572 }
2573 else if (argvars[0].v_type == VAR_LIST)
2574 {
2575 listitem_T *li;
2576 list_T *l;
2577 long idx;
2578
2579 if ((l = argvars[0].vval.v_list) != NULL)
2580 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002581 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002582 li = l->lv_first;
2583 if (argvars[2].v_type != VAR_UNKNOWN)
2584 {
2585 if (argvars[3].v_type != VAR_UNKNOWN)
2586 {
2587 idx = (long)tv_get_number_chk(&argvars[3], &error);
2588 if (!error)
2589 {
2590 li = list_find(l, idx);
2591 if (li == NULL)
2592 semsg(_(e_listidx), idx);
2593 }
2594 }
2595 if (error)
2596 li = NULL;
2597 }
2598
2599 for ( ; li != NULL; li = li->li_next)
2600 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2601 ++n;
2602 }
2603 }
2604 else if (argvars[0].v_type == VAR_DICT)
2605 {
2606 int todo;
2607 dict_T *d;
2608 hashitem_T *hi;
2609
2610 if ((d = argvars[0].vval.v_dict) != NULL)
2611 {
2612 if (argvars[2].v_type != VAR_UNKNOWN)
2613 {
2614 if (argvars[3].v_type != VAR_UNKNOWN)
2615 emsg(_(e_invarg));
2616 }
2617
2618 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2619 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2620 {
2621 if (!HASHITEM_EMPTY(hi))
2622 {
2623 --todo;
2624 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2625 ++n;
2626 }
2627 }
2628 }
2629 }
2630 else
2631 semsg(_(e_listdictarg), "count()");
2632 rettv->vval.v_number = n;
2633}
2634
2635/*
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002636 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002637 */
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002638 static void
2639extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002640{
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002641 type_T *type = NULL;
2642 garray_T type_list;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002643 char *func_name = is_new ? "extendnew()" : "extend()";
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002644
2645 if (!is_new && in_vim9script())
2646 {
2647 // Check that map() does not change the type of the dict.
2648 ga_init2(&type_list, sizeof(type_T *), 10);
Bram Moolenaarf2253962021-04-13 20:53:13 +02002649 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002650 }
2651
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002652 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2653 {
2654 list_T *l1, *l2;
2655 listitem_T *item;
2656 long before;
2657 int error = FALSE;
2658
2659 l1 = argvars[0].vval.v_list;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002660 if (l1 == NULL)
2661 {
2662 emsg(_(e_cannot_extend_null_list));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002663 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002664 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002665 l2 = argvars[1].vval.v_list;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002666 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
2667 && l2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002668 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002669 if (is_new)
2670 {
2671 l1 = list_copy(l1, FALSE, get_copyID());
2672 if (l1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002673 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002674 }
2675
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002676 if (argvars[2].v_type != VAR_UNKNOWN)
2677 {
2678 before = (long)tv_get_number_chk(&argvars[2], &error);
2679 if (error)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002680 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002681
2682 if (before == l1->lv_len)
2683 item = NULL;
2684 else
2685 {
2686 item = list_find(l1, before);
2687 if (item == NULL)
2688 {
2689 semsg(_(e_listidx), before);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002690 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002691 }
2692 }
2693 }
2694 else
2695 item = NULL;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002696 if (type != NULL && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002697 type, &argvars[1], func_name, 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002698 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002699 list_extend(l1, l2, item);
2700
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002701 if (is_new)
2702 {
2703 rettv->v_type = VAR_LIST;
2704 rettv->vval.v_list = l1;
2705 rettv->v_lock = FALSE;
2706 }
2707 else
2708 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002709 }
2710 }
2711 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2712 {
2713 dict_T *d1, *d2;
2714 char_u *action;
2715 int i;
2716
2717 d1 = argvars[0].vval.v_dict;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002718 if (d1 == NULL)
2719 {
2720 emsg(_(e_cannot_extend_null_dict));
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002721 goto theend;
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002722 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002723 d2 = argvars[1].vval.v_dict;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002724 if ((is_new || !value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
2725 && d2 != NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002726 {
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002727 if (is_new)
2728 {
2729 d1 = dict_copy(d1, FALSE, get_copyID());
2730 if (d1 == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002731 goto theend;
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002732 }
2733
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002734 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002735 if (argvars[2].v_type != VAR_UNKNOWN)
2736 {
2737 static char *(av[]) = {"keep", "force", "error"};
2738
2739 action = tv_get_string_chk(&argvars[2]);
2740 if (action == NULL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002741 goto theend; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002742 for (i = 0; i < 3; ++i)
2743 if (STRCMP(action, av[i]) == 0)
2744 break;
2745 if (i == 3)
2746 {
2747 semsg(_(e_invarg2), action);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002748 goto theend;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002749 }
2750 }
2751 else
2752 action = (char_u *)"force";
2753
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002754 if (type != NULL && check_typval_arg_type(type, &argvars[1],
2755 func_name, 2) == FAIL)
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002756 goto theend;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002757 dict_extend(d1, d2, action, func_name);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002758
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002759 if (is_new)
2760 {
2761 rettv->v_type = VAR_DICT;
2762 rettv->vval.v_dict = d1;
2763 rettv->v_lock = FALSE;
2764 }
2765 else
2766 copy_tv(&argvars[0], rettv);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002767 }
2768 }
2769 else
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02002770 semsg(_(e_listdictarg), func_name);
Bram Moolenaarc03f5c62021-01-31 17:48:30 +01002771
2772theend:
2773 if (type != NULL)
2774 clear_type_list(&type_list);
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01002775}
2776
2777/*
2778 * "extend(list, list [, idx])" function
2779 * "extend(dict, dict [, action])" function
2780 */
2781 void
2782f_extend(typval_T *argvars, typval_T *rettv)
2783{
2784 char_u *errmsg = (char_u *)N_("extend() argument");
2785
2786 extend(argvars, rettv, errmsg, FALSE);
2787}
2788
2789/*
2790 * "extendnew(list, list [, idx])" function
2791 * "extendnew(dict, dict [, action])" function
2792 */
2793 void
2794f_extendnew(typval_T *argvars, typval_T *rettv)
2795{
2796 char_u *errmsg = (char_u *)N_("extendnew() argument");
2797
2798 extend(argvars, rettv, errmsg, TRUE);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002799}
2800
2801/*
2802 * "insert()" function
2803 */
2804 void
2805f_insert(typval_T *argvars, typval_T *rettv)
2806{
2807 long before = 0;
2808 listitem_T *item;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002809 int error = FALSE;
2810
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002811 if (in_vim9script()
2812 && (check_for_list_or_blob_arg(argvars, 0) == FAIL
2813 || (argvars[0].v_type == VAR_BLOB
2814 && check_for_number_arg(argvars, 1) == FAIL)
2815 || check_for_opt_number_arg(argvars, 2) == FAIL))
2816 return;
2817
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002818 if (argvars[0].v_type == VAR_BLOB)
2819 {
Sean Dewar80d73952021-08-04 19:25:54 +02002820 blob_T *b = argvars[0].vval.v_blob;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002821
Sean Dewar80d73952021-08-04 19:25:54 +02002822 if (b == NULL)
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002823 {
2824 if (in_vim9script())
2825 emsg(_(e_cannot_add_to_null_blob));
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002826 }
Sean Dewar80d73952021-08-04 19:25:54 +02002827 else if (!value_check_lock(b->bv_lock,
2828 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002829 {
Sean Dewar80d73952021-08-04 19:25:54 +02002830 int val, len;
2831 char_u *p;
2832
2833 len = blob_len(b);
2834 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002835 {
Sean Dewar80d73952021-08-04 19:25:54 +02002836 before = (long)tv_get_number_chk(&argvars[2], &error);
2837 if (error)
2838 return; // type error; errmsg already given
2839 if (before < 0 || before > len)
2840 {
2841 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2842 return;
2843 }
2844 }
2845 val = tv_get_number_chk(&argvars[1], &error);
2846 if (error)
2847 return;
2848 if (val < 0 || val > 255)
2849 {
2850 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002851 return;
2852 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002853
Sean Dewar80d73952021-08-04 19:25:54 +02002854 if (ga_grow(&b->bv_ga, 1) == FAIL)
2855 return;
2856 p = (char_u *)b->bv_ga.ga_data;
2857 mch_memmove(p + before + 1, p + before, (size_t)len - before);
2858 *(p + before) = val;
2859 ++b->bv_ga.ga_len;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002860
Sean Dewar80d73952021-08-04 19:25:54 +02002861 copy_tv(&argvars[0], rettv);
2862 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002863 }
2864 else if (argvars[0].v_type != VAR_LIST)
2865 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002866 else
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002867 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002868 list_T *l = argvars[0].vval.v_list;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002869
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002870 if (l == NULL)
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002871 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002872 if (in_vim9script())
2873 emsg(_(e_cannot_add_to_null_list));
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002874 }
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002875 else if (!value_check_lock(l->lv_lock,
2876 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002877 {
Bram Moolenaar39211cb2021-04-18 15:48:04 +02002878 if (argvars[2].v_type != VAR_UNKNOWN)
2879 before = (long)tv_get_number_chk(&argvars[2], &error);
2880 if (error)
2881 return; // type error; errmsg already given
2882
2883 if (before == l->lv_len)
2884 item = NULL;
2885 else
2886 {
2887 item = list_find(l, before);
2888 if (item == NULL)
2889 {
2890 semsg(_(e_listidx), before);
2891 l = NULL;
2892 }
2893 }
2894 if (l != NULL)
2895 {
2896 (void)list_insert_tv(l, &argvars[1], item);
2897 copy_tv(&argvars[0], rettv);
2898 }
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002899 }
2900 }
2901}
2902
2903/*
2904 * "remove()" function
2905 */
2906 void
2907f_remove(typval_T *argvars, typval_T *rettv)
2908{
2909 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2910
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002911 if (in_vim9script()
2912 && (check_for_list_or_dict_or_blob_arg(argvars, 0) == FAIL
2913 || ((argvars[0].v_type == VAR_LIST
2914 || argvars[0].v_type == VAR_BLOB)
2915 && (check_for_number_arg(argvars, 1) == FAIL
2916 || check_for_opt_number_arg(argvars, 2) == FAIL))
2917 || (argvars[0].v_type == VAR_DICT
2918 && check_for_string_or_number_arg(argvars, 1) == FAIL)))
2919 return;
2920
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002921 if (argvars[0].v_type == VAR_DICT)
2922 dict_remove(argvars, rettv, arg_errmsg);
2923 else if (argvars[0].v_type == VAR_BLOB)
Sean Dewar80d73952021-08-04 19:25:54 +02002924 blob_remove(argvars, rettv, arg_errmsg);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002925 else if (argvars[0].v_type == VAR_LIST)
2926 list_remove(argvars, rettv, arg_errmsg);
2927 else
2928 semsg(_(e_listdictblobarg), "remove()");
2929}
2930
2931/*
2932 * "reverse({list})" function
2933 */
2934 void
2935f_reverse(typval_T *argvars, typval_T *rettv)
2936{
2937 list_T *l;
2938 listitem_T *li, *ni;
2939
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02002940 if (in_vim9script() && check_for_list_or_blob_arg(argvars, 0) == FAIL)
2941 return;
2942
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002943 if (argvars[0].v_type == VAR_BLOB)
2944 {
2945 blob_T *b = argvars[0].vval.v_blob;
2946 int i, len = blob_len(b);
2947
2948 for (i = 0; i < len / 2; i++)
2949 {
2950 int tmp = blob_get(b, i);
2951
2952 blob_set(b, i, blob_get(b, len - i - 1));
2953 blob_set(b, len - i - 1, tmp);
2954 }
2955 rettv_blob_set(rettv, b);
2956 return;
2957 }
2958
2959 if (argvars[0].v_type != VAR_LIST)
2960 semsg(_(e_listblobarg), "reverse()");
2961 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002962 && !value_check_lock(l->lv_lock,
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002963 (char_u *)N_("reverse() argument"), TRUE))
2964 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002965 if (l->lv_first == &range_list_item)
2966 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002967 varnumber_T new_start = l->lv_u.nonmat.lv_start
2968 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2969 l->lv_u.nonmat.lv_end = new_start
2970 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2971 l->lv_u.nonmat.lv_start = new_start;
2972 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002973 rettv_list_set(rettv, l);
2974 return;
2975 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002976 li = l->lv_u.mat.lv_last;
2977 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002978 l->lv_len = 0;
2979 while (li != NULL)
2980 {
2981 ni = li->li_prev;
2982 list_append(l, li);
2983 li = ni;
2984 }
2985 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002986 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002987 }
2988}
2989
Bram Moolenaar85629982020-06-01 18:39:20 +02002990/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002991 * "reduce(list, { accumulator, element -> value } [, initial])" function
Bram Moolenaar85629982020-06-01 18:39:20 +02002992 */
2993 void
2994f_reduce(typval_T *argvars, typval_T *rettv)
2995{
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002996 typval_T initial;
Bram Moolenaar85629982020-06-01 18:39:20 +02002997 char_u *func_name;
2998 partial_T *partial = NULL;
2999 funcexe_T funcexe;
3000 typval_T argv[3];
3001
3002 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
3003 {
3004 emsg(_(e_listblobreq));
3005 return;
3006 }
3007
3008 if (argvars[1].v_type == VAR_FUNC)
3009 func_name = argvars[1].vval.v_string;
3010 else if (argvars[1].v_type == VAR_PARTIAL)
3011 {
3012 partial = argvars[1].vval.v_partial;
3013 func_name = partial_name(partial);
3014 }
3015 else
3016 func_name = tv_get_string(&argvars[1]);
Bram Moolenaar0d90e722020-11-03 18:20:19 +01003017 if (func_name == NULL || *func_name == NUL)
3018 {
3019 emsg(_(e_missing_function_argument));
3020 return;
3021 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003022
3023 vim_memset(&funcexe, 0, sizeof(funcexe));
3024 funcexe.evaluate = TRUE;
3025 funcexe.partial = partial;
3026
3027 if (argvars[0].v_type == VAR_LIST)
3028 {
3029 list_T *l = argvars[0].vval.v_list;
3030 listitem_T *li = NULL;
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003031 int r;
Bram Moolenaarca275a02020-06-24 22:07:46 +02003032 int called_emsg_start = called_emsg;
Bram Moolenaar85629982020-06-01 18:39:20 +02003033
Bram Moolenaarfda20c42020-06-29 20:09:36 +02003034 if (l != NULL)
3035 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar85629982020-06-01 18:39:20 +02003036 if (argvars[2].v_type == VAR_UNKNOWN)
3037 {
3038 if (l == NULL || l->lv_first == NULL)
3039 {
3040 semsg(_(e_reduceempty), "List");
3041 return;
3042 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003043 initial = l->lv_first->li_tv;
Bram Moolenaar85629982020-06-01 18:39:20 +02003044 li = l->lv_first->li_next;
3045 }
3046 else
3047 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003048 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02003049 if (l != NULL)
3050 li = l->lv_first;
3051 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003052 copy_tv(&initial, rettv);
Bram Moolenaarfda20c42020-06-29 20:09:36 +02003053
3054 if (l != NULL)
Bram Moolenaar85629982020-06-01 18:39:20 +02003055 {
Bram Moolenaarfda20c42020-06-29 20:09:36 +02003056 int prev_locked = l->lv_lock;
3057
3058 l->lv_lock = VAR_FIXED; // disallow the list changing here
3059 for ( ; li != NULL; li = li->li_next)
3060 {
3061 argv[0] = *rettv;
3062 argv[1] = li->li_tv;
3063 rettv->v_type = VAR_UNKNOWN;
3064 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
3065 clear_tv(&argv[0]);
3066 if (r == FAIL || called_emsg != called_emsg_start)
3067 break;
3068 }
3069 l->lv_lock = prev_locked;
Bram Moolenaar85629982020-06-01 18:39:20 +02003070 }
3071 }
3072 else
3073 {
3074 blob_T *b = argvars[0].vval.v_blob;
3075 int i;
3076
3077 if (argvars[2].v_type == VAR_UNKNOWN)
3078 {
3079 if (b == NULL || b->bv_ga.ga_len == 0)
3080 {
3081 semsg(_(e_reduceempty), "Blob");
3082 return;
3083 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003084 initial.v_type = VAR_NUMBER;
3085 initial.vval.v_number = blob_get(b, 0);
Bram Moolenaar85629982020-06-01 18:39:20 +02003086 i = 1;
3087 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003088 else if (argvars[2].v_type != VAR_NUMBER)
3089 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003090 emsg(_(e_number_expected));
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003091 return;
3092 }
Bram Moolenaar85629982020-06-01 18:39:20 +02003093 else
3094 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003095 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02003096 i = 0;
3097 }
3098
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003099 copy_tv(&initial, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02003100 if (b != NULL)
3101 {
3102 for ( ; i < b->bv_ga.ga_len; i++)
3103 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02003104 argv[0] = *rettv;
Bram Moolenaar85629982020-06-01 18:39:20 +02003105 argv[1].v_type = VAR_NUMBER;
3106 argv[1].vval.v_number = blob_get(b, i);
3107 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
3108 return;
Bram Moolenaar85629982020-06-01 18:39:20 +02003109 }
3110 }
3111 }
3112}
3113
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02003114#endif // defined(FEAT_EVAL)