blob: 1dfaa210e6acd8dad9e872ad0efe9fb065ac5204 [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/*
11 * list.c: List support
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
18/* List heads for garbage collection. */
19static list_T *first_list = NULL; /* list of all lists */
20
21/*
22 * Add a watcher to a list.
23 */
24 void
25list_add_watch(list_T *l, listwatch_T *lw)
26{
27 lw->lw_next = l->lv_watch;
28 l->lv_watch = lw;
29}
30
31/*
32 * Remove a watcher from a list.
33 * No warning when it isn't found...
34 */
35 void
36list_rem_watch(list_T *l, listwatch_T *lwrem)
37{
38 listwatch_T *lw, **lwp;
39
40 lwp = &l->lv_watch;
41 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
42 {
43 if (lw == lwrem)
44 {
45 *lwp = lw->lw_next;
46 break;
47 }
48 lwp = &lw->lw_next;
49 }
50}
51
52/*
53 * Just before removing an item from a list: advance watchers to the next
54 * item.
55 */
56 void
57list_fix_watch(list_T *l, listitem_T *item)
58{
59 listwatch_T *lw;
60
61 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
62 if (lw->lw_item == item)
63 lw->lw_item = item->li_next;
64}
65
66/*
67 * Allocate an empty header for a list.
68 * Caller should take care of the reference count.
69 */
70 list_T *
71list_alloc(void)
72{
73 list_T *l;
74
75 l = (list_T *)alloc_clear(sizeof(list_T));
76 if (l != NULL)
77 {
78 /* Prepend the list to the list of lists for garbage collection. */
79 if (first_list != NULL)
80 first_list->lv_used_prev = l;
81 l->lv_used_prev = NULL;
82 l->lv_used_next = first_list;
83 first_list = l;
84 }
85 return l;
86}
87
88/*
89 * Allocate an empty list for a return value, with reference count set.
90 * Returns OK or FAIL.
91 */
92 int
93rettv_list_alloc(typval_T *rettv)
94{
95 list_T *l = list_alloc();
96
97 if (l == NULL)
98 return FAIL;
99
Bram Moolenaarda861d62016-07-17 15:46:27 +0200100 rettv->v_lock = 0;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200101 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200102 return OK;
103}
104
105/*
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200106 * Set a list as the return value
107 */
108 void
109rettv_list_set(typval_T *rettv, list_T *l)
110{
111 rettv->v_type = VAR_LIST;
112 rettv->vval.v_list = l;
113 if (l != NULL)
114 ++l->lv_refcount;
115}
116
117/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200118 * Unreference a list: decrement the reference count and free it when it
119 * becomes zero.
120 */
121 void
122list_unref(list_T *l)
123{
124 if (l != NULL && --l->lv_refcount <= 0)
125 list_free(l);
126}
127
128/*
129 * Free a list, including all non-container items it points to.
130 * Ignores the reference count.
131 */
132 static void
133list_free_contents(list_T *l)
134{
135 listitem_T *item;
136
137 for (item = l->lv_first; item != NULL; item = l->lv_first)
138 {
139 /* Remove the item before deleting it. */
140 l->lv_first = item->li_next;
141 clear_tv(&item->li_tv);
142 vim_free(item);
143 }
144}
145
146/*
147 * Go through the list of lists and free items without the copyID.
148 * But don't free a list that has a watcher (used in a for loop), these
149 * are not referenced anywhere.
150 */
151 int
152list_free_nonref(int copyID)
153{
154 list_T *ll;
155 int did_free = FALSE;
156
157 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
158 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
159 && ll->lv_watch == NULL)
160 {
161 /* Free the List and ordinary items it contains, but don't recurse
162 * into Lists and Dictionaries, they will be in the list of dicts
163 * or list of lists. */
164 list_free_contents(ll);
165 did_free = TRUE;
166 }
167 return did_free;
168}
169
170 static void
171list_free_list(list_T *l)
172{
173 /* Remove the list from the list of lists for garbage collection. */
174 if (l->lv_used_prev == NULL)
175 first_list = l->lv_used_next;
176 else
177 l->lv_used_prev->lv_used_next = l->lv_used_next;
178 if (l->lv_used_next != NULL)
179 l->lv_used_next->lv_used_prev = l->lv_used_prev;
180
181 vim_free(l);
182}
183
184 void
185list_free_items(int copyID)
186{
187 list_T *ll, *ll_next;
188
189 for (ll = first_list; ll != NULL; ll = ll_next)
190 {
191 ll_next = ll->lv_used_next;
192 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
193 && ll->lv_watch == NULL)
194 {
195 /* Free the List and ordinary items it contains, but don't recurse
196 * into Lists and Dictionaries, they will be in the list of dicts
197 * or list of lists. */
198 list_free_list(ll);
199 }
200 }
201}
202
203 void
204list_free(list_T *l)
205{
206 if (!in_free_unref_items)
207 {
208 list_free_contents(l);
209 list_free_list(l);
210 }
211}
212
213/*
214 * Allocate a list item.
215 * It is not initialized, don't forget to set v_lock.
216 */
217 listitem_T *
218listitem_alloc(void)
219{
220 return (listitem_T *)alloc(sizeof(listitem_T));
221}
222
223/*
224 * Free a list item. Also clears the value. Does not notify watchers.
225 */
226 void
227listitem_free(listitem_T *item)
228{
229 clear_tv(&item->li_tv);
230 vim_free(item);
231}
232
233/*
234 * Remove a list item from a List and free it. Also clears the value.
235 */
236 void
237listitem_remove(list_T *l, listitem_T *item)
238{
239 vimlist_remove(l, item, item);
240 listitem_free(item);
241}
242
243/*
244 * Get the number of items in a list.
245 */
246 long
247list_len(list_T *l)
248{
249 if (l == NULL)
250 return 0L;
251 return l->lv_len;
252}
253
254/*
255 * Return TRUE when two lists have exactly the same values.
256 */
257 int
258list_equal(
259 list_T *l1,
260 list_T *l2,
261 int ic, /* ignore case for strings */
262 int recursive) /* TRUE when used recursively */
263{
264 listitem_T *item1, *item2;
265
266 if (l1 == NULL || l2 == NULL)
267 return FALSE;
268 if (l1 == l2)
269 return TRUE;
270 if (list_len(l1) != list_len(l2))
271 return FALSE;
272
273 for (item1 = l1->lv_first, item2 = l2->lv_first;
274 item1 != NULL && item2 != NULL;
275 item1 = item1->li_next, item2 = item2->li_next)
276 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
277 return FALSE;
278 return item1 == NULL && item2 == NULL;
279}
280
281/*
282 * Locate item with index "n" in list "l" and return it.
283 * A negative index is counted from the end; -1 is the last item.
284 * Returns NULL when "n" is out of range.
285 */
286 listitem_T *
287list_find(list_T *l, long n)
288{
289 listitem_T *item;
290 long idx;
291
292 if (l == NULL)
293 return NULL;
294
295 /* Negative index is relative to the end. */
296 if (n < 0)
297 n = l->lv_len + n;
298
299 /* Check for index out of range. */
300 if (n < 0 || n >= l->lv_len)
301 return NULL;
302
303 /* When there is a cached index may start search from there. */
304 if (l->lv_idx_item != NULL)
305 {
306 if (n < l->lv_idx / 2)
307 {
308 /* closest to the start of the list */
309 item = l->lv_first;
310 idx = 0;
311 }
312 else if (n > (l->lv_idx + l->lv_len) / 2)
313 {
314 /* closest to the end of the list */
315 item = l->lv_last;
316 idx = l->lv_len - 1;
317 }
318 else
319 {
320 /* closest to the cached index */
321 item = l->lv_idx_item;
322 idx = l->lv_idx;
323 }
324 }
325 else
326 {
327 if (n < l->lv_len / 2)
328 {
329 /* closest to the start of the list */
330 item = l->lv_first;
331 idx = 0;
332 }
333 else
334 {
335 /* closest to the end of the list */
336 item = l->lv_last;
337 idx = l->lv_len - 1;
338 }
339 }
340
341 while (n > idx)
342 {
343 /* search forward */
344 item = item->li_next;
345 ++idx;
346 }
347 while (n < idx)
348 {
349 /* search backward */
350 item = item->li_prev;
351 --idx;
352 }
353
354 /* cache the used index */
355 l->lv_idx = idx;
356 l->lv_idx_item = item;
357
358 return item;
359}
360
361/*
362 * Get list item "l[idx]" as a number.
363 */
364 long
365list_find_nr(
366 list_T *l,
367 long idx,
368 int *errorp) /* set to TRUE when something wrong */
369{
370 listitem_T *li;
371
372 li = list_find(l, idx);
373 if (li == NULL)
374 {
375 if (errorp != NULL)
376 *errorp = TRUE;
377 return -1L;
378 }
379 return (long)get_tv_number_chk(&li->li_tv, errorp);
380}
381
382/*
383 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
384 */
385 char_u *
386list_find_str(list_T *l, long idx)
387{
388 listitem_T *li;
389
390 li = list_find(l, idx - 1);
391 if (li == NULL)
392 {
393 EMSGN(_(e_listidx), idx);
394 return NULL;
395 }
396 return get_tv_string(&li->li_tv);
397}
398
399/*
400 * Locate "item" list "l" and return its index.
401 * Returns -1 when "item" is not in the list.
402 */
403 long
404list_idx_of_item(list_T *l, listitem_T *item)
405{
406 long idx = 0;
407 listitem_T *li;
408
409 if (l == NULL)
410 return -1;
411 idx = 0;
412 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
413 ++idx;
414 if (li == NULL)
415 return -1;
416 return idx;
417}
418
419/*
420 * Append item "item" to the end of list "l".
421 */
422 void
423list_append(list_T *l, listitem_T *item)
424{
425 if (l->lv_last == NULL)
426 {
427 /* empty list */
428 l->lv_first = item;
429 l->lv_last = item;
430 item->li_prev = NULL;
431 }
432 else
433 {
434 l->lv_last->li_next = item;
435 item->li_prev = l->lv_last;
436 l->lv_last = item;
437 }
438 ++l->lv_len;
439 item->li_next = NULL;
440}
441
442/*
443 * Append typval_T "tv" to the end of list "l".
444 * Return FAIL when out of memory.
445 */
446 int
447list_append_tv(list_T *l, typval_T *tv)
448{
449 listitem_T *li = listitem_alloc();
450
451 if (li == NULL)
452 return FAIL;
453 copy_tv(tv, &li->li_tv);
454 list_append(l, li);
455 return OK;
456}
457
458/*
459 * Add a dictionary to a list. Used by getqflist().
460 * Return FAIL when out of memory.
461 */
462 int
463list_append_dict(list_T *list, dict_T *dict)
464{
465 listitem_T *li = listitem_alloc();
466
467 if (li == NULL)
468 return FAIL;
469 li->li_tv.v_type = VAR_DICT;
470 li->li_tv.v_lock = 0;
471 li->li_tv.vval.v_dict = dict;
472 list_append(list, li);
473 ++dict->dv_refcount;
474 return OK;
475}
476
477/*
Bram Moolenaar4f505882018-02-10 21:06:32 +0100478 * Append list2 to list1.
479 * Return FAIL when out of memory.
480 */
481 int
482list_append_list(list1, list2)
483 list_T *list1;
484 list_T *list2;
485{
486 listitem_T *li = listitem_alloc();
487
488 if (li == NULL)
489 return FAIL;
490 li->li_tv.v_type = VAR_LIST;
491 li->li_tv.v_lock = 0;
492 li->li_tv.vval.v_list = list2;
493 list_append(list1, li);
494 ++list2->lv_refcount;
495 return OK;
496}
497
498/*
Bram Moolenaarda861d62016-07-17 15:46:27 +0200499 * Make a copy of "str" and append it as an item to list "l".
500 * When "len" >= 0 use "str[len]".
501 * Returns FAIL when out of memory.
502 */
503 int
504list_append_string(list_T *l, char_u *str, int len)
505{
506 listitem_T *li = listitem_alloc();
507
508 if (li == NULL)
509 return FAIL;
510 list_append(l, li);
511 li->li_tv.v_type = VAR_STRING;
512 li->li_tv.v_lock = 0;
513 if (str == NULL)
514 li->li_tv.vval.v_string = NULL;
515 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
516 : vim_strsave(str))) == NULL)
517 return FAIL;
518 return OK;
519}
520
521/*
522 * Append "n" to list "l".
523 * Returns FAIL when out of memory.
524 */
525 int
526list_append_number(list_T *l, varnumber_T n)
527{
528 listitem_T *li;
529
530 li = listitem_alloc();
531 if (li == NULL)
532 return FAIL;
533 li->li_tv.v_type = VAR_NUMBER;
534 li->li_tv.v_lock = 0;
535 li->li_tv.vval.v_number = n;
536 list_append(l, li);
537 return OK;
538}
539
540/*
541 * Insert typval_T "tv" in list "l" before "item".
542 * If "item" is NULL append at the end.
543 * Return FAIL when out of memory.
544 */
545 int
546list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
547{
548 listitem_T *ni = listitem_alloc();
549
550 if (ni == NULL)
551 return FAIL;
552 copy_tv(tv, &ni->li_tv);
553 list_insert(l, ni, item);
554 return OK;
555}
556
557 void
558list_insert(list_T *l, listitem_T *ni, listitem_T *item)
559{
560 if (item == NULL)
561 /* Append new item at end of list. */
562 list_append(l, ni);
563 else
564 {
565 /* Insert new item before existing item. */
566 ni->li_prev = item->li_prev;
567 ni->li_next = item;
568 if (item->li_prev == NULL)
569 {
570 l->lv_first = ni;
571 ++l->lv_idx;
572 }
573 else
574 {
575 item->li_prev->li_next = ni;
576 l->lv_idx_item = NULL;
577 }
578 item->li_prev = ni;
579 ++l->lv_len;
580 }
581}
582
583/*
584 * Extend "l1" with "l2".
585 * If "bef" is NULL append at the end, otherwise insert before this item.
586 * Returns FAIL when out of memory.
587 */
588 int
589list_extend(list_T *l1, list_T *l2, listitem_T *bef)
590{
591 listitem_T *item;
592 int todo = l2->lv_len;
593
594 /* We also quit the loop when we have inserted the original item count of
595 * the list, avoid a hang when we extend a list with itself. */
596 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
597 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
598 return FAIL;
599 return OK;
600}
601
602/*
603 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
604 * Return FAIL when out of memory.
605 */
606 int
607list_concat(list_T *l1, list_T *l2, typval_T *tv)
608{
609 list_T *l;
610
611 if (l1 == NULL || l2 == NULL)
612 return FAIL;
613
614 /* make a copy of the first list. */
615 l = list_copy(l1, FALSE, 0);
616 if (l == NULL)
617 return FAIL;
618 tv->v_type = VAR_LIST;
619 tv->vval.v_list = l;
620
621 /* append all items from the second list */
622 return list_extend(l, l2, NULL);
623}
624
625/*
626 * Make a copy of list "orig". Shallow if "deep" is FALSE.
627 * The refcount of the new list is set to 1.
628 * See item_copy() for "copyID".
629 * Returns NULL when out of memory.
630 */
631 list_T *
632list_copy(list_T *orig, int deep, int copyID)
633{
634 list_T *copy;
635 listitem_T *item;
636 listitem_T *ni;
637
638 if (orig == NULL)
639 return NULL;
640
641 copy = list_alloc();
642 if (copy != NULL)
643 {
644 if (copyID != 0)
645 {
646 /* Do this before adding the items, because one of the items may
647 * refer back to this list. */
648 orig->lv_copyID = copyID;
649 orig->lv_copylist = copy;
650 }
651 for (item = orig->lv_first; item != NULL && !got_int;
652 item = item->li_next)
653 {
654 ni = listitem_alloc();
655 if (ni == NULL)
656 break;
657 if (deep)
658 {
659 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
660 {
661 vim_free(ni);
662 break;
663 }
664 }
665 else
666 copy_tv(&item->li_tv, &ni->li_tv);
667 list_append(copy, ni);
668 }
669 ++copy->lv_refcount;
670 if (item != NULL)
671 {
672 list_unref(copy);
673 copy = NULL;
674 }
675 }
676
677 return copy;
678}
679
680/*
681 * Remove items "item" to "item2" from list "l".
682 * Does not free the listitem or the value!
683 * This used to be called list_remove, but that conflicts with a Sun header
684 * file.
685 */
686 void
687vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
688{
689 listitem_T *ip;
690
691 /* notify watchers */
692 for (ip = item; ip != NULL; ip = ip->li_next)
693 {
694 --l->lv_len;
695 list_fix_watch(l, ip);
696 if (ip == item2)
697 break;
698 }
699
700 if (item2->li_next == NULL)
701 l->lv_last = item->li_prev;
702 else
703 item2->li_next->li_prev = item->li_prev;
704 if (item->li_prev == NULL)
705 l->lv_first = item2->li_next;
706 else
707 item->li_prev->li_next = item2->li_next;
708 l->lv_idx_item = NULL;
709}
710
711/*
712 * Return an allocated string with the string representation of a list.
713 * May return NULL.
714 */
715 char_u *
716list2string(typval_T *tv, int copyID, int restore_copyID)
717{
718 garray_T ga;
719
720 if (tv->vval.v_list == NULL)
721 return NULL;
722 ga_init2(&ga, (int)sizeof(char), 80);
723 ga_append(&ga, '[');
724 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
725 FALSE, restore_copyID, copyID) == FAIL)
726 {
727 vim_free(ga.ga_data);
728 return NULL;
729 }
730 ga_append(&ga, ']');
731 ga_append(&ga, NUL);
732 return (char_u *)ga.ga_data;
733}
734
735typedef struct join_S {
736 char_u *s;
737 char_u *tofree;
738} join_T;
739
740 static int
741list_join_inner(
742 garray_T *gap, /* to store the result in */
743 list_T *l,
744 char_u *sep,
745 int echo_style,
746 int restore_copyID,
747 int copyID,
748 garray_T *join_gap) /* to keep each list item string */
749{
750 int i;
751 join_T *p;
752 int len;
753 int sumlen = 0;
754 int first = TRUE;
755 char_u *tofree;
756 char_u numbuf[NUMBUFLEN];
757 listitem_T *item;
758 char_u *s;
759
760 /* Stringify each item in the list. */
761 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
762 {
763 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +0200764 echo_style, restore_copyID, !echo_style);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200765 if (s == NULL)
766 return FAIL;
767
768 len = (int)STRLEN(s);
769 sumlen += len;
770
771 (void)ga_grow(join_gap, 1);
772 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
773 if (tofree != NULL || s != numbuf)
774 {
775 p->s = s;
776 p->tofree = tofree;
777 }
778 else
779 {
780 p->s = vim_strnsave(s, len);
781 p->tofree = p->s;
782 }
783
784 line_breakcheck();
785 if (did_echo_string_emsg) /* recursion error, bail out */
786 break;
787 }
788
789 /* Allocate result buffer with its total size, avoid re-allocation and
790 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
791 if (join_gap->ga_len >= 2)
792 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
793 if (ga_grow(gap, sumlen + 2) == FAIL)
794 return FAIL;
795
796 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
797 {
798 if (first)
799 first = FALSE;
800 else
801 ga_concat(gap, sep);
802 p = ((join_T *)join_gap->ga_data) + i;
803
804 if (p->s != NULL)
805 ga_concat(gap, p->s);
806 line_breakcheck();
807 }
808
809 return OK;
810}
811
812/*
813 * Join list "l" into a string in "*gap", using separator "sep".
814 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
815 * Return FAIL or OK.
816 */
817 int
818list_join(
819 garray_T *gap,
820 list_T *l,
821 char_u *sep,
822 int echo_style,
823 int restore_copyID,
824 int copyID)
825{
826 garray_T join_ga;
827 int retval;
828 join_T *p;
829 int i;
830
831 if (l->lv_len < 1)
832 return OK; /* nothing to do */
833 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
834 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
835 copyID, &join_ga);
836
837 /* Dispose each item in join_ga. */
838 if (join_ga.ga_data != NULL)
839 {
840 p = (join_T *)join_ga.ga_data;
841 for (i = 0; i < join_ga.ga_len; ++i)
842 {
843 vim_free(p->tofree);
844 ++p;
845 }
846 ga_clear(&join_ga);
847 }
848
849 return retval;
850}
851
852/*
853 * Allocate a variable for a List and fill it from "*arg".
854 * Return OK or FAIL.
855 */
856 int
857get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
858{
859 list_T *l = NULL;
860 typval_T tv;
861 listitem_T *item;
862
863 if (evaluate)
864 {
865 l = list_alloc();
866 if (l == NULL)
867 return FAIL;
868 }
869
870 *arg = skipwhite(*arg + 1);
871 while (**arg != ']' && **arg != NUL)
872 {
873 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
874 goto failret;
875 if (evaluate)
876 {
877 item = listitem_alloc();
878 if (item != NULL)
879 {
880 item->li_tv = tv;
881 item->li_tv.v_lock = 0;
882 list_append(l, item);
883 }
884 else
885 clear_tv(&tv);
886 }
887
888 if (**arg == ']')
889 break;
890 if (**arg != ',')
891 {
892 EMSG2(_("E696: Missing comma in List: %s"), *arg);
893 goto failret;
894 }
895 *arg = skipwhite(*arg + 1);
896 }
897
898 if (**arg != ']')
899 {
900 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
901failret:
902 if (evaluate)
903 list_free(l);
904 return FAIL;
905 }
906
907 *arg = skipwhite(*arg + 1);
908 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200909 rettv_list_set(rettv, l);
Bram Moolenaarda861d62016-07-17 15:46:27 +0200910
911 return OK;
912}
913
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200914/*
Bram Moolenaarcaa55b62017-01-10 13:51:09 +0100915 * Write "list" of strings to file "fd".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200916 */
917 int
918write_list(FILE *fd, list_T *list, int binary)
919{
920 listitem_T *li;
921 int c;
922 int ret = OK;
923 char_u *s;
924
925 for (li = list->lv_first; li != NULL; li = li->li_next)
926 {
927 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
928 {
929 if (*s == '\n')
930 c = putc(NUL, fd);
931 else
932 c = putc(*s, fd);
933 if (c == EOF)
934 {
935 ret = FAIL;
936 break;
937 }
938 }
939 if (!binary || li->li_next != NULL)
940 if (putc('\n', fd) == EOF)
941 {
942 ret = FAIL;
943 break;
944 }
945 if (ret == FAIL)
946 {
947 EMSG(_(e_write));
948 break;
949 }
950 }
951 return ret;
952}
953
Bram Moolenaardf48fb42016-07-22 21:50:18 +0200954/*
955 * Initialize a static list with 10 items.
956 */
957 void
958init_static_list(staticList10_T *sl)
959{
960 list_T *l = &sl->sl_list;
961 int i;
962
963 memset(sl, 0, sizeof(staticList10_T));
964 l->lv_first = &sl->sl_items[0];
965 l->lv_last = &sl->sl_items[9];
966 l->lv_refcount = DO_NOT_FREE_CNT;
967 l->lv_lock = VAR_FIXED;
968 sl->sl_list.lv_len = 10;
969
970 for (i = 0; i < 10; ++i)
971 {
972 listitem_T *li = &sl->sl_items[i];
973
974 if (i == 0)
975 li->li_prev = NULL;
976 else
977 li->li_prev = li - 1;
978 if (i == 9)
979 li->li_next = NULL;
980 else
981 li->li_next = li + 1;
982 }
983}
984
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200985#endif /* defined(FEAT_EVAL) */