blob: 451e58591095b67b6b8dcb2af355b6467cad663c [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100381 range_list_materialize(l1);
382 range_list_materialize(l2);
383
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 Moolenaar8a7d6542020-01-26 15:56:19 +0100414 range_list_materialize(l);
415
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 Moolenaar8a7d6542020-01-26 15:56:19 +0100544 range_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 Moolenaar8a7d6542020-01-26 15:56:19 +0100559 range_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 Moolenaar8a7d6542020-01-26 15:56:19 +0100709 range_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/*
734 * Extend "l1" with "l2".
735 * If "bef" is NULL append at the end, otherwise insert before this item.
736 * Returns FAIL when out of memory.
737 */
738 int
739list_extend(list_T *l1, list_T *l2, listitem_T *bef)
740{
741 listitem_T *item;
742 int todo = l2->lv_len;
743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744 range_list_materialize(l1);
745 range_list_materialize(l2);
746
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100747 // We also quit the loop when we have inserted the original item count of
748 // the list, avoid a hang when we extend a list with itself.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200749 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
750 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
751 return FAIL;
752 return OK;
753}
754
755/*
756 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
757 * Return FAIL when out of memory.
758 */
759 int
760list_concat(list_T *l1, list_T *l2, typval_T *tv)
761{
762 list_T *l;
763
764 if (l1 == NULL || l2 == NULL)
765 return FAIL;
766
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100767 // make a copy of the first list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200768 l = list_copy(l1, FALSE, 0);
769 if (l == NULL)
770 return FAIL;
771 tv->v_type = VAR_LIST;
772 tv->vval.v_list = l;
773
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100774 // append all items from the second list
Bram Moolenaarda861d62016-07-17 15:46:27 +0200775 return list_extend(l, l2, NULL);
776}
777
778/*
779 * Make a copy of list "orig". Shallow if "deep" is FALSE.
780 * The refcount of the new list is set to 1.
781 * See item_copy() for "copyID".
782 * Returns NULL when out of memory.
783 */
784 list_T *
785list_copy(list_T *orig, int deep, int copyID)
786{
787 list_T *copy;
788 listitem_T *item;
789 listitem_T *ni;
790
791 if (orig == NULL)
792 return NULL;
793
794 copy = list_alloc();
795 if (copy != NULL)
796 {
797 if (copyID != 0)
798 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100799 // Do this before adding the items, because one of the items may
800 // refer back to this list.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200801 orig->lv_copyID = copyID;
802 orig->lv_copylist = copy;
803 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804 range_list_materialize(orig);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200805 for (item = orig->lv_first; item != NULL && !got_int;
806 item = item->li_next)
807 {
808 ni = listitem_alloc();
809 if (ni == NULL)
810 break;
811 if (deep)
812 {
813 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
814 {
815 vim_free(ni);
816 break;
817 }
818 }
819 else
820 copy_tv(&item->li_tv, &ni->li_tv);
821 list_append(copy, ni);
822 }
823 ++copy->lv_refcount;
824 if (item != NULL)
825 {
826 list_unref(copy);
827 copy = NULL;
828 }
829 }
830
831 return copy;
832}
833
834/*
835 * Remove items "item" to "item2" from list "l".
836 * Does not free the listitem or the value!
837 * This used to be called list_remove, but that conflicts with a Sun header
838 * file.
839 */
840 void
841vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
842{
843 listitem_T *ip;
844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 range_list_materialize(l);
846
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100847 // notify watchers
Bram Moolenaarda861d62016-07-17 15:46:27 +0200848 for (ip = item; ip != NULL; ip = ip->li_next)
849 {
850 --l->lv_len;
851 list_fix_watch(l, ip);
852 if (ip == item2)
853 break;
854 }
855
856 if (item2->li_next == NULL)
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100857 l->lv_u.mat.lv_last = item->li_prev;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200858 else
859 item2->li_next->li_prev = item->li_prev;
860 if (item->li_prev == NULL)
861 l->lv_first = item2->li_next;
862 else
863 item->li_prev->li_next = item2->li_next;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +0100864 l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaarda861d62016-07-17 15:46:27 +0200865}
866
867/*
868 * Return an allocated string with the string representation of a list.
869 * May return NULL.
870 */
871 char_u *
872list2string(typval_T *tv, int copyID, int restore_copyID)
873{
874 garray_T ga;
875
876 if (tv->vval.v_list == NULL)
877 return NULL;
878 ga_init2(&ga, (int)sizeof(char), 80);
879 ga_append(&ga, '[');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 range_list_materialize(tv->vval.v_list);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200881 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
882 FALSE, restore_copyID, copyID) == FAIL)
883 {
884 vim_free(ga.ga_data);
885 return NULL;
886 }
887 ga_append(&ga, ']');
888 ga_append(&ga, NUL);
889 return (char_u *)ga.ga_data;
890}
891
892typedef struct join_S {
893 char_u *s;
894 char_u *tofree;
895} join_T;
896
897 static int
898list_join_inner(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100899 garray_T *gap, // to store the result in
Bram Moolenaarda861d62016-07-17 15:46:27 +0200900 list_T *l,
901 char_u *sep,
902 int echo_style,
903 int restore_copyID,
904 int copyID,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100905 garray_T *join_gap) // to keep each list item string
Bram Moolenaarda861d62016-07-17 15:46:27 +0200906{
907 int i;
908 join_T *p;
909 int len;
910 int sumlen = 0;
911 int first = TRUE;
912 char_u *tofree;
913 char_u numbuf[NUMBUFLEN];
914 listitem_T *item;
915 char_u *s;
916
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100917 // Stringify each item in the list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100918 range_list_materialize(l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200919 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
920 {
921 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +0200922 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200923 if (s == NULL)
924 return FAIL;
925
926 len = (int)STRLEN(s);
927 sumlen += len;
928
929 (void)ga_grow(join_gap, 1);
930 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
931 if (tofree != NULL || s != numbuf)
932 {
933 p->s = s;
934 p->tofree = tofree;
935 }
936 else
937 {
938 p->s = vim_strnsave(s, len);
939 p->tofree = p->s;
940 }
941
942 line_breakcheck();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100943 if (did_echo_string_emsg) // recursion error, bail out
Bram Moolenaarda861d62016-07-17 15:46:27 +0200944 break;
945 }
946
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100947 // Allocate result buffer with its total size, avoid re-allocation and
948 // multiple copy operations. Add 2 for a tailing ']' and NUL.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200949 if (join_gap->ga_len >= 2)
950 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
951 if (ga_grow(gap, sumlen + 2) == FAIL)
952 return FAIL;
953
954 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
955 {
956 if (first)
957 first = FALSE;
958 else
959 ga_concat(gap, sep);
960 p = ((join_T *)join_gap->ga_data) + i;
961
962 if (p->s != NULL)
963 ga_concat(gap, p->s);
964 line_breakcheck();
965 }
966
967 return OK;
968}
969
970/*
971 * Join list "l" into a string in "*gap", using separator "sep".
972 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
973 * Return FAIL or OK.
974 */
975 int
976list_join(
977 garray_T *gap,
978 list_T *l,
979 char_u *sep,
980 int echo_style,
981 int restore_copyID,
982 int copyID)
983{
984 garray_T join_ga;
985 int retval;
986 join_T *p;
987 int i;
988
989 if (l->lv_len < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100990 return OK; // nothing to do
Bram Moolenaarda861d62016-07-17 15:46:27 +0200991 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
992 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
993 copyID, &join_ga);
994
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100995 // Dispose each item in join_ga.
Bram Moolenaarda861d62016-07-17 15:46:27 +0200996 if (join_ga.ga_data != NULL)
997 {
998 p = (join_T *)join_ga.ga_data;
999 for (i = 0; i < join_ga.ga_len; ++i)
1000 {
1001 vim_free(p->tofree);
1002 ++p;
1003 }
1004 ga_clear(&join_ga);
1005 }
1006
1007 return retval;
1008}
1009
1010/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001011 * "join()" function
1012 */
1013 void
1014f_join(typval_T *argvars, typval_T *rettv)
1015{
1016 garray_T ga;
1017 char_u *sep;
1018
1019 if (argvars[0].v_type != VAR_LIST)
1020 {
1021 emsg(_(e_listreq));
1022 return;
1023 }
1024 if (argvars[0].vval.v_list == NULL)
1025 return;
1026 if (argvars[1].v_type == VAR_UNKNOWN)
1027 sep = (char_u *)" ";
1028 else
1029 sep = tv_get_string_chk(&argvars[1]);
1030
1031 rettv->v_type = VAR_STRING;
1032
1033 if (sep != NULL)
1034 {
1035 ga_init2(&ga, (int)sizeof(char), 80);
1036 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
1037 ga_append(&ga, NUL);
1038 rettv->vval.v_string = (char_u *)ga.ga_data;
1039 }
1040 else
1041 rettv->vval.v_string = NULL;
1042}
1043
1044/*
Bram Moolenaarda861d62016-07-17 15:46:27 +02001045 * Allocate a variable for a List and fill it from "*arg".
1046 * Return OK or FAIL.
1047 */
1048 int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049get_list_tv(char_u **arg, typval_T *rettv, int evaluate, int do_error)
Bram Moolenaarda861d62016-07-17 15:46:27 +02001050{
1051 list_T *l = NULL;
1052 typval_T tv;
1053 listitem_T *item;
1054
1055 if (evaluate)
1056 {
1057 l = list_alloc();
1058 if (l == NULL)
1059 return FAIL;
1060 }
1061
1062 *arg = skipwhite(*arg + 1);
1063 while (**arg != ']' && **arg != NUL)
1064 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001065 if (eval1(arg, &tv, evaluate) == FAIL) // recursive!
Bram Moolenaarda861d62016-07-17 15:46:27 +02001066 goto failret;
1067 if (evaluate)
1068 {
1069 item = listitem_alloc();
1070 if (item != NULL)
1071 {
1072 item->li_tv = tv;
1073 item->li_tv.v_lock = 0;
1074 list_append(l, item);
1075 }
1076 else
1077 clear_tv(&tv);
1078 }
1079
1080 if (**arg == ']')
1081 break;
1082 if (**arg != ',')
1083 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 if (do_error)
1085 semsg(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001086 goto failret;
1087 }
1088 *arg = skipwhite(*arg + 1);
1089 }
1090
1091 if (**arg != ']')
1092 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 if (do_error)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001094 semsg(_(e_list_end), *arg);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001095failret:
1096 if (evaluate)
1097 list_free(l);
1098 return FAIL;
1099 }
1100
1101 *arg = skipwhite(*arg + 1);
1102 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001103 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +02001104
1105 return OK;
1106}
1107
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001108/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +01001109 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001110 */
1111 int
1112write_list(FILE *fd, list_T *list, int binary)
1113{
1114 listitem_T *li;
1115 int c;
1116 int ret = OK;
1117 char_u *s;
1118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 range_list_materialize(list);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001120 FOR_ALL_LIST_ITEMS(list, li)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001122 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001123 {
1124 if (*s == '\n')
1125 c = putc(NUL, fd);
1126 else
1127 c = putc(*s, fd);
1128 if (c == EOF)
1129 {
1130 ret = FAIL;
1131 break;
1132 }
1133 }
1134 if (!binary || li->li_next != NULL)
1135 if (putc('\n', fd) == EOF)
1136 {
1137 ret = FAIL;
1138 break;
1139 }
1140 if (ret == FAIL)
1141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001142 emsg(_(e_write));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001143 break;
1144 }
1145 }
1146 return ret;
1147}
1148
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001149/*
1150 * Initialize a static list with 10 items.
1151 */
1152 void
1153init_static_list(staticList10_T *sl)
1154{
1155 list_T *l = &sl->sl_list;
1156 int i;
1157
1158 memset(sl, 0, sizeof(staticList10_T));
1159 l->lv_first = &sl->sl_items[0];
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001160 l->lv_u.mat.lv_last = &sl->sl_items[9];
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001161 l->lv_refcount = DO_NOT_FREE_CNT;
1162 l->lv_lock = VAR_FIXED;
1163 sl->sl_list.lv_len = 10;
1164
1165 for (i = 0; i < 10; ++i)
1166 {
1167 listitem_T *li = &sl->sl_items[i];
1168
1169 if (i == 0)
1170 li->li_prev = NULL;
1171 else
1172 li->li_prev = li - 1;
1173 if (i == 9)
1174 li->li_next = NULL;
1175 else
1176 li->li_next = li + 1;
1177 }
1178}
1179
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001180/*
1181 * "list2str()" function
1182 */
1183 void
1184f_list2str(typval_T *argvars, typval_T *rettv)
1185{
1186 list_T *l;
1187 listitem_T *li;
1188 garray_T ga;
1189 int utf8 = FALSE;
1190
1191 rettv->v_type = VAR_STRING;
1192 rettv->vval.v_string = NULL;
1193 if (argvars[0].v_type != VAR_LIST)
1194 {
1195 emsg(_(e_invarg));
1196 return;
1197 }
1198
1199 l = argvars[0].vval.v_list;
1200 if (l == NULL)
1201 return; // empty list results in empty string
1202
1203 if (argvars[1].v_type != VAR_UNKNOWN)
1204 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
1205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 range_list_materialize(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001207 ga_init2(&ga, 1, 80);
1208 if (has_mbyte || utf8)
1209 {
1210 char_u buf[MB_MAXBYTES + 1];
1211 int (*char2bytes)(int, char_u *);
1212
1213 if (utf8 || enc_utf8)
1214 char2bytes = utf_char2bytes;
1215 else
1216 char2bytes = mb_char2bytes;
1217
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001218 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001219 {
1220 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
1221 ga_concat(&ga, buf);
1222 }
1223 ga_append(&ga, NUL);
1224 }
1225 else if (ga_grow(&ga, list_len(l) + 1) == OK)
1226 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001227 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001228 ga_append(&ga, tv_get_number(&li->li_tv));
1229 ga_append(&ga, NUL);
1230 }
1231
1232 rettv->v_type = VAR_STRING;
1233 rettv->vval.v_string = ga.ga_data;
1234}
1235
Bram Moolenaarbdff0122020-04-05 18:56:05 +02001236 static void
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001237list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1238{
1239 list_T *l;
1240 listitem_T *item, *item2;
1241 listitem_T *li;
1242 int error = FALSE;
1243 int idx;
1244
1245 if ((l = argvars[0].vval.v_list) == NULL
1246 || var_check_lock(l->lv_lock, arg_errmsg, TRUE))
1247 return;
1248
1249 idx = (long)tv_get_number_chk(&argvars[1], &error);
1250 if (error)
1251 ; // type error: do nothing, errmsg already given
1252 else if ((item = list_find(l, idx)) == NULL)
1253 semsg(_(e_listidx), idx);
1254 else
1255 {
1256 if (argvars[2].v_type == VAR_UNKNOWN)
1257 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001258 // Remove one item, return its value.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001259 vimlist_remove(l, item, item);
1260 *rettv = item->li_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 list_free_item(l, item);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001262 }
1263 else
1264 {
1265 // Remove range of items, return list with values.
1266 int end = (long)tv_get_number_chk(&argvars[2], &error);
1267
1268 if (error)
1269 ; // type error: do nothing
1270 else if ((item2 = list_find(l, end)) == NULL)
1271 semsg(_(e_listidx), end);
1272 else
1273 {
1274 int cnt = 0;
1275
1276 for (li = item; li != NULL; li = li->li_next)
1277 {
1278 ++cnt;
1279 if (li == item2)
1280 break;
1281 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001282 if (li == NULL) // didn't find "item2" after "item"
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001283 emsg(_(e_invrange));
1284 else
1285 {
1286 vimlist_remove(l, item, item2);
1287 if (rettv_list_alloc(rettv) == OK)
1288 {
1289 l = rettv->vval.v_list;
1290 l->lv_first = item;
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001291 l->lv_u.mat.lv_last = item2;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001292 item->li_prev = NULL;
1293 item2->li_next = NULL;
1294 l->lv_len = cnt;
1295 }
1296 }
1297 }
1298 }
1299 }
1300}
1301
1302static int item_compare(const void *s1, const void *s2);
1303static int item_compare2(const void *s1, const void *s2);
1304
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001305// struct used in the array that's given to qsort()
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001306typedef struct
1307{
1308 listitem_T *item;
1309 int idx;
1310} sortItem_T;
1311
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001312// struct storing information about current sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001313typedef struct
1314{
1315 int item_compare_ic;
1316 int item_compare_numeric;
1317 int item_compare_numbers;
1318#ifdef FEAT_FLOAT
1319 int item_compare_float;
1320#endif
1321 char_u *item_compare_func;
1322 partial_T *item_compare_partial;
1323 dict_T *item_compare_selfdict;
1324 int item_compare_func_err;
1325 int item_compare_keep_zero;
1326} sortinfo_T;
1327static sortinfo_T *sortinfo = NULL;
1328#define ITEM_COMPARE_FAIL 999
1329
1330/*
1331 * Compare functions for f_sort() and f_uniq() below.
1332 */
1333 static int
1334item_compare(const void *s1, const void *s2)
1335{
1336 sortItem_T *si1, *si2;
1337 typval_T *tv1, *tv2;
1338 char_u *p1, *p2;
1339 char_u *tofree1 = NULL, *tofree2 = NULL;
1340 int res;
1341 char_u numbuf1[NUMBUFLEN];
1342 char_u numbuf2[NUMBUFLEN];
1343
1344 si1 = (sortItem_T *)s1;
1345 si2 = (sortItem_T *)s2;
1346 tv1 = &si1->item->li_tv;
1347 tv2 = &si2->item->li_tv;
1348
1349 if (sortinfo->item_compare_numbers)
1350 {
1351 varnumber_T v1 = tv_get_number(tv1);
1352 varnumber_T v2 = tv_get_number(tv2);
1353
1354 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1355 }
1356
1357#ifdef FEAT_FLOAT
1358 if (sortinfo->item_compare_float)
1359 {
1360 float_T v1 = tv_get_float(tv1);
1361 float_T v2 = tv_get_float(tv2);
1362
1363 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
1364 }
1365#endif
1366
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001367 // tv2string() puts quotes around a string and allocates memory. Don't do
1368 // that for string variables. Use a single quote when comparing with a
1369 // non-string to do what the docs promise.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001370 if (tv1->v_type == VAR_STRING)
1371 {
1372 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1373 p1 = (char_u *)"'";
1374 else
1375 p1 = tv1->vval.v_string;
1376 }
1377 else
1378 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
1379 if (tv2->v_type == VAR_STRING)
1380 {
1381 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
1382 p2 = (char_u *)"'";
1383 else
1384 p2 = tv2->vval.v_string;
1385 }
1386 else
1387 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
1388 if (p1 == NULL)
1389 p1 = (char_u *)"";
1390 if (p2 == NULL)
1391 p2 = (char_u *)"";
1392 if (!sortinfo->item_compare_numeric)
1393 {
1394 if (sortinfo->item_compare_ic)
1395 res = STRICMP(p1, p2);
1396 else
1397 res = STRCMP(p1, p2);
1398 }
1399 else
1400 {
1401 double n1, n2;
1402 n1 = strtod((char *)p1, (char **)&p1);
1403 n2 = strtod((char *)p2, (char **)&p2);
1404 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
1405 }
1406
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001407 // When the result would be zero, compare the item indexes. Makes the
1408 // sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001409 if (res == 0 && !sortinfo->item_compare_keep_zero)
1410 res = si1->idx > si2->idx ? 1 : -1;
1411
1412 vim_free(tofree1);
1413 vim_free(tofree2);
1414 return res;
1415}
1416
1417 static int
1418item_compare2(const void *s1, const void *s2)
1419{
1420 sortItem_T *si1, *si2;
1421 int res;
1422 typval_T rettv;
1423 typval_T argv[3];
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001424 char_u *func_name;
1425 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001426 funcexe_T funcexe;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001427
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001428 // shortcut after failure in previous call; compare all items equal
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001429 if (sortinfo->item_compare_func_err)
1430 return 0;
1431
1432 si1 = (sortItem_T *)s1;
1433 si2 = (sortItem_T *)s2;
1434
1435 if (partial == NULL)
1436 func_name = sortinfo->item_compare_func;
1437 else
1438 func_name = partial_name(partial);
1439
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001440 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
1441 // in the copy without changing the original list items.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001442 copy_tv(&si1->item->li_tv, &argv[0]);
1443 copy_tv(&si2->item->li_tv, &argv[1]);
1444
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001445 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001446 vim_memset(&funcexe, 0, sizeof(funcexe));
1447 funcexe.evaluate = TRUE;
1448 funcexe.partial = partial;
1449 funcexe.selfdict = sortinfo->item_compare_selfdict;
1450 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001451 clear_tv(&argv[0]);
1452 clear_tv(&argv[1]);
1453
1454 if (res == FAIL)
1455 res = ITEM_COMPARE_FAIL;
1456 else
1457 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
1458 if (sortinfo->item_compare_func_err)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001459 res = ITEM_COMPARE_FAIL; // return value has wrong type
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001460 clear_tv(&rettv);
1461
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001462 // When the result would be zero, compare the pointers themselves. Makes
1463 // the sort stable.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001464 if (res == 0 && !sortinfo->item_compare_keep_zero)
1465 res = si1->idx > si2->idx ? 1 : -1;
1466
1467 return res;
1468}
1469
1470/*
1471 * "sort()" or "uniq()" function
1472 */
1473 static void
1474do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
1475{
1476 list_T *l;
1477 listitem_T *li;
1478 sortItem_T *ptrs;
1479 sortinfo_T *old_sortinfo;
1480 sortinfo_T info;
1481 long len;
1482 long i;
1483
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001484 // Pointer to current info struct used in compare function. Save and
1485 // restore the current one for nested calls.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001486 old_sortinfo = sortinfo;
1487 sortinfo = &info;
1488
1489 if (argvars[0].v_type != VAR_LIST)
1490 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
1491 else
1492 {
1493 l = argvars[0].vval.v_list;
1494 if (l == NULL || var_check_lock(l->lv_lock,
1495 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
1496 TRUE))
1497 goto theend;
1498 rettv_list_set(rettv, l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 range_list_materialize(l);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001500
1501 len = list_len(l);
1502 if (len <= 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001503 goto theend; // short list sorts pretty quickly
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001504
1505 info.item_compare_ic = FALSE;
1506 info.item_compare_numeric = FALSE;
1507 info.item_compare_numbers = FALSE;
1508#ifdef FEAT_FLOAT
1509 info.item_compare_float = FALSE;
1510#endif
1511 info.item_compare_func = NULL;
1512 info.item_compare_partial = NULL;
1513 info.item_compare_selfdict = NULL;
1514 if (argvars[1].v_type != VAR_UNKNOWN)
1515 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001516 // optional second argument: {func}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001517 if (argvars[1].v_type == VAR_FUNC)
1518 info.item_compare_func = argvars[1].vval.v_string;
1519 else if (argvars[1].v_type == VAR_PARTIAL)
1520 info.item_compare_partial = argvars[1].vval.v_partial;
1521 else
1522 {
1523 int error = FALSE;
1524
1525 i = (long)tv_get_number_chk(&argvars[1], &error);
1526 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001527 goto theend; // type error; errmsg already given
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001528 if (i == 1)
1529 info.item_compare_ic = TRUE;
1530 else if (argvars[1].v_type != VAR_NUMBER)
1531 info.item_compare_func = tv_get_string(&argvars[1]);
1532 else if (i != 0)
1533 {
1534 emsg(_(e_invarg));
1535 goto theend;
1536 }
1537 if (info.item_compare_func != NULL)
1538 {
1539 if (*info.item_compare_func == NUL)
1540 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001541 // empty string means default sort
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001542 info.item_compare_func = NULL;
1543 }
1544 else if (STRCMP(info.item_compare_func, "n") == 0)
1545 {
1546 info.item_compare_func = NULL;
1547 info.item_compare_numeric = TRUE;
1548 }
1549 else if (STRCMP(info.item_compare_func, "N") == 0)
1550 {
1551 info.item_compare_func = NULL;
1552 info.item_compare_numbers = TRUE;
1553 }
1554#ifdef FEAT_FLOAT
1555 else if (STRCMP(info.item_compare_func, "f") == 0)
1556 {
1557 info.item_compare_func = NULL;
1558 info.item_compare_float = TRUE;
1559 }
1560#endif
1561 else if (STRCMP(info.item_compare_func, "i") == 0)
1562 {
1563 info.item_compare_func = NULL;
1564 info.item_compare_ic = TRUE;
1565 }
1566 }
1567 }
1568
1569 if (argvars[2].v_type != VAR_UNKNOWN)
1570 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001571 // optional third argument: {dict}
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001572 if (argvars[2].v_type != VAR_DICT)
1573 {
1574 emsg(_(e_dictreq));
1575 goto theend;
1576 }
1577 info.item_compare_selfdict = argvars[2].vval.v_dict;
1578 }
1579 }
1580
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001581 // Make an array with each entry pointing to an item in the List.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001582 ptrs = ALLOC_MULT(sortItem_T, len);
1583 if (ptrs == NULL)
1584 goto theend;
1585
1586 i = 0;
1587 if (sort)
1588 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001589 // sort(): ptrs will be the list to sort
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001590 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001591 {
1592 ptrs[i].item = li;
1593 ptrs[i].idx = i;
1594 ++i;
1595 }
1596
1597 info.item_compare_func_err = FALSE;
1598 info.item_compare_keep_zero = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001599 // test the compare function
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001600 if ((info.item_compare_func != NULL
1601 || info.item_compare_partial != NULL)
1602 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
1603 == ITEM_COMPARE_FAIL)
1604 emsg(_("E702: Sort compare function failed"));
1605 else
1606 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001607 // Sort the array with item pointers.
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001608 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
1609 info.item_compare_func == NULL
1610 && info.item_compare_partial == NULL
1611 ? item_compare : item_compare2);
1612
1613 if (!info.item_compare_func_err)
1614 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001615 // Clear the List and append the items in sorted order.
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001616 l->lv_first = l->lv_u.mat.lv_last
1617 = l->lv_u.mat.lv_idx_item = NULL;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001618 l->lv_len = 0;
1619 for (i = 0; i < len; ++i)
1620 list_append(l, ptrs[i].item);
1621 }
1622 }
1623 }
1624 else
1625 {
1626 int (*item_compare_func_ptr)(const void *, const void *);
1627
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001628 // f_uniq(): ptrs will be a stack of items to remove
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001629 info.item_compare_func_err = FALSE;
1630 info.item_compare_keep_zero = TRUE;
1631 item_compare_func_ptr = info.item_compare_func != NULL
1632 || info.item_compare_partial != NULL
1633 ? item_compare2 : item_compare;
1634
1635 for (li = l->lv_first; li != NULL && li->li_next != NULL;
1636 li = li->li_next)
1637 {
1638 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
1639 == 0)
1640 ptrs[i++].item = li;
1641 if (info.item_compare_func_err)
1642 {
1643 emsg(_("E882: Uniq compare function failed"));
1644 break;
1645 }
1646 }
1647
1648 if (!info.item_compare_func_err)
1649 {
1650 while (--i >= 0)
1651 {
1652 li = ptrs[i].item->li_next;
1653 ptrs[i].item->li_next = li->li_next;
1654 if (li->li_next != NULL)
1655 li->li_next->li_prev = ptrs[i].item;
1656 else
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01001657 l->lv_u.mat.lv_last = ptrs[i].item;
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001658 list_fix_watch(l, li);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 listitem_free(l, li);
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001660 l->lv_len--;
1661 }
1662 }
1663 }
1664
1665 vim_free(ptrs);
1666 }
1667theend:
1668 sortinfo = old_sortinfo;
1669}
1670
1671/*
1672 * "sort({list})" function
1673 */
1674 void
1675f_sort(typval_T *argvars, typval_T *rettv)
1676{
1677 do_sort_uniq(argvars, rettv, TRUE);
1678}
1679
1680/*
1681 * "uniq({list})" function
1682 */
1683 void
1684f_uniq(typval_T *argvars, typval_T *rettv)
1685{
1686 do_sort_uniq(argvars, rettv, FALSE);
1687}
1688
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001689/*
1690 * Handle one item for map() and filter().
1691 */
1692 static int
1693filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
1694{
1695 typval_T rettv;
1696 typval_T argv[3];
1697 int retval = FAIL;
1698
1699 copy_tv(tv, get_vim_var_tv(VV_VAL));
1700 argv[0] = *get_vim_var_tv(VV_KEY);
1701 argv[1] = *get_vim_var_tv(VV_VAL);
1702 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
1703 goto theend;
1704 if (map)
1705 {
1706 // map(): replace the list item value
1707 clear_tv(tv);
1708 rettv.v_lock = 0;
1709 *tv = rettv;
1710 }
1711 else
1712 {
1713 int error = FALSE;
1714
1715 // filter(): when expr is zero remove the item
1716 *remp = (tv_get_number_chk(&rettv, &error) == 0);
1717 clear_tv(&rettv);
1718 // On type error, nothing has been removed; return FAIL to stop the
1719 // loop. The error message was given by tv_get_number_chk().
1720 if (error)
1721 goto theend;
1722 }
1723 retval = OK;
1724theend:
1725 clear_tv(get_vim_var_tv(VV_VAL));
1726 return retval;
1727}
1728
1729/*
1730 * Implementation of map() and filter().
1731 */
1732 static void
1733filter_map(typval_T *argvars, typval_T *rettv, int map)
1734{
1735 typval_T *expr;
1736 listitem_T *li, *nli;
1737 list_T *l = NULL;
1738 dictitem_T *di;
1739 hashtab_T *ht;
1740 hashitem_T *hi;
1741 dict_T *d = NULL;
1742 blob_T *b = NULL;
1743 int rem;
1744 int todo;
1745 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
1746 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
1747 : N_("filter() argument"));
1748 int save_did_emsg;
1749 int idx = 0;
1750
1751 if (argvars[0].v_type == VAR_BLOB)
1752 {
1753 if ((b = argvars[0].vval.v_blob) == NULL)
1754 return;
1755 }
1756 else if (argvars[0].v_type == VAR_LIST)
1757 {
1758 if ((l = argvars[0].vval.v_list) == NULL
1759 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
1760 return;
1761 }
1762 else if (argvars[0].v_type == VAR_DICT)
1763 {
1764 if ((d = argvars[0].vval.v_dict) == NULL
1765 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
1766 return;
1767 }
1768 else
1769 {
1770 semsg(_(e_listdictarg), ermsg);
1771 return;
1772 }
1773
1774 expr = &argvars[1];
1775 // On type errors, the preceding call has already displayed an error
1776 // message. Avoid a misleading error message for an empty string that
1777 // was not passed as argument.
1778 if (expr->v_type != VAR_UNKNOWN)
1779 {
1780 typval_T save_val;
1781 typval_T save_key;
1782
1783 prepare_vimvar(VV_VAL, &save_val);
1784 prepare_vimvar(VV_KEY, &save_key);
1785
1786 // We reset "did_emsg" to be able to detect whether an error
1787 // occurred during evaluation of the expression.
1788 save_did_emsg = did_emsg;
1789 did_emsg = FALSE;
1790
1791 if (argvars[0].v_type == VAR_DICT)
1792 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01001793 int prev_lock = d->dv_lock;
1794
1795 if (map && d->dv_lock == 0)
1796 d->dv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001797 ht = &d->dv_hashtab;
1798 hash_lock(ht);
1799 todo = (int)ht->ht_used;
1800 for (hi = ht->ht_array; todo > 0; ++hi)
1801 {
1802 if (!HASHITEM_EMPTY(hi))
1803 {
1804 int r;
1805
1806 --todo;
1807 di = HI2DI(hi);
1808 if (map && (var_check_lock(di->di_tv.v_lock,
1809 arg_errmsg, TRUE)
1810 || var_check_ro(di->di_flags,
1811 arg_errmsg, TRUE)))
1812 break;
1813 set_vim_var_string(VV_KEY, di->di_key, -1);
1814 r = filter_map_one(&di->di_tv, expr, map, &rem);
1815 clear_tv(get_vim_var_tv(VV_KEY));
1816 if (r == FAIL || did_emsg)
1817 break;
1818 if (!map && rem)
1819 {
1820 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1821 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
1822 break;
1823 dictitem_remove(d, di);
1824 }
1825 }
1826 }
1827 hash_unlock(ht);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01001828 d->dv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001829 }
1830 else if (argvars[0].v_type == VAR_BLOB)
1831 {
1832 int i;
1833 typval_T tv;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01001834 varnumber_T val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001835
1836 // set_vim_var_nr() doesn't set the type
1837 set_vim_var_type(VV_KEY, VAR_NUMBER);
1838
1839 for (i = 0; i < b->bv_ga.ga_len; i++)
1840 {
1841 tv.v_type = VAR_NUMBER;
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01001842 val = blob_get(b, i);
1843 tv.vval.v_number = val;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001844 set_vim_var_nr(VV_KEY, idx);
1845 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
1846 break;
1847 if (tv.v_type != VAR_NUMBER)
1848 {
1849 emsg(_(e_invalblob));
1850 break;
1851 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01001852 if (map)
1853 {
1854 if (tv.vval.v_number != val)
1855 blob_set(b, i, tv.vval.v_number);
1856 }
1857 else if (rem)
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001858 {
1859 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
1860
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01001861 mch_memmove(p + i, p + i + 1,
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001862 (size_t)b->bv_ga.ga_len - i - 1);
1863 --b->bv_ga.ga_len;
1864 --i;
1865 }
Bram Moolenaar49c57ce2020-01-15 20:51:34 +01001866 ++idx;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001867 }
1868 }
1869 else // argvars[0].v_type == VAR_LIST
1870 {
Bram Moolenaardb661fb2020-01-29 22:17:16 +01001871 int prev_lock = l->lv_lock;
1872
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001873 // set_vim_var_nr() doesn't set the type
1874 set_vim_var_type(VV_KEY, VAR_NUMBER);
1875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 range_list_materialize(l);
Bram Moolenaardb661fb2020-01-29 22:17:16 +01001877 if (map && l->lv_lock == 0)
1878 l->lv_lock = VAR_LOCKED;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001879 for (li = l->lv_first; li != NULL; li = nli)
1880 {
1881 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
1882 break;
1883 nli = li->li_next;
1884 set_vim_var_nr(VV_KEY, idx);
1885 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
1886 || did_emsg)
1887 break;
1888 if (!map && rem)
1889 listitem_remove(l, li);
1890 ++idx;
1891 }
Bram Moolenaardb661fb2020-01-29 22:17:16 +01001892 l->lv_lock = prev_lock;
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02001893 }
1894
1895 restore_vimvar(VV_KEY, &save_key);
1896 restore_vimvar(VV_VAL, &save_val);
1897
1898 did_emsg |= save_did_emsg;
1899 }
1900
1901 copy_tv(&argvars[0], rettv);
1902}
1903
1904/*
1905 * "filter()" function
1906 */
1907 void
1908f_filter(typval_T *argvars, typval_T *rettv)
1909{
1910 filter_map(argvars, rettv, FALSE);
1911}
1912
1913/*
1914 * "map()" function
1915 */
1916 void
1917f_map(typval_T *argvars, typval_T *rettv)
1918{
1919 filter_map(argvars, rettv, TRUE);
1920}
1921
Bram Moolenaar08c308a2019-09-04 17:48:15 +02001922/*
1923 * "add(list, item)" function
1924 */
1925 void
1926f_add(typval_T *argvars, typval_T *rettv)
1927{
1928 list_T *l;
1929 blob_T *b;
1930
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001931 rettv->vval.v_number = 1; // Default: Failed
Bram Moolenaar08c308a2019-09-04 17:48:15 +02001932 if (argvars[0].v_type == VAR_LIST)
1933 {
1934 if ((l = argvars[0].vval.v_list) != NULL
1935 && !var_check_lock(l->lv_lock,
1936 (char_u *)N_("add() argument"), TRUE)
1937 && list_append_tv(l, &argvars[1]) == OK)
1938 copy_tv(&argvars[0], rettv);
1939 }
1940 else if (argvars[0].v_type == VAR_BLOB)
1941 {
1942 if ((b = argvars[0].vval.v_blob) != NULL
1943 && !var_check_lock(b->bv_lock,
1944 (char_u *)N_("add() argument"), TRUE))
1945 {
1946 int error = FALSE;
1947 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1948
1949 if (!error)
1950 {
1951 ga_append(&b->bv_ga, (int)n);
1952 copy_tv(&argvars[0], rettv);
1953 }
1954 }
1955 }
1956 else
1957 emsg(_(e_listblobreq));
1958}
1959
1960/*
1961 * "count()" function
1962 */
1963 void
1964f_count(typval_T *argvars, typval_T *rettv)
1965{
1966 long n = 0;
1967 int ic = FALSE;
1968 int error = FALSE;
1969
1970 if (argvars[2].v_type != VAR_UNKNOWN)
1971 ic = (int)tv_get_number_chk(&argvars[2], &error);
1972
1973 if (argvars[0].v_type == VAR_STRING)
1974 {
1975 char_u *expr = tv_get_string_chk(&argvars[1]);
1976 char_u *p = argvars[0].vval.v_string;
1977 char_u *next;
1978
1979 if (!error && expr != NULL && *expr != NUL && p != NULL)
1980 {
1981 if (ic)
1982 {
1983 size_t len = STRLEN(expr);
1984
1985 while (*p != NUL)
1986 {
1987 if (MB_STRNICMP(p, expr, len) == 0)
1988 {
1989 ++n;
1990 p += len;
1991 }
1992 else
1993 MB_PTR_ADV(p);
1994 }
1995 }
1996 else
1997 while ((next = (char_u *)strstr((char *)p, (char *)expr))
1998 != NULL)
1999 {
2000 ++n;
2001 p = next + STRLEN(expr);
2002 }
2003 }
2004
2005 }
2006 else if (argvars[0].v_type == VAR_LIST)
2007 {
2008 listitem_T *li;
2009 list_T *l;
2010 long idx;
2011
2012 if ((l = argvars[0].vval.v_list) != NULL)
2013 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002014 range_list_materialize(l);
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002015 li = l->lv_first;
2016 if (argvars[2].v_type != VAR_UNKNOWN)
2017 {
2018 if (argvars[3].v_type != VAR_UNKNOWN)
2019 {
2020 idx = (long)tv_get_number_chk(&argvars[3], &error);
2021 if (!error)
2022 {
2023 li = list_find(l, idx);
2024 if (li == NULL)
2025 semsg(_(e_listidx), idx);
2026 }
2027 }
2028 if (error)
2029 li = NULL;
2030 }
2031
2032 for ( ; li != NULL; li = li->li_next)
2033 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2034 ++n;
2035 }
2036 }
2037 else if (argvars[0].v_type == VAR_DICT)
2038 {
2039 int todo;
2040 dict_T *d;
2041 hashitem_T *hi;
2042
2043 if ((d = argvars[0].vval.v_dict) != NULL)
2044 {
2045 if (argvars[2].v_type != VAR_UNKNOWN)
2046 {
2047 if (argvars[3].v_type != VAR_UNKNOWN)
2048 emsg(_(e_invarg));
2049 }
2050
2051 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2052 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2053 {
2054 if (!HASHITEM_EMPTY(hi))
2055 {
2056 --todo;
2057 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2058 ++n;
2059 }
2060 }
2061 }
2062 }
2063 else
2064 semsg(_(e_listdictarg), "count()");
2065 rettv->vval.v_number = n;
2066}
2067
2068/*
2069 * "extend(list, list [, idx])" function
2070 * "extend(dict, dict [, action])" function
2071 */
2072 void
2073f_extend(typval_T *argvars, typval_T *rettv)
2074{
2075 char_u *arg_errmsg = (char_u *)N_("extend() argument");
2076
2077 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
2078 {
2079 list_T *l1, *l2;
2080 listitem_T *item;
2081 long before;
2082 int error = FALSE;
2083
2084 l1 = argvars[0].vval.v_list;
2085 l2 = argvars[1].vval.v_list;
2086 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
2087 && l2 != NULL)
2088 {
2089 if (argvars[2].v_type != VAR_UNKNOWN)
2090 {
2091 before = (long)tv_get_number_chk(&argvars[2], &error);
2092 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002093 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002094
2095 if (before == l1->lv_len)
2096 item = NULL;
2097 else
2098 {
2099 item = list_find(l1, before);
2100 if (item == NULL)
2101 {
2102 semsg(_(e_listidx), before);
2103 return;
2104 }
2105 }
2106 }
2107 else
2108 item = NULL;
2109 list_extend(l1, l2, item);
2110
2111 copy_tv(&argvars[0], rettv);
2112 }
2113 }
2114 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
2115 {
2116 dict_T *d1, *d2;
2117 char_u *action;
2118 int i;
2119
2120 d1 = argvars[0].vval.v_dict;
2121 d2 = argvars[1].vval.v_dict;
2122 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
2123 && d2 != NULL)
2124 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002125 // Check the third argument.
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002126 if (argvars[2].v_type != VAR_UNKNOWN)
2127 {
2128 static char *(av[]) = {"keep", "force", "error"};
2129
2130 action = tv_get_string_chk(&argvars[2]);
2131 if (action == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002132 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002133 for (i = 0; i < 3; ++i)
2134 if (STRCMP(action, av[i]) == 0)
2135 break;
2136 if (i == 3)
2137 {
2138 semsg(_(e_invarg2), action);
2139 return;
2140 }
2141 }
2142 else
2143 action = (char_u *)"force";
2144
2145 dict_extend(d1, d2, action);
2146
2147 copy_tv(&argvars[0], rettv);
2148 }
2149 }
2150 else
2151 semsg(_(e_listdictarg), "extend()");
2152}
2153
2154/*
2155 * "insert()" function
2156 */
2157 void
2158f_insert(typval_T *argvars, typval_T *rettv)
2159{
2160 long before = 0;
2161 listitem_T *item;
2162 list_T *l;
2163 int error = FALSE;
2164
2165 if (argvars[0].v_type == VAR_BLOB)
2166 {
2167 int val, len;
2168 char_u *p;
2169
2170 len = blob_len(argvars[0].vval.v_blob);
2171 if (argvars[2].v_type != VAR_UNKNOWN)
2172 {
2173 before = (long)tv_get_number_chk(&argvars[2], &error);
2174 if (error)
2175 return; // type error; errmsg already given
2176 if (before < 0 || before > len)
2177 {
2178 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
2179 return;
2180 }
2181 }
2182 val = tv_get_number_chk(&argvars[1], &error);
2183 if (error)
2184 return;
2185 if (val < 0 || val > 255)
2186 {
2187 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
2188 return;
2189 }
2190
2191 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
2192 return;
2193 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
2194 mch_memmove(p + before + 1, p + before, (size_t)len - before);
2195 *(p + before) = val;
2196 ++argvars[0].vval.v_blob->bv_ga.ga_len;
2197
2198 copy_tv(&argvars[0], rettv);
2199 }
2200 else if (argvars[0].v_type != VAR_LIST)
2201 semsg(_(e_listblobarg), "insert()");
2202 else if ((l = argvars[0].vval.v_list) != NULL
2203 && !var_check_lock(l->lv_lock,
2204 (char_u *)N_("insert() argument"), TRUE))
2205 {
2206 if (argvars[2].v_type != VAR_UNKNOWN)
2207 before = (long)tv_get_number_chk(&argvars[2], &error);
2208 if (error)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002209 return; // type error; errmsg already given
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002210
2211 if (before == l->lv_len)
2212 item = NULL;
2213 else
2214 {
2215 item = list_find(l, before);
2216 if (item == NULL)
2217 {
2218 semsg(_(e_listidx), before);
2219 l = NULL;
2220 }
2221 }
2222 if (l != NULL)
2223 {
2224 list_insert_tv(l, &argvars[1], item);
2225 copy_tv(&argvars[0], rettv);
2226 }
2227 }
2228}
2229
2230/*
2231 * "remove()" function
2232 */
2233 void
2234f_remove(typval_T *argvars, typval_T *rettv)
2235{
2236 char_u *arg_errmsg = (char_u *)N_("remove() argument");
2237
2238 if (argvars[0].v_type == VAR_DICT)
2239 dict_remove(argvars, rettv, arg_errmsg);
2240 else if (argvars[0].v_type == VAR_BLOB)
2241 blob_remove(argvars, rettv);
2242 else if (argvars[0].v_type == VAR_LIST)
2243 list_remove(argvars, rettv, arg_errmsg);
2244 else
2245 semsg(_(e_listdictblobarg), "remove()");
2246}
2247
2248/*
2249 * "reverse({list})" function
2250 */
2251 void
2252f_reverse(typval_T *argvars, typval_T *rettv)
2253{
2254 list_T *l;
2255 listitem_T *li, *ni;
2256
2257 if (argvars[0].v_type == VAR_BLOB)
2258 {
2259 blob_T *b = argvars[0].vval.v_blob;
2260 int i, len = blob_len(b);
2261
2262 for (i = 0; i < len / 2; i++)
2263 {
2264 int tmp = blob_get(b, i);
2265
2266 blob_set(b, i, blob_get(b, len - i - 1));
2267 blob_set(b, len - i - 1, tmp);
2268 }
2269 rettv_blob_set(rettv, b);
2270 return;
2271 }
2272
2273 if (argvars[0].v_type != VAR_LIST)
2274 semsg(_(e_listblobarg), "reverse()");
2275 else if ((l = argvars[0].vval.v_list) != NULL
2276 && !var_check_lock(l->lv_lock,
2277 (char_u *)N_("reverse() argument"), TRUE))
2278 {
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002279 if (l->lv_first == &range_list_item)
2280 {
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002281 varnumber_T new_start = l->lv_u.nonmat.lv_start
2282 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
2283 l->lv_u.nonmat.lv_end = new_start
2284 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
2285 l->lv_u.nonmat.lv_start = new_start;
2286 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
Bram Moolenaar89bfc822020-01-27 22:37:23 +01002287 rettv_list_set(rettv, l);
2288 return;
2289 }
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002290 li = l->lv_u.mat.lv_last;
2291 l->lv_first = l->lv_u.mat.lv_last = NULL;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002292 l->lv_len = 0;
2293 while (li != NULL)
2294 {
2295 ni = li->li_prev;
2296 list_append(l, li);
2297 li = ni;
2298 }
2299 rettv_list_set(rettv, l);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002300 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
Bram Moolenaar08c308a2019-09-04 17:48:15 +02002301 }
2302}
2303
Bram Moolenaar1e1d3002019-09-04 14:41:14 +02002304#endif // defined(FEAT_EVAL)