blob: 955272c37bae1fd387c89563891a8cad2502f0da [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
273 vim_free(l);
274}
275
276 void
277list_free_items(int copyID)
278{
279 list_T *ll, *ll_next;
280
281 for (ll = first_list; ll != NULL; ll = ll_next)
282 {
283 ll_next = ll->lv_used_next;
284 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
285 && ll->lv_watch == NULL)
286 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100287 // Free the List and ordinary items it contains, but don't recurse
288 // into Lists and Dictionaries, they will be in the list of dicts
289 // or list of lists.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200290 list_free_list(ll);
291 }
292 }
293}
294
295 void
296list_free(list_T *l)
297{
298 if (!in_free_unref_items)
299 {
300 list_free_contents(l);
301 list_free_list(l);
302 }
303}
304
305/*
306 * Allocate a list item.
307 * It is not initialized, don't forget to set v_lock.
308 */
309 listitem_T *
310listitem_alloc(void)
311{
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200312 return ALLOC_ONE(listitem_T);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200313}
314
315/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100316 * Free a list item, unless it was allocated together with the list itself.
317 * Does not clear the value. Does not notify watchers.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200318 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +0200319 static void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100320list_free_item(list_T *l, listitem_T *item)
321{
322 if (l->lv_with_items == 0 || item < (listitem_T *)l
323 || item >= (listitem_T *)(l + 1) + l->lv_with_items)
324 vim_free(item);
325}
326
327/*
328 * Free a list item, unless it was allocated together with the list itself.
329 * Also clears the value. Does not notify watchers.
330 */
331 void
332listitem_free(list_T *l, listitem_T *item)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200333{
334 clear_tv(&item->li_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335 list_free_item(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200336}
337
338/*
339 * Remove a list item from a List and free it. Also clears the value.
340 */
341 void
342listitem_remove(list_T *l, listitem_T *item)
343{
344 vimlist_remove(l, item, item);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100345 listitem_free(l, item);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200346}
347
348/*
349 * Get the number of items in a list.
350 */
351 long
352list_len(list_T *l)
353{
354 if (l == NULL)
355 return 0L;
356 return l->lv_len;
357}
358
359/*
360 * Return TRUE when two lists have exactly the same values.
361 */
362 int
363list_equal(
364 list_T *l1,
365 list_T *l2,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100366 int ic, // ignore case for strings
367 int recursive) // TRUE when used recursively
Bram Moolenaarda861d62016-07-17 15:46:27 +0200368{
369 listitem_T *item1, *item2;
370
Bram Moolenaarda861d62016-07-17 15:46:27 +0200371 if (l1 == l2)
372 return TRUE;
373 if (list_len(l1) != list_len(l2))
374 return FALSE;
Bram Moolenaar7b293c72020-04-09 21:33:22 +0200375 if (list_len(l1) == 0)
376 // empty and NULL list are considered equal
377 return TRUE;
378 if (l1 == NULL || l2 == NULL)
379 return FALSE;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200380
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200381 CHECK_LIST_MATERIALIZE(l1);
382 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383
Bram Moolenaarda861d62016-07-17 15:46:27 +0200384 for (item1 = l1->lv_first, item2 = l2->lv_first;
385 item1 != NULL && item2 != NULL;
386 item1 = item1->li_next, item2 = item2->li_next)
387 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
388 return FALSE;
389 return item1 == NULL && item2 == NULL;
390}
391
392/*
393 * Locate item with index "n" in list "l" and return it.
394 * A negative index is counted from the end; -1 is the last item.
395 * Returns NULL when "n" is out of range.
396 */
397 listitem_T *
398list_find(list_T *l, long n)
399{
400 listitem_T *item;
401 long idx;
402
403 if (l == NULL)
404 return NULL;
405
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100406 // Negative index is relative to the end.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200407 if (n < 0)
408 n = l->lv_len + n;
409
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100410 // Check for index out of range.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200411 if (n < 0 || n >= l->lv_len)
412 return NULL;
413
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200414 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100415
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100416 // When there is a cached index may start search from there.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100417 if (l->lv_u.mat.lv_idx_item != NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200418 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100419 if (n < l->lv_u.mat.lv_idx / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200420 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100421 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200422 item = l->lv_first;
423 idx = 0;
424 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100425 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200426 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100427 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100428 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200429 idx = l->lv_len - 1;
430 }
431 else
432 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100433 // closest to the cached index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100434 item = l->lv_u.mat.lv_idx_item;
435 idx = l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200436 }
437 }
438 else
439 {
440 if (n < l->lv_len / 2)
441 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100442 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200443 item = l->lv_first;
444 idx = 0;
445 }
446 else
447 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100448 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100449 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200450 idx = l->lv_len - 1;
451 }
452 }
453
454 while (n > idx)
455 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100456 // search forward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200457 item = item->li_next;
458 ++idx;
459 }
460 while (n < idx)
461 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100462 // search backward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200463 item = item->li_prev;
464 --idx;
465 }
466
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100467 // cache the used index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100468 l->lv_u.mat.lv_idx = idx;
469 l->lv_u.mat.lv_idx_item = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200470
471 return item;
472}
473
474/*
475 * Get list item "l[idx]" as a number.
476 */
477 long
478list_find_nr(
479 list_T *l,
480 long idx,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100481 int *errorp) // set to TRUE when something wrong
Bram Moolenaarda861d62016-07-17 15:46:27 +0200482{
483 listitem_T *li;
484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100485 if (l != NULL && l->lv_first == &range_list_item)
486 {
487 long n = idx;
488
489 // not materialized range() list: compute the value.
490 // Negative index is relative to the end.
491 if (n < 0)
492 n = l->lv_len + n;
493
494 // Check for index out of range.
495 if (n < 0 || n >= l->lv_len)
496 {
497 if (errorp != NULL)
498 *errorp = TRUE;
499 return -1L;
500 }
501
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100502 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503 }
504
Bram Moolenaarda861d62016-07-17 15:46:27 +0200505 li = list_find(l, idx);
506 if (li == NULL)
507 {
508 if (errorp != NULL)
509 *errorp = TRUE;
510 return -1L;
511 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100512 return (long)tv_get_number_chk(&li->li_tv, errorp);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200513}
514
515/*
516 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
517 */
518 char_u *
519list_find_str(list_T *l, long idx)
520{
521 listitem_T *li;
522
523 li = list_find(l, idx - 1);
524 if (li == NULL)
525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100526 semsg(_(e_listidx), idx);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200527 return NULL;
528 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100529 return tv_get_string(&li->li_tv);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200530}
531
532/*
533 * Locate "item" list "l" and return its index.
534 * Returns -1 when "item" is not in the list.
535 */
536 long
537list_idx_of_item(list_T *l, listitem_T *item)
538{
539 long idx = 0;
540 listitem_T *li;
541
542 if (l == NULL)
543 return -1;
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200544 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200545 idx = 0;
546 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
547 ++idx;
548 if (li == NULL)
549 return -1;
550 return idx;
551}
552
553/*
554 * Append item "item" to the end of list "l".
555 */
556 void
557list_append(list_T *l, listitem_T *item)
558{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200559 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100560 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200561 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100562 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200563 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100564 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200565 item->li_prev = NULL;
566 }
567 else
568 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100569 l->lv_u.mat.lv_last->li_next = item;
570 item->li_prev = l->lv_u.mat.lv_last;
571 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200572 }
573 ++l->lv_len;
574 item->li_next = NULL;
575}
576
577/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200579 * Return FAIL when out of memory.
580 */
581 int
582list_append_tv(list_T *l, typval_T *tv)
583{
584 listitem_T *li = listitem_alloc();
585
586 if (li == NULL)
587 return FAIL;
588 copy_tv(tv, &li->li_tv);
589 list_append(l, li);
590 return OK;
591}
592
593/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594 * As list_append_tv() but move the value instead of copying it.
595 * Return FAIL when out of memory.
596 */
597 int
598list_append_tv_move(list_T *l, typval_T *tv)
599{
600 listitem_T *li = listitem_alloc();
601
602 if (li == NULL)
603 return FAIL;
604 li->li_tv = *tv;
605 list_append(l, li);
606 return OK;
607}
608
609/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200610 * Add a dictionary to a list. Used by getqflist().
611 * Return FAIL when out of memory.
612 */
613 int
614list_append_dict(list_T *list, dict_T *dict)
615{
616 listitem_T *li = listitem_alloc();
617
618 if (li == NULL)
619 return FAIL;
620 li->li_tv.v_type = VAR_DICT;
621 li->li_tv.v_lock = 0;
622 li->li_tv.vval.v_dict = dict;
623 list_append(list, li);
624 ++dict->dv_refcount;
625 return OK;
626}
627
628/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100629 * Append list2 to list1.
630 * Return FAIL when out of memory.
631 */
632 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200633list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100634{
635 listitem_T *li = listitem_alloc();
636
637 if (li == NULL)
638 return FAIL;
639 li->li_tv.v_type = VAR_LIST;
640 li->li_tv.v_lock = 0;
641 li->li_tv.vval.v_list = list2;
642 list_append(list1, li);
643 ++list2->lv_refcount;
644 return OK;
645}
646
647/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200648 * Make a copy of "str" and append it as an item to list "l".
649 * When "len" >= 0 use "str[len]".
650 * Returns FAIL when out of memory.
651 */
652 int
653list_append_string(list_T *l, char_u *str, int len)
654{
655 listitem_T *li = listitem_alloc();
656
657 if (li == NULL)
658 return FAIL;
659 list_append(l, li);
660 li->li_tv.v_type = VAR_STRING;
661 li->li_tv.v_lock = 0;
662 if (str == NULL)
663 li->li_tv.vval.v_string = NULL;
664 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
665 : vim_strsave(str))) == NULL)
666 return FAIL;
667 return OK;
668}
669
670/*
671 * Append "n" to list "l".
672 * Returns FAIL when out of memory.
673 */
674 int
675list_append_number(list_T *l, varnumber_T n)
676{
677 listitem_T *li;
678
679 li = listitem_alloc();
680 if (li == NULL)
681 return FAIL;
682 li->li_tv.v_type = VAR_NUMBER;
683 li->li_tv.v_lock = 0;
684 li->li_tv.vval.v_number = n;
685 list_append(l, li);
686 return OK;
687}
688
689/*
690 * Insert typval_T "tv" in list "l" before "item".
691 * If "item" is NULL append at the end.
692 * Return FAIL when out of memory.
693 */
694 int
695list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
696{
697 listitem_T *ni = listitem_alloc();
698
699 if (ni == NULL)
700 return FAIL;
701 copy_tv(tv, &ni->li_tv);
702 list_insert(l, ni, item);
703 return OK;
704}
705
706 void
707list_insert(list_T *l, listitem_T *ni, listitem_T *item)
708{
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200709 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200710 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100711 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200712 list_append(l, ni);
713 else
714 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100715 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200716 ni->li_prev = item->li_prev;
717 ni->li_next = item;
718 if (item->li_prev == NULL)
719 {
720 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100721 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200722 }
723 else
724 {
725 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100726 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200727 }
728 item->li_prev = ni;
729 ++l->lv_len;
730 }
731}
732
733/*
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200734 * Flatten "list" to depth "maxdepth".
735 * It does nothing if "maxdepth" is 0.
736 * Returns FAIL when out of memory.
737 */
738 static int
739list_flatten(list_T *list, long maxdepth)
740{
741 listitem_T *item;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200742 listitem_T *tofree;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200743 int n;
744
745 if (maxdepth == 0)
746 return OK;
747 CHECK_LIST_MATERIALIZE(list);
748
749 n = 0;
750 item = list->lv_first;
751 while (item != NULL)
752 {
753 fast_breakcheck();
754 if (got_int)
755 return FAIL;
756
757 if (item->li_tv.v_type == VAR_LIST)
758 {
759 listitem_T *next = item->li_next;
760
761 vimlist_remove(list, item, item);
762 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
763 return FAIL;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200764 clear_tv(&item->li_tv);
765 tofree = item;
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200766
767 if (item->li_prev == NULL)
768 item = list->lv_first;
769 else
770 item = item->li_prev->li_next;
Bram Moolenaardcf59c32020-06-09 17:30:04 +0200771 list_free_item(list, tofree);
Bram Moolenaar077a1e62020-06-08 20:50:43 +0200772
773 if (++n >= maxdepth)
774 {
775 n = 0;
776 item = next;
777 }
778 }
779 else
780 {
781 n = 0;
782 item = item->li_next;
783 }
784 }
785
786 return OK;
787}
788
789/*
790 * "flatten(list[, {maxdepth}])" function
791 */
792 void
793f_flatten(typval_T *argvars, typval_T *rettv)
794{
795 list_T *l;
796 long maxdepth;
797 int error = FALSE;
798
799 if (argvars[0].v_type != VAR_LIST)
800 {
801 semsg(_(e_listarg), "flatten()");
802 return;
803 }
804
805 if (argvars[1].v_type == VAR_UNKNOWN)
806 maxdepth = 999999;
807 else
808 {
809 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
810 if (error)
811 return;
812 if (maxdepth < 0)
813 {
814 emsg(_("E900: maxdepth must be non-negative number"));
815 return;
816 }
817 }
818
819 l = argvars[0].vval.v_list;
820 if (l != NULL && !var_check_lock(l->lv_lock,
821 (char_u *)N_("flatten() argument"), TRUE)
822 && list_flatten(l, maxdepth) == OK)
823 copy_tv(&argvars[0], rettv);
824}
825
826/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200827 * Extend "l1" with "l2".
828 * If "bef" is NULL append at the end, otherwise insert before this item.
829 * Returns FAIL when out of memory.
830 */
831 int
832list_extend(list_T *l1, list_T *l2, listitem_T *bef)
833{
834 listitem_T *item;
835 int todo = l2->lv_len;
836
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200837 CHECK_LIST_MATERIALIZE(l1);
838 CHECK_LIST_MATERIALIZE(l2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100840 // We also quit the loop when we have inserted the original item count of
841 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200842 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
843 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
844 return FAIL;
845 return OK;
846}
847
848/*
849 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
850 * Return FAIL when out of memory.
851 */
852 int
853list_concat(list_T *l1, list_T *l2, typval_T *tv)
854{
855 list_T *l;
856
857 if (l1 == NULL || l2 == NULL)
858 return FAIL;
859
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100860 // make a copy of the first list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200861 l = list_copy(l1, FALSE, 0);
862 if (l == NULL)
863 return FAIL;
864 tv->v_type = VAR_LIST;
865 tv->vval.v_list = l;
866
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100867 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200868 return list_extend(l, l2, NULL);
869}
870
Bram Moolenaar9af78762020-06-16 11:34:42 +0200871 list_T *
872list_slice(list_T *ol, long n1, long n2)
873{
874 listitem_T *item;
875 list_T *l = list_alloc();
876
877 if (l == NULL)
878 return NULL;
879 for (item = list_find(ol, n1); n1 <= n2; ++n1)
880 {
881 if (list_append_tv(l, &item->li_tv) == FAIL)
882 {
883 list_free(l);
884 return NULL;
885 }
886 item = item->li_next;
887 }
888 return l;
889}
890
Bram Moolenaared591872020-08-15 22:14:53 +0200891 int
892list_slice_or_index(
893 list_T *list,
894 int range,
895 long n1_arg,
896 long n2_arg,
897 typval_T *rettv,
898 int verbose)
899{
900 long len = list_len(list);
901 long n1 = n1_arg;
902 long n2 = n2_arg;
903 typval_T var1;
904
905 if (n1 < 0)
906 n1 = len + n1;
907 if (n1 < 0 || n1 >= len)
908 {
909 // For a range we allow invalid values and return an empty
910 // list. A list index out of range is an error.
911 if (!range)
912 {
913 if (verbose)
914 semsg(_(e_listidx), n1);
915 return FAIL;
916 }
917 n1 = len;
918 }
919 if (range)
920 {
921 list_T *l;
922
923 if (n2 < 0)
924 n2 = len + n2;
925 else if (n2 >= len)
926 n2 = len - 1;
927 if (n2 < 0 || n2 + 1 < n1)
928 n2 = -1;
929 l = list_slice(list, n1, n2);
930 if (l == NULL)
931 return FAIL;
932 clear_tv(rettv);
933 rettv_list_set(rettv, l);
934 }
935 else
936 {
937 // copy the item to "var1" to avoid that freeing the list makes it
938 // invalid.
939 copy_tv(&list_find(list, n1)->li_tv, &var1);
940 clear_tv(rettv);
941 *rettv = var1;
942 }
943 return OK;
944}
945
Bram Moolenaarda861d62016-07-17 15:46:27 +0200946/*
947 * Make a copy of list "orig". Shallow if "deep" is FALSE.
948 * The refcount of the new list is set to 1.
949 * See item_copy() for "copyID".
950 * Returns NULL when out of memory.
951 */
952 list_T *
953list_copy(list_T *orig, int deep, int copyID)
954{
955 list_T *copy;
956 listitem_T *item;
957 listitem_T *ni;
958
959 if (orig == NULL)
960 return NULL;
961
962 copy = list_alloc();
963 if (copy != NULL)
964 {
965 if (copyID != 0)
966 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100967 // Do this before adding the items, because one of the items may
968 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200969 orig->lv_copyID = copyID;
970 orig->lv_copylist = copy;
971 }
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200972 CHECK_LIST_MATERIALIZE(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200973 for (item = orig->lv_first; item != NULL && !got_int;
974 item = item->li_next)
975 {
976 ni = listitem_alloc();
977 if (ni == NULL)
978 break;
979 if (deep)
980 {
981 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
982 {
983 vim_free(ni);
984 break;
985 }
986 }
987 else
988 copy_tv(&item->li_tv, &ni->li_tv);
989 list_append(copy, ni);
990 }
991 ++copy->lv_refcount;
992 if (item != NULL)
993 {
994 list_unref(copy);
995 copy = NULL;
996 }
997 }
998
999 return copy;
1000}
1001
1002/*
1003 * Remove items "item" to "item2" from list "l".
1004 * Does not free the listitem or the value!
1005 * This used to be called list_remove, but that conflicts with a Sun header
1006 * file.
1007 */
1008 void
1009vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1010{
1011 listitem_T *ip;
1012
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001013 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001015 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +02001016 for (ip = item; ip != NULL; ip = ip->li_next)
1017 {
1018 --l->lv_len;
1019 list_fix_watch(l, ip);
1020 if (ip == item2)
1021 break;
1022 }
1023
1024 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001025 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001026 else
1027 item2->li_next->li_prev = item->li_prev;
1028 if (item->li_prev == NULL)
1029 l->lv_first = item2->li_next;
1030 else
1031 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001032 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001033}
1034
1035/*
1036 * Return an allocated string with the string representation of a list.
1037 * May return NULL.
1038 */
1039 char_u *
1040list2string(typval_T *tv, int copyID, int restore_copyID)
1041{
1042 garray_T ga;
1043
1044 if (tv->vval.v_list == NULL)
1045 return NULL;
1046 ga_init2(&ga, (int)sizeof(char), 80);
1047 ga_append(&ga, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001048 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001049 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1050 FALSE, restore_copyID, copyID) == FAIL)
1051 {
1052 vim_free(ga.ga_data);
1053 return NULL;
1054 }
1055 ga_append(&ga, ']');
1056 ga_append(&ga, NUL);
1057 return (char_u *)ga.ga_data;
1058}
1059
1060typedef struct join_S {
1061 char_u *s;
1062 char_u *tofree;
1063} join_T;
1064
1065 static int
1066list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001067 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +02001068 list_T *l,
1069 char_u *sep,
1070 int echo_style,
1071 int restore_copyID,
1072 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001073 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +02001074{
1075 int i;
1076 join_T *p;
1077 int len;
1078 int sumlen = 0;
1079 int first = TRUE;
1080 char_u *tofree;
1081 char_u numbuf[NUMBUFLEN];
1082 listitem_T *item;
1083 char_u *s;
1084
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001085 // Stringify each item in the list.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001086 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001087 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1088 {
1089 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02001090 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001091 if (s == NULL)
1092 return FAIL;
1093
1094 len = (int)STRLEN(s);
1095 sumlen += len;
1096
1097 (void)ga_grow(join_gap, 1);
1098 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1099 if (tofree != NULL || s != numbuf)
1100 {
1101 p->s = s;
1102 p->tofree = tofree;
1103 }
1104 else
1105 {
1106 p->s = vim_strnsave(s, len);
1107 p->tofree = p->s;
1108 }
1109
1110 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001111 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +02001112 break;
1113 }
1114
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001115 // Allocate result buffer with its total size, avoid re-allocation and
1116 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001117 if (join_gap->ga_len >= 2)
1118 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1119 if (ga_grow(gap, sumlen + 2) == FAIL)
1120 return FAIL;
1121
1122 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1123 {
1124 if (first)
1125 first = FALSE;
1126 else
1127 ga_concat(gap, sep);
1128 p = ((join_T *)join_gap->ga_data) + i;
1129
1130 if (p->s != NULL)
1131 ga_concat(gap, p->s);
1132 line_breakcheck();
1133 }
1134
1135 return OK;
1136}
1137
1138/*
1139 * Join list "l" into a string in "*gap", using separator "sep".
1140 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1141 * Return FAIL or OK.
1142 */
1143 int
1144list_join(
1145 garray_T *gap,
1146 list_T *l,
1147 char_u *sep,
1148 int echo_style,
1149 int restore_copyID,
1150 int copyID)
1151{
1152 garray_T join_ga;
1153 int retval;
1154 join_T *p;
1155 int i;
1156
1157 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001158 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +02001159 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1160 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1161 copyID, &join_ga);
1162
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001163 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +02001164 if (join_ga.ga_data != NULL)
1165 {
1166 p = (join_T *)join_ga.ga_data;
1167 for (i = 0; i < join_ga.ga_len; ++i)
1168 {
1169 vim_free(p->tofree);
1170 ++p;
1171 }
1172 ga_clear(&join_ga);
1173 }
1174
1175 return retval;
1176}
1177
1178/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001179 * "join()" function
1180 */
1181 void
1182f_join(typval_T *argvars, typval_T *rettv)
1183{
1184 garray_T ga;
1185 char_u *sep;
1186
1187 if (argvars[0].v_type != VAR_LIST)
1188 {
1189 emsg(_(e_listreq));
1190 return;
1191 }
1192 if (argvars[0].vval.v_list == NULL)
1193 return;
1194 if (argvars[1].v_type == VAR_UNKNOWN)
1195 sep = (char_u *)" ";
1196 else
1197 sep = tv_get_string_chk(&argvars[1]);
1198
1199 rettv->v_type = VAR_STRING;
1200
1201 if (sep != NULL)
1202 {
1203 ga_init2(&ga, (int)sizeof(char), 80);
1204 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1205 ga_append(&ga, NUL);
1206 rettv->vval.v_string = (char_u *)ga.ga_data;
1207 }
1208 else
1209 rettv->vval.v_string = NULL;
1210}
1211
1212/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001213 * Allocate a variable for a List and fill it from "*arg".
Bram Moolenaar71478202020-06-26 22:46:27 +02001214 * "*arg" points to the "[".
Bram Moolenaarda861d62016-07-17 15:46:27 +02001215 * Return OK or FAIL.
1216 */
1217 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001218eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001219{
Bram Moolenaar71478202020-06-26 22:46:27 +02001220 int evaluate = evalarg == NULL ? FALSE
1221 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001222 list_T *l = NULL;
1223 typval_T tv;
1224 listitem_T *item;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001225 int vim9script = in_vim9script();
Bram Moolenaar71478202020-06-26 22:46:27 +02001226 int had_comma;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001227
1228 if (evaluate)
1229 {
1230 l = list_alloc();
1231 if (l == NULL)
1232 return FAIL;
1233 }
1234
Bram Moolenaar962d7212020-07-04 14:15:00 +02001235 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001236 while (**arg != ']' && **arg != NUL)
1237 {
Bram Moolenaar71478202020-06-26 22:46:27 +02001238 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001239 goto failret;
1240 if (evaluate)
1241 {
1242 item = listitem_alloc();
1243 if (item != NULL)
1244 {
1245 item->li_tv = tv;
1246 item->li_tv.v_lock = 0;
1247 list_append(l, item);
1248 }
1249 else
1250 clear_tv(&tv);
1251 }
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001252 // Legacy Vim script allowed a space before the comma.
1253 if (!vim9script)
1254 *arg = skipwhite(*arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001255
Bram Moolenaare6e03172020-06-27 16:36:05 +02001256 // the comma must come after the value
Bram Moolenaar71478202020-06-26 22:46:27 +02001257 had_comma = **arg == ',';
1258 if (had_comma)
Bram Moolenaare6e03172020-06-27 16:36:05 +02001259 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02001260 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaare6e03172020-06-27 16:36:05 +02001261 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001262 semsg(_(e_white_space_required_after), ",");
Bram Moolenaare6e03172020-06-27 16:36:05 +02001263 goto failret;
1264 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001265 *arg = skipwhite(*arg + 1);
Bram Moolenaare6e03172020-06-27 16:36:05 +02001266 }
Bram Moolenaar71478202020-06-26 22:46:27 +02001267
Bram Moolenaare6b53242020-07-01 17:28:33 +02001268 // The "]" can be on the next line. But a double quoted string may
1269 // follow, not a comment.
Bram Moolenaar962d7212020-07-04 14:15:00 +02001270 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001271 if (**arg == ']')
1272 break;
Bram Moolenaar71478202020-06-26 22:46:27 +02001273
1274 if (!had_comma)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001275 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 if (do_error)
Bram Moolenaardb199212020-08-12 18:01:53 +02001277 {
1278 if (**arg == ',')
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001279 semsg(_(e_no_white_space_allowed_before), ",");
Bram Moolenaardb199212020-08-12 18:01:53 +02001280 else
1281 semsg(_("E696: Missing comma in List: %s"), *arg);
1282 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001283 goto failret;
1284 }
Bram Moolenaarda861d62016-07-17 15:46:27 +02001285 }
1286
1287 if (**arg != ']')
1288 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001290 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001291failret:
1292 if (evaluate)
1293 list_free(l);
1294 return FAIL;
1295 }
1296
Bram Moolenaar9d489562020-07-30 20:08:50 +02001297 *arg += 1;
Bram Moolenaarda861d62016-07-17 15:46:27 +02001298 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001299 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001300
1301 return OK;
1302}
1303
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001304/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001305 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001306 */
1307 int
1308write_list(FILE *fd, list_T *list, int binary)
1309{
1310 listitem_T *li;
1311 int c;
1312 int ret = OK;
1313 char_u *s;
1314
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001315 CHECK_LIST_MATERIALIZE(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001316 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001318 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001319 {
1320 if (*s == '\n')
1321 c = putc(NUL, fd);
1322 else
1323 c = putc(*s, fd);
1324 if (c == EOF)
1325 {
1326 ret = FAIL;
1327 break;
1328 }
1329 }
1330 if (!binary || li->li_next != NULL)
1331 if (putc('\n', fd) == EOF)
1332 {
1333 ret = FAIL;
1334 break;
1335 }
1336 if (ret == FAIL)
1337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001338 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001339 break;
1340 }
1341 }
1342 return ret;
1343}
1344
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001345/*
1346 * Initialize a static list with 10 items.
1347 */
1348 void
1349init_static_list(staticList10_T *sl)
1350{
1351 list_T *l = &sl->sl_list;
1352 int i;
1353
1354 memset(sl, 0, sizeof(staticList10_T));
1355 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001356 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001357 l->lv_refcount = DO_NOT_FREE_CNT;
1358 l->lv_lock = VAR_FIXED;
1359 sl->sl_list.lv_len = 10;
1360
1361 for (i = 0; i < 10; ++i)
1362 {
1363 listitem_T *li = &sl->sl_items[i];
1364
1365 if (i == 0)
1366 li->li_prev = NULL;
1367 else
1368 li->li_prev = li - 1;
1369 if (i == 9)
1370 li->li_next = NULL;
1371 else
1372 li->li_next = li + 1;
1373 }
1374}
1375
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001376/*
1377 * "list2str()" function
1378 */
1379 void
1380f_list2str(typval_T *argvars, typval_T *rettv)
1381{
1382 list_T *l;
1383 listitem_T *li;
1384 garray_T ga;
1385 int utf8 = FALSE;
1386
1387 rettv->v_type = VAR_STRING;
1388 rettv->vval.v_string = NULL;
1389 if (argvars[0].v_type != VAR_LIST)
1390 {
1391 emsg(_(e_invarg));
1392 return;
1393 }
1394
1395 l = argvars[0].vval.v_list;
1396 if (l == NULL)
1397 return; // empty list results in empty string
1398
1399 if (argvars[1].v_type != VAR_UNKNOWN)
1400 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
1401
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001402 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001403 ga_init2(&ga, 1, 80);
1404 if (has_mbyte || utf8)
1405 {
1406 char_u buf[MB_MAXBYTES + 1];
1407 int (*char2bytes)(int, char_u *);
1408
1409 if (utf8 || enc_utf8)
1410 char2bytes = utf_char2bytes;
1411 else
1412 char2bytes = mb_char2bytes;
1413
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001414 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001415 {
1416 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1417 ga_concat(&ga, buf);
1418 }
1419 ga_append(&ga, NUL);
1420 }
1421 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1422 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001423 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001424 ga_append(&ga, tv_get_number(&li->li_tv));
1425 ga_append(&ga, NUL);
1426 }
1427
1428 rettv->v_type = VAR_STRING;
1429 rettv->vval.v_string = ga.ga_data;
1430}
1431
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001432 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001433list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1434{
1435 list_T *l;
1436 listitem_T *item, *item2;
1437 listitem_T *li;
1438 int error = FALSE;
1439 int idx;
1440
1441 if ((l = argvars[0].vval.v_list) == NULL
1442 || var_check_lock(l->lv_lock, arg_errmsg, TRUE))
1443 return;
1444
1445 idx = (long)tv_get_number_chk(&argvars[1], &error);
1446 if (error)
1447 ; // type error: do nothing, errmsg already given
1448 else if ((item = list_find(l, idx)) == NULL)
1449 semsg(_(e_listidx), idx);
1450 else
1451 {
1452 if (argvars[2].v_type == VAR_UNKNOWN)
1453 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001454 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001455 vimlist_remove(l, item, item);
1456 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001458 }
1459 else
1460 {
1461 // Remove range of items, return list with values.
1462 int end = (long)tv_get_number_chk(&argvars[2], &error);
1463
1464 if (error)
1465 ; // type error: do nothing
1466 else if ((item2 = list_find(l, end)) == NULL)
1467 semsg(_(e_listidx), end);
1468 else
1469 {
1470 int cnt = 0;
1471
1472 for (li = item; li != NULL; li = li->li_next)
1473 {
1474 ++cnt;
1475 if (li == item2)
1476 break;
1477 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001478 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001479 emsg(_(e_invrange));
1480 else
1481 {
1482 vimlist_remove(l, item, item2);
1483 if (rettv_list_alloc(rettv) == OK)
1484 {
1485 l = rettv->vval.v_list;
1486 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001487 l->lv_u.mat.lv_last = item2;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001488 item->li_prev = NULL;
1489 item2->li_next = NULL;
1490 l->lv_len = cnt;
1491 }
1492 }
1493 }
1494 }
1495 }
1496}
1497
1498static int item_compare(const void *s1, const void *s2);
1499static int item_compare2(const void *s1, const void *s2);
1500
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001501// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001502typedef struct
1503{
1504 listitem_T *item;
1505 int idx;
1506} sortItem_T;
1507
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001508// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001509typedef struct
1510{
1511 int item_compare_ic;
1512 int item_compare_numeric;
1513 int item_compare_numbers;
1514#ifdef FEAT_FLOAT
1515 int item_compare_float;
1516#endif
1517 char_u *item_compare_func;
1518 partial_T *item_compare_partial;
1519 dict_T *item_compare_selfdict;
1520 int item_compare_func_err;
1521 int item_compare_keep_zero;
1522} sortinfo_T;
1523static sortinfo_T *sortinfo = NULL;
1524#define ITEM_COMPARE_FAIL 999
1525
1526/*
1527 * Compare functions for f_sort() and f_uniq() below.
1528 */
1529 static int
1530item_compare(const void *s1, const void *s2)
1531{
1532 sortItem_T *si1, *si2;
1533 typval_T *tv1, *tv2;
1534 char_u *p1, *p2;
1535 char_u *tofree1 = NULL, *tofree2 = NULL;
1536 int res;
1537 char_u numbuf1[NUMBUFLEN];
1538 char_u numbuf2[NUMBUFLEN];
1539
1540 si1 = (sortItem_T *)s1;
1541 si2 = (sortItem_T *)s2;
1542 tv1 = &si1->item->li_tv;
1543 tv2 = &si2->item->li_tv;
1544
1545 if (sortinfo->item_compare_numbers)
1546 {
1547 varnumber_T v1 = tv_get_number(tv1);
1548 varnumber_T v2 = tv_get_number(tv2);
1549
1550 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1551 }
1552
1553#ifdef FEAT_FLOAT
1554 if (sortinfo->item_compare_float)
1555 {
1556 float_T v1 = tv_get_float(tv1);
1557 float_T v2 = tv_get_float(tv2);
1558
1559 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1560 }
1561#endif
1562
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001563 // tv2string() puts quotes around a string and allocates memory. Don't do
1564 // that for string variables. Use a single quote when comparing with a
1565 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001566 if (tv1->v_type == VAR_STRING)
1567 {
1568 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1569 p1 = (char_u *)"'";
1570 else
1571 p1 = tv1->vval.v_string;
1572 }
1573 else
1574 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1575 if (tv2->v_type == VAR_STRING)
1576 {
1577 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1578 p2 = (char_u *)"'";
1579 else
1580 p2 = tv2->vval.v_string;
1581 }
1582 else
1583 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1584 if (p1 == NULL)
1585 p1 = (char_u *)"";
1586 if (p2 == NULL)
1587 p2 = (char_u *)"";
1588 if (!sortinfo->item_compare_numeric)
1589 {
1590 if (sortinfo->item_compare_ic)
1591 res = STRICMP(p1, p2);
1592 else
1593 res = STRCMP(p1, p2);
1594 }
1595 else
1596 {
1597 double n1, n2;
1598 n1 = strtod((char *)p1, (char **)&p1);
1599 n2 = strtod((char *)p2, (char **)&p2);
1600 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1601 }
1602
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001603 // When the result would be zero, compare the item indexes. Makes the
1604 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001605 if (res == 0 && !sortinfo->item_compare_keep_zero)
1606 res = si1->idx > si2->idx ? 1 : -1;
1607
1608 vim_free(tofree1);
1609 vim_free(tofree2);
1610 return res;
1611}
1612
1613 static int
1614item_compare2(const void *s1, const void *s2)
1615{
1616 sortItem_T *si1, *si2;
1617 int res;
1618 typval_T rettv;
1619 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001620 char_u *func_name;
1621 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001622 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001623
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001624 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001625 if (sortinfo->item_compare_func_err)
1626 return 0;
1627
1628 si1 = (sortItem_T *)s1;
1629 si2 = (sortItem_T *)s2;
1630
1631 if (partial == NULL)
1632 func_name = sortinfo->item_compare_func;
1633 else
1634 func_name = partial_name(partial);
1635
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001636 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1637 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001638 copy_tv(&si1->item->li_tv, &argv[0]);
1639 copy_tv(&si2->item->li_tv, &argv[1]);
1640
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001641 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +02001642 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001643 funcexe.evaluate = TRUE;
1644 funcexe.partial = partial;
1645 funcexe.selfdict = sortinfo->item_compare_selfdict;
1646 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001647 clear_tv(&argv[0]);
1648 clear_tv(&argv[1]);
1649
1650 if (res == FAIL)
1651 res = ITEM_COMPARE_FAIL;
1652 else
1653 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1654 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001655 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001656 clear_tv(&rettv);
1657
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001658 // When the result would be zero, compare the pointers themselves. Makes
1659 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001660 if (res == 0 && !sortinfo->item_compare_keep_zero)
1661 res = si1->idx > si2->idx ? 1 : -1;
1662
1663 return res;
1664}
1665
1666/*
1667 * "sort()" or "uniq()" function
1668 */
1669 static void
1670do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1671{
1672 list_T *l;
1673 listitem_T *li;
1674 sortItem_T *ptrs;
1675 sortinfo_T *old_sortinfo;
1676 sortinfo_T info;
1677 long len;
1678 long i;
1679
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001680 // Pointer to current info struct used in compare function. Save and
1681 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001682 old_sortinfo = sortinfo;
1683 sortinfo = &info;
1684
1685 if (argvars[0].v_type != VAR_LIST)
1686 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1687 else
1688 {
1689 l = argvars[0].vval.v_list;
1690 if (l == NULL || var_check_lock(l->lv_lock,
1691 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1692 TRUE))
1693 goto theend;
1694 rettv_list_set(rettv, l);
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001695 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001696
1697 len = list_len(l);
1698 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001699 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001700
1701 info.item_compare_ic = FALSE;
1702 info.item_compare_numeric = FALSE;
1703 info.item_compare_numbers = FALSE;
1704#ifdef FEAT_FLOAT
1705 info.item_compare_float = FALSE;
1706#endif
1707 info.item_compare_func = NULL;
1708 info.item_compare_partial = NULL;
1709 info.item_compare_selfdict = NULL;
1710 if (argvars[1].v_type != VAR_UNKNOWN)
1711 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001712 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001713 if (argvars[1].v_type == VAR_FUNC)
1714 info.item_compare_func = argvars[1].vval.v_string;
1715 else if (argvars[1].v_type == VAR_PARTIAL)
1716 info.item_compare_partial = argvars[1].vval.v_partial;
1717 else
1718 {
1719 int error = FALSE;
1720
1721 i = (long)tv_get_number_chk(&argvars[1], &error);
1722 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001723 goto theend; // type error; errmsg already given
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001724 if (i == 1)
1725 info.item_compare_ic = TRUE;
1726 else if (argvars[1].v_type != VAR_NUMBER)
1727 info.item_compare_func = tv_get_string(&argvars[1]);
1728 else if (i != 0)
1729 {
1730 emsg(_(e_invarg));
1731 goto theend;
1732 }
1733 if (info.item_compare_func != NULL)
1734 {
1735 if (*info.item_compare_func == NUL)
1736 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001737 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001738 info.item_compare_func = NULL;
1739 }
1740 else if (STRCMP(info.item_compare_func, "n") == 0)
1741 {
1742 info.item_compare_func = NULL;
1743 info.item_compare_numeric = TRUE;
1744 }
1745 else if (STRCMP(info.item_compare_func, "N") == 0)
1746 {
1747 info.item_compare_func = NULL;
1748 info.item_compare_numbers = TRUE;
1749 }
1750#ifdef FEAT_FLOAT
1751 else if (STRCMP(info.item_compare_func, "f") == 0)
1752 {
1753 info.item_compare_func = NULL;
1754 info.item_compare_float = TRUE;
1755 }
1756#endif
1757 else if (STRCMP(info.item_compare_func, "i") == 0)
1758 {
1759 info.item_compare_func = NULL;
1760 info.item_compare_ic = TRUE;
1761 }
1762 }
1763 }
1764
1765 if (argvars[2].v_type != VAR_UNKNOWN)
1766 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001767 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001768 if (argvars[2].v_type != VAR_DICT)
1769 {
1770 emsg(_(e_dictreq));
1771 goto theend;
1772 }
1773 info.item_compare_selfdict = argvars[2].vval.v_dict;
1774 }
1775 }
1776
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001777 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001778 ptrs = ALLOC_MULT(sortItem_T, len);
1779 if (ptrs == NULL)
1780 goto theend;
1781
1782 i = 0;
1783 if (sort)
1784 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001785 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001786 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001787 {
1788 ptrs[i].item = li;
1789 ptrs[i].idx = i;
1790 ++i;
1791 }
1792
1793 info.item_compare_func_err = FALSE;
1794 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001795 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001796 if ((info.item_compare_func != NULL
1797 || info.item_compare_partial != NULL)
1798 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
1799 == ITEM_COMPARE_FAIL)
1800 emsg(_("E702: Sort compare function failed"));
1801 else
1802 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001803 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001804 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
1805 info.item_compare_func == NULL
1806 && info.item_compare_partial == NULL
1807 ? item_compare : item_compare2);
1808
1809 if (!info.item_compare_func_err)
1810 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001811 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001812 l->lv_first = l->lv_u.mat.lv_last
1813 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001814 l->lv_len = 0;
1815 for (i = 0; i < len; ++i)
1816 list_append(l, ptrs[i].item);
1817 }
1818 }
1819 }
1820 else
1821 {
1822 int (*item_compare_func_ptr)(const void *, const void *);
1823
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001824 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001825 info.item_compare_func_err = FALSE;
1826 info.item_compare_keep_zero = TRUE;
1827 item_compare_func_ptr = info.item_compare_func != NULL
1828 || info.item_compare_partial != NULL
1829 ? item_compare2 : item_compare;
1830
1831 for (li = l->lv_first; li != NULL && li->li_next != NULL;
1832 li = li->li_next)
1833 {
1834 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
1835 == 0)
1836 ptrs[i++].item = li;
1837 if (info.item_compare_func_err)
1838 {
1839 emsg(_("E882: Uniq compare function failed"));
1840 break;
1841 }
1842 }
1843
1844 if (!info.item_compare_func_err)
1845 {
1846 while (--i >= 0)
1847 {
1848 li = ptrs[i].item->li_next;
1849 ptrs[i].item->li_next = li->li_next;
1850 if (li->li_next != NULL)
1851 li->li_next->li_prev = ptrs[i].item;
1852 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001853 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001854 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001856 l->lv_len--;
1857 }
1858 }
1859 }
1860
1861 vim_free(ptrs);
1862 }
1863theend:
1864 sortinfo = old_sortinfo;
1865}
1866
1867/*
1868 * "sort({list})" function
1869 */
1870 void
1871f_sort(typval_T *argvars, typval_T *rettv)
1872{
1873 do_sort_uniq(argvars, rettv, TRUE);
1874}
1875
1876/*
1877 * "uniq({list})" function
1878 */
1879 void
1880f_uniq(typval_T *argvars, typval_T *rettv)
1881{
1882 do_sort_uniq(argvars, rettv, FALSE);
1883}
1884
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001885/*
1886 * Handle one item for map() and filter().
1887 */
1888 static int
1889filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
1890{
1891 typval_T rettv;
1892 typval_T argv[3];
1893 int retval = FAIL;
1894
1895 copy_tv(tv, get_vim_var_tv(VV_VAL));
1896 argv[0] = *get_vim_var_tv(VV_KEY);
1897 argv[1] = *get_vim_var_tv(VV_VAL);
1898 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
1899 goto theend;
1900 if (map)
1901 {
1902 // map(): replace the list item value
1903 clear_tv(tv);
1904 rettv.v_lock = 0;
1905 *tv = rettv;
1906 }
1907 else
1908 {
1909 int error = FALSE;
1910
1911 // filter(): when expr is zero remove the item
1912 *remp = (tv_get_number_chk(&rettv, &error) == 0);
1913 clear_tv(&rettv);
1914 // On type error, nothing has been removed; return FAIL to stop the
1915 // loop. The error message was given by tv_get_number_chk().
1916 if (error)
1917 goto theend;
1918 }
1919 retval = OK;
1920theend:
1921 clear_tv(get_vim_var_tv(VV_VAL));
1922 return retval;
1923}
1924
1925/*
1926 * Implementation of map() and filter().
1927 */
1928 static void
1929filter_map(typval_T *argvars, typval_T *rettv, int map)
1930{
1931 typval_T *expr;
1932 listitem_T *li, *nli;
1933 list_T *l = NULL;
1934 dictitem_T *di;
1935 hashtab_T *ht;
1936 hashitem_T *hi;
1937 dict_T *d = NULL;
1938 blob_T *b = NULL;
1939 int rem;
1940 int todo;
1941 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
1942 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
1943 : N_("filter() argument"));
1944 int save_did_emsg;
1945 int idx = 0;
1946
1947 if (argvars[0].v_type == VAR_BLOB)
1948 {
1949 if ((b = argvars[0].vval.v_blob) == NULL)
1950 return;
1951 }
1952 else if (argvars[0].v_type == VAR_LIST)
1953 {
1954 if ((l = argvars[0].vval.v_list) == NULL
1955 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
1956 return;
1957 }
1958 else if (argvars[0].v_type == VAR_DICT)
1959 {
1960 if ((d = argvars[0].vval.v_dict) == NULL
1961 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
1962 return;
1963 }
1964 else
1965 {
Bram Moolenaarfcb0b612020-05-26 20:22:01 +02001966 semsg(_(e_listdictblobarg), ermsg);
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001967 return;
1968 }
1969
1970 expr = &argvars[1];
1971 // On type errors, the preceding call has already displayed an error
1972 // message. Avoid a misleading error message for an empty string that
1973 // was not passed as argument.
1974 if (expr->v_type != VAR_UNKNOWN)
1975 {
1976 typval_T save_val;
1977 typval_T save_key;
1978
1979 prepare_vimvar(VV_VAL, &save_val);
1980 prepare_vimvar(VV_KEY, &save_key);
1981
1982 // We reset "did_emsg" to be able to detect whether an error
1983 // occurred during evaluation of the expression.
1984 save_did_emsg = did_emsg;
1985 did_emsg = FALSE;
1986
1987 if (argvars[0].v_type == VAR_DICT)
1988 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01001989 int prev_lock = d->dv_lock;
1990
1991 if (map && d->dv_lock == 0)
1992 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001993 ht = &d->dv_hashtab;
1994 hash_lock(ht);
1995 todo = (int)ht->ht_used;
1996 for (hi = ht->ht_array; todo > 0; ++hi)
1997 {
1998 if (!HASHITEM_EMPTY(hi))
1999 {
2000 int r;
2001
2002 --todo;
2003 di = HI2DI(hi);
2004 if (map && (var_check_lock(di->di_tv.v_lock,
2005 arg_errmsg, TRUE)
2006 || var_check_ro(di->di_flags,
2007 arg_errmsg, TRUE)))
2008 break;
2009 set_vim_var_string(VV_KEY, di->di_key, -1);
2010 r = filter_map_one(&di->di_tv, expr, map, &rem);
2011 clear_tv(get_vim_var_tv(VV_KEY));
2012 if (r == FAIL || did_emsg)
2013 break;
2014 if (!map && rem)
2015 {
2016 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
2017 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
2018 break;
2019 dictitem_remove(d, di);
2020 }
2021 }
2022 }
2023 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002024 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002025 }
2026 else if (argvars[0].v_type == VAR_BLOB)
2027 {
2028 int i;
2029 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002030 varnumber_T val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002031
2032 // set_vim_var_nr() doesn't set the type
2033 set_vim_var_type(VV_KEY, VAR_NUMBER);
2034
2035 for (i = 0; i < b->bv_ga.ga_len; i++)
2036 {
2037 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002038 val = blob_get(b, i);
2039 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002040 set_vim_var_nr(VV_KEY, idx);
2041 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
2042 break;
2043 if (tv.v_type != VAR_NUMBER)
2044 {
2045 emsg(_(e_invalblob));
2046 break;
2047 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002048 if (map)
2049 {
2050 if (tv.vval.v_number != val)
2051 blob_set(b, i, tv.vval.v_number);
2052 }
2053 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002054 {
2055 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2056
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002057 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002058 (size_t)b->bv_ga.ga_len - i - 1);
2059 --b->bv_ga.ga_len;
2060 --i;
2061 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01002062 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002063 }
2064 }
2065 else // argvars[0].v_type == VAR_LIST
2066 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002067 int prev_lock = l->lv_lock;
2068
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002069 // set_vim_var_nr() doesn't set the type
2070 set_vim_var_type(VV_KEY, VAR_NUMBER);
2071
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002072 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002073 if (map && l->lv_lock == 0)
2074 l->lv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002075 for (li = l->lv_first; li != NULL; li = nli)
2076 {
2077 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
2078 break;
2079 nli = li->li_next;
2080 set_vim_var_nr(VV_KEY, idx);
2081 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
2082 || did_emsg)
2083 break;
2084 if (!map && rem)
2085 listitem_remove(l, li);
2086 ++idx;
2087 }
Bram Moolenaardb661fb2020-01-29 22:17:16 +01002088 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002089 }
2090
2091 restore_vimvar(VV_KEY, &save_key);
2092 restore_vimvar(VV_VAL, &save_val);
2093
2094 did_emsg |= save_did_emsg;
2095 }
2096
2097 copy_tv(&argvars[0], rettv);
2098}
2099
2100/*
2101 * "filter()" function
2102 */
2103 void
2104f_filter(typval_T *argvars, typval_T *rettv)
2105{
2106 filter_map(argvars, rettv, FALSE);
2107}
2108
2109/*
2110 * "map()" function
2111 */
2112 void
2113f_map(typval_T *argvars, typval_T *rettv)
2114{
2115 filter_map(argvars, rettv, TRUE);
2116}
2117
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002118/*
2119 * "add(list, item)" function
2120 */
2121 void
2122f_add(typval_T *argvars, typval_T *rettv)
2123{
2124 list_T *l;
2125 blob_T *b;
2126
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002127 rettv->vval.v_number = 1; // Default: Failed
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002128 if (argvars[0].v_type == VAR_LIST)
2129 {
2130 if ((l = argvars[0].vval.v_list) != NULL
2131 && !var_check_lock(l->lv_lock,
2132 (char_u *)N_("add() argument"), TRUE)
2133 && list_append_tv(l, &argvars[1]) == OK)
2134 copy_tv(&argvars[0], rettv);
2135 }
2136 else if (argvars[0].v_type == VAR_BLOB)
2137 {
2138 if ((b = argvars[0].vval.v_blob) != NULL
2139 && !var_check_lock(b->bv_lock,
2140 (char_u *)N_("add() argument"), TRUE))
2141 {
2142 int error = FALSE;
2143 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
2144
2145 if (!error)
2146 {
2147 ga_append(&b->bv_ga, (int)n);
2148 copy_tv(&argvars[0], rettv);
2149 }
2150 }
2151 }
2152 else
2153 emsg(_(e_listblobreq));
2154}
2155
2156/*
2157 * "count()" function
2158 */
2159 void
2160f_count(typval_T *argvars, typval_T *rettv)
2161{
2162 long n = 0;
2163 int ic = FALSE;
2164 int error = FALSE;
2165
2166 if (argvars[2].v_type != VAR_UNKNOWN)
2167 ic = (int)tv_get_number_chk(&argvars[2], &error);
2168
2169 if (argvars[0].v_type == VAR_STRING)
2170 {
2171 char_u *expr = tv_get_string_chk(&argvars[1]);
2172 char_u *p = argvars[0].vval.v_string;
2173 char_u *next;
2174
2175 if (!error && expr != NULL && *expr != NUL && p != NULL)
2176 {
2177 if (ic)
2178 {
2179 size_t len = STRLEN(expr);
2180
2181 while (*p != NUL)
2182 {
2183 if (MB_STRNICMP(p, expr, len) == 0)
2184 {
2185 ++n;
2186 p += len;
2187 }
2188 else
2189 MB_PTR_ADV(p);
2190 }
2191 }
2192 else
2193 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2194 != NULL)
2195 {
2196 ++n;
2197 p = next + STRLEN(expr);
2198 }
2199 }
2200
2201 }
2202 else if (argvars[0].v_type == VAR_LIST)
2203 {
2204 listitem_T *li;
2205 list_T *l;
2206 long idx;
2207
2208 if ((l = argvars[0].vval.v_list) != NULL)
2209 {
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002210 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002211 li = l->lv_first;
2212 if (argvars[2].v_type != VAR_UNKNOWN)
2213 {
2214 if (argvars[3].v_type != VAR_UNKNOWN)
2215 {
2216 idx = (long)tv_get_number_chk(&argvars[3], &error);
2217 if (!error)
2218 {
2219 li = list_find(l, idx);
2220 if (li == NULL)
2221 semsg(_(e_listidx), idx);
2222 }
2223 }
2224 if (error)
2225 li = NULL;
2226 }
2227
2228 for ( ; li != NULL; li = li->li_next)
2229 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2230 ++n;
2231 }
2232 }
2233 else if (argvars[0].v_type == VAR_DICT)
2234 {
2235 int todo;
2236 dict_T *d;
2237 hashitem_T *hi;
2238
2239 if ((d = argvars[0].vval.v_dict) != NULL)
2240 {
2241 if (argvars[2].v_type != VAR_UNKNOWN)
2242 {
2243 if (argvars[3].v_type != VAR_UNKNOWN)
2244 emsg(_(e_invarg));
2245 }
2246
2247 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2248 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2249 {
2250 if (!HASHITEM_EMPTY(hi))
2251 {
2252 --todo;
2253 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2254 ++n;
2255 }
2256 }
2257 }
2258 }
2259 else
2260 semsg(_(e_listdictarg), "count()");
2261 rettv->vval.v_number = n;
2262}
2263
2264/*
2265 * "extend(list, list [, idx])" function
2266 * "extend(dict, dict [, action])" function
2267 */
2268 void
2269f_extend(typval_T *argvars, typval_T *rettv)
2270{
2271 char_u *arg_errmsg = (char_u *)N_("extend() argument");
2272
2273 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2274 {
2275 list_T *l1, *l2;
2276 listitem_T *item;
2277 long before;
2278 int error = FALSE;
2279
2280 l1 = argvars[0].vval.v_list;
2281 l2 = argvars[1].vval.v_list;
2282 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
2283 && l2 != NULL)
2284 {
2285 if (argvars[2].v_type != VAR_UNKNOWN)
2286 {
2287 before = (long)tv_get_number_chk(&argvars[2], &error);
2288 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002289 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002290
2291 if (before == l1->lv_len)
2292 item = NULL;
2293 else
2294 {
2295 item = list_find(l1, before);
2296 if (item == NULL)
2297 {
2298 semsg(_(e_listidx), before);
2299 return;
2300 }
2301 }
2302 }
2303 else
2304 item = NULL;
2305 list_extend(l1, l2, item);
2306
2307 copy_tv(&argvars[0], rettv);
2308 }
2309 }
2310 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2311 {
2312 dict_T *d1, *d2;
2313 char_u *action;
2314 int i;
2315
2316 d1 = argvars[0].vval.v_dict;
2317 d2 = argvars[1].vval.v_dict;
2318 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
2319 && d2 != NULL)
2320 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002321 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002322 if (argvars[2].v_type != VAR_UNKNOWN)
2323 {
2324 static char *(av[]) = {"keep", "force", "error"};
2325
2326 action = tv_get_string_chk(&argvars[2]);
2327 if (action == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002328 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002329 for (i = 0; i < 3; ++i)
2330 if (STRCMP(action, av[i]) == 0)
2331 break;
2332 if (i == 3)
2333 {
2334 semsg(_(e_invarg2), action);
2335 return;
2336 }
2337 }
2338 else
2339 action = (char_u *)"force";
2340
2341 dict_extend(d1, d2, action);
2342
2343 copy_tv(&argvars[0], rettv);
2344 }
2345 }
2346 else
2347 semsg(_(e_listdictarg), "extend()");
2348}
2349
2350/*
2351 * "insert()" function
2352 */
2353 void
2354f_insert(typval_T *argvars, typval_T *rettv)
2355{
2356 long before = 0;
2357 listitem_T *item;
2358 list_T *l;
2359 int error = FALSE;
2360
2361 if (argvars[0].v_type == VAR_BLOB)
2362 {
2363 int val, len;
2364 char_u *p;
2365
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02002366 if (argvars[0].vval.v_blob == NULL)
2367 return;
2368
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002369 len = blob_len(argvars[0].vval.v_blob);
2370 if (argvars[2].v_type != VAR_UNKNOWN)
2371 {
2372 before = (long)tv_get_number_chk(&argvars[2], &error);
2373 if (error)
2374 return; // type error; errmsg already given
2375 if (before < 0 || before > len)
2376 {
2377 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2378 return;
2379 }
2380 }
2381 val = tv_get_number_chk(&argvars[1], &error);
2382 if (error)
2383 return;
2384 if (val < 0 || val > 255)
2385 {
2386 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2387 return;
2388 }
2389
2390 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
2391 return;
2392 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2393 mch_memmove(p + before + 1, p + before, (size_t)len - before);
2394 *(p + before) = val;
2395 ++argvars[0].vval.v_blob->bv_ga.ga_len;
2396
2397 copy_tv(&argvars[0], rettv);
2398 }
2399 else if (argvars[0].v_type != VAR_LIST)
2400 semsg(_(e_listblobarg), "insert()");
2401 else if ((l = argvars[0].vval.v_list) != NULL
2402 && !var_check_lock(l->lv_lock,
2403 (char_u *)N_("insert() argument"), TRUE))
2404 {
2405 if (argvars[2].v_type != VAR_UNKNOWN)
2406 before = (long)tv_get_number_chk(&argvars[2], &error);
2407 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002408 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002409
2410 if (before == l->lv_len)
2411 item = NULL;
2412 else
2413 {
2414 item = list_find(l, before);
2415 if (item == NULL)
2416 {
2417 semsg(_(e_listidx), before);
2418 l = NULL;
2419 }
2420 }
2421 if (l != NULL)
2422 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02002423 (void)list_insert_tv(l, &argvars[1], item);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002424 copy_tv(&argvars[0], rettv);
2425 }
2426 }
2427}
2428
2429/*
2430 * "remove()" function
2431 */
2432 void
2433f_remove(typval_T *argvars, typval_T *rettv)
2434{
2435 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2436
2437 if (argvars[0].v_type == VAR_DICT)
2438 dict_remove(argvars, rettv, arg_errmsg);
2439 else if (argvars[0].v_type == VAR_BLOB)
2440 blob_remove(argvars, rettv);
2441 else if (argvars[0].v_type == VAR_LIST)
2442 list_remove(argvars, rettv, arg_errmsg);
2443 else
2444 semsg(_(e_listdictblobarg), "remove()");
2445}
2446
2447/*
2448 * "reverse({list})" function
2449 */
2450 void
2451f_reverse(typval_T *argvars, typval_T *rettv)
2452{
2453 list_T *l;
2454 listitem_T *li, *ni;
2455
2456 if (argvars[0].v_type == VAR_BLOB)
2457 {
2458 blob_T *b = argvars[0].vval.v_blob;
2459 int i, len = blob_len(b);
2460
2461 for (i = 0; i < len / 2; i++)
2462 {
2463 int tmp = blob_get(b, i);
2464
2465 blob_set(b, i, blob_get(b, len - i - 1));
2466 blob_set(b, len - i - 1, tmp);
2467 }
2468 rettv_blob_set(rettv, b);
2469 return;
2470 }
2471
2472 if (argvars[0].v_type != VAR_LIST)
2473 semsg(_(e_listblobarg), "reverse()");
2474 else if ((l = argvars[0].vval.v_list) != NULL
2475 && !var_check_lock(l->lv_lock,
2476 (char_u *)N_("reverse() argument"), TRUE))
2477 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002478 if (l->lv_first == &range_list_item)
2479 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002480 varnumber_T new_start = l->lv_u.nonmat.lv_start
2481 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2482 l->lv_u.nonmat.lv_end = new_start
2483 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2484 l->lv_u.nonmat.lv_start = new_start;
2485 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002486 rettv_list_set(rettv, l);
2487 return;
2488 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002489 li = l->lv_u.mat.lv_last;
2490 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002491 l->lv_len = 0;
2492 while (li != NULL)
2493 {
2494 ni = li->li_prev;
2495 list_append(l, li);
2496 li = ni;
2497 }
2498 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002499 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002500 }
2501}
2502
Bram Moolenaar85629982020-06-01 18:39:20 +02002503/*
2504 * "reduce(list, { accumlator, element -> value } [, initial])" function
2505 */
2506 void
2507f_reduce(typval_T *argvars, typval_T *rettv)
2508{
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002509 typval_T initial;
Bram Moolenaar85629982020-06-01 18:39:20 +02002510 char_u *func_name;
2511 partial_T *partial = NULL;
2512 funcexe_T funcexe;
2513 typval_T argv[3];
2514
2515 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
2516 {
2517 emsg(_(e_listblobreq));
2518 return;
2519 }
2520
2521 if (argvars[1].v_type == VAR_FUNC)
2522 func_name = argvars[1].vval.v_string;
2523 else if (argvars[1].v_type == VAR_PARTIAL)
2524 {
2525 partial = argvars[1].vval.v_partial;
2526 func_name = partial_name(partial);
2527 }
2528 else
2529 func_name = tv_get_string(&argvars[1]);
2530 if (*func_name == NUL)
2531 return; // type error or empty name
2532
2533 vim_memset(&funcexe, 0, sizeof(funcexe));
2534 funcexe.evaluate = TRUE;
2535 funcexe.partial = partial;
2536
2537 if (argvars[0].v_type == VAR_LIST)
2538 {
2539 list_T *l = argvars[0].vval.v_list;
2540 listitem_T *li = NULL;
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002541 int r;
Bram Moolenaarca275a02020-06-24 22:07:46 +02002542 int called_emsg_start = called_emsg;
Bram Moolenaar85629982020-06-01 18:39:20 +02002543
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002544 if (l != NULL)
2545 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar85629982020-06-01 18:39:20 +02002546 if (argvars[2].v_type == VAR_UNKNOWN)
2547 {
2548 if (l == NULL || l->lv_first == NULL)
2549 {
2550 semsg(_(e_reduceempty), "List");
2551 return;
2552 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002553 initial = l->lv_first->li_tv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002554 li = l->lv_first->li_next;
2555 }
2556 else
2557 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002558 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002559 if (l != NULL)
2560 li = l->lv_first;
2561 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002562 copy_tv(&initial, rettv);
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002563
2564 if (l != NULL)
Bram Moolenaar85629982020-06-01 18:39:20 +02002565 {
Bram Moolenaarfda20c42020-06-29 20:09:36 +02002566 int prev_locked = l->lv_lock;
2567
2568 l->lv_lock = VAR_FIXED; // disallow the list changing here
2569 for ( ; li != NULL; li = li->li_next)
2570 {
2571 argv[0] = *rettv;
2572 argv[1] = li->li_tv;
2573 rettv->v_type = VAR_UNKNOWN;
2574 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
2575 clear_tv(&argv[0]);
2576 if (r == FAIL || called_emsg != called_emsg_start)
2577 break;
2578 }
2579 l->lv_lock = prev_locked;
Bram Moolenaar85629982020-06-01 18:39:20 +02002580 }
2581 }
2582 else
2583 {
2584 blob_T *b = argvars[0].vval.v_blob;
2585 int i;
2586
2587 if (argvars[2].v_type == VAR_UNKNOWN)
2588 {
2589 if (b == NULL || b->bv_ga.ga_len == 0)
2590 {
2591 semsg(_(e_reduceempty), "Blob");
2592 return;
2593 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002594 initial.v_type = VAR_NUMBER;
2595 initial.vval.v_number = blob_get(b, 0);
Bram Moolenaar85629982020-06-01 18:39:20 +02002596 i = 1;
2597 }
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002598 else if (argvars[2].v_type != VAR_NUMBER)
2599 {
2600 emsg(_(e_number_exp));
2601 return;
2602 }
Bram Moolenaar85629982020-06-01 18:39:20 +02002603 else
2604 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002605 initial = argvars[2];
Bram Moolenaar85629982020-06-01 18:39:20 +02002606 i = 0;
2607 }
2608
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002609 copy_tv(&initial, rettv);
Bram Moolenaar85629982020-06-01 18:39:20 +02002610 if (b != NULL)
2611 {
2612 for ( ; i < b->bv_ga.ga_len; i++)
2613 {
Bram Moolenaar48b1c212020-06-01 20:11:02 +02002614 argv[0] = *rettv;
Bram Moolenaar85629982020-06-01 18:39:20 +02002615 argv[1].v_type = VAR_NUMBER;
2616 argv[1].vval.v_number = blob_get(b, i);
2617 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
2618 return;
Bram Moolenaar85629982020-06-01 18:39:20 +02002619 }
2620 }
2621 }
2622}
2623
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002624#endif // defined(FEAT_EVAL)