blob: 9fe53841434710a786a959a04fdca924d3f10ead [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
371 if (l1 == NULL || l2 == NULL)
372 return FALSE;
373 if (l1 == l2)
374 return TRUE;
375 if (list_len(l1) != list_len(l2))
376 return FALSE;
377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100378 range_list_materialize(l1);
379 range_list_materialize(l2);
380
Bram Moolenaarda861d62016-07-17 15:46:27 +0200381 for (item1 = l1->lv_first, item2 = l2->lv_first;
382 item1 != NULL && item2 != NULL;
383 item1 = item1->li_next, item2 = item2->li_next)
384 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
385 return FALSE;
386 return item1 == NULL && item2 == NULL;
387}
388
389/*
390 * Locate item with index "n" in list "l" and return it.
391 * A negative index is counted from the end; -1 is the last item.
392 * Returns NULL when "n" is out of range.
393 */
394 listitem_T *
395list_find(list_T *l, long n)
396{
397 listitem_T *item;
398 long idx;
399
400 if (l == NULL)
401 return NULL;
402
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100403 // Negative index is relative to the end.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200404 if (n < 0)
405 n = l->lv_len + n;
406
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100407 // Check for index out of range.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200408 if (n < 0 || n >= l->lv_len)
409 return NULL;
410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411 range_list_materialize(l);
412
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100413 // When there is a cached index may start search from there.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100414 if (l->lv_u.mat.lv_idx_item != NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200415 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100416 if (n < l->lv_u.mat.lv_idx / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200417 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100418 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200419 item = l->lv_first;
420 idx = 0;
421 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100422 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200423 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100424 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100425 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200426 idx = l->lv_len - 1;
427 }
428 else
429 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100430 // closest to the cached index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100431 item = l->lv_u.mat.lv_idx_item;
432 idx = l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200433 }
434 }
435 else
436 {
437 if (n < l->lv_len / 2)
438 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100439 // closest to the start of the list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200440 item = l->lv_first;
441 idx = 0;
442 }
443 else
444 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100445 // closest to the end of the list
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100446 item = l->lv_u.mat.lv_last;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200447 idx = l->lv_len - 1;
448 }
449 }
450
451 while (n > idx)
452 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100453 // search forward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200454 item = item->li_next;
455 ++idx;
456 }
457 while (n < idx)
458 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100459 // search backward
Bram Moolenaarda861d62016-07-17 15:46:27 +0200460 item = item->li_prev;
461 --idx;
462 }
463
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100464 // cache the used index
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100465 l->lv_u.mat.lv_idx = idx;
466 l->lv_u.mat.lv_idx_item = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200467
468 return item;
469}
470
471/*
472 * Get list item "l[idx]" as a number.
473 */
474 long
475list_find_nr(
476 list_T *l,
477 long idx,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100478 int *errorp) // set to TRUE when something wrong
Bram Moolenaarda861d62016-07-17 15:46:27 +0200479{
480 listitem_T *li;
481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 if (l != NULL && l->lv_first == &range_list_item)
483 {
484 long n = idx;
485
486 // not materialized range() list: compute the value.
487 // Negative index is relative to the end.
488 if (n < 0)
489 n = l->lv_len + n;
490
491 // Check for index out of range.
492 if (n < 0 || n >= l->lv_len)
493 {
494 if (errorp != NULL)
495 *errorp = TRUE;
496 return -1L;
497 }
498
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100499 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 }
501
Bram Moolenaarda861d62016-07-17 15:46:27 +0200502 li = list_find(l, idx);
503 if (li == NULL)
504 {
505 if (errorp != NULL)
506 *errorp = TRUE;
507 return -1L;
508 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100509 return (long)tv_get_number_chk(&li->li_tv, errorp);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200510}
511
512/*
513 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
514 */
515 char_u *
516list_find_str(list_T *l, long idx)
517{
518 listitem_T *li;
519
520 li = list_find(l, idx - 1);
521 if (li == NULL)
522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100523 semsg(_(e_listidx), idx);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200524 return NULL;
525 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100526 return tv_get_string(&li->li_tv);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200527}
528
529/*
530 * Locate "item" list "l" and return its index.
531 * Returns -1 when "item" is not in the list.
532 */
533 long
534list_idx_of_item(list_T *l, listitem_T *item)
535{
536 long idx = 0;
537 listitem_T *li;
538
539 if (l == NULL)
540 return -1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541 range_list_materialize(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200542 idx = 0;
543 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
544 ++idx;
545 if (li == NULL)
546 return -1;
547 return idx;
548}
549
550/*
551 * Append item "item" to the end of list "l".
552 */
553 void
554list_append(list_T *l, listitem_T *item)
555{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556 range_list_materialize(l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100557 if (l->lv_u.mat.lv_last == NULL)
Bram Moolenaarda861d62016-07-17 15:46:27 +0200558 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100559 // empty list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200560 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100561 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200562 item->li_prev = NULL;
563 }
564 else
565 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100566 l->lv_u.mat.lv_last->li_next = item;
567 item->li_prev = l->lv_u.mat.lv_last;
568 l->lv_u.mat.lv_last = item;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200569 }
570 ++l->lv_len;
571 item->li_next = NULL;
572}
573
574/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 * Append typval_T "tv" to the end of list "l". "tv" is copied.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200576 * Return FAIL when out of memory.
577 */
578 int
579list_append_tv(list_T *l, typval_T *tv)
580{
581 listitem_T *li = listitem_alloc();
582
583 if (li == NULL)
584 return FAIL;
585 copy_tv(tv, &li->li_tv);
586 list_append(l, li);
587 return OK;
588}
589
590/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 * As list_append_tv() but move the value instead of copying it.
592 * Return FAIL when out of memory.
593 */
594 int
595list_append_tv_move(list_T *l, typval_T *tv)
596{
597 listitem_T *li = listitem_alloc();
598
599 if (li == NULL)
600 return FAIL;
601 li->li_tv = *tv;
602 list_append(l, li);
603 return OK;
604}
605
606/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200607 * Add a dictionary to a list. Used by getqflist().
608 * Return FAIL when out of memory.
609 */
610 int
611list_append_dict(list_T *list, dict_T *dict)
612{
613 listitem_T *li = listitem_alloc();
614
615 if (li == NULL)
616 return FAIL;
617 li->li_tv.v_type = VAR_DICT;
618 li->li_tv.v_lock = 0;
619 li->li_tv.vval.v_dict = dict;
620 list_append(list, li);
621 ++dict->dv_refcount;
622 return OK;
623}
624
625/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100626 * Append list2 to list1.
627 * Return FAIL when out of memory.
628 */
629 int
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +0200630list_append_list(list_T *list1, list_T *list2)
Bram Moolenaar4f505882018-02-10 21:06:32 +0100631{
632 listitem_T *li = listitem_alloc();
633
634 if (li == NULL)
635 return FAIL;
636 li->li_tv.v_type = VAR_LIST;
637 li->li_tv.v_lock = 0;
638 li->li_tv.vval.v_list = list2;
639 list_append(list1, li);
640 ++list2->lv_refcount;
641 return OK;
642}
643
644/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200645 * Make a copy of "str" and append it as an item to list "l".
646 * When "len" >= 0 use "str[len]".
647 * Returns FAIL when out of memory.
648 */
649 int
650list_append_string(list_T *l, char_u *str, int len)
651{
652 listitem_T *li = listitem_alloc();
653
654 if (li == NULL)
655 return FAIL;
656 list_append(l, li);
657 li->li_tv.v_type = VAR_STRING;
658 li->li_tv.v_lock = 0;
659 if (str == NULL)
660 li->li_tv.vval.v_string = NULL;
661 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
662 : vim_strsave(str))) == NULL)
663 return FAIL;
664 return OK;
665}
666
667/*
668 * Append "n" to list "l".
669 * Returns FAIL when out of memory.
670 */
671 int
672list_append_number(list_T *l, varnumber_T n)
673{
674 listitem_T *li;
675
676 li = listitem_alloc();
677 if (li == NULL)
678 return FAIL;
679 li->li_tv.v_type = VAR_NUMBER;
680 li->li_tv.v_lock = 0;
681 li->li_tv.vval.v_number = n;
682 list_append(l, li);
683 return OK;
684}
685
686/*
687 * Insert typval_T "tv" in list "l" before "item".
688 * If "item" is NULL append at the end.
689 * Return FAIL when out of memory.
690 */
691 int
692list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
693{
694 listitem_T *ni = listitem_alloc();
695
696 if (ni == NULL)
697 return FAIL;
698 copy_tv(tv, &ni->li_tv);
699 list_insert(l, ni, item);
700 return OK;
701}
702
703 void
704list_insert(list_T *l, listitem_T *ni, listitem_T *item)
705{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 range_list_materialize(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200707 if (item == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100708 // Append new item at end of list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200709 list_append(l, ni);
710 else
711 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100712 // Insert new item before existing item.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200713 ni->li_prev = item->li_prev;
714 ni->li_next = item;
715 if (item->li_prev == NULL)
716 {
717 l->lv_first = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100718 ++l->lv_u.mat.lv_idx;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200719 }
720 else
721 {
722 item->li_prev->li_next = ni;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100723 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200724 }
725 item->li_prev = ni;
726 ++l->lv_len;
727 }
728}
729
730/*
731 * Extend "l1" with "l2".
732 * If "bef" is NULL append at the end, otherwise insert before this item.
733 * Returns FAIL when out of memory.
734 */
735 int
736list_extend(list_T *l1, list_T *l2, listitem_T *bef)
737{
738 listitem_T *item;
739 int todo = l2->lv_len;
740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 range_list_materialize(l1);
742 range_list_materialize(l2);
743
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100744 // We also quit the loop when we have inserted the original item count of
745 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200746 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
747 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
748 return FAIL;
749 return OK;
750}
751
752/*
753 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
754 * Return FAIL when out of memory.
755 */
756 int
757list_concat(list_T *l1, list_T *l2, typval_T *tv)
758{
759 list_T *l;
760
761 if (l1 == NULL || l2 == NULL)
762 return FAIL;
763
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100764 // make a copy of the first list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200765 l = list_copy(l1, FALSE, 0);
766 if (l == NULL)
767 return FAIL;
768 tv->v_type = VAR_LIST;
769 tv->vval.v_list = l;
770
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100771 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200772 return list_extend(l, l2, NULL);
773}
774
775/*
776 * Make a copy of list "orig". Shallow if "deep" is FALSE.
777 * The refcount of the new list is set to 1.
778 * See item_copy() for "copyID".
779 * Returns NULL when out of memory.
780 */
781 list_T *
782list_copy(list_T *orig, int deep, int copyID)
783{
784 list_T *copy;
785 listitem_T *item;
786 listitem_T *ni;
787
788 if (orig == NULL)
789 return NULL;
790
791 copy = list_alloc();
792 if (copy != NULL)
793 {
794 if (copyID != 0)
795 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100796 // Do this before adding the items, because one of the items may
797 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200798 orig->lv_copyID = copyID;
799 orig->lv_copylist = copy;
800 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801 range_list_materialize(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200802 for (item = orig->lv_first; item != NULL && !got_int;
803 item = item->li_next)
804 {
805 ni = listitem_alloc();
806 if (ni == NULL)
807 break;
808 if (deep)
809 {
810 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
811 {
812 vim_free(ni);
813 break;
814 }
815 }
816 else
817 copy_tv(&item->li_tv, &ni->li_tv);
818 list_append(copy, ni);
819 }
820 ++copy->lv_refcount;
821 if (item != NULL)
822 {
823 list_unref(copy);
824 copy = NULL;
825 }
826 }
827
828 return copy;
829}
830
831/*
832 * Remove items "item" to "item2" from list "l".
833 * Does not free the listitem or the value!
834 * This used to be called list_remove, but that conflicts with a Sun header
835 * file.
836 */
837 void
838vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
839{
840 listitem_T *ip;
841
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842 range_list_materialize(l);
843
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100844 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +0200845 for (ip = item; ip != NULL; ip = ip->li_next)
846 {
847 --l->lv_len;
848 list_fix_watch(l, ip);
849 if (ip == item2)
850 break;
851 }
852
853 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100854 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200855 else
856 item2->li_next->li_prev = item->li_prev;
857 if (item->li_prev == NULL)
858 l->lv_first = item2->li_next;
859 else
860 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100861 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200862}
863
864/*
865 * Return an allocated string with the string representation of a list.
866 * May return NULL.
867 */
868 char_u *
869list2string(typval_T *tv, int copyID, int restore_copyID)
870{
871 garray_T ga;
872
873 if (tv->vval.v_list == NULL)
874 return NULL;
875 ga_init2(&ga, (int)sizeof(char), 80);
876 ga_append(&ga, '[');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 range_list_materialize(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200878 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
879 FALSE, restore_copyID, copyID) == FAIL)
880 {
881 vim_free(ga.ga_data);
882 return NULL;
883 }
884 ga_append(&ga, ']');
885 ga_append(&ga, NUL);
886 return (char_u *)ga.ga_data;
887}
888
889typedef struct join_S {
890 char_u *s;
891 char_u *tofree;
892} join_T;
893
894 static int
895list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100896 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +0200897 list_T *l,
898 char_u *sep,
899 int echo_style,
900 int restore_copyID,
901 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100902 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +0200903{
904 int i;
905 join_T *p;
906 int len;
907 int sumlen = 0;
908 int first = TRUE;
909 char_u *tofree;
910 char_u numbuf[NUMBUFLEN];
911 listitem_T *item;
912 char_u *s;
913
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100914 // Stringify each item in the list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 range_list_materialize(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200916 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
917 {
918 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +0200919 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200920 if (s == NULL)
921 return FAIL;
922
923 len = (int)STRLEN(s);
924 sumlen += len;
925
926 (void)ga_grow(join_gap, 1);
927 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
928 if (tofree != NULL || s != numbuf)
929 {
930 p->s = s;
931 p->tofree = tofree;
932 }
933 else
934 {
935 p->s = vim_strnsave(s, len);
936 p->tofree = p->s;
937 }
938
939 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100940 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +0200941 break;
942 }
943
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100944 // Allocate result buffer with its total size, avoid re-allocation and
945 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200946 if (join_gap->ga_len >= 2)
947 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
948 if (ga_grow(gap, sumlen + 2) == FAIL)
949 return FAIL;
950
951 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
952 {
953 if (first)
954 first = FALSE;
955 else
956 ga_concat(gap, sep);
957 p = ((join_T *)join_gap->ga_data) + i;
958
959 if (p->s != NULL)
960 ga_concat(gap, p->s);
961 line_breakcheck();
962 }
963
964 return OK;
965}
966
967/*
968 * Join list "l" into a string in "*gap", using separator "sep".
969 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
970 * Return FAIL or OK.
971 */
972 int
973list_join(
974 garray_T *gap,
975 list_T *l,
976 char_u *sep,
977 int echo_style,
978 int restore_copyID,
979 int copyID)
980{
981 garray_T join_ga;
982 int retval;
983 join_T *p;
984 int i;
985
986 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100987 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +0200988 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
989 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
990 copyID, &join_ga);
991
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100992 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200993 if (join_ga.ga_data != NULL)
994 {
995 p = (join_T *)join_ga.ga_data;
996 for (i = 0; i < join_ga.ga_len; ++i)
997 {
998 vim_free(p->tofree);
999 ++p;
1000 }
1001 ga_clear(&join_ga);
1002 }
1003
1004 return retval;
1005}
1006
1007/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001008 * "join()" function
1009 */
1010 void
1011f_join(typval_T *argvars, typval_T *rettv)
1012{
1013 garray_T ga;
1014 char_u *sep;
1015
1016 if (argvars[0].v_type != VAR_LIST)
1017 {
1018 emsg(_(e_listreq));
1019 return;
1020 }
1021 if (argvars[0].vval.v_list == NULL)
1022 return;
1023 if (argvars[1].v_type == VAR_UNKNOWN)
1024 sep = (char_u *)" ";
1025 else
1026 sep = tv_get_string_chk(&argvars[1]);
1027
1028 rettv->v_type = VAR_STRING;
1029
1030 if (sep != NULL)
1031 {
1032 ga_init2(&ga, (int)sizeof(char), 80);
1033 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1034 ga_append(&ga, NUL);
1035 rettv->vval.v_string = (char_u *)ga.ga_data;
1036 }
1037 else
1038 rettv->vval.v_string = NULL;
1039}
1040
1041/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001042 * Allocate a variable for a List and fill it from "*arg".
1043 * Return OK or FAIL.
1044 */
1045 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046get_list_tv(char_u **arg, typval_T *rettv, int evaluate, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001047{
1048 list_T *l = NULL;
1049 typval_T tv;
1050 listitem_T *item;
1051
1052 if (evaluate)
1053 {
1054 l = list_alloc();
1055 if (l == NULL)
1056 return FAIL;
1057 }
1058
1059 *arg = skipwhite(*arg + 1);
1060 while (**arg != ']' && **arg != NUL)
1061 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001062 if (eval1(arg, &tv, evaluate) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001063 goto failret;
1064 if (evaluate)
1065 {
1066 item = listitem_alloc();
1067 if (item != NULL)
1068 {
1069 item->li_tv = tv;
1070 item->li_tv.v_lock = 0;
1071 list_append(l, item);
1072 }
1073 else
1074 clear_tv(&tv);
1075 }
1076
1077 if (**arg == ']')
1078 break;
1079 if (**arg != ',')
1080 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 if (do_error)
1082 semsg(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001083 goto failret;
1084 }
1085 *arg = skipwhite(*arg + 1);
1086 }
1087
1088 if (**arg != ']')
1089 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001091 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001092failret:
1093 if (evaluate)
1094 list_free(l);
1095 return FAIL;
1096 }
1097
1098 *arg = skipwhite(*arg + 1);
1099 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001100 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001101
1102 return OK;
1103}
1104
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001105/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001106 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001107 */
1108 int
1109write_list(FILE *fd, list_T *list, int binary)
1110{
1111 listitem_T *li;
1112 int c;
1113 int ret = OK;
1114 char_u *s;
1115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 range_list_materialize(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001117 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001118 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001119 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001120 {
1121 if (*s == '\n')
1122 c = putc(NUL, fd);
1123 else
1124 c = putc(*s, fd);
1125 if (c == EOF)
1126 {
1127 ret = FAIL;
1128 break;
1129 }
1130 }
1131 if (!binary || li->li_next != NULL)
1132 if (putc('\n', fd) == EOF)
1133 {
1134 ret = FAIL;
1135 break;
1136 }
1137 if (ret == FAIL)
1138 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001139 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001140 break;
1141 }
1142 }
1143 return ret;
1144}
1145
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001146/*
1147 * Initialize a static list with 10 items.
1148 */
1149 void
1150init_static_list(staticList10_T *sl)
1151{
1152 list_T *l = &sl->sl_list;
1153 int i;
1154
1155 memset(sl, 0, sizeof(staticList10_T));
1156 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001157 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001158 l->lv_refcount = DO_NOT_FREE_CNT;
1159 l->lv_lock = VAR_FIXED;
1160 sl->sl_list.lv_len = 10;
1161
1162 for (i = 0; i < 10; ++i)
1163 {
1164 listitem_T *li = &sl->sl_items[i];
1165
1166 if (i == 0)
1167 li->li_prev = NULL;
1168 else
1169 li->li_prev = li - 1;
1170 if (i == 9)
1171 li->li_next = NULL;
1172 else
1173 li->li_next = li + 1;
1174 }
1175}
1176
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001177/*
1178 * "list2str()" function
1179 */
1180 void
1181f_list2str(typval_T *argvars, typval_T *rettv)
1182{
1183 list_T *l;
1184 listitem_T *li;
1185 garray_T ga;
1186 int utf8 = FALSE;
1187
1188 rettv->v_type = VAR_STRING;
1189 rettv->vval.v_string = NULL;
1190 if (argvars[0].v_type != VAR_LIST)
1191 {
1192 emsg(_(e_invarg));
1193 return;
1194 }
1195
1196 l = argvars[0].vval.v_list;
1197 if (l == NULL)
1198 return; // empty list results in empty string
1199
1200 if (argvars[1].v_type != VAR_UNKNOWN)
1201 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
1202
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 range_list_materialize(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001204 ga_init2(&ga, 1, 80);
1205 if (has_mbyte || utf8)
1206 {
1207 char_u buf[MB_MAXBYTES + 1];
1208 int (*char2bytes)(int, char_u *);
1209
1210 if (utf8 || enc_utf8)
1211 char2bytes = utf_char2bytes;
1212 else
1213 char2bytes = mb_char2bytes;
1214
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001215 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001216 {
1217 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1218 ga_concat(&ga, buf);
1219 }
1220 ga_append(&ga, NUL);
1221 }
1222 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1223 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001224 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001225 ga_append(&ga, tv_get_number(&li->li_tv));
1226 ga_append(&ga, NUL);
1227 }
1228
1229 rettv->v_type = VAR_STRING;
1230 rettv->vval.v_string = ga.ga_data;
1231}
1232
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001233 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001234list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1235{
1236 list_T *l;
1237 listitem_T *item, *item2;
1238 listitem_T *li;
1239 int error = FALSE;
1240 int idx;
1241
1242 if ((l = argvars[0].vval.v_list) == NULL
1243 || var_check_lock(l->lv_lock, arg_errmsg, TRUE))
1244 return;
1245
1246 idx = (long)tv_get_number_chk(&argvars[1], &error);
1247 if (error)
1248 ; // type error: do nothing, errmsg already given
1249 else if ((item = list_find(l, idx)) == NULL)
1250 semsg(_(e_listidx), idx);
1251 else
1252 {
1253 if (argvars[2].v_type == VAR_UNKNOWN)
1254 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001255 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001256 vimlist_remove(l, item, item);
1257 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001259 }
1260 else
1261 {
1262 // Remove range of items, return list with values.
1263 int end = (long)tv_get_number_chk(&argvars[2], &error);
1264
1265 if (error)
1266 ; // type error: do nothing
1267 else if ((item2 = list_find(l, end)) == NULL)
1268 semsg(_(e_listidx), end);
1269 else
1270 {
1271 int cnt = 0;
1272
1273 for (li = item; li != NULL; li = li->li_next)
1274 {
1275 ++cnt;
1276 if (li == item2)
1277 break;
1278 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001279 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001280 emsg(_(e_invrange));
1281 else
1282 {
1283 vimlist_remove(l, item, item2);
1284 if (rettv_list_alloc(rettv) == OK)
1285 {
1286 l = rettv->vval.v_list;
1287 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001288 l->lv_u.mat.lv_last = item2;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001289 item->li_prev = NULL;
1290 item2->li_next = NULL;
1291 l->lv_len = cnt;
1292 }
1293 }
1294 }
1295 }
1296 }
1297}
1298
1299static int item_compare(const void *s1, const void *s2);
1300static int item_compare2(const void *s1, const void *s2);
1301
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001302// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001303typedef struct
1304{
1305 listitem_T *item;
1306 int idx;
1307} sortItem_T;
1308
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001309// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001310typedef struct
1311{
1312 int item_compare_ic;
1313 int item_compare_numeric;
1314 int item_compare_numbers;
1315#ifdef FEAT_FLOAT
1316 int item_compare_float;
1317#endif
1318 char_u *item_compare_func;
1319 partial_T *item_compare_partial;
1320 dict_T *item_compare_selfdict;
1321 int item_compare_func_err;
1322 int item_compare_keep_zero;
1323} sortinfo_T;
1324static sortinfo_T *sortinfo = NULL;
1325#define ITEM_COMPARE_FAIL 999
1326
1327/*
1328 * Compare functions for f_sort() and f_uniq() below.
1329 */
1330 static int
1331item_compare(const void *s1, const void *s2)
1332{
1333 sortItem_T *si1, *si2;
1334 typval_T *tv1, *tv2;
1335 char_u *p1, *p2;
1336 char_u *tofree1 = NULL, *tofree2 = NULL;
1337 int res;
1338 char_u numbuf1[NUMBUFLEN];
1339 char_u numbuf2[NUMBUFLEN];
1340
1341 si1 = (sortItem_T *)s1;
1342 si2 = (sortItem_T *)s2;
1343 tv1 = &si1->item->li_tv;
1344 tv2 = &si2->item->li_tv;
1345
1346 if (sortinfo->item_compare_numbers)
1347 {
1348 varnumber_T v1 = tv_get_number(tv1);
1349 varnumber_T v2 = tv_get_number(tv2);
1350
1351 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1352 }
1353
1354#ifdef FEAT_FLOAT
1355 if (sortinfo->item_compare_float)
1356 {
1357 float_T v1 = tv_get_float(tv1);
1358 float_T v2 = tv_get_float(tv2);
1359
1360 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1361 }
1362#endif
1363
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001364 // tv2string() puts quotes around a string and allocates memory. Don't do
1365 // that for string variables. Use a single quote when comparing with a
1366 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001367 if (tv1->v_type == VAR_STRING)
1368 {
1369 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1370 p1 = (char_u *)"'";
1371 else
1372 p1 = tv1->vval.v_string;
1373 }
1374 else
1375 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1376 if (tv2->v_type == VAR_STRING)
1377 {
1378 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1379 p2 = (char_u *)"'";
1380 else
1381 p2 = tv2->vval.v_string;
1382 }
1383 else
1384 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1385 if (p1 == NULL)
1386 p1 = (char_u *)"";
1387 if (p2 == NULL)
1388 p2 = (char_u *)"";
1389 if (!sortinfo->item_compare_numeric)
1390 {
1391 if (sortinfo->item_compare_ic)
1392 res = STRICMP(p1, p2);
1393 else
1394 res = STRCMP(p1, p2);
1395 }
1396 else
1397 {
1398 double n1, n2;
1399 n1 = strtod((char *)p1, (char **)&p1);
1400 n2 = strtod((char *)p2, (char **)&p2);
1401 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1402 }
1403
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001404 // When the result would be zero, compare the item indexes. Makes the
1405 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001406 if (res == 0 && !sortinfo->item_compare_keep_zero)
1407 res = si1->idx > si2->idx ? 1 : -1;
1408
1409 vim_free(tofree1);
1410 vim_free(tofree2);
1411 return res;
1412}
1413
1414 static int
1415item_compare2(const void *s1, const void *s2)
1416{
1417 sortItem_T *si1, *si2;
1418 int res;
1419 typval_T rettv;
1420 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001421 char_u *func_name;
1422 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001423 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001424
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001425 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001426 if (sortinfo->item_compare_func_err)
1427 return 0;
1428
1429 si1 = (sortItem_T *)s1;
1430 si2 = (sortItem_T *)s2;
1431
1432 if (partial == NULL)
1433 func_name = sortinfo->item_compare_func;
1434 else
1435 func_name = partial_name(partial);
1436
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001437 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1438 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001439 copy_tv(&si1->item->li_tv, &argv[0]);
1440 copy_tv(&si2->item->li_tv, &argv[1]);
1441
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001442 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001443 vim_memset(&funcexe, 0, sizeof(funcexe));
1444 funcexe.evaluate = TRUE;
1445 funcexe.partial = partial;
1446 funcexe.selfdict = sortinfo->item_compare_selfdict;
1447 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001448 clear_tv(&argv[0]);
1449 clear_tv(&argv[1]);
1450
1451 if (res == FAIL)
1452 res = ITEM_COMPARE_FAIL;
1453 else
1454 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1455 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001456 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001457 clear_tv(&rettv);
1458
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001459 // When the result would be zero, compare the pointers themselves. Makes
1460 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001461 if (res == 0 && !sortinfo->item_compare_keep_zero)
1462 res = si1->idx > si2->idx ? 1 : -1;
1463
1464 return res;
1465}
1466
1467/*
1468 * "sort()" or "uniq()" function
1469 */
1470 static void
1471do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1472{
1473 list_T *l;
1474 listitem_T *li;
1475 sortItem_T *ptrs;
1476 sortinfo_T *old_sortinfo;
1477 sortinfo_T info;
1478 long len;
1479 long i;
1480
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001481 // Pointer to current info struct used in compare function. Save and
1482 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001483 old_sortinfo = sortinfo;
1484 sortinfo = &info;
1485
1486 if (argvars[0].v_type != VAR_LIST)
1487 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1488 else
1489 {
1490 l = argvars[0].vval.v_list;
1491 if (l == NULL || var_check_lock(l->lv_lock,
1492 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1493 TRUE))
1494 goto theend;
1495 rettv_list_set(rettv, l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 range_list_materialize(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001497
1498 len = list_len(l);
1499 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001500 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001501
1502 info.item_compare_ic = FALSE;
1503 info.item_compare_numeric = FALSE;
1504 info.item_compare_numbers = FALSE;
1505#ifdef FEAT_FLOAT
1506 info.item_compare_float = FALSE;
1507#endif
1508 info.item_compare_func = NULL;
1509 info.item_compare_partial = NULL;
1510 info.item_compare_selfdict = NULL;
1511 if (argvars[1].v_type != VAR_UNKNOWN)
1512 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001513 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001514 if (argvars[1].v_type == VAR_FUNC)
1515 info.item_compare_func = argvars[1].vval.v_string;
1516 else if (argvars[1].v_type == VAR_PARTIAL)
1517 info.item_compare_partial = argvars[1].vval.v_partial;
1518 else
1519 {
1520 int error = FALSE;
1521
1522 i = (long)tv_get_number_chk(&argvars[1], &error);
1523 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001524 goto theend; // type error; errmsg already given
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001525 if (i == 1)
1526 info.item_compare_ic = TRUE;
1527 else if (argvars[1].v_type != VAR_NUMBER)
1528 info.item_compare_func = tv_get_string(&argvars[1]);
1529 else if (i != 0)
1530 {
1531 emsg(_(e_invarg));
1532 goto theend;
1533 }
1534 if (info.item_compare_func != NULL)
1535 {
1536 if (*info.item_compare_func == NUL)
1537 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001538 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001539 info.item_compare_func = NULL;
1540 }
1541 else if (STRCMP(info.item_compare_func, "n") == 0)
1542 {
1543 info.item_compare_func = NULL;
1544 info.item_compare_numeric = TRUE;
1545 }
1546 else if (STRCMP(info.item_compare_func, "N") == 0)
1547 {
1548 info.item_compare_func = NULL;
1549 info.item_compare_numbers = TRUE;
1550 }
1551#ifdef FEAT_FLOAT
1552 else if (STRCMP(info.item_compare_func, "f") == 0)
1553 {
1554 info.item_compare_func = NULL;
1555 info.item_compare_float = TRUE;
1556 }
1557#endif
1558 else if (STRCMP(info.item_compare_func, "i") == 0)
1559 {
1560 info.item_compare_func = NULL;
1561 info.item_compare_ic = TRUE;
1562 }
1563 }
1564 }
1565
1566 if (argvars[2].v_type != VAR_UNKNOWN)
1567 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001568 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001569 if (argvars[2].v_type != VAR_DICT)
1570 {
1571 emsg(_(e_dictreq));
1572 goto theend;
1573 }
1574 info.item_compare_selfdict = argvars[2].vval.v_dict;
1575 }
1576 }
1577
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001578 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001579 ptrs = ALLOC_MULT(sortItem_T, len);
1580 if (ptrs == NULL)
1581 goto theend;
1582
1583 i = 0;
1584 if (sort)
1585 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001586 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001587 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001588 {
1589 ptrs[i].item = li;
1590 ptrs[i].idx = i;
1591 ++i;
1592 }
1593
1594 info.item_compare_func_err = FALSE;
1595 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001596 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001597 if ((info.item_compare_func != NULL
1598 || info.item_compare_partial != NULL)
1599 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
1600 == ITEM_COMPARE_FAIL)
1601 emsg(_("E702: Sort compare function failed"));
1602 else
1603 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001604 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001605 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
1606 info.item_compare_func == NULL
1607 && info.item_compare_partial == NULL
1608 ? item_compare : item_compare2);
1609
1610 if (!info.item_compare_func_err)
1611 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001612 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001613 l->lv_first = l->lv_u.mat.lv_last
1614 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001615 l->lv_len = 0;
1616 for (i = 0; i < len; ++i)
1617 list_append(l, ptrs[i].item);
1618 }
1619 }
1620 }
1621 else
1622 {
1623 int (*item_compare_func_ptr)(const void *, const void *);
1624
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001625 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001626 info.item_compare_func_err = FALSE;
1627 info.item_compare_keep_zero = TRUE;
1628 item_compare_func_ptr = info.item_compare_func != NULL
1629 || info.item_compare_partial != NULL
1630 ? item_compare2 : item_compare;
1631
1632 for (li = l->lv_first; li != NULL && li->li_next != NULL;
1633 li = li->li_next)
1634 {
1635 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
1636 == 0)
1637 ptrs[i++].item = li;
1638 if (info.item_compare_func_err)
1639 {
1640 emsg(_("E882: Uniq compare function failed"));
1641 break;
1642 }
1643 }
1644
1645 if (!info.item_compare_func_err)
1646 {
1647 while (--i >= 0)
1648 {
1649 li = ptrs[i].item->li_next;
1650 ptrs[i].item->li_next = li->li_next;
1651 if (li->li_next != NULL)
1652 li->li_next->li_prev = ptrs[i].item;
1653 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001654 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001655 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001657 l->lv_len--;
1658 }
1659 }
1660 }
1661
1662 vim_free(ptrs);
1663 }
1664theend:
1665 sortinfo = old_sortinfo;
1666}
1667
1668/*
1669 * "sort({list})" function
1670 */
1671 void
1672f_sort(typval_T *argvars, typval_T *rettv)
1673{
1674 do_sort_uniq(argvars, rettv, TRUE);
1675}
1676
1677/*
1678 * "uniq({list})" function
1679 */
1680 void
1681f_uniq(typval_T *argvars, typval_T *rettv)
1682{
1683 do_sort_uniq(argvars, rettv, FALSE);
1684}
1685
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001686/*
1687 * Handle one item for map() and filter().
1688 */
1689 static int
1690filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
1691{
1692 typval_T rettv;
1693 typval_T argv[3];
1694 int retval = FAIL;
1695
1696 copy_tv(tv, get_vim_var_tv(VV_VAL));
1697 argv[0] = *get_vim_var_tv(VV_KEY);
1698 argv[1] = *get_vim_var_tv(VV_VAL);
1699 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
1700 goto theend;
1701 if (map)
1702 {
1703 // map(): replace the list item value
1704 clear_tv(tv);
1705 rettv.v_lock = 0;
1706 *tv = rettv;
1707 }
1708 else
1709 {
1710 int error = FALSE;
1711
1712 // filter(): when expr is zero remove the item
1713 *remp = (tv_get_number_chk(&rettv, &error) == 0);
1714 clear_tv(&rettv);
1715 // On type error, nothing has been removed; return FAIL to stop the
1716 // loop. The error message was given by tv_get_number_chk().
1717 if (error)
1718 goto theend;
1719 }
1720 retval = OK;
1721theend:
1722 clear_tv(get_vim_var_tv(VV_VAL));
1723 return retval;
1724}
1725
1726/*
1727 * Implementation of map() and filter().
1728 */
1729 static void
1730filter_map(typval_T *argvars, typval_T *rettv, int map)
1731{
1732 typval_T *expr;
1733 listitem_T *li, *nli;
1734 list_T *l = NULL;
1735 dictitem_T *di;
1736 hashtab_T *ht;
1737 hashitem_T *hi;
1738 dict_T *d = NULL;
1739 blob_T *b = NULL;
1740 int rem;
1741 int todo;
1742 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
1743 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
1744 : N_("filter() argument"));
1745 int save_did_emsg;
1746 int idx = 0;
1747
1748 if (argvars[0].v_type == VAR_BLOB)
1749 {
1750 if ((b = argvars[0].vval.v_blob) == NULL)
1751 return;
1752 }
1753 else if (argvars[0].v_type == VAR_LIST)
1754 {
1755 if ((l = argvars[0].vval.v_list) == NULL
1756 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
1757 return;
1758 }
1759 else if (argvars[0].v_type == VAR_DICT)
1760 {
1761 if ((d = argvars[0].vval.v_dict) == NULL
1762 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
1763 return;
1764 }
1765 else
1766 {
1767 semsg(_(e_listdictarg), ermsg);
1768 return;
1769 }
1770
1771 expr = &argvars[1];
1772 // On type errors, the preceding call has already displayed an error
1773 // message. Avoid a misleading error message for an empty string that
1774 // was not passed as argument.
1775 if (expr->v_type != VAR_UNKNOWN)
1776 {
1777 typval_T save_val;
1778 typval_T save_key;
1779
1780 prepare_vimvar(VV_VAL, &save_val);
1781 prepare_vimvar(VV_KEY, &save_key);
1782
1783 // We reset "did_emsg" to be able to detect whether an error
1784 // occurred during evaluation of the expression.
1785 save_did_emsg = did_emsg;
1786 did_emsg = FALSE;
1787
1788 if (argvars[0].v_type == VAR_DICT)
1789 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01001790 int prev_lock = d->dv_lock;
1791
1792 if (map && d->dv_lock == 0)
1793 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001794 ht = &d->dv_hashtab;
1795 hash_lock(ht);
1796 todo = (int)ht->ht_used;
1797 for (hi = ht->ht_array; todo > 0; ++hi)
1798 {
1799 if (!HASHITEM_EMPTY(hi))
1800 {
1801 int r;
1802
1803 --todo;
1804 di = HI2DI(hi);
1805 if (map && (var_check_lock(di->di_tv.v_lock,
1806 arg_errmsg, TRUE)
1807 || var_check_ro(di->di_flags,
1808 arg_errmsg, TRUE)))
1809 break;
1810 set_vim_var_string(VV_KEY, di->di_key, -1);
1811 r = filter_map_one(&di->di_tv, expr, map, &rem);
1812 clear_tv(get_vim_var_tv(VV_KEY));
1813 if (r == FAIL || did_emsg)
1814 break;
1815 if (!map && rem)
1816 {
1817 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1818 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
1819 break;
1820 dictitem_remove(d, di);
1821 }
1822 }
1823 }
1824 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01001825 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001826 }
1827 else if (argvars[0].v_type == VAR_BLOB)
1828 {
1829 int i;
1830 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01001831 varnumber_T val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001832
1833 // set_vim_var_nr() doesn't set the type
1834 set_vim_var_type(VV_KEY, VAR_NUMBER);
1835
1836 for (i = 0; i < b->bv_ga.ga_len; i++)
1837 {
1838 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01001839 val = blob_get(b, i);
1840 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001841 set_vim_var_nr(VV_KEY, idx);
1842 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
1843 break;
1844 if (tv.v_type != VAR_NUMBER)
1845 {
1846 emsg(_(e_invalblob));
1847 break;
1848 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01001849 if (map)
1850 {
1851 if (tv.vval.v_number != val)
1852 blob_set(b, i, tv.vval.v_number);
1853 }
1854 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001855 {
1856 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
1857
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01001858 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001859 (size_t)b->bv_ga.ga_len - i - 1);
1860 --b->bv_ga.ga_len;
1861 --i;
1862 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01001863 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001864 }
1865 }
1866 else // argvars[0].v_type == VAR_LIST
1867 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01001868 int prev_lock = l->lv_lock;
1869
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001870 // set_vim_var_nr() doesn't set the type
1871 set_vim_var_type(VV_KEY, VAR_NUMBER);
1872
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 range_list_materialize(l);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01001874 if (map && l->lv_lock == 0)
1875 l->lv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001876 for (li = l->lv_first; li != NULL; li = nli)
1877 {
1878 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
1879 break;
1880 nli = li->li_next;
1881 set_vim_var_nr(VV_KEY, idx);
1882 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
1883 || did_emsg)
1884 break;
1885 if (!map && rem)
1886 listitem_remove(l, li);
1887 ++idx;
1888 }
Bram Moolenaardb661fb2020-01-29 22:17:16 +01001889 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001890 }
1891
1892 restore_vimvar(VV_KEY, &save_key);
1893 restore_vimvar(VV_VAL, &save_val);
1894
1895 did_emsg |= save_did_emsg;
1896 }
1897
1898 copy_tv(&argvars[0], rettv);
1899}
1900
1901/*
1902 * "filter()" function
1903 */
1904 void
1905f_filter(typval_T *argvars, typval_T *rettv)
1906{
1907 filter_map(argvars, rettv, FALSE);
1908}
1909
1910/*
1911 * "map()" function
1912 */
1913 void
1914f_map(typval_T *argvars, typval_T *rettv)
1915{
1916 filter_map(argvars, rettv, TRUE);
1917}
1918
Bram Moolenaar08c308a2019-09-04 17:48:15 +02001919/*
1920 * "add(list, item)" function
1921 */
1922 void
1923f_add(typval_T *argvars, typval_T *rettv)
1924{
1925 list_T *l;
1926 blob_T *b;
1927
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001928 rettv->vval.v_number = 1; // Default: Failed
Bram Moolenaar08c308a2019-09-04 17:48:15 +02001929 if (argvars[0].v_type == VAR_LIST)
1930 {
1931 if ((l = argvars[0].vval.v_list) != NULL
1932 && !var_check_lock(l->lv_lock,
1933 (char_u *)N_("add() argument"), TRUE)
1934 && list_append_tv(l, &argvars[1]) == OK)
1935 copy_tv(&argvars[0], rettv);
1936 }
1937 else if (argvars[0].v_type == VAR_BLOB)
1938 {
1939 if ((b = argvars[0].vval.v_blob) != NULL
1940 && !var_check_lock(b->bv_lock,
1941 (char_u *)N_("add() argument"), TRUE))
1942 {
1943 int error = FALSE;
1944 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1945
1946 if (!error)
1947 {
1948 ga_append(&b->bv_ga, (int)n);
1949 copy_tv(&argvars[0], rettv);
1950 }
1951 }
1952 }
1953 else
1954 emsg(_(e_listblobreq));
1955}
1956
1957/*
1958 * "count()" function
1959 */
1960 void
1961f_count(typval_T *argvars, typval_T *rettv)
1962{
1963 long n = 0;
1964 int ic = FALSE;
1965 int error = FALSE;
1966
1967 if (argvars[2].v_type != VAR_UNKNOWN)
1968 ic = (int)tv_get_number_chk(&argvars[2], &error);
1969
1970 if (argvars[0].v_type == VAR_STRING)
1971 {
1972 char_u *expr = tv_get_string_chk(&argvars[1]);
1973 char_u *p = argvars[0].vval.v_string;
1974 char_u *next;
1975
1976 if (!error && expr != NULL && *expr != NUL && p != NULL)
1977 {
1978 if (ic)
1979 {
1980 size_t len = STRLEN(expr);
1981
1982 while (*p != NUL)
1983 {
1984 if (MB_STRNICMP(p, expr, len) == 0)
1985 {
1986 ++n;
1987 p += len;
1988 }
1989 else
1990 MB_PTR_ADV(p);
1991 }
1992 }
1993 else
1994 while ((next = (char_u *)strstr((char *)p, (char *)expr))
1995 != NULL)
1996 {
1997 ++n;
1998 p = next + STRLEN(expr);
1999 }
2000 }
2001
2002 }
2003 else if (argvars[0].v_type == VAR_LIST)
2004 {
2005 listitem_T *li;
2006 list_T *l;
2007 long idx;
2008
2009 if ((l = argvars[0].vval.v_list) != NULL)
2010 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002011 range_list_materialize(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002012 li = l->lv_first;
2013 if (argvars[2].v_type != VAR_UNKNOWN)
2014 {
2015 if (argvars[3].v_type != VAR_UNKNOWN)
2016 {
2017 idx = (long)tv_get_number_chk(&argvars[3], &error);
2018 if (!error)
2019 {
2020 li = list_find(l, idx);
2021 if (li == NULL)
2022 semsg(_(e_listidx), idx);
2023 }
2024 }
2025 if (error)
2026 li = NULL;
2027 }
2028
2029 for ( ; li != NULL; li = li->li_next)
2030 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2031 ++n;
2032 }
2033 }
2034 else if (argvars[0].v_type == VAR_DICT)
2035 {
2036 int todo;
2037 dict_T *d;
2038 hashitem_T *hi;
2039
2040 if ((d = argvars[0].vval.v_dict) != NULL)
2041 {
2042 if (argvars[2].v_type != VAR_UNKNOWN)
2043 {
2044 if (argvars[3].v_type != VAR_UNKNOWN)
2045 emsg(_(e_invarg));
2046 }
2047
2048 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2049 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2050 {
2051 if (!HASHITEM_EMPTY(hi))
2052 {
2053 --todo;
2054 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2055 ++n;
2056 }
2057 }
2058 }
2059 }
2060 else
2061 semsg(_(e_listdictarg), "count()");
2062 rettv->vval.v_number = n;
2063}
2064
2065/*
2066 * "extend(list, list [, idx])" function
2067 * "extend(dict, dict [, action])" function
2068 */
2069 void
2070f_extend(typval_T *argvars, typval_T *rettv)
2071{
2072 char_u *arg_errmsg = (char_u *)N_("extend() argument");
2073
2074 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2075 {
2076 list_T *l1, *l2;
2077 listitem_T *item;
2078 long before;
2079 int error = FALSE;
2080
2081 l1 = argvars[0].vval.v_list;
2082 l2 = argvars[1].vval.v_list;
2083 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
2084 && l2 != NULL)
2085 {
2086 if (argvars[2].v_type != VAR_UNKNOWN)
2087 {
2088 before = (long)tv_get_number_chk(&argvars[2], &error);
2089 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002090 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002091
2092 if (before == l1->lv_len)
2093 item = NULL;
2094 else
2095 {
2096 item = list_find(l1, before);
2097 if (item == NULL)
2098 {
2099 semsg(_(e_listidx), before);
2100 return;
2101 }
2102 }
2103 }
2104 else
2105 item = NULL;
2106 list_extend(l1, l2, item);
2107
2108 copy_tv(&argvars[0], rettv);
2109 }
2110 }
2111 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2112 {
2113 dict_T *d1, *d2;
2114 char_u *action;
2115 int i;
2116
2117 d1 = argvars[0].vval.v_dict;
2118 d2 = argvars[1].vval.v_dict;
2119 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
2120 && d2 != NULL)
2121 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002122 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002123 if (argvars[2].v_type != VAR_UNKNOWN)
2124 {
2125 static char *(av[]) = {"keep", "force", "error"};
2126
2127 action = tv_get_string_chk(&argvars[2]);
2128 if (action == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002129 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002130 for (i = 0; i < 3; ++i)
2131 if (STRCMP(action, av[i]) == 0)
2132 break;
2133 if (i == 3)
2134 {
2135 semsg(_(e_invarg2), action);
2136 return;
2137 }
2138 }
2139 else
2140 action = (char_u *)"force";
2141
2142 dict_extend(d1, d2, action);
2143
2144 copy_tv(&argvars[0], rettv);
2145 }
2146 }
2147 else
2148 semsg(_(e_listdictarg), "extend()");
2149}
2150
2151/*
2152 * "insert()" function
2153 */
2154 void
2155f_insert(typval_T *argvars, typval_T *rettv)
2156{
2157 long before = 0;
2158 listitem_T *item;
2159 list_T *l;
2160 int error = FALSE;
2161
2162 if (argvars[0].v_type == VAR_BLOB)
2163 {
2164 int val, len;
2165 char_u *p;
2166
2167 len = blob_len(argvars[0].vval.v_blob);
2168 if (argvars[2].v_type != VAR_UNKNOWN)
2169 {
2170 before = (long)tv_get_number_chk(&argvars[2], &error);
2171 if (error)
2172 return; // type error; errmsg already given
2173 if (before < 0 || before > len)
2174 {
2175 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2176 return;
2177 }
2178 }
2179 val = tv_get_number_chk(&argvars[1], &error);
2180 if (error)
2181 return;
2182 if (val < 0 || val > 255)
2183 {
2184 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2185 return;
2186 }
2187
2188 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
2189 return;
2190 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2191 mch_memmove(p + before + 1, p + before, (size_t)len - before);
2192 *(p + before) = val;
2193 ++argvars[0].vval.v_blob->bv_ga.ga_len;
2194
2195 copy_tv(&argvars[0], rettv);
2196 }
2197 else if (argvars[0].v_type != VAR_LIST)
2198 semsg(_(e_listblobarg), "insert()");
2199 else if ((l = argvars[0].vval.v_list) != NULL
2200 && !var_check_lock(l->lv_lock,
2201 (char_u *)N_("insert() argument"), TRUE))
2202 {
2203 if (argvars[2].v_type != VAR_UNKNOWN)
2204 before = (long)tv_get_number_chk(&argvars[2], &error);
2205 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002206 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002207
2208 if (before == l->lv_len)
2209 item = NULL;
2210 else
2211 {
2212 item = list_find(l, before);
2213 if (item == NULL)
2214 {
2215 semsg(_(e_listidx), before);
2216 l = NULL;
2217 }
2218 }
2219 if (l != NULL)
2220 {
2221 list_insert_tv(l, &argvars[1], item);
2222 copy_tv(&argvars[0], rettv);
2223 }
2224 }
2225}
2226
2227/*
2228 * "remove()" function
2229 */
2230 void
2231f_remove(typval_T *argvars, typval_T *rettv)
2232{
2233 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2234
2235 if (argvars[0].v_type == VAR_DICT)
2236 dict_remove(argvars, rettv, arg_errmsg);
2237 else if (argvars[0].v_type == VAR_BLOB)
2238 blob_remove(argvars, rettv);
2239 else if (argvars[0].v_type == VAR_LIST)
2240 list_remove(argvars, rettv, arg_errmsg);
2241 else
2242 semsg(_(e_listdictblobarg), "remove()");
2243}
2244
2245/*
2246 * "reverse({list})" function
2247 */
2248 void
2249f_reverse(typval_T *argvars, typval_T *rettv)
2250{
2251 list_T *l;
2252 listitem_T *li, *ni;
2253
2254 if (argvars[0].v_type == VAR_BLOB)
2255 {
2256 blob_T *b = argvars[0].vval.v_blob;
2257 int i, len = blob_len(b);
2258
2259 for (i = 0; i < len / 2; i++)
2260 {
2261 int tmp = blob_get(b, i);
2262
2263 blob_set(b, i, blob_get(b, len - i - 1));
2264 blob_set(b, len - i - 1, tmp);
2265 }
2266 rettv_blob_set(rettv, b);
2267 return;
2268 }
2269
2270 if (argvars[0].v_type != VAR_LIST)
2271 semsg(_(e_listblobarg), "reverse()");
2272 else if ((l = argvars[0].vval.v_list) != NULL
2273 && !var_check_lock(l->lv_lock,
2274 (char_u *)N_("reverse() argument"), TRUE))
2275 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002276 if (l->lv_first == &range_list_item)
2277 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002278 varnumber_T new_start = l->lv_u.nonmat.lv_start
2279 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2280 l->lv_u.nonmat.lv_end = new_start
2281 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2282 l->lv_u.nonmat.lv_start = new_start;
2283 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002284 rettv_list_set(rettv, l);
2285 return;
2286 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002287 li = l->lv_u.mat.lv_last;
2288 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002289 l->lv_len = 0;
2290 while (li != NULL)
2291 {
2292 ni = li->li_prev;
2293 list_append(l, li);
2294 li = ni;
2295 }
2296 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002297 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002298 }
2299}
2300
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002301#endif // defined(FEAT_EVAL)