annotate src/list.c @ 24484:bc1a533148d7 v8.2.2782

patch 8.2.2782: Vim9: blob operations not fully tested Commit: https://github.com/vim/vim/commit/39211cba723a2cb58a97c7e08826713164b86efc Author: Bram Moolenaar <Bram@vim.org> Date: Sun Apr 18 15:48:04 2021 +0200 patch 8.2.2782: Vim9: blob operations not fully tested Problem: Vim9: blob operations not fully tested. Solution: Make more blob tests run in Vim9 script. Fix filter(). Make insert() give an error for a null blob, like add().
author Bram Moolenaar <Bram@vim.org>
date Sun, 18 Apr 2021 16:00:06 +0200
parents 3d5a66e478f8
children 8f2262c72178
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10042
4aead6a9b7a9 commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents: 9626
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
2 *
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
4 *
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
8 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
10 /*
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
11 * list.c: List support and container (List, Dict, Blob) functions.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
12 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
13
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
14 #include "vim.h"
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
15
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16 #if defined(FEAT_EVAL) || defined(PROTO)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
17
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
18 static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
19
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
20 // List heads for garbage collection.
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
21 static list_T *first_list = NULL; // list of all lists
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
22
19934
3ff714d765ba patch 8.2.0523: loops are repeated
Bram Moolenaar <Bram@vim.org>
parents: 19916
diff changeset
23 #define FOR_ALL_WATCHERS(l, lw) \
3ff714d765ba patch 8.2.0523: loops are repeated
Bram Moolenaar <Bram@vim.org>
parents: 19916
diff changeset
24 for ((lw) = (l)->lv_watch; (lw) != NULL; (lw) = (lw)->lw_next)
3ff714d765ba patch 8.2.0523: loops are repeated
Bram Moolenaar <Bram@vim.org>
parents: 19916
diff changeset
25
19916
dcec86d796bc patch 8.2.0514: several global functions are used in only one file
Bram Moolenaar <Bram@vim.org>
parents: 19888
diff changeset
26 static void list_free_item(list_T *l, listitem_T *item);
dcec86d796bc patch 8.2.0514: several global functions are used in only one file
Bram Moolenaar <Bram@vim.org>
parents: 19888
diff changeset
27
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
28 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
29 * Add a watcher to a list.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
30 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
31 void
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
32 list_add_watch(list_T *l, listwatch_T *lw)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
33 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
34 lw->lw_next = l->lv_watch;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
35 l->lv_watch = lw;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
36 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
37
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
38 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
39 * Remove a watcher from a list.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
40 * No warning when it isn't found...
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
41 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
42 void
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
43 list_rem_watch(list_T *l, listwatch_T *lwrem)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
44 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
45 listwatch_T *lw, **lwp;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
46
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
47 lwp = &l->lv_watch;
19934
3ff714d765ba patch 8.2.0523: loops are repeated
Bram Moolenaar <Bram@vim.org>
parents: 19916
diff changeset
48 FOR_ALL_WATCHERS(l, lw)
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
49 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
50 if (lw == lwrem)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
51 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
52 *lwp = lw->lw_next;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
53 break;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
54 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
55 lwp = &lw->lw_next;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
56 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
57 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
58
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
59 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
60 * Just before removing an item from a list: advance watchers to the next
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
61 * item.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
62 */
17789
0f7ae8010787 patch 8.1.1891: functions used in one file are global
Bram Moolenaar <Bram@vim.org>
parents: 17606
diff changeset
63 static void
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
64 list_fix_watch(list_T *l, listitem_T *item)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
65 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
66 listwatch_T *lw;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
67
19934
3ff714d765ba patch 8.2.0523: loops are repeated
Bram Moolenaar <Bram@vim.org>
parents: 19916
diff changeset
68 FOR_ALL_WATCHERS(l, lw)
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
69 if (lw->lw_item == item)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
70 lw->lw_item = item->li_next;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
71 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
72
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
73 static void
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
74 list_init(list_T *l)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
75 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
76 // Prepend the list to the list of lists for garbage collection.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
77 if (first_list != NULL)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
78 first_list->lv_used_prev = l;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
79 l->lv_used_prev = NULL;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
80 l->lv_used_next = first_list;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
81 first_list = l;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
82 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
83
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
84 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
85 * Allocate an empty header for a list.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
86 * Caller should take care of the reference count.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
87 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
88 list_T *
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
89 list_alloc(void)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
90 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
91 list_T *l;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
92
16825
ce04ebdf26b8 patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents: 16782
diff changeset
93 l = ALLOC_CLEAR_ONE(list_T);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
94 if (l != NULL)
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
95 list_init(l);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
96 return l;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
97 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
98
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
99 /*
15016
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14391
diff changeset
100 * list_alloc() with an ID for alloc_fail().
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14391
diff changeset
101 */
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14391
diff changeset
102 list_T *
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14391
diff changeset
103 list_alloc_id(alloc_id_T id UNUSED)
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14391
diff changeset
104 {
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14391
diff changeset
105 #ifdef FEAT_EVAL
16782
fc58fee685e2 patch 8.1.1393: unnecessary type casts
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
106 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
15016
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14391
diff changeset
107 return NULL;
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14391
diff changeset
108 #endif
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14391
diff changeset
109 return (list_alloc());
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14391
diff changeset
110 }
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14391
diff changeset
111
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14391
diff changeset
112 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
113 * Allocate space for a list, plus "count" items.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
114 * Next list_set_item() must be called for each item.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
115 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
116 list_T *
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
117 list_alloc_with_items(int count)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
118 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
119 list_T *l;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
120
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
121 l = (list_T *)alloc_clear(sizeof(list_T) + count * sizeof(listitem_T));
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
122 if (l != NULL)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
123 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
124 list_init(l);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
125
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
126 if (count > 0)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
127 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
128 listitem_T *li = (listitem_T *)(l + 1);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
129 int i;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
130
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
131 l->lv_len = count;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
132 l->lv_with_items = count;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
133 l->lv_first = li;
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
134 l->lv_u.mat.lv_last = li + count - 1;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
135 for (i = 0; i < count; ++i)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
136 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
137 if (i == 0)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
138 li->li_prev = NULL;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
139 else
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
140 li->li_prev = li - 1;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
141 if (i == count - 1)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
142 li->li_next = NULL;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
143 else
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
144 li->li_next = li + 1;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
145 ++li;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
146 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
147 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
148 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
149 return l;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
150 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
151
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
152 /*
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
153 * Set item "idx" for a list previously allocated with list_alloc_with_items().
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
154 * The contents of "tv" is moved into the list item.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
155 * Each item must be set exactly once.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
156 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
157 void
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
158 list_set_item(list_T *l, int idx, typval_T *tv)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
159 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
160 listitem_T *li = (listitem_T *)(l + 1) + idx;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
161
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
162 li->li_tv = *tv;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
163 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
164
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
165 /*
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
166 * Allocate an empty list for a return value, with reference count set.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
167 * Returns OK or FAIL.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
168 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
169 int
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
170 rettv_list_alloc(typval_T *rettv)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
171 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
172 list_T *l = list_alloc();
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
173
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
174 if (l == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
175 return FAIL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
176
11418
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
177 rettv->v_lock = 0;
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
178 rettv_list_set(rettv, l);
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
179 return OK;
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
180 }
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
181
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
182 /*
15209
3a99b2e6d136 patch 8.1.0614: placing signs can be complicated
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
183 * Same as rettv_list_alloc() but uses an allocation id for testing.
3a99b2e6d136 patch 8.1.0614: placing signs can be complicated
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
184 */
3a99b2e6d136 patch 8.1.0614: placing signs can be complicated
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
185 int
3a99b2e6d136 patch 8.1.0614: placing signs can be complicated
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
186 rettv_list_alloc_id(typval_T *rettv, alloc_id_T id UNUSED)
3a99b2e6d136 patch 8.1.0614: placing signs can be complicated
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
187 {
3a99b2e6d136 patch 8.1.0614: placing signs can be complicated
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
188 #ifdef FEAT_EVAL
16782
fc58fee685e2 patch 8.1.1393: unnecessary type casts
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
189 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
15209
3a99b2e6d136 patch 8.1.0614: placing signs can be complicated
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
190 return FAIL;
3a99b2e6d136 patch 8.1.0614: placing signs can be complicated
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
191 #endif
3a99b2e6d136 patch 8.1.0614: placing signs can be complicated
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
192 return rettv_list_alloc(rettv);
3a99b2e6d136 patch 8.1.0614: placing signs can be complicated
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
193 }
3a99b2e6d136 patch 8.1.0614: placing signs can be complicated
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
194
3a99b2e6d136 patch 8.1.0614: placing signs can be complicated
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
195
3a99b2e6d136 patch 8.1.0614: placing signs can be complicated
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
196 /*
17262
041156ce1d22 patch 8.1.1630: various small problems
Bram Moolenaar <Bram@vim.org>
parents: 16825
diff changeset
197 * Set a list as the return value. Increments the reference count.
11418
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
198 */
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
199 void
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
200 rettv_list_set(typval_T *rettv, list_T *l)
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
201 {
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
202 rettv->v_type = VAR_LIST;
11418
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
203 rettv->vval.v_list = l;
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
204 if (l != NULL)
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
205 ++l->lv_refcount;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
206 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
207
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
208 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
209 * Unreference a list: decrement the reference count and free it when it
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
210 * becomes zero.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
211 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
212 void
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
213 list_unref(list_T *l)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
214 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
215 if (l != NULL && --l->lv_refcount <= 0)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
216 list_free(l);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
217 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
218
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
219 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
220 * Free a list, including all non-container items it points to.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
221 * Ignores the reference count.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
222 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
223 static void
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
224 list_free_contents(list_T *l)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
225 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
226 listitem_T *item;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
227
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
228 if (l->lv_first != &range_list_item)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
229 for (item = l->lv_first; item != NULL; item = l->lv_first)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
230 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
231 // Remove the item before deleting it.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
232 l->lv_first = item->li_next;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
233 clear_tv(&item->li_tv);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
234 list_free_item(l, item);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
235 }
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
236 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
237
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
238 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
239 * Go through the list of lists and free items without the copyID.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
240 * But don't free a list that has a watcher (used in a for loop), these
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
241 * are not referenced anywhere.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
242 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
243 int
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
244 list_free_nonref(int copyID)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
245 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
246 list_T *ll;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
247 int did_free = FALSE;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
248
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
249 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
250 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
251 && ll->lv_watch == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
252 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
253 // Free the List and ordinary items it contains, but don't recurse
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
254 // into Lists and Dictionaries, they will be in the list of dicts
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
255 // or list of lists.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
256 list_free_contents(ll);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
257 did_free = TRUE;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
258 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
259 return did_free;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
260 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
261
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
262 static void
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
263 list_free_list(list_T *l)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
264 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
265 // Remove the list from the list of lists for garbage collection.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
266 if (l->lv_used_prev == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
267 first_list = l->lv_used_next;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
268 else
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
269 l->lv_used_prev->lv_used_next = l->lv_used_next;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
270 if (l->lv_used_next != NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
271 l->lv_used_next->lv_used_prev = l->lv_used_prev;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
272
23458
d2b1269c2c68 patch 8.2.2272: Vim9: extend() can violate the type of a variable
Bram Moolenaar <Bram@vim.org>
parents: 23229
diff changeset
273 free_type(l->lv_type);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
274 vim_free(l);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
275 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
276
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
277 void
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
278 list_free_items(int copyID)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
279 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
280 list_T *ll, *ll_next;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
281
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
282 for (ll = first_list; ll != NULL; ll = ll_next)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
283 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
284 ll_next = ll->lv_used_next;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
285 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
286 && ll->lv_watch == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
287 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
288 // Free the List and ordinary items it contains, but don't recurse
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
289 // into Lists and Dictionaries, they will be in the list of dicts
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
290 // or list of lists.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
291 list_free_list(ll);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
292 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
293 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
294 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
295
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
296 void
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
297 list_free(list_T *l)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
298 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
299 if (!in_free_unref_items)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
300 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
301 list_free_contents(l);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
302 list_free_list(l);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
303 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
304 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
305
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
306 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
307 * Allocate a list item.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
308 * It is not initialized, don't forget to set v_lock.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
309 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
310 listitem_T *
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
311 listitem_alloc(void)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
312 {
16825
ce04ebdf26b8 patch 8.1.1414: alloc() returning "char_u *" causes a lot of type casts
Bram Moolenaar <Bram@vim.org>
parents: 16782
diff changeset
313 return ALLOC_ONE(listitem_T);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
314 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
315
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
316 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
317 * Free a list item, unless it was allocated together with the list itself.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
318 * Does not clear the value. Does not notify watchers.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
319 */
19916
dcec86d796bc patch 8.2.0514: several global functions are used in only one file
Bram Moolenaar <Bram@vim.org>
parents: 19888
diff changeset
320 static void
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
321 list_free_item(list_T *l, listitem_T *item)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
322 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
323 if (l->lv_with_items == 0 || item < (listitem_T *)l
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
324 || item >= (listitem_T *)(l + 1) + l->lv_with_items)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
325 vim_free(item);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
326 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
327
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
328 /*
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
329 * Free a list item, unless it was allocated together with the list itself.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
330 * Also clears the value. Does not notify watchers.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
331 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
332 void
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
333 listitem_free(list_T *l, listitem_T *item)
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
334 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
335 clear_tv(&item->li_tv);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
336 list_free_item(l, item);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
337 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
338
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
339 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
340 * Remove a list item from a List and free it. Also clears the value.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
341 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
342 void
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
343 listitem_remove(list_T *l, listitem_T *item)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
344 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
345 vimlist_remove(l, item, item);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
346 listitem_free(l, item);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
347 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
348
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
349 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
350 * Get the number of items in a list.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
351 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
352 long
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
353 list_len(list_T *l)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
354 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
355 if (l == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
356 return 0L;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
357 return l->lv_len;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
358 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
359
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
360 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
361 * Return TRUE when two lists have exactly the same values.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
362 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
363 int
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
364 list_equal(
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
365 list_T *l1,
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
366 list_T *l2,
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
367 int ic, // ignore case for strings
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
368 int recursive) // TRUE when used recursively
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
369 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
370 listitem_T *item1, *item2;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
371
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
372 if (l1 == l2)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
373 return TRUE;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
374 if (list_len(l1) != list_len(l2))
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
375 return FALSE;
19966
c0eb073378e7 patch 8.2.0539: comparing two NULL list fails
Bram Moolenaar <Bram@vim.org>
parents: 19934
diff changeset
376 if (list_len(l1) == 0)
c0eb073378e7 patch 8.2.0539: comparing two NULL list fails
Bram Moolenaar <Bram@vim.org>
parents: 19934
diff changeset
377 // empty and NULL list are considered equal
c0eb073378e7 patch 8.2.0539: comparing two NULL list fails
Bram Moolenaar <Bram@vim.org>
parents: 19934
diff changeset
378 return TRUE;
c0eb073378e7 patch 8.2.0539: comparing two NULL list fails
Bram Moolenaar <Bram@vim.org>
parents: 19934
diff changeset
379 if (l1 == NULL || l2 == NULL)
c0eb073378e7 patch 8.2.0539: comparing two NULL list fails
Bram Moolenaar <Bram@vim.org>
parents: 19934
diff changeset
380 return FALSE;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
381
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
382 CHECK_LIST_MATERIALIZE(l1);
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
383 CHECK_LIST_MATERIALIZE(l2);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
384
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
385 for (item1 = l1->lv_first, item2 = l2->lv_first;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
386 item1 != NULL && item2 != NULL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
387 item1 = item1->li_next, item2 = item2->li_next)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
388 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
389 return FALSE;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
390 return item1 == NULL && item2 == NULL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
391 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
392
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
393 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
394 * Locate item with index "n" in list "l" and return it.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
395 * A negative index is counted from the end; -1 is the last item.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
396 * Returns NULL when "n" is out of range.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
397 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
398 listitem_T *
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
399 list_find(list_T *l, long n)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
400 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
401 listitem_T *item;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
402 long idx;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
403
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
404 if (l == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
405 return NULL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
406
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
407 // Negative index is relative to the end.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
408 if (n < 0)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
409 n = l->lv_len + n;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
410
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
411 // Check for index out of range.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
412 if (n < 0 || n >= l->lv_len)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
413 return NULL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
414
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
415 CHECK_LIST_MATERIALIZE(l);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
416
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
417 // When there is a cached index may start search from there.
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
418 if (l->lv_u.mat.lv_idx_item != NULL)
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
419 {
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
420 if (n < l->lv_u.mat.lv_idx / 2)
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
421 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
422 // closest to the start of the list
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
423 item = l->lv_first;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
424 idx = 0;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
425 }
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
426 else if (n > (l->lv_u.mat.lv_idx + l->lv_len) / 2)
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
427 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
428 // closest to the end of the list
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
429 item = l->lv_u.mat.lv_last;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
430 idx = l->lv_len - 1;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
431 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
432 else
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
433 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
434 // closest to the cached index
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
435 item = l->lv_u.mat.lv_idx_item;
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
436 idx = l->lv_u.mat.lv_idx;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
437 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
438 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
439 else
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
440 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
441 if (n < l->lv_len / 2)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
442 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
443 // closest to the start of the list
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
444 item = l->lv_first;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
445 idx = 0;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
446 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
447 else
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
448 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
449 // closest to the end of the list
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
450 item = l->lv_u.mat.lv_last;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
451 idx = l->lv_len - 1;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
452 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
453 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
454
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
455 while (n > idx)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
456 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
457 // search forward
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
458 item = item->li_next;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
459 ++idx;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
460 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
461 while (n < idx)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
462 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
463 // search backward
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
464 item = item->li_prev;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
465 --idx;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
466 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
467
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
468 // cache the used index
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
469 l->lv_u.mat.lv_idx = idx;
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
470 l->lv_u.mat.lv_idx_item = item;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
471
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
472 return item;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
473 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
474
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
475 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
476 * Get list item "l[idx]" as a number.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
477 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
478 long
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
479 list_find_nr(
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
480 list_T *l,
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
481 long idx,
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
482 int *errorp) // set to TRUE when something wrong
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
483 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
484 listitem_T *li;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
485
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
486 if (l != NULL && l->lv_first == &range_list_item)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
487 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
488 long n = idx;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
489
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
490 // not materialized range() list: compute the value.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
491 // Negative index is relative to the end.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
492 if (n < 0)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
493 n = l->lv_len + n;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
494
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
495 // Check for index out of range.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
496 if (n < 0 || n >= l->lv_len)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
497 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
498 if (errorp != NULL)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
499 *errorp = TRUE;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
500 return -1L;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
501 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
502
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
503 return l->lv_u.nonmat.lv_start + n * l->lv_u.nonmat.lv_stride;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
504 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
505
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
506 li = list_find(l, idx);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
507 if (li == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
508 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
509 if (errorp != NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
510 *errorp = TRUE;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
511 return -1L;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
512 }
15211
de63593896b3 patch 8.1.0615: get_tv function names are not consistent
Bram Moolenaar <Bram@vim.org>
parents: 15209
diff changeset
513 return (long)tv_get_number_chk(&li->li_tv, errorp);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
514 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
515
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
516 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
517 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
518 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
519 char_u *
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
520 list_find_str(list_T *l, long idx)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
521 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
522 listitem_T *li;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
523
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
524 li = list_find(l, idx - 1);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
525 if (li == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
526 {
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
527 semsg(_(e_listidx), idx);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
528 return NULL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
529 }
15211
de63593896b3 patch 8.1.0615: get_tv function names are not consistent
Bram Moolenaar <Bram@vim.org>
parents: 15209
diff changeset
530 return tv_get_string(&li->li_tv);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
531 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
532
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
533 /*
23982
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
534 * Like list_find() but when a negative index is used that is not found use
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
535 * zero and set "idx" to zero. Used for first index of a range.
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
536 */
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
537 listitem_T *
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
538 list_find_index(list_T *l, long *idx)
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
539 {
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
540 listitem_T *li = list_find(l, *idx);
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
541
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
542 if (li == NULL)
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
543 {
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
544 if (*idx < 0)
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
545 {
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
546 *idx = 0;
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
547 li = list_find(l, *idx);
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
548 }
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
549 }
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
550 return li;
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
551 }
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
552
9fcd71d0db89 patch 8.2.2533: Vim9: cannot use a range with :unlet
Bram Moolenaar <Bram@vim.org>
parents: 23917
diff changeset
553 /*
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
554 * Locate "item" list "l" and return its index.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
555 * Returns -1 when "item" is not in the list.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
556 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
557 long
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
558 list_idx_of_item(list_T *l, listitem_T *item)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
559 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
560 long idx = 0;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
561 listitem_T *li;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
562
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
563 if (l == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
564 return -1;
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
565 CHECK_LIST_MATERIALIZE(l);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
566 idx = 0;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
567 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
568 ++idx;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
569 if (li == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
570 return -1;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
571 return idx;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
572 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
573
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
574 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
575 * Append item "item" to the end of list "l".
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
576 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
577 void
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
578 list_append(list_T *l, listitem_T *item)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
579 {
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
580 CHECK_LIST_MATERIALIZE(l);
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
581 if (l->lv_u.mat.lv_last == NULL)
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
582 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
583 // empty list
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
584 l->lv_first = item;
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
585 l->lv_u.mat.lv_last = item;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
586 item->li_prev = NULL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
587 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
588 else
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
589 {
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
590 l->lv_u.mat.lv_last->li_next = item;
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
591 item->li_prev = l->lv_u.mat.lv_last;
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
592 l->lv_u.mat.lv_last = item;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
593 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
594 ++l->lv_len;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
595 item->li_next = NULL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
596 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
597
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
598 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
599 * Append typval_T "tv" to the end of list "l". "tv" is copied.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
600 * Return FAIL when out of memory.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
601 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
602 int
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
603 list_append_tv(list_T *l, typval_T *tv)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
604 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
605 listitem_T *li = listitem_alloc();
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
606
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
607 if (li == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
608 return FAIL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
609 copy_tv(tv, &li->li_tv);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
610 list_append(l, li);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
611 return OK;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
612 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
613
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
614 /*
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
615 * As list_append_tv() but move the value instead of copying it.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
616 * Return FAIL when out of memory.
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
617 */
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
618 int
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
619 list_append_tv_move(list_T *l, typval_T *tv)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
620 {
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
621 listitem_T *li = listitem_alloc();
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
622
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
623 if (li == NULL)
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
624 return FAIL;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
625 li->li_tv = *tv;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
626 list_append(l, li);
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
627 return OK;
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
628 }
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
629
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
630 /*
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
631 * Add a dictionary to a list. Used by getqflist().
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
632 * Return FAIL when out of memory.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
633 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
634 int
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
635 list_append_dict(list_T *list, dict_T *dict)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
636 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
637 listitem_T *li = listitem_alloc();
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
638
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
639 if (li == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
640 return FAIL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
641 li->li_tv.v_type = VAR_DICT;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
642 li->li_tv.v_lock = 0;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
643 li->li_tv.vval.v_dict = dict;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
644 list_append(list, li);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
645 ++dict->dv_refcount;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
646 return OK;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
647 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
648
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
649 /*
13246
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
650 * Append list2 to list1.
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
651 * Return FAIL when out of memory.
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
652 */
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
653 int
14391
46f14852a919 patch 8.1.0210: still a few K&R function declarations
Christian Brabandt <cb@256bit.org>
parents: 13246
diff changeset
654 list_append_list(list_T *list1, list_T *list2)
13246
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
655 {
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
656 listitem_T *li = listitem_alloc();
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
657
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
658 if (li == NULL)
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
659 return FAIL;
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
660 li->li_tv.v_type = VAR_LIST;
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
661 li->li_tv.v_lock = 0;
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
662 li->li_tv.vval.v_list = list2;
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
663 list_append(list1, li);
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
664 ++list2->lv_refcount;
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
665 return OK;
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
666 }
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
667
dd3b2ecf91f6 patch 8.0.1497: getting the jump list requires parsing the output of :jumps
Christian Brabandt <cb@256bit.org>
parents: 11973
diff changeset
668 /*
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
669 * Make a copy of "str" and append it as an item to list "l".
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
670 * When "len" >= 0 use "str[len]".
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
671 * Returns FAIL when out of memory.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
672 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
673 int
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
674 list_append_string(list_T *l, char_u *str, int len)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
675 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
676 listitem_T *li = listitem_alloc();
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
677
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
678 if (li == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
679 return FAIL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
680 list_append(l, li);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
681 li->li_tv.v_type = VAR_STRING;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
682 li->li_tv.v_lock = 0;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
683 if (str == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
684 li->li_tv.vval.v_string = NULL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
685 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
686 : vim_strsave(str))) == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
687 return FAIL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
688 return OK;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
689 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
690
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
691 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
692 * Append "n" to list "l".
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
693 * Returns FAIL when out of memory.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
694 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
695 int
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
696 list_append_number(list_T *l, varnumber_T n)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
697 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
698 listitem_T *li;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
699
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
700 li = listitem_alloc();
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
701 if (li == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
702 return FAIL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
703 li->li_tv.v_type = VAR_NUMBER;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
704 li->li_tv.v_lock = 0;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
705 li->li_tv.vval.v_number = n;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
706 list_append(l, li);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
707 return OK;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
708 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
709
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
710 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
711 * Insert typval_T "tv" in list "l" before "item".
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
712 * If "item" is NULL append at the end.
23458
d2b1269c2c68 patch 8.2.2272: Vim9: extend() can violate the type of a variable
Bram Moolenaar <Bram@vim.org>
parents: 23229
diff changeset
713 * Return FAIL when out of memory or the type is wrong.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
714 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
715 int
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
716 list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
717 {
23458
d2b1269c2c68 patch 8.2.2272: Vim9: extend() can violate the type of a variable
Bram Moolenaar <Bram@vim.org>
parents: 23229
diff changeset
718 listitem_T *ni;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
719
23458
d2b1269c2c68 patch 8.2.2272: Vim9: extend() can violate the type of a variable
Bram Moolenaar <Bram@vim.org>
parents: 23229
diff changeset
720 if (l->lv_type != NULL && l->lv_type->tt_member != NULL
23917
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23888
diff changeset
721 && check_typval_arg_type(l->lv_type->tt_member, tv, 0) == FAIL)
23458
d2b1269c2c68 patch 8.2.2272: Vim9: extend() can violate the type of a variable
Bram Moolenaar <Bram@vim.org>
parents: 23229
diff changeset
722 return FAIL;
d2b1269c2c68 patch 8.2.2272: Vim9: extend() can violate the type of a variable
Bram Moolenaar <Bram@vim.org>
parents: 23229
diff changeset
723 ni = listitem_alloc();
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
724 if (ni == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
725 return FAIL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
726 copy_tv(tv, &ni->li_tv);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
727 list_insert(l, ni, item);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
728 return OK;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
729 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
730
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
731 void
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
732 list_insert(list_T *l, listitem_T *ni, listitem_T *item)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
733 {
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
734 CHECK_LIST_MATERIALIZE(l);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
735 if (item == NULL)
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
736 // Append new item at end of list.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
737 list_append(l, ni);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
738 else
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
739 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
740 // Insert new item before existing item.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
741 ni->li_prev = item->li_prev;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
742 ni->li_next = item;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
743 if (item->li_prev == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
744 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
745 l->lv_first = ni;
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
746 ++l->lv_u.mat.lv_idx;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
747 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
748 else
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
749 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
750 item->li_prev->li_next = ni;
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
751 l->lv_u.mat.lv_idx_item = NULL;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
752 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
753 item->li_prev = ni;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
754 ++l->lv_len;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
755 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
756 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
757
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
758 /*
20766
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
759 * Flatten "list" to depth "maxdepth".
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
760 * It does nothing if "maxdepth" is 0.
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
761 * Returns FAIL when out of memory.
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
762 */
23816
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
763 static void
20766
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
764 list_flatten(list_T *list, long maxdepth)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
765 {
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
766 listitem_T *item;
20770
b366a0fe8296 patch 8.2.0937: asan failure in the flatten() test
Bram Moolenaar <Bram@vim.org>
parents: 20766
diff changeset
767 listitem_T *tofree;
20766
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
768 int n;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
769
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
770 if (maxdepth == 0)
23816
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
771 return;
20766
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
772 CHECK_LIST_MATERIALIZE(list);
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
773
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
774 n = 0;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
775 item = list->lv_first;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
776 while (item != NULL)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
777 {
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
778 fast_breakcheck();
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
779 if (got_int)
23816
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
780 return;
20766
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
781
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
782 if (item->li_tv.v_type == VAR_LIST)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
783 {
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
784 listitem_T *next = item->li_next;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
785
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
786 vimlist_remove(list, item, item);
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
787 if (list_extend(list, item->li_tv.vval.v_list, next) == FAIL)
23816
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
788 return;
20770
b366a0fe8296 patch 8.2.0937: asan failure in the flatten() test
Bram Moolenaar <Bram@vim.org>
parents: 20766
diff changeset
789 clear_tv(&item->li_tv);
b366a0fe8296 patch 8.2.0937: asan failure in the flatten() test
Bram Moolenaar <Bram@vim.org>
parents: 20766
diff changeset
790 tofree = item;
20766
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
791
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
792 if (item->li_prev == NULL)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
793 item = list->lv_first;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
794 else
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
795 item = item->li_prev->li_next;
20770
b366a0fe8296 patch 8.2.0937: asan failure in the flatten() test
Bram Moolenaar <Bram@vim.org>
parents: 20766
diff changeset
796 list_free_item(list, tofree);
20766
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
797
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
798 if (++n >= maxdepth)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
799 {
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
800 n = 0;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
801 item = next;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
802 }
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
803 }
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
804 else
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
805 {
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
806 n = 0;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
807 item = item->li_next;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
808 }
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
809 }
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
810 }
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
811
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
812 /*
23816
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
813 * "flatten()" and "flattennew()" functions
20766
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
814 */
23816
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
815 static void
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
816 flatten_common(typval_T *argvars, typval_T *rettv, int make_copy)
20766
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
817 {
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
818 list_T *l;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
819 long maxdepth;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
820 int error = FALSE;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
821
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
822 if (argvars[0].v_type != VAR_LIST)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
823 {
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
824 semsg(_(e_listarg), "flatten()");
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
825 return;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
826 }
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
827
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
828 if (argvars[1].v_type == VAR_UNKNOWN)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
829 maxdepth = 999999;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
830 else
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
831 {
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
832 maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
833 if (error)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
834 return;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
835 if (maxdepth < 0)
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
836 {
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
837 emsg(_("E900: maxdepth must be non-negative number"));
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
838 return;
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
839 }
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
840 }
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
841
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
842 l = argvars[0].vval.v_list;
23816
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
843 rettv->v_type = VAR_LIST;
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
844 rettv->vval.v_list = l;
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
845 if (l == NULL)
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
846 return;
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
847
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
848 if (make_copy)
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
849 {
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
850 l = list_copy(l, TRUE, get_copyID());
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
851 rettv->vval.v_list = l;
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
852 if (l == NULL)
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
853 return;
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
854 }
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
855 else
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
856 {
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
857 if (value_check_lock(l->lv_lock,
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
858 (char_u *)N_("flatten() argument"), TRUE))
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
859 return;
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
860 ++l->lv_refcount;
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
861 }
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
862
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
863 list_flatten(l, maxdepth);
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
864 }
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
865
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
866 /*
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
867 * "flatten(list[, {maxdepth}])" function
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
868 */
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
869 void
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
870 f_flatten(typval_T *argvars, typval_T *rettv)
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
871 {
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
872 if (in_vim9script())
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
873 emsg(_(e_cannot_use_flatten_in_vim9_script));
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
874 else
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
875 flatten_common(argvars, rettv, FALSE);
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
876 }
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
877
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
878 /*
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
879 * "flattennew(list[, {maxdepth}])" function
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
880 */
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
881 void
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
882 f_flattennew(typval_T *argvars, typval_T *rettv)
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
883 {
525c9e218c69 patch 8.2.2449: Vim9: flatten() always changes the list type
Bram Moolenaar <Bram@vim.org>
parents: 23800
diff changeset
884 flatten_common(argvars, rettv, TRUE);
20766
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
885 }
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
886
821925509d8c patch 8.2.0935: flattening a list with existing code is slow
Bram Moolenaar <Bram@vim.org>
parents: 20657
diff changeset
887 /*
22545
47596deedfb5 patch 8.2.1821: Vim9: concatenating to a NULL list doesn't work
Bram Moolenaar <Bram@vim.org>
parents: 22303
diff changeset
888 * Extend "l1" with "l2". "l1" must not be NULL.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
889 * If "bef" is NULL append at the end, otherwise insert before this item.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
890 * Returns FAIL when out of memory.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
891 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
892 int
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
893 list_extend(list_T *l1, list_T *l2, listitem_T *bef)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
894 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
895 listitem_T *item;
22545
47596deedfb5 patch 8.2.1821: Vim9: concatenating to a NULL list doesn't work
Bram Moolenaar <Bram@vim.org>
parents: 22303
diff changeset
896 int todo;
24396
d406858354a6 patch 8.2.2738: extending a list with itself can give wrong result
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
897 listitem_T *bef_prev;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
898
22545
47596deedfb5 patch 8.2.1821: Vim9: concatenating to a NULL list doesn't work
Bram Moolenaar <Bram@vim.org>
parents: 22303
diff changeset
899 // NULL list is equivalent to an empty list: nothing to do.
47596deedfb5 patch 8.2.1821: Vim9: concatenating to a NULL list doesn't work
Bram Moolenaar <Bram@vim.org>
parents: 22303
diff changeset
900 if (l2 == NULL || l2->lv_len == 0)
47596deedfb5 patch 8.2.1821: Vim9: concatenating to a NULL list doesn't work
Bram Moolenaar <Bram@vim.org>
parents: 22303
diff changeset
901 return OK;
47596deedfb5 patch 8.2.1821: Vim9: concatenating to a NULL list doesn't work
Bram Moolenaar <Bram@vim.org>
parents: 22303
diff changeset
902
47596deedfb5 patch 8.2.1821: Vim9: concatenating to a NULL list doesn't work
Bram Moolenaar <Bram@vim.org>
parents: 22303
diff changeset
903 todo = l2->lv_len;
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
904 CHECK_LIST_MATERIALIZE(l1);
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
905 CHECK_LIST_MATERIALIZE(l2);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
906
24396
d406858354a6 patch 8.2.2738: extending a list with itself can give wrong result
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
907 // When exending a list with itself, at some point we run into the item
d406858354a6 patch 8.2.2738: extending a list with itself can give wrong result
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
908 // that was before "bef" and need to skip over the already inserted items
d406858354a6 patch 8.2.2738: extending a list with itself can give wrong result
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
909 // to "bef".
d406858354a6 patch 8.2.2738: extending a list with itself can give wrong result
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
910 bef_prev = bef == NULL ? NULL : bef->li_prev;
d406858354a6 patch 8.2.2738: extending a list with itself can give wrong result
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
911
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
912 // We also quit the loop when we have inserted the original item count of
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
913 // the list, avoid a hang when we extend a list with itself.
24396
d406858354a6 patch 8.2.2738: extending a list with itself can give wrong result
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
914 for (item = l2->lv_first; item != NULL && --todo >= 0;
d406858354a6 patch 8.2.2738: extending a list with itself can give wrong result
Bram Moolenaar <Bram@vim.org>
parents: 24160
diff changeset
915 item = item == bef_prev ? bef : item->li_next)
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
916 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
917 return FAIL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
918 return OK;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
919 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
920
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
921 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
922 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
923 * Return FAIL when out of memory.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
924 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
925 int
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
926 list_concat(list_T *l1, list_T *l2, typval_T *tv)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
927 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
928 list_T *l;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
929
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
930 // make a copy of the first list.
22545
47596deedfb5 patch 8.2.1821: Vim9: concatenating to a NULL list doesn't work
Bram Moolenaar <Bram@vim.org>
parents: 22303
diff changeset
931 if (l1 == NULL)
47596deedfb5 patch 8.2.1821: Vim9: concatenating to a NULL list doesn't work
Bram Moolenaar <Bram@vim.org>
parents: 22303
diff changeset
932 l = list_alloc();
47596deedfb5 patch 8.2.1821: Vim9: concatenating to a NULL list doesn't work
Bram Moolenaar <Bram@vim.org>
parents: 22303
diff changeset
933 else
47596deedfb5 patch 8.2.1821: Vim9: concatenating to a NULL list doesn't work
Bram Moolenaar <Bram@vim.org>
parents: 22303
diff changeset
934 l = list_copy(l1, FALSE, 0);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
935 if (l == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
936 return FAIL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
937 tv->v_type = VAR_LIST;
22961
6426acc72539 patch 8.2.2027: Coverity warnts for uninitialized field
Bram Moolenaar <Bram@vim.org>
parents: 22866
diff changeset
938 tv->v_lock = 0;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
939 tv->vval.v_list = l;
22545
47596deedfb5 patch 8.2.1821: Vim9: concatenating to a NULL list doesn't work
Bram Moolenaar <Bram@vim.org>
parents: 22303
diff changeset
940 if (l1 == NULL)
47596deedfb5 patch 8.2.1821: Vim9: concatenating to a NULL list doesn't work
Bram Moolenaar <Bram@vim.org>
parents: 22303
diff changeset
941 ++l->lv_refcount;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
942
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
943 // append all items from the second list
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
944 return list_extend(l, l2, NULL);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
945 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
946
20871
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
947 list_T *
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
948 list_slice(list_T *ol, long n1, long n2)
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
949 {
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
950 listitem_T *item;
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
951 list_T *l = list_alloc();
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
952
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
953 if (l == NULL)
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
954 return NULL;
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
955 for (item = list_find(ol, n1); n1 <= n2; ++n1)
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
956 {
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
957 if (list_append_tv(l, &item->li_tv) == FAIL)
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
958 {
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
959 list_free(l);
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
960 return NULL;
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
961 }
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
962 item = item->li_next;
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
963 }
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
964 return l;
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
965 }
65d9189d4dca patch 8.2.0987: Vim9: cannot assign to [var; var]
Bram Moolenaar <Bram@vim.org>
parents: 20770
diff changeset
966
21828
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
967 int
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
968 list_slice_or_index(
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
969 list_T *list,
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
970 int range,
23604
1816ea68c022 patch 8.2.2344: using inclusive index for slice is not always desired
Bram Moolenaar <Bram@vim.org>
parents: 23588
diff changeset
971 varnumber_T n1_arg,
1816ea68c022 patch 8.2.2344: using inclusive index for slice is not always desired
Bram Moolenaar <Bram@vim.org>
parents: 23588
diff changeset
972 varnumber_T n2_arg,
1816ea68c022 patch 8.2.2344: using inclusive index for slice is not always desired
Bram Moolenaar <Bram@vim.org>
parents: 23588
diff changeset
973 int exclusive,
21828
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
974 typval_T *rettv,
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
975 int verbose)
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
976 {
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
977 long len = list_len(list);
23604
1816ea68c022 patch 8.2.2344: using inclusive index for slice is not always desired
Bram Moolenaar <Bram@vim.org>
parents: 23588
diff changeset
978 varnumber_T n1 = n1_arg;
1816ea68c022 patch 8.2.2344: using inclusive index for slice is not always desired
Bram Moolenaar <Bram@vim.org>
parents: 23588
diff changeset
979 varnumber_T n2 = n2_arg;
21828
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
980 typval_T var1;
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
981
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
982 if (n1 < 0)
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
983 n1 = len + n1;
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
984 if (n1 < 0 || n1 >= len)
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
985 {
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
986 // For a range we allow invalid values and return an empty
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
987 // list. A list index out of range is an error.
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
988 if (!range)
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
989 {
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
990 if (verbose)
23650
83a69ada0274 patch 8.2.2367: test failures on some less often used systems
Bram Moolenaar <Bram@vim.org>
parents: 23646
diff changeset
991 semsg(_(e_listidx), (long)n1_arg);
21828
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
992 return FAIL;
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
993 }
21833
e3f9528bddda patch 8.2.1466: Vim9: cannot index or slice a variable with type "any"
Bram Moolenaar <Bram@vim.org>
parents: 21831
diff changeset
994 n1 = n1 < 0 ? 0 : len;
21828
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
995 }
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
996 if (range)
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
997 {
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
998 list_T *l;
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
999
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1000 if (n2 < 0)
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1001 n2 = len + n2;
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1002 else if (n2 >= len)
23604
1816ea68c022 patch 8.2.2344: using inclusive index for slice is not always desired
Bram Moolenaar <Bram@vim.org>
parents: 23588
diff changeset
1003 n2 = len - (exclusive ? 0 : 1);
1816ea68c022 patch 8.2.2344: using inclusive index for slice is not always desired
Bram Moolenaar <Bram@vim.org>
parents: 23588
diff changeset
1004 if (exclusive)
1816ea68c022 patch 8.2.2344: using inclusive index for slice is not always desired
Bram Moolenaar <Bram@vim.org>
parents: 23588
diff changeset
1005 --n2;
21828
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1006 if (n2 < 0 || n2 + 1 < n1)
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1007 n2 = -1;
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1008 l = list_slice(list, n1, n2);
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1009 if (l == NULL)
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1010 return FAIL;
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1011 clear_tv(rettv);
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1012 rettv_list_set(rettv, l);
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1013 }
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1014 else
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1015 {
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1016 // copy the item to "var1" to avoid that freeing the list makes it
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1017 // invalid.
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1018 copy_tv(&list_find(list, n1)->li_tv, &var1);
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1019 clear_tv(rettv);
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1020 *rettv = var1;
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1021 }
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1022 return OK;
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1023 }
af5db9b6d210 patch 8.2.1463: Vim9: list slice not supported yet
Bram Moolenaar <Bram@vim.org>
parents: 21789
diff changeset
1024
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1025 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1026 * Make a copy of list "orig". Shallow if "deep" is FALSE.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1027 * The refcount of the new list is set to 1.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1028 * See item_copy() for "copyID".
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1029 * Returns NULL when out of memory.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1030 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1031 list_T *
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1032 list_copy(list_T *orig, int deep, int copyID)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1033 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1034 list_T *copy;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1035 listitem_T *item;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1036 listitem_T *ni;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1037
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1038 if (orig == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1039 return NULL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1040
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1041 copy = list_alloc();
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1042 if (copy != NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1043 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1044 if (copyID != 0)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1045 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1046 // Do this before adding the items, because one of the items may
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1047 // refer back to this list.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1048 orig->lv_copyID = copyID;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1049 orig->lv_copylist = copy;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1050 }
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
1051 CHECK_LIST_MATERIALIZE(orig);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1052 for (item = orig->lv_first; item != NULL && !got_int;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1053 item = item->li_next)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1054 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1055 ni = listitem_alloc();
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1056 if (ni == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1057 break;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1058 if (deep)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1059 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1060 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1061 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1062 vim_free(ni);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1063 break;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1064 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1065 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1066 else
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1067 copy_tv(&item->li_tv, &ni->li_tv);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1068 list_append(copy, ni);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1069 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1070 ++copy->lv_refcount;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1071 if (item != NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1072 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1073 list_unref(copy);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1074 copy = NULL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1075 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1076 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1077
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1078 return copy;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1079 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1080
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1081 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1082 * Remove items "item" to "item2" from list "l".
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1083 * Does not free the listitem or the value!
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1084 * This used to be called list_remove, but that conflicts with a Sun header
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1085 * file.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1086 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1087 void
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1088 vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1089 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1090 listitem_T *ip;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1091
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
1092 CHECK_LIST_MATERIALIZE(l);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
1093
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1094 // notify watchers
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1095 for (ip = item; ip != NULL; ip = ip->li_next)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1096 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1097 --l->lv_len;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1098 list_fix_watch(l, ip);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1099 if (ip == item2)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1100 break;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1101 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1102
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1103 if (item2->li_next == NULL)
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
1104 l->lv_u.mat.lv_last = item->li_prev;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1105 else
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1106 item2->li_next->li_prev = item->li_prev;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1107 if (item->li_prev == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1108 l->lv_first = item2->li_next;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1109 else
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1110 item->li_prev->li_next = item2->li_next;
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
1111 l->lv_u.mat.lv_idx_item = NULL;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1112 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1113
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1114 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1115 * Return an allocated string with the string representation of a list.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1116 * May return NULL.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1117 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1118 char_u *
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1119 list2string(typval_T *tv, int copyID, int restore_copyID)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1120 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1121 garray_T ga;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1122
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1123 if (tv->vval.v_list == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1124 return NULL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1125 ga_init2(&ga, (int)sizeof(char), 80);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1126 ga_append(&ga, '[');
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
1127 CHECK_LIST_MATERIALIZE(tv->vval.v_list);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1128 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1129 FALSE, restore_copyID, copyID) == FAIL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1130 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1131 vim_free(ga.ga_data);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1132 return NULL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1133 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1134 ga_append(&ga, ']');
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1135 ga_append(&ga, NUL);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1136 return (char_u *)ga.ga_data;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1137 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1138
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1139 typedef struct join_S {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1140 char_u *s;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1141 char_u *tofree;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1142 } join_T;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1143
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1144 static int
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1145 list_join_inner(
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1146 garray_T *gap, // to store the result in
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1147 list_T *l,
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1148 char_u *sep,
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1149 int echo_style,
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1150 int restore_copyID,
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1151 int copyID,
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1152 garray_T *join_gap) // to keep each list item string
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1153 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1154 int i;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1155 join_T *p;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1156 int len;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1157 int sumlen = 0;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1158 int first = TRUE;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1159 char_u *tofree;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1160 char_u numbuf[NUMBUFLEN];
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1161 listitem_T *item;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1162 char_u *s;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1163
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1164 // Stringify each item in the list.
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
1165 CHECK_LIST_MATERIALIZE(l);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1166 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1167 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1168 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
11973
aec3df2af27c patch 8.0.0867: job and channel in a dict value not quoted
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
1169 echo_style, restore_copyID, !echo_style);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1170 if (s == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1171 return FAIL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1172
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1173 len = (int)STRLEN(s);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1174 sumlen += len;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1175
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1176 (void)ga_grow(join_gap, 1);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1177 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1178 if (tofree != NULL || s != numbuf)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1179 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1180 p->s = s;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1181 p->tofree = tofree;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1182 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1183 else
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1184 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1185 p->s = vim_strnsave(s, len);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1186 p->tofree = p->s;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1187 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1188
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1189 line_breakcheck();
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1190 if (did_echo_string_emsg) // recursion error, bail out
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1191 break;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1192 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1193
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1194 // Allocate result buffer with its total size, avoid re-allocation and
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1195 // multiple copy operations. Add 2 for a tailing ']' and NUL.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1196 if (join_gap->ga_len >= 2)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1197 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1198 if (ga_grow(gap, sumlen + 2) == FAIL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1199 return FAIL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1200
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1201 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1202 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1203 if (first)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1204 first = FALSE;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1205 else
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1206 ga_concat(gap, sep);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1207 p = ((join_T *)join_gap->ga_data) + i;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1208
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1209 if (p->s != NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1210 ga_concat(gap, p->s);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1211 line_breakcheck();
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1212 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1213
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1214 return OK;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1215 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1216
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1217 /*
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1218 * Join list "l" into a string in "*gap", using separator "sep".
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1219 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1220 * Return FAIL or OK.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1221 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1222 int
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1223 list_join(
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1224 garray_T *gap,
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1225 list_T *l,
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1226 char_u *sep,
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1227 int echo_style,
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1228 int restore_copyID,
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1229 int copyID)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1230 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1231 garray_T join_ga;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1232 int retval;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1233 join_T *p;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1234 int i;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1235
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1236 if (l->lv_len < 1)
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1237 return OK; // nothing to do
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1238 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1239 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1240 copyID, &join_ga);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1241
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1242 // Dispose each item in join_ga.
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1243 if (join_ga.ga_data != NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1244 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1245 p = (join_T *)join_ga.ga_data;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1246 for (i = 0; i < join_ga.ga_len; ++i)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1247 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1248 vim_free(p->tofree);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1249 ++p;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1250 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1251 ga_clear(&join_ga);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1252 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1253
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1254 return retval;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1255 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1256
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1257 /*
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1258 * "join()" function
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1259 */
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1260 void
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1261 f_join(typval_T *argvars, typval_T *rettv)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1262 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1263 garray_T ga;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1264 char_u *sep;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1265
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1266 if (argvars[0].v_type != VAR_LIST)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1267 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1268 emsg(_(e_listreq));
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1269 return;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1270 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1271 if (argvars[0].vval.v_list == NULL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1272 return;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1273 if (argvars[1].v_type == VAR_UNKNOWN)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1274 sep = (char_u *)" ";
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1275 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1276 sep = tv_get_string_chk(&argvars[1]);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1277
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1278 rettv->v_type = VAR_STRING;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1279
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1280 if (sep != NULL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1281 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1282 ga_init2(&ga, (int)sizeof(char), 80);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1283 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1284 ga_append(&ga, NUL);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1285 rettv->vval.v_string = (char_u *)ga.ga_data;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1286 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1287 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1288 rettv->vval.v_string = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1289 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1290
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1291 /*
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1292 * Allocate a variable for a List and fill it from "*arg".
21028
7acceb76669f patch 8.2.1065: Vim9: no line break allowed inside a list
Bram Moolenaar <Bram@vim.org>
parents: 21000
diff changeset
1293 * "*arg" points to the "[".
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1294 * Return OK or FAIL.
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1295 */
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1296 int
21120
4d844a65183d patch 8.2.1111: inconsistent naming of get_list_tv() and eval_dict()
Bram Moolenaar <Bram@vim.org>
parents: 21118
diff changeset
1297 eval_list(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int do_error)
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1298 {
21028
7acceb76669f patch 8.2.1065: Vim9: no line break allowed inside a list
Bram Moolenaar <Bram@vim.org>
parents: 21000
diff changeset
1299 int evaluate = evalarg == NULL ? FALSE
7acceb76669f patch 8.2.1065: Vim9: no line break allowed inside a list
Bram Moolenaar <Bram@vim.org>
parents: 21000
diff changeset
1300 : evalarg->eval_flags & EVAL_EVALUATE;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1301 list_T *l = NULL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1302 typval_T tv;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1303 listitem_T *item;
21279
8d1d11afd8c8 patch 8.2.1190: Vim9: checking for Vim9 syntax is spread out
Bram Moolenaar <Bram@vim.org>
parents: 21148
diff changeset
1304 int vim9script = in_vim9script();
21028
7acceb76669f patch 8.2.1065: Vim9: no line break allowed inside a list
Bram Moolenaar <Bram@vim.org>
parents: 21000
diff changeset
1305 int had_comma;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1306
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1307 if (evaluate)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1308 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1309 l = list_alloc();
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1310 if (l == NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1311 return FAIL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1312 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1313
21148
667192c5938b patch 8.2.1125: Vim9: double quote can be a string or a comment
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
1314 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1315 while (**arg != ']' && **arg != NUL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1316 {
21028
7acceb76669f patch 8.2.1065: Vim9: no line break allowed inside a list
Bram Moolenaar <Bram@vim.org>
parents: 21000
diff changeset
1317 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1318 goto failret;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1319 if (evaluate)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1320 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1321 item = listitem_alloc();
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1322 if (item != NULL)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1323 {
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1324 item->li_tv = tv;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1325 item->li_tv.v_lock = 0;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1326 list_append(l, item);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1327 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1328 else
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1329 clear_tv(&tv);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1330 }
21556
963913d80284 patch 8.2.1328: no space allowed before comma in list
Bram Moolenaar <Bram@vim.org>
parents: 21552
diff changeset
1331 // Legacy Vim script allowed a space before the comma.
963913d80284 patch 8.2.1328: no space allowed before comma in list
Bram Moolenaar <Bram@vim.org>
parents: 21552
diff changeset
1332 if (!vim9script)
963913d80284 patch 8.2.1328: no space allowed before comma in list
Bram Moolenaar <Bram@vim.org>
parents: 21552
diff changeset
1333 *arg = skipwhite(*arg);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1334
21036
f58ee30d863f patch 8.2.1069: Vim9: fail to check for white space in list
Bram Moolenaar <Bram@vim.org>
parents: 21028
diff changeset
1335 // the comma must come after the value
21028
7acceb76669f patch 8.2.1065: Vim9: no line break allowed inside a list
Bram Moolenaar <Bram@vim.org>
parents: 21000
diff changeset
1336 had_comma = **arg == ',';
7acceb76669f patch 8.2.1065: Vim9: no line break allowed inside a list
Bram Moolenaar <Bram@vim.org>
parents: 21000
diff changeset
1337 if (had_comma)
21036
f58ee30d863f patch 8.2.1069: Vim9: fail to check for white space in list
Bram Moolenaar <Bram@vim.org>
parents: 21028
diff changeset
1338 {
22866
52e64d340a98 patch 8.2.1980: Vim9: some tests are not done at the script level
Bram Moolenaar <Bram@vim.org>
parents: 22848
diff changeset
1339 if (vim9script && !IS_WHITE_OR_NUL((*arg)[1]) && (*arg)[1] != ']')
21036
f58ee30d863f patch 8.2.1069: Vim9: fail to check for white space in list
Bram Moolenaar <Bram@vim.org>
parents: 21028
diff changeset
1340 {
23877
85cf06ddb2a8 patch 8.2.2480: Vim9: some errors for white space do not show context
Bram Moolenaar <Bram@vim.org>
parents: 23816
diff changeset
1341 semsg(_(e_white_space_required_after_str_str), ",", *arg);
21036
f58ee30d863f patch 8.2.1069: Vim9: fail to check for white space in list
Bram Moolenaar <Bram@vim.org>
parents: 21028
diff changeset
1342 goto failret;
f58ee30d863f patch 8.2.1069: Vim9: fail to check for white space in list
Bram Moolenaar <Bram@vim.org>
parents: 21028
diff changeset
1343 }
21028
7acceb76669f patch 8.2.1065: Vim9: no line break allowed inside a list
Bram Moolenaar <Bram@vim.org>
parents: 21000
diff changeset
1344 *arg = skipwhite(*arg + 1);
21036
f58ee30d863f patch 8.2.1069: Vim9: fail to check for white space in list
Bram Moolenaar <Bram@vim.org>
parents: 21028
diff changeset
1345 }
21028
7acceb76669f patch 8.2.1065: Vim9: no line break allowed inside a list
Bram Moolenaar <Bram@vim.org>
parents: 21000
diff changeset
1346
21118
b0baa80cb53f patch 8.2.1110: Vim9: line continuation does not work in function arguments
Bram Moolenaar <Bram@vim.org>
parents: 21064
diff changeset
1347 // The "]" can be on the next line. But a double quoted string may
b0baa80cb53f patch 8.2.1110: Vim9: line continuation does not work in function arguments
Bram Moolenaar <Bram@vim.org>
parents: 21064
diff changeset
1348 // follow, not a comment.
21148
667192c5938b patch 8.2.1125: Vim9: double quote can be a string or a comment
Bram Moolenaar <Bram@vim.org>
parents: 21120
diff changeset
1349 *arg = skipwhite_and_linebreak(*arg, evalarg);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1350 if (**arg == ']')
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1351 break;
21028
7acceb76669f patch 8.2.1065: Vim9: no line break allowed inside a list
Bram Moolenaar <Bram@vim.org>
parents: 21000
diff changeset
1352
7acceb76669f patch 8.2.1065: Vim9: no line break allowed inside a list
Bram Moolenaar <Bram@vim.org>
parents: 21000
diff changeset
1353 if (!had_comma)
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1354 {
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
1355 if (do_error)
21761
5a2373c25a86 patch 8.2.1430: Vim9: error for missing comma instead of extra white space
Bram Moolenaar <Bram@vim.org>
parents: 21556
diff changeset
1356 {
5a2373c25a86 patch 8.2.1430: Vim9: error for missing comma instead of extra white space
Bram Moolenaar <Bram@vim.org>
parents: 21556
diff changeset
1357 if (**arg == ',')
23888
d95403445b6b patch 8.2.2486: Vim9: some errors for white space do not show context
Bram Moolenaar <Bram@vim.org>
parents: 23877
diff changeset
1358 semsg(_(e_no_white_space_allowed_before_str_str),
d95403445b6b patch 8.2.2486: Vim9: some errors for white space do not show context
Bram Moolenaar <Bram@vim.org>
parents: 23877
diff changeset
1359 ",", *arg);
21761
5a2373c25a86 patch 8.2.1430: Vim9: error for missing comma instead of extra white space
Bram Moolenaar <Bram@vim.org>
parents: 21556
diff changeset
1360 else
5a2373c25a86 patch 8.2.1430: Vim9: error for missing comma instead of extra white space
Bram Moolenaar <Bram@vim.org>
parents: 21556
diff changeset
1361 semsg(_("E696: Missing comma in List: %s"), *arg);
5a2373c25a86 patch 8.2.1430: Vim9: error for missing comma instead of extra white space
Bram Moolenaar <Bram@vim.org>
parents: 21556
diff changeset
1362 }
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1363 goto failret;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1364 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1365 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1366
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1367 if (**arg != ']')
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1368 {
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
1369 if (do_error)
19826
293a22b677a8 patch 8.2.0469: Vim9: no error for missing ] after list
Bram Moolenaar <Bram@vim.org>
parents: 19233
diff changeset
1370 semsg(_(e_list_end), *arg);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1371 failret:
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1372 if (evaluate)
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1373 list_free(l);
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1374 return FAIL;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1375 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1376
21552
cbc570e66d11 patch 8.2.1326: Vim9: skipping over white space after list
Bram Moolenaar <Bram@vim.org>
parents: 21279
diff changeset
1377 *arg += 1;
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1378 if (evaluate)
11418
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 10549
diff changeset
1379 rettv_list_set(rettv, l);
9560
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1380
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1381 return OK;
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1382 }
1e68dfd7931b commit https://github.com/vim/vim/commit/da861d631d7e22654faee2789286c685ad548911
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1383
9571
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1384 /*
10549
055b1633aed7 patch 8.0.0164: outdated and misplaced comments
Christian Brabandt <cb@256bit.org>
parents: 10042
diff changeset
1385 * Write "list" of strings to file "fd".
9571
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1386 */
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1387 int
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1388 write_list(FILE *fd, list_T *list, int binary)
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1389 {
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1390 listitem_T *li;
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1391 int c;
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1392 int ret = OK;
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1393 char_u *s;
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1394
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
1395 CHECK_LIST_MATERIALIZE(list);
19888
435726a03481 patch 8.2.0500: using the same loop in many places
Bram Moolenaar <Bram@vim.org>
parents: 19826
diff changeset
1396 FOR_ALL_LIST_ITEMS(list, li)
9571
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1397 {
15211
de63593896b3 patch 8.1.0615: get_tv function names are not consistent
Bram Moolenaar <Bram@vim.org>
parents: 15209
diff changeset
1398 for (s = tv_get_string(&li->li_tv); *s != NUL; ++s)
9571
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1399 {
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1400 if (*s == '\n')
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1401 c = putc(NUL, fd);
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1402 else
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1403 c = putc(*s, fd);
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1404 if (c == EOF)
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1405 {
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1406 ret = FAIL;
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1407 break;
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1408 }
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1409 }
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1410 if (!binary || li->li_next != NULL)
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1411 if (putc('\n', fd) == EOF)
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1412 {
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1413 ret = FAIL;
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1414 break;
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1415 }
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1416 if (ret == FAIL)
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1417 {
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
1418 emsg(_(e_write));
9571
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1419 break;
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1420 }
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1421 }
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1422 return ret;
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1423 }
5eaa708ab50d commit https://github.com/vim/vim/commit/73dad1e64cb42842d8259cb1a255a6fa59822f76
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
1424
9626
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1425 /*
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1426 * Initialize a static list with 10 items.
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1427 */
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1428 void
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1429 init_static_list(staticList10_T *sl)
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1430 {
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1431 list_T *l = &sl->sl_list;
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1432 int i;
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1433
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1434 memset(sl, 0, sizeof(staticList10_T));
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1435 l->lv_first = &sl->sl_items[0];
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
1436 l->lv_u.mat.lv_last = &sl->sl_items[9];
9626
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1437 l->lv_refcount = DO_NOT_FREE_CNT;
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1438 l->lv_lock = VAR_FIXED;
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1439 sl->sl_list.lv_len = 10;
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1440
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1441 for (i = 0; i < 10; ++i)
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1442 {
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1443 listitem_T *li = &sl->sl_items[i];
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1444
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1445 if (i == 0)
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1446 li->li_prev = NULL;
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1447 else
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1448 li->li_prev = li - 1;
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1449 if (i == 9)
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1450 li->li_next = NULL;
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1451 else
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1452 li->li_next = li + 1;
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1453 }
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1454 }
172131507c85 commit https://github.com/vim/vim/commit/df48fb456fb6bf63d94cad9b302ff01d8ee8d311
Christian Brabandt <cb@256bit.org>
parents: 9571
diff changeset
1455
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1456 /*
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1457 * "list2str()" function
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1458 */
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1459 void
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1460 f_list2str(typval_T *argvars, typval_T *rettv)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1461 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1462 list_T *l;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1463 listitem_T *li;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1464 garray_T ga;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1465 int utf8 = FALSE;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1466
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1467 rettv->v_type = VAR_STRING;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1468 rettv->vval.v_string = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1469 if (argvars[0].v_type != VAR_LIST)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1470 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1471 emsg(_(e_invarg));
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1472 return;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1473 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1474
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1475 l = argvars[0].vval.v_list;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1476 if (l == NULL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1477 return; // empty list results in empty string
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1478
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1479 if (argvars[1].v_type != VAR_UNKNOWN)
22121
b50610a6aee0 patch 8.2.1610: Vim9: cannot pass "true" to list2str() and str2list()
Bram Moolenaar <Bram@vim.org>
parents: 22043
diff changeset
1480 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL);
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1481
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
1482 CHECK_LIST_MATERIALIZE(l);
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1483 ga_init2(&ga, 1, 80);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1484 if (has_mbyte || utf8)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1485 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1486 char_u buf[MB_MAXBYTES + 1];
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1487 int (*char2bytes)(int, char_u *);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1488
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1489 if (utf8 || enc_utf8)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1490 char2bytes = utf_char2bytes;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1491 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1492 char2bytes = mb_char2bytes;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1493
19888
435726a03481 patch 8.2.0500: using the same loop in many places
Bram Moolenaar <Bram@vim.org>
parents: 19826
diff changeset
1494 FOR_ALL_LIST_ITEMS(l, li)
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1495 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1496 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1497 ga_concat(&ga, buf);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1498 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1499 ga_append(&ga, NUL);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1500 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1501 else if (ga_grow(&ga, list_len(l) + 1) == OK)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1502 {
19888
435726a03481 patch 8.2.0500: using the same loop in many places
Bram Moolenaar <Bram@vim.org>
parents: 19826
diff changeset
1503 FOR_ALL_LIST_ITEMS(l, li)
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1504 ga_append(&ga, tv_get_number(&li->li_tv));
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1505 ga_append(&ga, NUL);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1506 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1507
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1508 rettv->v_type = VAR_STRING;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1509 rettv->vval.v_string = ga.ga_data;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1510 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1511
19916
dcec86d796bc patch 8.2.0514: several global functions are used in only one file
Bram Moolenaar <Bram@vim.org>
parents: 19888
diff changeset
1512 static void
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1513 list_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1514 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1515 list_T *l;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1516 listitem_T *item, *item2;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1517 listitem_T *li;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1518 int error = FALSE;
23650
83a69ada0274 patch 8.2.2367: test failures on some less often used systems
Bram Moolenaar <Bram@vim.org>
parents: 23646
diff changeset
1519 long idx;
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1520
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1521 if ((l = argvars[0].vval.v_list) == NULL
22298
07e48ee8c3bb patch 8.2.1698: cannot lock a variable in legacy Vim script like in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 22121
diff changeset
1522 || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1523 return;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1524
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1525 idx = (long)tv_get_number_chk(&argvars[1], &error);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1526 if (error)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1527 ; // type error: do nothing, errmsg already given
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1528 else if ((item = list_find(l, idx)) == NULL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1529 semsg(_(e_listidx), idx);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1530 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1531 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1532 if (argvars[2].v_type == VAR_UNKNOWN)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1533 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1534 // Remove one item, return its value.
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1535 vimlist_remove(l, item, item);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1536 *rettv = item->li_tv;
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
1537 list_free_item(l, item);
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1538 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1539 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1540 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1541 // Remove range of items, return list with values.
23650
83a69ada0274 patch 8.2.2367: test failures on some less often used systems
Bram Moolenaar <Bram@vim.org>
parents: 23646
diff changeset
1542 long end = (long)tv_get_number_chk(&argvars[2], &error);
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1543
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1544 if (error)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1545 ; // type error: do nothing
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1546 else if ((item2 = list_find(l, end)) == NULL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1547 semsg(_(e_listidx), end);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1548 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1549 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1550 int cnt = 0;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1551
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1552 for (li = item; li != NULL; li = li->li_next)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1553 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1554 ++cnt;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1555 if (li == item2)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1556 break;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1557 }
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1558 if (li == NULL) // didn't find "item2" after "item"
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1559 emsg(_(e_invrange));
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1560 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1561 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1562 vimlist_remove(l, item, item2);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1563 if (rettv_list_alloc(rettv) == OK)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1564 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1565 l = rettv->vval.v_list;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1566 l->lv_first = item;
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
1567 l->lv_u.mat.lv_last = item2;
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1568 item->li_prev = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1569 item2->li_next = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1570 l->lv_len = cnt;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1571 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1572 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1573 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1574 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1575 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1576 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1577
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1578 static int item_compare(const void *s1, const void *s2);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1579 static int item_compare2(const void *s1, const void *s2);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1580
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1581 // struct used in the array that's given to qsort()
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1582 typedef struct
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1583 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1584 listitem_T *item;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1585 int idx;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1586 } sortItem_T;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1587
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1588 // struct storing information about current sort
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1589 typedef struct
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1590 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1591 int item_compare_ic;
22770
3e4981de5636 patch 8.2.1933: cannot sort using locale ordering
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
1592 int item_compare_lc;
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1593 int item_compare_numeric;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1594 int item_compare_numbers;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1595 #ifdef FEAT_FLOAT
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1596 int item_compare_float;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1597 #endif
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1598 char_u *item_compare_func;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1599 partial_T *item_compare_partial;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1600 dict_T *item_compare_selfdict;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1601 int item_compare_func_err;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1602 int item_compare_keep_zero;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1603 } sortinfo_T;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1604 static sortinfo_T *sortinfo = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1605 #define ITEM_COMPARE_FAIL 999
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1606
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1607 /*
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1608 * Compare functions for f_sort() and f_uniq() below.
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1609 */
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1610 static int
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1611 item_compare(const void *s1, const void *s2)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1612 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1613 sortItem_T *si1, *si2;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1614 typval_T *tv1, *tv2;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1615 char_u *p1, *p2;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1616 char_u *tofree1 = NULL, *tofree2 = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1617 int res;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1618 char_u numbuf1[NUMBUFLEN];
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1619 char_u numbuf2[NUMBUFLEN];
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1620
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1621 si1 = (sortItem_T *)s1;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1622 si2 = (sortItem_T *)s2;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1623 tv1 = &si1->item->li_tv;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1624 tv2 = &si2->item->li_tv;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1625
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1626 if (sortinfo->item_compare_numbers)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1627 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1628 varnumber_T v1 = tv_get_number(tv1);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1629 varnumber_T v2 = tv_get_number(tv2);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1630
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1631 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1632 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1633
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1634 #ifdef FEAT_FLOAT
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1635 if (sortinfo->item_compare_float)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1636 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1637 float_T v1 = tv_get_float(tv1);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1638 float_T v2 = tv_get_float(tv2);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1639
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1640 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1641 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1642 #endif
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1643
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1644 // tv2string() puts quotes around a string and allocates memory. Don't do
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1645 // that for string variables. Use a single quote when comparing with a
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1646 // non-string to do what the docs promise.
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1647 if (tv1->v_type == VAR_STRING)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1648 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1649 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1650 p1 = (char_u *)"'";
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1651 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1652 p1 = tv1->vval.v_string;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1653 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1654 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1655 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1656 if (tv2->v_type == VAR_STRING)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1657 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1658 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1659 p2 = (char_u *)"'";
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1660 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1661 p2 = tv2->vval.v_string;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1662 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1663 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1664 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1665 if (p1 == NULL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1666 p1 = (char_u *)"";
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1667 if (p2 == NULL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1668 p2 = (char_u *)"";
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1669 if (!sortinfo->item_compare_numeric)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1670 {
22770
3e4981de5636 patch 8.2.1933: cannot sort using locale ordering
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
1671 if (sortinfo->item_compare_lc)
3e4981de5636 patch 8.2.1933: cannot sort using locale ordering
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
1672 res = strcoll((char *)p1, (char *)p2);
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1673 else
22770
3e4981de5636 patch 8.2.1933: cannot sort using locale ordering
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
1674 res = sortinfo->item_compare_ic ? STRICMP(p1, p2): STRCMP(p1, p2);
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1675 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1676 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1677 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1678 double n1, n2;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1679 n1 = strtod((char *)p1, (char **)&p1);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1680 n2 = strtod((char *)p2, (char **)&p2);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1681 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1682 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1683
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1684 // When the result would be zero, compare the item indexes. Makes the
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1685 // sort stable.
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1686 if (res == 0 && !sortinfo->item_compare_keep_zero)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1687 res = si1->idx > si2->idx ? 1 : -1;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1688
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1689 vim_free(tofree1);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1690 vim_free(tofree2);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1691 return res;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1692 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1693
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1694 static int
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1695 item_compare2(const void *s1, const void *s2)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1696 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1697 sortItem_T *si1, *si2;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1698 int res;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1699 typval_T rettv;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1700 typval_T argv[3];
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1701 char_u *func_name;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1702 partial_T *partial = sortinfo->item_compare_partial;
17606
ff097edaae89 patch 8.1.1800: function call functions have too many arguments
Bram Moolenaar <Bram@vim.org>
parents: 17530
diff changeset
1703 funcexe_T funcexe;
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1704
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1705 // shortcut after failure in previous call; compare all items equal
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1706 if (sortinfo->item_compare_func_err)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1707 return 0;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1708
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1709 si1 = (sortItem_T *)s1;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1710 si2 = (sortItem_T *)s2;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1711
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1712 if (partial == NULL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1713 func_name = sortinfo->item_compare_func;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1714 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1715 func_name = partial_name(partial);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1716
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1717 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1718 // in the copy without changing the original list items.
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1719 copy_tv(&si1->item->li_tv, &argv[0]);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1720 copy_tv(&si2->item->li_tv, &argv[1]);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1721
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1722 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
20007
aadd1cae2ff5 patch 8.2.0559: clearing a struct is verbose
Bram Moolenaar <Bram@vim.org>
parents: 19966
diff changeset
1723 CLEAR_FIELD(funcexe);
17606
ff097edaae89 patch 8.1.1800: function call functions have too many arguments
Bram Moolenaar <Bram@vim.org>
parents: 17530
diff changeset
1724 funcexe.evaluate = TRUE;
ff097edaae89 patch 8.1.1800: function call functions have too many arguments
Bram Moolenaar <Bram@vim.org>
parents: 17530
diff changeset
1725 funcexe.partial = partial;
ff097edaae89 patch 8.1.1800: function call functions have too many arguments
Bram Moolenaar <Bram@vim.org>
parents: 17530
diff changeset
1726 funcexe.selfdict = sortinfo->item_compare_selfdict;
ff097edaae89 patch 8.1.1800: function call functions have too many arguments
Bram Moolenaar <Bram@vim.org>
parents: 17530
diff changeset
1727 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe);
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1728 clear_tv(&argv[0]);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1729 clear_tv(&argv[1]);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1730
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1731 if (res == FAIL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1732 res = ITEM_COMPARE_FAIL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1733 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1734 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1735 if (sortinfo->item_compare_func_err)
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1736 res = ITEM_COMPARE_FAIL; // return value has wrong type
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1737 clear_tv(&rettv);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1738
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1739 // When the result would be zero, compare the pointers themselves. Makes
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1740 // the sort stable.
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1741 if (res == 0 && !sortinfo->item_compare_keep_zero)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1742 res = si1->idx > si2->idx ? 1 : -1;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1743
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1744 return res;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1745 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1746
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1747 /*
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1748 * "sort()" or "uniq()" function
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1749 */
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1750 static void
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1751 do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1752 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1753 list_T *l;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1754 listitem_T *li;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1755 sortItem_T *ptrs;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1756 sortinfo_T *old_sortinfo;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1757 sortinfo_T info;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1758 long len;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1759 long i;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1760
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1761 // Pointer to current info struct used in compare function. Save and
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1762 // restore the current one for nested calls.
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1763 old_sortinfo = sortinfo;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1764 sortinfo = &info;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1765
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1766 if (argvars[0].v_type != VAR_LIST)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1767 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1768 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1769 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1770 l = argvars[0].vval.v_list;
22298
07e48ee8c3bb patch 8.2.1698: cannot lock a variable in legacy Vim script like in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 22121
diff changeset
1771 if (l == NULL || value_check_lock(l->lv_lock,
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1772 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1773 TRUE))
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1774 goto theend;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1775 rettv_list_set(rettv, l);
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
1776 CHECK_LIST_MATERIALIZE(l);
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1777
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1778 len = list_len(l);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1779 if (len <= 1)
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1780 goto theend; // short list sorts pretty quickly
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1781
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1782 info.item_compare_ic = FALSE;
22770
3e4981de5636 patch 8.2.1933: cannot sort using locale ordering
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
1783 info.item_compare_lc = FALSE;
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1784 info.item_compare_numeric = FALSE;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1785 info.item_compare_numbers = FALSE;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1786 #ifdef FEAT_FLOAT
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1787 info.item_compare_float = FALSE;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1788 #endif
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1789 info.item_compare_func = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1790 info.item_compare_partial = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1791 info.item_compare_selfdict = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1792 if (argvars[1].v_type != VAR_UNKNOWN)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1793 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1794 // optional second argument: {func}
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1795 if (argvars[1].v_type == VAR_FUNC)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1796 info.item_compare_func = argvars[1].vval.v_string;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1797 else if (argvars[1].v_type == VAR_PARTIAL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1798 info.item_compare_partial = argvars[1].vval.v_partial;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1799 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1800 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1801 int error = FALSE;
22303
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1802 int nr = 0;
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1803
22303
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1804 if (argvars[1].v_type == VAR_NUMBER)
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1805 {
22303
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1806 nr = tv_get_number_chk(&argvars[1], &error);
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1807 if (error)
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1808 goto theend; // type error; errmsg already given
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1809 if (nr == 1)
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1810 info.item_compare_ic = TRUE;
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1811 }
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1812 if (nr != 1)
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1813 {
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1814 if (argvars[1].v_type != VAR_NUMBER)
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1815 info.item_compare_func = tv_get_string(&argvars[1]);
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1816 else if (nr != 0)
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1817 {
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1818 emsg(_(e_invarg));
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1819 goto theend;
4ce3906e5997 patch 8.2.1701: Vim9: sort("i") does not work
Bram Moolenaar <Bram@vim.org>
parents: 22298
diff changeset
1820 }
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1821 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1822 if (info.item_compare_func != NULL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1823 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1824 if (*info.item_compare_func == NUL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1825 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1826 // empty string means default sort
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1827 info.item_compare_func = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1828 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1829 else if (STRCMP(info.item_compare_func, "n") == 0)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1830 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1831 info.item_compare_func = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1832 info.item_compare_numeric = TRUE;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1833 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1834 else if (STRCMP(info.item_compare_func, "N") == 0)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1835 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1836 info.item_compare_func = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1837 info.item_compare_numbers = TRUE;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1838 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1839 #ifdef FEAT_FLOAT
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1840 else if (STRCMP(info.item_compare_func, "f") == 0)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1841 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1842 info.item_compare_func = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1843 info.item_compare_float = TRUE;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1844 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1845 #endif
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1846 else if (STRCMP(info.item_compare_func, "i") == 0)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1847 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1848 info.item_compare_func = NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1849 info.item_compare_ic = TRUE;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1850 }
22770
3e4981de5636 patch 8.2.1933: cannot sort using locale ordering
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
1851 else if (STRCMP(info.item_compare_func, "l") == 0)
3e4981de5636 patch 8.2.1933: cannot sort using locale ordering
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
1852 {
3e4981de5636 patch 8.2.1933: cannot sort using locale ordering
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
1853 info.item_compare_func = NULL;
3e4981de5636 patch 8.2.1933: cannot sort using locale ordering
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
1854 info.item_compare_lc = TRUE;
3e4981de5636 patch 8.2.1933: cannot sort using locale ordering
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
1855 }
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1856 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1857 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1858
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1859 if (argvars[2].v_type != VAR_UNKNOWN)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1860 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1861 // optional third argument: {dict}
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1862 if (argvars[2].v_type != VAR_DICT)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1863 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1864 emsg(_(e_dictreq));
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1865 goto theend;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1866 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1867 info.item_compare_selfdict = argvars[2].vval.v_dict;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1868 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1869 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1870
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1871 // Make an array with each entry pointing to an item in the List.
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1872 ptrs = ALLOC_MULT(sortItem_T, len);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1873 if (ptrs == NULL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1874 goto theend;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1875
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1876 i = 0;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1877 if (sort)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1878 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1879 // sort(): ptrs will be the list to sort
19888
435726a03481 patch 8.2.0500: using the same loop in many places
Bram Moolenaar <Bram@vim.org>
parents: 19826
diff changeset
1880 FOR_ALL_LIST_ITEMS(l, li)
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1881 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1882 ptrs[i].item = li;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1883 ptrs[i].idx = i;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1884 ++i;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1885 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1886
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1887 info.item_compare_func_err = FALSE;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1888 info.item_compare_keep_zero = FALSE;
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1889 // test the compare function
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1890 if ((info.item_compare_func != NULL
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1891 || info.item_compare_partial != NULL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1892 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1893 == ITEM_COMPARE_FAIL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1894 emsg(_("E702: Sort compare function failed"));
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1895 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1896 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1897 // Sort the array with item pointers.
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1898 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1899 info.item_compare_func == NULL
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1900 && info.item_compare_partial == NULL
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1901 ? item_compare : item_compare2);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1902
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1903 if (!info.item_compare_func_err)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1904 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1905 // Clear the List and append the items in sorted order.
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
1906 l->lv_first = l->lv_u.mat.lv_last
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
1907 = l->lv_u.mat.lv_idx_item = NULL;
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1908 l->lv_len = 0;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1909 for (i = 0; i < len; ++i)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1910 list_append(l, ptrs[i].item);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1911 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1912 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1913 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1914 else
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1915 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1916 int (*item_compare_func_ptr)(const void *, const void *);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1917
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
1918 // f_uniq(): ptrs will be a stack of items to remove
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1919 info.item_compare_func_err = FALSE;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1920 info.item_compare_keep_zero = TRUE;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1921 item_compare_func_ptr = info.item_compare_func != NULL
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1922 || info.item_compare_partial != NULL
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1923 ? item_compare2 : item_compare;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1924
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1925 for (li = l->lv_first; li != NULL && li->li_next != NULL;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1926 li = li->li_next)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1927 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1928 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1929 == 0)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1930 ptrs[i++].item = li;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1931 if (info.item_compare_func_err)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1932 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1933 emsg(_("E882: Uniq compare function failed"));
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1934 break;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1935 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1936 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1937
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1938 if (!info.item_compare_func_err)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1939 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1940 while (--i >= 0)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1941 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1942 li = ptrs[i].item->li_next;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1943 ptrs[i].item->li_next = li->li_next;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1944 if (li->li_next != NULL)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1945 li->li_next->li_prev = ptrs[i].item;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1946 else
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
1947 l->lv_u.mat.lv_last = ptrs[i].item;
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1948 list_fix_watch(l, li);
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19123
diff changeset
1949 listitem_free(l, li);
17530
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1950 l->lv_len--;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1951 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1952 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1953 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1954
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1955 vim_free(ptrs);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1956 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1957 theend:
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1958 sortinfo = old_sortinfo;
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1959 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1960
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1961 /*
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1962 * "sort({list})" function
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1963 */
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1964 void
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1965 f_sort(typval_T *argvars, typval_T *rettv)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1966 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1967 do_sort_uniq(argvars, rettv, TRUE);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1968 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1969
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1970 /*
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1971 * "uniq({list})" function
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1972 */
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1973 void
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1974 f_uniq(typval_T *argvars, typval_T *rettv)
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1975 {
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1976 do_sort_uniq(argvars, rettv, FALSE);
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1977 }
ef23ec1eee54 patch 8.1.1763: evalfunc.c is still too big
Bram Moolenaar <Bram@vim.org>
parents: 17262
diff changeset
1978
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
1979 typedef enum {
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
1980 FILTERMAP_FILTER,
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
1981 FILTERMAP_MAP,
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
1982 FILTERMAP_MAPNEW
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
1983 } filtermap_T;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
1984
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
1985 /*
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
1986 * Handle one item for map() and filter().
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
1987 * Sets v:val to "tv". Caller must set v:key.
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
1988 */
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
1989 static int
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
1990 filter_map_one(
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
1991 typval_T *tv, // original value
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
1992 typval_T *expr, // callback
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
1993 filtermap_T filtermap,
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
1994 typval_T *newtv, // for map() and mapnew(): new value
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
1995 int *remp) // for filter(): remove flag
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
1996 {
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
1997 typval_T argv[3];
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
1998 int retval = FAIL;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
1999
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2000 copy_tv(tv, get_vim_var_tv(VV_VAL));
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2001 argv[0] = *get_vim_var_tv(VV_KEY);
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2002 argv[1] = *get_vim_var_tv(VV_VAL);
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2003 if (eval_expr_typval(expr, argv, 2, newtv) == FAIL)
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2004 goto theend;
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2005 if (filtermap == FILTERMAP_FILTER)
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2006 {
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2007 int error = FALSE;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2008
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2009 // filter(): when expr is zero remove the item
21831
d8422de73113 patch 8.2.1465: Vim9: subscript not handled properly
Bram Moolenaar <Bram@vim.org>
parents: 21828
diff changeset
2010 if (in_vim9script())
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2011 *remp = !tv2bool(newtv);
21831
d8422de73113 patch 8.2.1465: Vim9: subscript not handled properly
Bram Moolenaar <Bram@vim.org>
parents: 21828
diff changeset
2012 else
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2013 *remp = (tv_get_number_chk(newtv, &error) == 0);
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2014 clear_tv(newtv);
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2015 // On type error, nothing has been removed; return FAIL to stop the
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2016 // loop. The error message was given by tv_get_number_chk().
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2017 if (error)
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2018 goto theend;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2019 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2020 retval = OK;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2021 theend:
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2022 clear_tv(get_vim_var_tv(VV_VAL));
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2023 return retval;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2024 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2025
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2026 /*
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2027 * Implementation of map() and filter().
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2028 */
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2029 static void
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2030 filter_map(typval_T *argvars, typval_T *rettv, filtermap_T filtermap)
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2031 {
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2032 typval_T *expr;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2033 listitem_T *li, *nli;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2034 list_T *l = NULL;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2035 dictitem_T *di;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2036 hashtab_T *ht;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2037 hashitem_T *hi;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2038 dict_T *d = NULL;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2039 blob_T *b = NULL;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2040 int rem;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2041 int todo;
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2042 char_u *ermsg = (char_u *)(filtermap == FILTERMAP_MAP ? "map()"
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2043 : filtermap == FILTERMAP_MAPNEW ? "mapnew()"
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2044 : "filter()");
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2045 char_u *arg_errmsg = (char_u *)(filtermap == FILTERMAP_MAP
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2046 ? N_("map() argument")
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2047 : filtermap == FILTERMAP_MAPNEW
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2048 ? N_("mapnew() argument")
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2049 : N_("filter() argument"));
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2050 int save_did_emsg;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2051 int idx = 0;
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2052 type_T *type = NULL;
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2053 garray_T type_list;
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2054
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2055 // map() and filter() return the first argument, also on failure.
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2056 if (filtermap != FILTERMAP_MAPNEW)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2057 copy_tv(&argvars[0], rettv);
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2058 if (filtermap == FILTERMAP_MAP && in_vim9script())
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2059 {
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2060 // Check that map() does not change the type of the dict.
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2061 ga_init2(&type_list, sizeof(type_T *), 10);
24438
5c6ccab68d1e patch 8.2.2759: Vim9: for loop infers type of loop variable
Bram Moolenaar <Bram@vim.org>
parents: 24396
diff changeset
2062 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2063 }
22608
f140b9036aa5 patch 8.2.1852: map() returing zero for NULL list is unexpected
Bram Moolenaar <Bram@vim.org>
parents: 22545
diff changeset
2064
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2065 if (argvars[0].v_type == VAR_BLOB)
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2066 {
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2067 if (filtermap == FILTERMAP_MAPNEW)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2068 {
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2069 rettv->v_type = VAR_BLOB;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2070 rettv->vval.v_blob = NULL;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2071 }
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2072 if ((b = argvars[0].vval.v_blob) == NULL)
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2073 goto theend;
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2074 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2075 else if (argvars[0].v_type == VAR_LIST)
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2076 {
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2077 if (filtermap == FILTERMAP_MAPNEW)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2078 {
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2079 rettv->v_type = VAR_LIST;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2080 rettv->vval.v_list = NULL;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2081 }
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2082 if ((l = argvars[0].vval.v_list) == NULL
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2083 || (filtermap == FILTERMAP_FILTER
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2084 && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2085 goto theend;
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2086 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2087 else if (argvars[0].v_type == VAR_DICT)
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2088 {
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2089 if (filtermap == FILTERMAP_MAPNEW)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2090 {
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2091 rettv->v_type = VAR_DICT;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2092 rettv->vval.v_dict = NULL;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2093 }
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2094 if ((d = argvars[0].vval.v_dict) == NULL
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2095 || (filtermap == FILTERMAP_FILTER
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2096 && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2097 goto theend;
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2098 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2099 else
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2100 {
20550
09143ab0fbbd patch 8.2.0829: filter() may give misleading error message
Bram Moolenaar <Bram@vim.org>
parents: 20397
diff changeset
2101 semsg(_(e_listdictblobarg), ermsg);
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2102 goto theend;
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2103 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2104
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2105 expr = &argvars[1];
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2106 // On type errors, the preceding call has already displayed an error
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2107 // message. Avoid a misleading error message for an empty string that
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2108 // was not passed as argument.
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2109 if (expr->v_type != VAR_UNKNOWN)
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2110 {
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2111 typval_T save_val;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2112 typval_T save_key;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2113
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2114 prepare_vimvar(VV_VAL, &save_val);
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2115 prepare_vimvar(VV_KEY, &save_key);
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2116
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2117 // We reset "did_emsg" to be able to detect whether an error
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2118 // occurred during evaluation of the expression.
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2119 save_did_emsg = did_emsg;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2120 did_emsg = FALSE;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2121
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2122 if (argvars[0].v_type == VAR_DICT)
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2123 {
19233
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 19229
diff changeset
2124 int prev_lock = d->dv_lock;
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2125 dict_T *d_ret = NULL;
19233
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 19229
diff changeset
2126
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2127 if (filtermap == FILTERMAP_MAPNEW)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2128 {
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2129 if (rettv_dict_alloc(rettv) == FAIL)
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2130 goto theend;
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2131 d_ret = rettv->vval.v_dict;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2132 }
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2133
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2134 if (filtermap != FILTERMAP_FILTER && d->dv_lock == 0)
19233
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 19229
diff changeset
2135 d->dv_lock = VAR_LOCKED;
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2136 ht = &d->dv_hashtab;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2137 hash_lock(ht);
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2138 todo = (int)ht->ht_used;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2139 for (hi = ht->ht_array; todo > 0; ++hi)
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2140 {
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2141 if (!HASHITEM_EMPTY(hi))
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2142 {
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2143 int r;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2144 typval_T newtv;
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2145
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2146 --todo;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2147 di = HI2DI(hi);
23058
77f181975381 patch 8.2.2075: error for const argument to mapnew()
Bram Moolenaar <Bram@vim.org>
parents: 23037
diff changeset
2148 if (filtermap == FILTERMAP_MAP
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2149 && (value_check_lock(di->di_tv.v_lock,
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2150 arg_errmsg, TRUE)
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2151 || var_check_ro(di->di_flags,
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2152 arg_errmsg, TRUE)))
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2153 break;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2154 set_vim_var_string(VV_KEY, di->di_key, -1);
23990
06da0685077b patch 8.2.2537: Vim9: crash when map() fails
Bram Moolenaar <Bram@vim.org>
parents: 23982
diff changeset
2155 newtv.v_type = VAR_UNKNOWN;
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2156 r = filter_map_one(&di->di_tv, expr, filtermap,
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2157 &newtv, &rem);
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2158 clear_tv(get_vim_var_tv(VV_KEY));
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2159 if (r == FAIL || did_emsg)
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2160 {
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2161 clear_tv(&newtv);
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2162 break;
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2163 }
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2164 if (filtermap == FILTERMAP_MAP)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2165 {
23917
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23888
diff changeset
2166 if (type != NULL && check_typval_arg_type(
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23888
diff changeset
2167 type->tt_member, &newtv, 0) == FAIL)
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2168 {
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2169 clear_tv(&newtv);
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2170 break;
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2171 }
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2172 // map(): replace the dict item value
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2173 clear_tv(&di->di_tv);
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2174 newtv.v_lock = 0;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2175 di->di_tv = newtv;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2176 }
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2177 else if (filtermap == FILTERMAP_MAPNEW)
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2178 {
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2179 // mapnew(): add the item value to the new dict
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2180 r = dict_add_tv(d_ret, (char *)di->di_key, &newtv);
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2181 clear_tv(&newtv);
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2182 if (r == FAIL)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2183 break;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2184 }
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2185 else if (filtermap == FILTERMAP_FILTER && rem)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2186 {
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2187 // filter(false): remove the item from the dict
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2188 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2189 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2190 break;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2191 dictitem_remove(d, di);
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2192 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2193 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2194 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2195 hash_unlock(ht);
19233
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 19229
diff changeset
2196 d->dv_lock = prev_lock;
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2197 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2198 else if (argvars[0].v_type == VAR_BLOB)
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2199 {
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2200 int i;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2201 typval_T tv;
19123
09f28c17ac58 patch 8.2.0121: filter() and map() on blob don't work
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
2202 varnumber_T val;
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2203 blob_T *b_ret = b;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2204
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2205 if (filtermap == FILTERMAP_MAPNEW)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2206 {
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2207 if (blob_copy(b, rettv) == FAIL)
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2208 goto theend;
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2209 b_ret = rettv->vval.v_blob;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2210 }
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2211
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2212 // set_vim_var_nr() doesn't set the type
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2213 set_vim_var_type(VV_KEY, VAR_NUMBER);
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2214
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2215 for (i = 0; i < b->bv_ga.ga_len; i++)
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2216 {
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2217 typval_T newtv;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2218
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2219 tv.v_type = VAR_NUMBER;
19123
09f28c17ac58 patch 8.2.0121: filter() and map() on blob don't work
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
2220 val = blob_get(b, i);
09f28c17ac58 patch 8.2.0121: filter() and map() on blob don't work
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
2221 tv.vval.v_number = val;
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2222 set_vim_var_nr(VV_KEY, idx);
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2223 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2224 || did_emsg)
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2225 break;
24484
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2226 if (newtv.v_type != VAR_NUMBER && newtv.v_type != VAR_BOOL)
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2227 {
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2228 clear_tv(&newtv);
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2229 emsg(_(e_invalblob));
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2230 break;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2231 }
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2232 if (filtermap != FILTERMAP_FILTER)
19123
09f28c17ac58 patch 8.2.0121: filter() and map() on blob don't work
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
2233 {
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2234 if (newtv.vval.v_number != val)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2235 blob_set(b_ret, i, newtv.vval.v_number);
19123
09f28c17ac58 patch 8.2.0121: filter() and map() on blob don't work
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
2236 }
09f28c17ac58 patch 8.2.0121: filter() and map() on blob don't work
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
2237 else if (rem)
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2238 {
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2239 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2240
19123
09f28c17ac58 patch 8.2.0121: filter() and map() on blob don't work
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
2241 mch_memmove(p + i, p + i + 1,
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2242 (size_t)b->bv_ga.ga_len - i - 1);
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2243 --b->bv_ga.ga_len;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2244 --i;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2245 }
19123
09f28c17ac58 patch 8.2.0121: filter() and map() on blob don't work
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
2246 ++idx;
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2247 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2248 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2249 else // argvars[0].v_type == VAR_LIST
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2250 {
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2251 int prev_lock = l->lv_lock;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2252 list_T *l_ret = NULL;
19233
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 19229
diff changeset
2253
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2254 if (filtermap == FILTERMAP_MAPNEW)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2255 {
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2256 if (rettv_list_alloc(rettv) == FAIL)
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2257 goto theend;
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2258 l_ret = rettv->vval.v_list;
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2259 }
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2260 // set_vim_var_nr() doesn't set the type
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2261 set_vim_var_type(VV_KEY, VAR_NUMBER);
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2262
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2263 if (filtermap != FILTERMAP_FILTER && l->lv_lock == 0)
19233
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 19229
diff changeset
2264 l->lv_lock = VAR_LOCKED;
23037
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2265
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2266 if (l->lv_first == &range_list_item)
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2267 {
23037
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2268 varnumber_T val = l->lv_u.nonmat.lv_start;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2269 int len = l->lv_len;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2270 int stride = l->lv_u.nonmat.lv_stride;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2271
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2272 // List from range(): loop over the numbers
23565
34aa2907082a patch 8.2.2325: Vim9: crash if map() changes the item type
Bram Moolenaar <Bram@vim.org>
parents: 23551
diff changeset
2273 if (filtermap != FILTERMAP_MAPNEW)
34aa2907082a patch 8.2.2325: Vim9: crash if map() changes the item type
Bram Moolenaar <Bram@vim.org>
parents: 23551
diff changeset
2274 {
34aa2907082a patch 8.2.2325: Vim9: crash if map() changes the item type
Bram Moolenaar <Bram@vim.org>
parents: 23551
diff changeset
2275 l->lv_first = NULL;
34aa2907082a patch 8.2.2325: Vim9: crash if map() changes the item type
Bram Moolenaar <Bram@vim.org>
parents: 23551
diff changeset
2276 l->lv_u.mat.lv_last = NULL;
34aa2907082a patch 8.2.2325: Vim9: crash if map() changes the item type
Bram Moolenaar <Bram@vim.org>
parents: 23551
diff changeset
2277 l->lv_len = 0;
34aa2907082a patch 8.2.2325: Vim9: crash if map() changes the item type
Bram Moolenaar <Bram@vim.org>
parents: 23551
diff changeset
2278 l->lv_u.mat.lv_idx_item = NULL;
34aa2907082a patch 8.2.2325: Vim9: crash if map() changes the item type
Bram Moolenaar <Bram@vim.org>
parents: 23551
diff changeset
2279 }
23037
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2280
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2281 for (idx = 0; idx < len; ++idx)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2282 {
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2283 typval_T tv;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2284 typval_T newtv;
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2285
23037
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2286 tv.v_type = VAR_NUMBER;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2287 tv.v_lock = 0;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2288 tv.vval.v_number = val;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2289 set_vim_var_nr(VV_KEY, idx);
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2290 if (filter_map_one(&tv, expr, filtermap, &newtv, &rem)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2291 == FAIL)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2292 break;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2293 if (did_emsg)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2294 {
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2295 clear_tv(&newtv);
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2296 break;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2297 }
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2298 if (filtermap != FILTERMAP_FILTER)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2299 {
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2300 if (filtermap == FILTERMAP_MAP && type != NULL
23917
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23888
diff changeset
2301 && check_typval_arg_type(
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23888
diff changeset
2302 type->tt_member, &newtv, 0) == FAIL)
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2303 {
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2304 clear_tv(&newtv);
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2305 break;
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2306 }
23037
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2307 // map(), mapnew(): always append the new value to the
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2308 // list
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2309 if (list_append_tv_move(filtermap == FILTERMAP_MAP
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2310 ? l : l_ret, &newtv) == FAIL)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2311 break;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2312 }
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2313 else if (!rem)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2314 {
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2315 // filter(): append the list item value when not rem
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2316 if (list_append_tv_move(l, &tv) == FAIL)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2317 break;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2318 }
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2319
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2320 val += stride;
22848
e4fbe8d1bde9 patch 8.2.1971: memory leak when map() fails
Bram Moolenaar <Bram@vim.org>
parents: 22844
diff changeset
2321 }
23037
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2322 }
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2323 else
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2324 {
23058
77f181975381 patch 8.2.2075: error for const argument to mapnew()
Bram Moolenaar <Bram@vim.org>
parents: 23037
diff changeset
2325 // Materialized list: loop over the items
23037
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2326 for (li = l->lv_first; li != NULL; li = nli)
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2327 {
23037
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2328 typval_T newtv;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2329
23058
77f181975381 patch 8.2.2075: error for const argument to mapnew()
Bram Moolenaar <Bram@vim.org>
parents: 23037
diff changeset
2330 if (filtermap == FILTERMAP_MAP && value_check_lock(
23037
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2331 li->li_tv.v_lock, arg_errmsg, TRUE))
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2332 break;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2333 nli = li->li_next;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2334 set_vim_var_nr(VV_KEY, idx);
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2335 if (filter_map_one(&li->li_tv, expr, filtermap,
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2336 &newtv, &rem) == FAIL)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2337 break;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2338 if (did_emsg)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2339 {
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2340 clear_tv(&newtv);
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2341 break;
23037
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2342 }
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2343 if (filtermap == FILTERMAP_MAP)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2344 {
23917
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23888
diff changeset
2345 if (type != NULL && check_typval_arg_type(
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23888
diff changeset
2346 type->tt_member, &newtv, 0) == FAIL)
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2347 {
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2348 clear_tv(&newtv);
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2349 break;
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2350 }
23037
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2351 // map(): replace the list item value
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2352 clear_tv(&li->li_tv);
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2353 newtv.v_lock = 0;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2354 li->li_tv = newtv;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2355 }
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2356 else if (filtermap == FILTERMAP_MAPNEW)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2357 {
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2358 // mapnew(): append the list item value
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2359 if (list_append_tv_move(l_ret, &newtv) == FAIL)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2360 break;
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2361 }
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2362 else if (filtermap == FILTERMAP_FILTER && rem)
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2363 listitem_remove(l, li);
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2364 ++idx;
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2365 }
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2366 }
23037
4ba6c5eebb28 patch 8.2.2065: using map() and filter() on a range() is inefficient
Bram Moolenaar <Bram@vim.org>
parents: 22961
diff changeset
2367
19233
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 19229
diff changeset
2368 l->lv_lock = prev_lock;
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2369 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2370
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2371 restore_vimvar(VV_KEY, &save_key);
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2372 restore_vimvar(VV_VAL, &save_val);
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2373
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2374 did_emsg |= save_did_emsg;
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2375 }
23646
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2376
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2377 theend:
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2378 if (type != NULL)
5d77a7587927 patch 8.2.2365: Vim9: no check for map() changing item type at script level
Bram Moolenaar <Bram@vim.org>
parents: 23604
diff changeset
2379 clear_type_list(&type_list);
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2380 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2381
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2382 /*
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2383 * "filter()" function
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2384 */
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2385 void
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2386 f_filter(typval_T *argvars, typval_T *rettv)
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2387 {
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2388 filter_map(argvars, rettv, FILTERMAP_FILTER);
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2389 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2390
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2391 /*
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2392 * "map()" function
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2393 */
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2394 void
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2395 f_map(typval_T *argvars, typval_T *rettv)
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2396 {
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2397 filter_map(argvars, rettv, FILTERMAP_MAP);
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2398 }
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2399
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2400 /*
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2401 * "mapnew()" function
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2402 */
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2403 void
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2404 f_mapnew(typval_T *argvars, typval_T *rettv)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2405 {
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22802
diff changeset
2406 filter_map(argvars, rettv, FILTERMAP_MAPNEW);
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2407 }
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
2408
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2409 /*
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2410 * "add(list, item)" function
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2411 */
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2412 void
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2413 f_add(typval_T *argvars, typval_T *rettv)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2414 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
2415 rettv->vval.v_number = 1; // Default: Failed
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2416 if (argvars[0].v_type == VAR_LIST)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2417 {
24482
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2418 list_T *l = argvars[0].vval.v_list;
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2419
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2420 if (l == NULL)
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2421 {
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2422 if (in_vim9script())
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2423 emsg(_(e_cannot_add_to_null_list));
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2424 }
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2425 else if (!value_check_lock(l->lv_lock,
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2426 (char_u *)N_("add() argument"), TRUE)
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2427 && list_append_tv(l, &argvars[1]) == OK)
24482
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2428 {
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2429 copy_tv(&argvars[0], rettv);
24482
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2430 }
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2431 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2432 else if (argvars[0].v_type == VAR_BLOB)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2433 {
24482
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2434 blob_T *b = argvars[0].vval.v_blob;
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2435
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2436 if (b == NULL)
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2437 {
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2438 if (in_vim9script())
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2439 emsg(_(e_cannot_add_to_null_blob));
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2440 }
3d5a66e478f8 patch 8.2.2781: add() silently skips when adding to null list or blob
Bram Moolenaar <Bram@vim.org>
parents: 24438
diff changeset
2441 else if (!value_check_lock(b->bv_lock,
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2442 (char_u *)N_("add() argument"), TRUE))
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2443 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2444 int error = FALSE;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2445 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2446
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2447 if (!error)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2448 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2449 ga_append(&b->bv_ga, (int)n);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2450 copy_tv(&argvars[0], rettv);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2451 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2452 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2453 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2454 else
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2455 emsg(_(e_listblobreq));
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2456 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2457
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2458 /*
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2459 * "count()" function
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2460 */
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2461 void
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2462 f_count(typval_T *argvars, typval_T *rettv)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2463 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2464 long n = 0;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2465 int ic = FALSE;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2466 int error = FALSE;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2467
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2468 if (argvars[2].v_type != VAR_UNKNOWN)
22043
4bc644f4dd2d patch 8.2.1571: Vim9: count() third argument cannot be "true"
Bram Moolenaar <Bram@vim.org>
parents: 21909
diff changeset
2469 ic = (int)tv_get_bool_chk(&argvars[2], &error);
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2470
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2471 if (argvars[0].v_type == VAR_STRING)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2472 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2473 char_u *expr = tv_get_string_chk(&argvars[1]);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2474 char_u *p = argvars[0].vval.v_string;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2475 char_u *next;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2476
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2477 if (!error && expr != NULL && *expr != NUL && p != NULL)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2478 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2479 if (ic)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2480 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2481 size_t len = STRLEN(expr);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2482
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2483 while (*p != NUL)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2484 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2485 if (MB_STRNICMP(p, expr, len) == 0)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2486 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2487 ++n;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2488 p += len;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2489 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2490 else
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2491 MB_PTR_ADV(p);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2492 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2493 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2494 else
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2495 while ((next = (char_u *)strstr((char *)p, (char *)expr))
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2496 != NULL)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2497 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2498 ++n;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2499 p = next + STRLEN(expr);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2500 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2501 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2502
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2503 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2504 else if (argvars[0].v_type == VAR_LIST)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2505 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2506 listitem_T *li;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2507 list_T *l;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2508 long idx;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2509
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2510 if ((l = argvars[0].vval.v_list) != NULL)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2511 {
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 20158
diff changeset
2512 CHECK_LIST_MATERIALIZE(l);
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2513 li = l->lv_first;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2514 if (argvars[2].v_type != VAR_UNKNOWN)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2515 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2516 if (argvars[3].v_type != VAR_UNKNOWN)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2517 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2518 idx = (long)tv_get_number_chk(&argvars[3], &error);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2519 if (!error)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2520 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2521 li = list_find(l, idx);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2522 if (li == NULL)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2523 semsg(_(e_listidx), idx);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2524 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2525 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2526 if (error)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2527 li = NULL;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2528 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2529
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2530 for ( ; li != NULL; li = li->li_next)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2531 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2532 ++n;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2533 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2534 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2535 else if (argvars[0].v_type == VAR_DICT)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2536 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2537 int todo;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2538 dict_T *d;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2539 hashitem_T *hi;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2540
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2541 if ((d = argvars[0].vval.v_dict) != NULL)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2542 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2543 if (argvars[2].v_type != VAR_UNKNOWN)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2544 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2545 if (argvars[3].v_type != VAR_UNKNOWN)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2546 emsg(_(e_invarg));
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2547 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2548
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2549 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2550 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2551 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2552 if (!HASHITEM_EMPTY(hi))
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2553 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2554 --todo;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2555 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2556 ++n;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2557 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2558 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2559 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2560 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2561 else
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2562 semsg(_(e_listdictarg), "count()");
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2563 rettv->vval.v_number = n;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2564 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2565
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2566 /*
23588
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2567 * "extend()" or "extendnew()" function. "is_new" is TRUE for extendnew().
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2568 */
23588
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2569 static void
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2570 extend(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg, int is_new)
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2571 {
23800
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2572 type_T *type = NULL;
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2573 garray_T type_list;
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2574
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2575 if (!is_new && in_vim9script())
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2576 {
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2577 // Check that map() does not change the type of the dict.
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2578 ga_init2(&type_list, sizeof(type_T *), 10);
24438
5c6ccab68d1e patch 8.2.2759: Vim9: for loop infers type of loop variable
Bram Moolenaar <Bram@vim.org>
parents: 24396
diff changeset
2579 type = typval2type(argvars, get_copyID(), &type_list, TRUE);
23800
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2580 }
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2581
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2582 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2583 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2584 list_T *l1, *l2;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2585 listitem_T *item;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2586 long before;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2587 int error = FALSE;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2588
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2589 l1 = argvars[0].vval.v_list;
22802
3e0f909ca1f2 patch 8.2.1949: Vim9: using extend() on null dict is silently ignored
Bram Moolenaar <Bram@vim.org>
parents: 22794
diff changeset
2590 if (l1 == NULL)
3e0f909ca1f2 patch 8.2.1949: Vim9: using extend() on null dict is silently ignored
Bram Moolenaar <Bram@vim.org>
parents: 22794
diff changeset
2591 {
3e0f909ca1f2 patch 8.2.1949: Vim9: using extend() on null dict is silently ignored
Bram Moolenaar <Bram@vim.org>
parents: 22794
diff changeset
2592 emsg(_(e_cannot_extend_null_list));
23800
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2593 goto theend;
22802
3e0f909ca1f2 patch 8.2.1949: Vim9: using extend() on null dict is silently ignored
Bram Moolenaar <Bram@vim.org>
parents: 22794
diff changeset
2594 }
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2595 l2 = argvars[1].vval.v_list;
23588
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2596 if ((is_new || !value_check_lock(l1->lv_lock, arg_errmsg, TRUE))
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2597 && l2 != NULL)
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2598 {
23588
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2599 if (is_new)
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2600 {
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2601 l1 = list_copy(l1, FALSE, get_copyID());
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2602 if (l1 == NULL)
23800
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2603 goto theend;
23588
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2604 }
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2605
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2606 if (argvars[2].v_type != VAR_UNKNOWN)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2607 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2608 before = (long)tv_get_number_chk(&argvars[2], &error);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2609 if (error)
23800
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2610 goto theend; // type error; errmsg already given
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2611
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2612 if (before == l1->lv_len)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2613 item = NULL;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2614 else
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2615 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2616 item = list_find(l1, before);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2617 if (item == NULL)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2618 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2619 semsg(_(e_listidx), before);
23800
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2620 goto theend;
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2621 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2622 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2623 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2624 else
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2625 item = NULL;
23917
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23888
diff changeset
2626 if (type != NULL && check_typval_arg_type(
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23888
diff changeset
2627 type, &argvars[1], 2) == FAIL)
23800
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2628 goto theend;
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2629 list_extend(l1, l2, item);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2630
23588
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2631 if (is_new)
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2632 {
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2633 rettv->v_type = VAR_LIST;
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2634 rettv->vval.v_list = l1;
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2635 rettv->v_lock = FALSE;
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2636 }
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2637 else
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2638 copy_tv(&argvars[0], rettv);
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2639 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2640 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2641 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2642 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2643 dict_T *d1, *d2;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2644 char_u *action;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2645 int i;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2646
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2647 d1 = argvars[0].vval.v_dict;
22802
3e0f909ca1f2 patch 8.2.1949: Vim9: using extend() on null dict is silently ignored
Bram Moolenaar <Bram@vim.org>
parents: 22794
diff changeset
2648 if (d1 == NULL)
3e0f909ca1f2 patch 8.2.1949: Vim9: using extend() on null dict is silently ignored
Bram Moolenaar <Bram@vim.org>
parents: 22794
diff changeset
2649 {
3e0f909ca1f2 patch 8.2.1949: Vim9: using extend() on null dict is silently ignored
Bram Moolenaar <Bram@vim.org>
parents: 22794
diff changeset
2650 emsg(_(e_cannot_extend_null_dict));
23800
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2651 goto theend;
22802
3e0f909ca1f2 patch 8.2.1949: Vim9: using extend() on null dict is silently ignored
Bram Moolenaar <Bram@vim.org>
parents: 22794
diff changeset
2652 }
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2653 d2 = argvars[1].vval.v_dict;
23588
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2654 if ((is_new || !value_check_lock(d1->dv_lock, arg_errmsg, TRUE))
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2655 && d2 != NULL)
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2656 {
23588
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2657 if (is_new)
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2658 {
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2659 d1 = dict_copy(d1, FALSE, get_copyID());
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2660 if (d1 == NULL)
23800
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2661 goto theend;
23588
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2662 }
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2663
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17970
diff changeset
2664 // Check the third argument.
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2665 if (argvars[2].v_type != VAR_UNKNOWN)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2666 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2667 static char *(av[]) = {"keep", "force", "error"};
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2668
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2669 action = tv_get_string_chk(&argvars[2]);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2670 if (action == NULL)
23800
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2671 goto theend; // type error; errmsg already given
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2672 for (i = 0; i < 3; ++i)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2673 if (STRCMP(action, av[i]) == 0)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2674 break;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2675 if (i == 3)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2676 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2677 semsg(_(e_invarg2), action);
23800
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2678 goto theend;
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2679 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2680 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2681 else
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2682 action = (char_u *)"force";
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2683
23917
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23888
diff changeset
2684 if (type != NULL && check_typval_arg_type(
4b417b776b95 patch 8.2.2501: not always clear where an error is reported
Bram Moolenaar <Bram@vim.org>
parents: 23888
diff changeset
2685 type, &argvars[1], 2) == FAIL)
23800
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2686 goto theend;
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2687 dict_extend(d1, d2, action);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2688
23588
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2689 if (is_new)
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2690 {
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2691 rettv->v_type = VAR_DICT;
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2692 rettv->vval.v_dict = d1;
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2693 rettv->v_lock = FALSE;
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2694 }
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2695 else
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2696 copy_tv(&argvars[0], rettv);
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2697 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2698 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2699 else
23588
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2700 semsg(_(e_listdictarg), is_new ? "extendnew()" : "extend()");
23800
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2701
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2702 theend:
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2703 if (type != NULL)
57f0e3fd7c05 patch 8.2.2441: Vim9: extend() does not give an error for a type mismatch
Bram Moolenaar <Bram@vim.org>
parents: 23650
diff changeset
2704 clear_type_list(&type_list);
23588
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2705 }
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2706
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2707 /*
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2708 * "extend(list, list [, idx])" function
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2709 * "extend(dict, dict [, action])" function
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2710 */
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2711 void
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2712 f_extend(typval_T *argvars, typval_T *rettv)
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2713 {
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2714 char_u *errmsg = (char_u *)N_("extend() argument");
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2715
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2716 extend(argvars, rettv, errmsg, FALSE);
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2717 }
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2718
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2719 /*
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2720 * "extendnew(list, list [, idx])" function
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2721 * "extendnew(dict, dict [, action])" function
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2722 */
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2723 void
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2724 f_extendnew(typval_T *argvars, typval_T *rettv)
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2725 {
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2726 char_u *errmsg = (char_u *)N_("extendnew() argument");
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2727
510088f8c66f patch 8.2.2336: Vim9: not possible to extend dictionary with different type
Bram Moolenaar <Bram@vim.org>
parents: 23565
diff changeset
2728 extend(argvars, rettv, errmsg, TRUE);
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2729 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2730
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2731 /*
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2732 * "insert()" function
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2733 */
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2734 void
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2735 f_insert(typval_T *argvars, typval_T *rettv)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2736 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2737 long before = 0;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2738 listitem_T *item;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2739 int error = FALSE;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2740
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2741 if (argvars[0].v_type == VAR_BLOB)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2742 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2743 int val, len;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2744 char_u *p;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2745
20158
94f05de75e9f patch 8.2.0634: crash with null partial and blob
Bram Moolenaar <Bram@vim.org>
parents: 20007
diff changeset
2746 if (argvars[0].vval.v_blob == NULL)
24484
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2747 {
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2748 if (in_vim9script())
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2749 emsg(_(e_cannot_add_to_null_blob));
20158
94f05de75e9f patch 8.2.0634: crash with null partial and blob
Bram Moolenaar <Bram@vim.org>
parents: 20007
diff changeset
2750 return;
24484
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2751 }
20158
94f05de75e9f patch 8.2.0634: crash with null partial and blob
Bram Moolenaar <Bram@vim.org>
parents: 20007
diff changeset
2752
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2753 len = blob_len(argvars[0].vval.v_blob);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2754 if (argvars[2].v_type != VAR_UNKNOWN)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2755 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2756 before = (long)tv_get_number_chk(&argvars[2], &error);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2757 if (error)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2758 return; // type error; errmsg already given
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2759 if (before < 0 || before > len)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2760 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2761 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2762 return;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2763 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2764 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2765 val = tv_get_number_chk(&argvars[1], &error);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2766 if (error)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2767 return;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2768 if (val < 0 || val > 255)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2769 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2770 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2771 return;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2772 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2773
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2774 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2775 return;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2776 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2777 mch_memmove(p + before + 1, p + before, (size_t)len - before);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2778 *(p + before) = val;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2779 ++argvars[0].vval.v_blob->bv_ga.ga_len;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2780
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2781 copy_tv(&argvars[0], rettv);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2782 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2783 else if (argvars[0].v_type != VAR_LIST)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2784 semsg(_(e_listblobarg), "insert()");
24484
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2785 else
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2786 {
24484
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2787 list_T *l = argvars[0].vval.v_list;
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2788
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2789 if (l == NULL)
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2790 {
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2791 if (in_vim9script())
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2792 emsg(_(e_cannot_add_to_null_list));
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2793 }
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2794 else if (!value_check_lock(l->lv_lock,
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2795 (char_u *)N_("insert() argument"), TRUE))
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2796 {
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2797 if (argvars[2].v_type != VAR_UNKNOWN)
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2798 before = (long)tv_get_number_chk(&argvars[2], &error);
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2799 if (error)
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2800 return; // type error; errmsg already given
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2801
24484
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2802 if (before == l->lv_len)
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2803 item = NULL;
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2804 else
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2805 {
24484
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2806 item = list_find(l, before);
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2807 if (item == NULL)
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2808 {
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2809 semsg(_(e_listidx), before);
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2810 l = NULL;
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2811 }
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2812 }
24484
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2813 if (l != NULL)
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2814 {
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2815 (void)list_insert_tv(l, &argvars[1], item);
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2816 copy_tv(&argvars[0], rettv);
bc1a533148d7 patch 8.2.2782: Vim9: blob operations not fully tested
Bram Moolenaar <Bram@vim.org>
parents: 24482
diff changeset
2817 }
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2818 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2819 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2820 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2821
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2822 /*
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2823 * "remove()" function
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2824 */
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2825 void
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2826 f_remove(typval_T *argvars, typval_T *rettv)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2827 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2828 char_u *arg_errmsg = (char_u *)N_("remove() argument");
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2829
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2830 if (argvars[0].v_type == VAR_DICT)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2831 dict_remove(argvars, rettv, arg_errmsg);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2832 else if (argvars[0].v_type == VAR_BLOB)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2833 blob_remove(argvars, rettv);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2834 else if (argvars[0].v_type == VAR_LIST)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2835 list_remove(argvars, rettv, arg_errmsg);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2836 else
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2837 semsg(_(e_listdictblobarg), "remove()");
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2838 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2839
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2840 /*
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2841 * "reverse({list})" function
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2842 */
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2843 void
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2844 f_reverse(typval_T *argvars, typval_T *rettv)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2845 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2846 list_T *l;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2847 listitem_T *li, *ni;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2848
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2849 if (argvars[0].v_type == VAR_BLOB)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2850 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2851 blob_T *b = argvars[0].vval.v_blob;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2852 int i, len = blob_len(b);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2853
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2854 for (i = 0; i < len / 2; i++)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2855 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2856 int tmp = blob_get(b, i);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2857
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2858 blob_set(b, i, blob_get(b, len - i - 1));
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2859 blob_set(b, len - i - 1, tmp);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2860 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2861 rettv_blob_set(rettv, b);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2862 return;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2863 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2864
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2865 if (argvars[0].v_type != VAR_LIST)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2866 semsg(_(e_listblobarg), "reverse()");
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2867 else if ((l = argvars[0].vval.v_list) != NULL
22298
07e48ee8c3bb patch 8.2.1698: cannot lock a variable in legacy Vim script like in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 22121
diff changeset
2868 && !value_check_lock(l->lv_lock,
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2869 (char_u *)N_("reverse() argument"), TRUE))
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2870 {
19203
09f01421a356 patch 8.2.0160: range test fails
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
2871 if (l->lv_first == &range_list_item)
09f01421a356 patch 8.2.0160: range test fails
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
2872 {
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
2873 varnumber_T new_start = l->lv_u.nonmat.lv_start
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
2874 + (l->lv_len - 1) * l->lv_u.nonmat.lv_stride;
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
2875 l->lv_u.nonmat.lv_end = new_start
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
2876 - (l->lv_u.nonmat.lv_end - l->lv_u.nonmat.lv_start);
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
2877 l->lv_u.nonmat.lv_start = new_start;
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
2878 l->lv_u.nonmat.lv_stride = -l->lv_u.nonmat.lv_stride;
19203
09f01421a356 patch 8.2.0160: range test fails
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
2879 rettv_list_set(rettv, l);
09f01421a356 patch 8.2.0160: range test fails
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
2880 return;
09f01421a356 patch 8.2.0160: range test fails
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
2881 }
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
2882 li = l->lv_u.mat.lv_last;
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
2883 l->lv_first = l->lv_u.mat.lv_last = NULL;
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2884 l->lv_len = 0;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2885 while (li != NULL)
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2886 {
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2887 ni = li->li_prev;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2888 list_append(l, li);
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2889 li = ni;
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2890 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2891 rettv_list_set(rettv, l);
19229
d776967d0f0d patch 8.2.0173: build fails with old compiler
Bram Moolenaar <Bram@vim.org>
parents: 19203
diff changeset
2892 l->lv_u.mat.lv_idx = l->lv_len - l->lv_u.mat.lv_idx - 1;
17970
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2893 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2894 }
684a15da9929 patch 8.1.1981: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17964
diff changeset
2895
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2896 /*
23229
b545334ae654 patch 8.2.2160: various typos
Bram Moolenaar <Bram@vim.org>
parents: 23058
diff changeset
2897 * "reduce(list, { accumulator, element -> value } [, initial])" function
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2898 */
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2899 void
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2900 f_reduce(typval_T *argvars, typval_T *rettv)
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2901 {
20657
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
2902 typval_T initial;
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2903 char_u *func_name;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2904 partial_T *partial = NULL;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2905 funcexe_T funcexe;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2906 typval_T argv[3];
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2907
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2908 if (argvars[0].v_type != VAR_LIST && argvars[0].v_type != VAR_BLOB)
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2909 {
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2910 emsg(_(e_listblobreq));
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2911 return;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2912 }
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2913
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2914 if (argvars[1].v_type == VAR_FUNC)
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2915 func_name = argvars[1].vval.v_string;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2916 else if (argvars[1].v_type == VAR_PARTIAL)
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2917 {
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2918 partial = argvars[1].vval.v_partial;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2919 func_name = partial_name(partial);
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2920 }
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2921 else
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2922 func_name = tv_get_string(&argvars[1]);
22794
42bb78d46354 patch 8.2.1945: crash when passing NULL function to reduce()
Bram Moolenaar <Bram@vim.org>
parents: 22770
diff changeset
2923 if (func_name == NULL || *func_name == NUL)
42bb78d46354 patch 8.2.1945: crash when passing NULL function to reduce()
Bram Moolenaar <Bram@vim.org>
parents: 22770
diff changeset
2924 {
42bb78d46354 patch 8.2.1945: crash when passing NULL function to reduce()
Bram Moolenaar <Bram@vim.org>
parents: 22770
diff changeset
2925 emsg(_(e_missing_function_argument));
42bb78d46354 patch 8.2.1945: crash when passing NULL function to reduce()
Bram Moolenaar <Bram@vim.org>
parents: 22770
diff changeset
2926 return;
42bb78d46354 patch 8.2.1945: crash when passing NULL function to reduce()
Bram Moolenaar <Bram@vim.org>
parents: 22770
diff changeset
2927 }
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2928
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2929 vim_memset(&funcexe, 0, sizeof(funcexe));
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2930 funcexe.evaluate = TRUE;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2931 funcexe.partial = partial;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2932
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2933 if (argvars[0].v_type == VAR_LIST)
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2934 {
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2935 list_T *l = argvars[0].vval.v_list;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2936 listitem_T *li = NULL;
20657
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
2937 int r;
21000
3b29ac3394dc patch 8.2.1051: crash when changing a list while using reduce() on it
Bram Moolenaar <Bram@vim.org>
parents: 20992
diff changeset
2938 int called_emsg_start = called_emsg;
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2939
21064
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2940 if (l != NULL)
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2941 CHECK_LIST_MATERIALIZE(l);
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2942 if (argvars[2].v_type == VAR_UNKNOWN)
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2943 {
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2944 if (l == NULL || l->lv_first == NULL)
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2945 {
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2946 semsg(_(e_reduceempty), "List");
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2947 return;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2948 }
20657
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
2949 initial = l->lv_first->li_tv;
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2950 li = l->lv_first->li_next;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2951 }
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2952 else
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2953 {
20657
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
2954 initial = argvars[2];
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2955 if (l != NULL)
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2956 li = l->lv_first;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2957 }
21064
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2958 copy_tv(&initial, rettv);
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2959
21064
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2960 if (l != NULL)
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2961 {
21064
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2962 int prev_locked = l->lv_lock;
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2963
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2964 l->lv_lock = VAR_FIXED; // disallow the list changing here
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2965 for ( ; li != NULL; li = li->li_next)
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2966 {
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2967 argv[0] = *rettv;
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2968 argv[1] = li->li_tv;
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2969 rettv->v_type = VAR_UNKNOWN;
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2970 r = call_func(func_name, -1, rettv, 2, argv, &funcexe);
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2971 clear_tv(&argv[0]);
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2972 if (r == FAIL || called_emsg != called_emsg_start)
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2973 break;
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2974 }
6dc8625889fe patch 8.2.1083: crash when using reduce() on a NULL list
Bram Moolenaar <Bram@vim.org>
parents: 21046
diff changeset
2975 l->lv_lock = prev_locked;
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2976 }
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2977 }
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2978 else
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2979 {
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2980 blob_T *b = argvars[0].vval.v_blob;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2981 int i;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2982
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2983 if (argvars[2].v_type == VAR_UNKNOWN)
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2984 {
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2985 if (b == NULL || b->bv_ga.ga_len == 0)
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2986 {
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2987 semsg(_(e_reduceempty), "Blob");
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2988 return;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2989 }
20657
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
2990 initial.v_type = VAR_NUMBER;
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
2991 initial.vval.v_number = blob_get(b, 0);
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2992 i = 1;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2993 }
20657
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
2994 else if (argvars[2].v_type != VAR_NUMBER)
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
2995 {
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
2996 emsg(_(e_number_exp));
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
2997 return;
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
2998 }
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
2999 else
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3000 {
20657
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
3001 initial = argvars[2];
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3002 i = 0;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3003 }
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3004
20657
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
3005 copy_tv(&initial, rettv);
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3006 if (b != NULL)
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3007 {
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3008 for ( ; i < b->bv_ga.ga_len; i++)
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3009 {
20657
a1e6d9353736 patch 8.2.0882: leaking memory when using reduce()
Bram Moolenaar <Bram@vim.org>
parents: 20649
diff changeset
3010 argv[0] = *rettv;
20649
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3011 argv[1].v_type = VAR_NUMBER;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3012 argv[1].vval.v_number = blob_get(b, i);
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3013 if (call_func(func_name, -1, rettv, 2, argv, &funcexe) == FAIL)
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3014 return;
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3015 }
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3016 }
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3017 }
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3018 }
1fa0ace0ba65 patch 8.2.0878: no reduce() function
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
3019
17964
6d4d3bce365d patch 8.1.1978: the eval.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
3020 #endif // defined(FEAT_EVAL)