annotate src/dict.c @ 15211:de63593896b3 v8.1.0615

patch 8.1.0615: get_tv function names are not consistent commit https://github.com/vim/vim/commit/d155d7a8519987361169459b8d464ae1caef5e9c Author: Bram Moolenaar <Bram@vim.org> Date: Fri Dec 21 16:04:21 2018 +0100 patch 8.1.0615: get_tv function names are not consistent Problem: Get_tv function names are not consistent. Solution: Rename to tv_get.
author Bram Moolenaar <Bram@vim.org>
date Fri, 21 Dec 2018 16:15:06 +0100
parents 7903dce131d4
children 762fccd84b7c
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: 9858
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
2 *
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
4 *
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
8 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
10 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11 * dict.c: Dictionary support
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
12 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
13
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
14 #include "vim.h"
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
15
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16 #if defined(FEAT_EVAL) || defined(PROTO)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
17
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
18 /* List head for garbage collection. Although there can be a reference loop
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
19 * from partial to dict to partial, we don't need to keep track of the partial,
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
20 * since it will get freed when the dict is unused and gets freed. */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
21 static dict_T *first_dict = NULL; /* list of all dicts */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
22
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
23 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
24 * Allocate an empty header for a dictionary.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
25 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
26 dict_T *
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
27 dict_alloc(void)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
28 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
29 dict_T *d;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
30
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
31 d = (dict_T *)alloc(sizeof(dict_T));
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
32 if (d != NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
33 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
34 /* Add the dict to the list of dicts for garbage collection. */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
35 if (first_dict != NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
36 first_dict->dv_used_prev = d;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
37 d->dv_used_next = first_dict;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
38 d->dv_used_prev = NULL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
39 first_dict = d;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
40
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
41 hash_init(&d->dv_hashtab);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
42 d->dv_lock = 0;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
43 d->dv_scope = 0;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
44 d->dv_refcount = 0;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
45 d->dv_copyID = 0;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
46 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
47 return d;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
48 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
49
15016
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14303
diff changeset
50 /*
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14303
diff changeset
51 * dict_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: 14303
diff changeset
52 */
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14303
diff changeset
53 dict_T *
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14303
diff changeset
54 dict_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: 14303
diff changeset
55 {
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14303
diff changeset
56 #ifdef FEAT_EVAL
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14303
diff changeset
57 if (alloc_fail_id == id && alloc_does_fail((long_u)sizeof(list_T)))
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14303
diff changeset
58 return NULL;
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14303
diff changeset
59 #endif
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14303
diff changeset
60 return (dict_alloc());
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14303
diff changeset
61 }
c338c91086b9 patch 8.1.0519: cannot save and restore the tag stack
Bram Moolenaar <Bram@vim.org>
parents: 14303
diff changeset
62
13037
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
63 dict_T *
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
64 dict_alloc_lock(int lock)
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
65 {
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
66 dict_T *d = dict_alloc();
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
67
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
68 if (d != NULL)
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
69 d->dv_lock = lock;
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
70 return d;
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
71 }
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
72
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
73 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
74 * Allocate an empty dict for a return value.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
75 * Returns OK or FAIL.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
76 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
77 int
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
78 rettv_dict_alloc(typval_T *rettv)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
79 {
13037
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
80 dict_T *d = dict_alloc_lock(0);
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
81
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
82 if (d == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
83 return FAIL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
84
11418
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 11142
diff changeset
85 rettv_dict_set(rettv, d);
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
86 return OK;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
87 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
88
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
89 /*
11418
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 11142
diff changeset
90 * Set a dictionary as the return value
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 11142
diff changeset
91 */
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 11142
diff changeset
92 void
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 11142
diff changeset
93 rettv_dict_set(typval_T *rettv, dict_T *d)
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 11142
diff changeset
94 {
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 11142
diff changeset
95 rettv->v_type = VAR_DICT;
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 11142
diff changeset
96 rettv->vval.v_dict = d;
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 11142
diff changeset
97 if (d != NULL)
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 11142
diff changeset
98 ++d->dv_refcount;
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 11142
diff changeset
99 }
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 11142
diff changeset
100
162bcd0debd7 patch 8.0.0593: duplication of code for adding a list or dict return value
Christian Brabandt <cb@256bit.org>
parents: 11142
diff changeset
101 /*
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
102 * Free a Dictionary, including all non-container items it contains.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
103 * Ignores the reference count.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
104 */
13037
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
105 void
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
106 dict_free_contents(dict_T *d)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
107 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
108 int todo;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
109 hashitem_T *hi;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
110 dictitem_T *di;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
111
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
112 /* Lock the hashtab, we don't want it to resize while freeing items. */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
113 hash_lock(&d->dv_hashtab);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
114 todo = (int)d->dv_hashtab.ht_used;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
115 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
116 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
117 if (!HASHITEM_EMPTY(hi))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
118 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
119 /* Remove the item before deleting it, just in case there is
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
120 * something recursive causing trouble. */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
121 di = HI2DI(hi);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
122 hash_remove(&d->dv_hashtab, hi);
10952
835604f3c37a patch 8.0.0365: might free a dict item that wasn't allocated
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
123 dictitem_free(di);
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
124 --todo;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
125 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
126 }
13037
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
127
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
128 /* The hashtab is still locked, it has to be re-initialized anyway */
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
129 hash_clear(&d->dv_hashtab);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
130 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
131
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
132 static void
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
133 dict_free_dict(dict_T *d)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
134 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
135 /* Remove the dict from the list of dicts for garbage collection. */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
136 if (d->dv_used_prev == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
137 first_dict = d->dv_used_next;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
138 else
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
139 d->dv_used_prev->dv_used_next = d->dv_used_next;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
140 if (d->dv_used_next != NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
141 d->dv_used_next->dv_used_prev = d->dv_used_prev;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
142 vim_free(d);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
143 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
144
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
145 static void
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
146 dict_free(dict_T *d)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
147 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
148 if (!in_free_unref_items)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
149 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
150 dict_free_contents(d);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
151 dict_free_dict(d);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
152 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
153 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
154
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
155 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
156 * Unreference a Dictionary: decrement the reference count and free it when it
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
157 * becomes zero.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
158 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
159 void
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
160 dict_unref(dict_T *d)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
161 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
162 if (d != NULL && --d->dv_refcount <= 0)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
163 dict_free(d);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
164 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
165
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
166 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
167 * Go through the list of dicts and free items without the copyID.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
168 * Returns TRUE if something was freed.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
169 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
170 int
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
171 dict_free_nonref(int copyID)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
172 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
173 dict_T *dd;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
174 int did_free = FALSE;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
175
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
176 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
177 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
178 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
179 /* Free the Dictionary and ordinary items it contains, but don't
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
180 * recurse into Lists and Dictionaries, they will be in the list
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
181 * of dicts or list of lists. */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
182 dict_free_contents(dd);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
183 did_free = TRUE;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
184 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
185 return did_free;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
186 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
187
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
188 void
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
189 dict_free_items(int copyID)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
190 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
191 dict_T *dd, *dd_next;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
192
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
193 for (dd = first_dict; dd != NULL; dd = dd_next)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
194 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
195 dd_next = dd->dv_used_next;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
196 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
197 dict_free_dict(dd);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
198 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
199 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
200
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
201 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
202 * Allocate a Dictionary item.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
203 * The "key" is copied to the new item.
14303
f761a55a8aed patch 8.1.0167: lock flag in new dictitem is reset in many places
Christian Brabandt <cb@256bit.org>
parents: 14301
diff changeset
204 * Note that the type and value of the item "di_tv" still needs to be
f761a55a8aed patch 8.1.0167: lock flag in new dictitem is reset in many places
Christian Brabandt <cb@256bit.org>
parents: 14301
diff changeset
205 * initialized!
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
206 * Returns NULL when out of memory.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
207 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
208 dictitem_T *
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
209 dictitem_alloc(char_u *key)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
210 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
211 dictitem_T *di;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
212
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
213 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
214 if (di != NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
215 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
216 STRCPY(di->di_key, key);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
217 di->di_flags = DI_FLAGS_ALLOC;
14303
f761a55a8aed patch 8.1.0167: lock flag in new dictitem is reset in many places
Christian Brabandt <cb@256bit.org>
parents: 14301
diff changeset
218 di->di_tv.v_lock = 0;
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
219 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
220 return di;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
221 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
222
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
223 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
224 * Make a copy of a Dictionary item.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
225 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
226 static dictitem_T *
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
227 dictitem_copy(dictitem_T *org)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
228 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
229 dictitem_T *di;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
230
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
231 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
232 + STRLEN(org->di_key)));
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
233 if (di != NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
234 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
235 STRCPY(di->di_key, org->di_key);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
236 di->di_flags = DI_FLAGS_ALLOC;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
237 copy_tv(&org->di_tv, &di->di_tv);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
238 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
239 return di;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
240 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
241
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
242 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
243 * Remove item "item" from Dictionary "dict" and free it.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
244 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
245 void
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
246 dictitem_remove(dict_T *dict, dictitem_T *item)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
247 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
248 hashitem_T *hi;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
249
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
250 hi = hash_find(&dict->dv_hashtab, item->di_key);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
251 if (HASHITEM_EMPTY(hi))
10359
66f1b5bf3fa6 commit https://github.com/vim/vim/commit/95f096030ed1a8afea028f2ea295d6f6a70f466f
Christian Brabandt <cb@256bit.org>
parents: 10042
diff changeset
252 internal_error("dictitem_remove()");
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
253 else
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
254 hash_remove(&dict->dv_hashtab, hi);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
255 dictitem_free(item);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
256 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
257
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
258 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
259 * Free a dict item. Also clears the value.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
260 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
261 void
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
262 dictitem_free(dictitem_T *item)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
263 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
264 clear_tv(&item->di_tv);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
265 if (item->di_flags & DI_FLAGS_ALLOC)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
266 vim_free(item);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
267 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
268
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
269 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
270 * Make a copy of dict "d". Shallow if "deep" is FALSE.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
271 * The refcount of the new dict is set to 1.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
272 * See item_copy() for "copyID".
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
273 * Returns NULL when out of memory.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
274 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
275 dict_T *
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
276 dict_copy(dict_T *orig, int deep, int copyID)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
277 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
278 dict_T *copy;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
279 dictitem_T *di;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
280 int todo;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
281 hashitem_T *hi;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
282
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
283 if (orig == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
284 return NULL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
285
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
286 copy = dict_alloc();
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
287 if (copy != NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
288 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
289 if (copyID != 0)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
290 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
291 orig->dv_copyID = copyID;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
292 orig->dv_copydict = copy;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
293 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
294 todo = (int)orig->dv_hashtab.ht_used;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
295 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
296 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
297 if (!HASHITEM_EMPTY(hi))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
298 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
299 --todo;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
300
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
301 di = dictitem_alloc(hi->hi_key);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
302 if (di == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
303 break;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
304 if (deep)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
305 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
306 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
307 copyID) == FAIL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
308 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
309 vim_free(di);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
310 break;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
311 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
312 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
313 else
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
314 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
315 if (dict_add(copy, di) == FAIL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
316 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
317 dictitem_free(di);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
318 break;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
319 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
320 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
321 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
322
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
323 ++copy->dv_refcount;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
324 if (todo > 0)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
325 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
326 dict_unref(copy);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
327 copy = NULL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
328 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
329 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
330
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
331 return copy;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
332 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
333
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
334 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
335 * Add item "item" to Dictionary "d".
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
336 * Returns FAIL when out of memory and when key already exists.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
337 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
338 int
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
339 dict_add(dict_T *d, dictitem_T *item)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
340 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
341 return hash_add(&d->dv_hashtab, item->di_key);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
342 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
343
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
344 /*
14301
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
345 * Add a number entry to dictionary "d".
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
346 * Returns FAIL when out of memory and when key already exists.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
347 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
348 int
14301
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
349 dict_add_number(dict_T *d, char *key, varnumber_T nr)
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
350 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
351 dictitem_T *item;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
352
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
353 item = dictitem_alloc((char_u *)key);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
354 if (item == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
355 return FAIL;
14301
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
356 item->di_tv.v_type = VAR_NUMBER;
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
357 item->di_tv.vval.v_number = nr;
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
358 if (dict_add(d, item) == FAIL)
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
359 {
14301
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
360 dictitem_free(item);
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
361 return FAIL;
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
362 }
14301
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
363 return OK;
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
364 }
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
365
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
366 /*
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
367 * Add a string entry to dictionary "d".
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
368 * Returns FAIL when out of memory and when key already exists.
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
369 */
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
370 int
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
371 dict_add_string(dict_T *d, char *key, char_u *str)
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
372 {
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
373 dictitem_T *item;
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
374
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
375 item = dictitem_alloc((char_u *)key);
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
376 if (item == NULL)
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
377 return FAIL;
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
378 item->di_tv.v_type = VAR_STRING;
3c80092eb211 patch 8.1.0166: using dict_add_nr_str() is clumsy
Christian Brabandt <cb@256bit.org>
parents: 13037
diff changeset
379 item->di_tv.vval.v_string = str != NULL ? vim_strsave(str) : NULL;
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
380 if (dict_add(d, item) == FAIL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
381 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
382 dictitem_free(item);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
383 return FAIL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
384 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
385 return OK;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
386 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
387
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
388 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
389 * Add a list entry to dictionary "d".
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
390 * Returns FAIL when out of memory and when key already exists.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
391 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
392 int
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
393 dict_add_list(dict_T *d, char *key, list_T *list)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
394 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
395 dictitem_T *item;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
396
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
397 item = dictitem_alloc((char_u *)key);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
398 if (item == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
399 return FAIL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
400 item->di_tv.v_type = VAR_LIST;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
401 item->di_tv.vval.v_list = list;
11142
a54985a73ab2 patch 8.0.0458: potential crash if adding list or dict to dict fails
Christian Brabandt <cb@256bit.org>
parents: 10952
diff changeset
402 ++list->lv_refcount;
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
403 if (dict_add(d, item) == FAIL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
404 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
405 dictitem_free(item);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
406 return FAIL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
407 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
408 return OK;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
409 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
410
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
411 /*
9858
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
412 * Add a dict entry to dictionary "d".
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
413 * Returns FAIL when out of memory and when key already exists.
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
414 */
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
415 int
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
416 dict_add_dict(dict_T *d, char *key, dict_T *dict)
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
417 {
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
418 dictitem_T *item;
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
419
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
420 item = dictitem_alloc((char_u *)key);
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
421 if (item == NULL)
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
422 return FAIL;
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
423 item->di_tv.v_type = VAR_DICT;
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
424 item->di_tv.vval.v_dict = dict;
11142
a54985a73ab2 patch 8.0.0458: potential crash if adding list or dict to dict fails
Christian Brabandt <cb@256bit.org>
parents: 10952
diff changeset
425 ++dict->dv_refcount;
9858
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
426 if (dict_add(d, item) == FAIL)
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
427 {
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
428 dictitem_free(item);
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
429 return FAIL;
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
430 }
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
431 return OK;
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
432 }
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
433
3e96d9ed2ca1 commit https://github.com/vim/vim/commit/b5ae48e9ffd3b8eb6ca4057de11f1bddcde8ce6f
Christian Brabandt <cb@256bit.org>
parents: 9770
diff changeset
434 /*
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
435 * Get the number of items in a Dictionary.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
436 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
437 long
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
438 dict_len(dict_T *d)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
439 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
440 if (d == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
441 return 0L;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
442 return (long)d->dv_hashtab.ht_used;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
443 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
444
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
445 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
446 * Find item "key[len]" in Dictionary "d".
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
447 * If "len" is negative use strlen(key).
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
448 * Returns NULL when not found.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
449 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
450 dictitem_T *
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
451 dict_find(dict_T *d, char_u *key, int len)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
452 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
453 #define AKEYLEN 200
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
454 char_u buf[AKEYLEN];
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
455 char_u *akey;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
456 char_u *tofree = NULL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
457 hashitem_T *hi;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
458
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
459 if (d == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
460 return NULL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
461 if (len < 0)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
462 akey = key;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
463 else if (len >= AKEYLEN)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
464 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
465 tofree = akey = vim_strnsave(key, len);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
466 if (akey == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
467 return NULL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
468 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
469 else
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
470 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
471 /* Avoid a malloc/free by using buf[]. */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
472 vim_strncpy(buf, key, len);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
473 akey = buf;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
474 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
475
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
476 hi = hash_find(&d->dv_hashtab, akey);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
477 vim_free(tofree);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
478 if (HASHITEM_EMPTY(hi))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
479 return NULL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
480 return HI2DI(hi);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
481 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
482
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
483 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
484 * Get a string item from a dictionary.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
485 * When "save" is TRUE allocate memory for it.
9770
ba98d7ce0d74 commit https://github.com/vim/vim/commit/7dc5e2e486fe0287601968e535902a41a39f65bb
Christian Brabandt <cb@256bit.org>
parents: 9560
diff changeset
486 * When FALSE a shared buffer is used, can only be used once!
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
487 * Returns NULL if the entry doesn't exist or out of memory.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
488 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
489 char_u *
15146
7903dce131d4 patch 8.1.0583: using illogical name for get_dict_number()/get_dict_string()
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
490 dict_get_string(dict_T *d, char_u *key, int save)
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
491 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
492 dictitem_T *di;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
493 char_u *s;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
494
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
495 di = dict_find(d, key, -1);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
496 if (di == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
497 return NULL;
15211
de63593896b3 patch 8.1.0615: get_tv function names are not consistent
Bram Moolenaar <Bram@vim.org>
parents: 15146
diff changeset
498 s = tv_get_string(&di->di_tv);
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
499 if (save && s != NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
500 s = vim_strsave(s);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
501 return s;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
502 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
503
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
504 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
505 * Get a number item from a dictionary.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
506 * Returns 0 if the entry doesn't exist.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
507 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
508 varnumber_T
15146
7903dce131d4 patch 8.1.0583: using illogical name for get_dict_number()/get_dict_string()
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
509 dict_get_number(dict_T *d, char_u *key)
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
510 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
511 dictitem_T *di;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
512
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
513 di = dict_find(d, key, -1);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
514 if (di == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
515 return 0;
15211
de63593896b3 patch 8.1.0615: get_tv function names are not consistent
Bram Moolenaar <Bram@vim.org>
parents: 15146
diff changeset
516 return tv_get_number(&di->di_tv);
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
517 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
518
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
519 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
520 * Return an allocated string with the string representation of a Dictionary.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
521 * May return NULL.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
522 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
523 char_u *
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
524 dict2string(typval_T *tv, int copyID, int restore_copyID)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
525 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
526 garray_T ga;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
527 int first = TRUE;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
528 char_u *tofree;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
529 char_u numbuf[NUMBUFLEN];
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
530 hashitem_T *hi;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
531 char_u *s;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
532 dict_T *d;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
533 int todo;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
534
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
535 if ((d = tv->vval.v_dict) == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
536 return NULL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
537 ga_init2(&ga, (int)sizeof(char), 80);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
538 ga_append(&ga, '{');
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
539
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
540 todo = (int)d->dv_hashtab.ht_used;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
541 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
542 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
543 if (!HASHITEM_EMPTY(hi))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
544 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
545 --todo;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
546
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
547 if (first)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
548 first = FALSE;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
549 else
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
550 ga_concat(&ga, (char_u *)", ");
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
551
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
552 tofree = string_quote(hi->hi_key, FALSE);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
553 if (tofree != NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
554 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
555 ga_concat(&ga, tofree);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
556 vim_free(tofree);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
557 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
558 ga_concat(&ga, (char_u *)": ");
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
559 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
560 FALSE, restore_copyID, TRUE);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
561 if (s != NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
562 ga_concat(&ga, s);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
563 vim_free(tofree);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
564 if (s == NULL || did_echo_string_emsg)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
565 break;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
566 line_breakcheck();
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
567
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
568 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
569 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
570 if (todo > 0)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
571 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
572 vim_free(ga.ga_data);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
573 return NULL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
574 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
575
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
576 ga_append(&ga, '}');
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
577 ga_append(&ga, NUL);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
578 return (char_u *)ga.ga_data;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
579 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
580
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
581 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
582 * Allocate a variable for a Dictionary and fill it from "*arg".
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
583 * Return OK or FAIL. Returns NOTDONE for {expr}.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
584 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
585 int
15146
7903dce131d4 patch 8.1.0583: using illogical name for get_dict_number()/get_dict_string()
Bram Moolenaar <Bram@vim.org>
parents: 15016
diff changeset
586 dict_get_tv(char_u **arg, typval_T *rettv, int evaluate)
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
587 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
588 dict_T *d = NULL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
589 typval_T tvkey;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
590 typval_T tv;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
591 char_u *key = NULL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
592 dictitem_T *item;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
593 char_u *start = skipwhite(*arg + 1);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
594 char_u buf[NUMBUFLEN];
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
595
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
596 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
597 * First check if it's not a curly-braces thing: {expr}.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
598 * Must do this without evaluating, otherwise a function may be called
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
599 * twice. Unfortunately this means we need to call eval1() twice for the
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
600 * first item.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
601 * But {} is an empty Dictionary.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
602 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
603 if (*start != '}')
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
604 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
605 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
606 return FAIL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
607 if (*start == '}')
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
608 return NOTDONE;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
609 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
610
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
611 if (evaluate)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
612 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
613 d = dict_alloc();
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
614 if (d == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
615 return FAIL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
616 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
617 tvkey.v_type = VAR_UNKNOWN;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
618 tv.v_type = VAR_UNKNOWN;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
619
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
620 *arg = skipwhite(*arg + 1);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
621 while (**arg != '}' && **arg != NUL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
622 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
623 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
624 goto failret;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
625 if (**arg != ':')
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
626 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
627 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
628 clear_tv(&tvkey);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
629 goto failret;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
630 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
631 if (evaluate)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
632 {
15211
de63593896b3 patch 8.1.0615: get_tv function names are not consistent
Bram Moolenaar <Bram@vim.org>
parents: 15146
diff changeset
633 key = tv_get_string_buf_chk(&tvkey, buf);
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
634 if (key == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
635 {
15211
de63593896b3 patch 8.1.0615: get_tv function names are not consistent
Bram Moolenaar <Bram@vim.org>
parents: 15146
diff changeset
636 /* "key" is NULL when tv_get_string_buf_chk() gave an errmsg */
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
637 clear_tv(&tvkey);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
638 goto failret;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
639 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
640 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
641
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
642 *arg = skipwhite(*arg + 1);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
643 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
644 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
645 if (evaluate)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
646 clear_tv(&tvkey);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
647 goto failret;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
648 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
649 if (evaluate)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
650 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
651 item = dict_find(d, key, -1);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
652 if (item != NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
653 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
654 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
655 clear_tv(&tvkey);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
656 clear_tv(&tv);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
657 goto failret;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
658 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
659 item = dictitem_alloc(key);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
660 clear_tv(&tvkey);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
661 if (item != NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
662 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
663 item->di_tv = tv;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
664 item->di_tv.v_lock = 0;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
665 if (dict_add(d, item) == FAIL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
666 dictitem_free(item);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
667 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
668 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
669
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
670 if (**arg == '}')
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
671 break;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
672 if (**arg != ',')
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
673 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
674 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
675 goto failret;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
676 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
677 *arg = skipwhite(*arg + 1);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
678 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
679
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
680 if (**arg != '}')
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
681 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
682 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
683 failret:
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
684 if (evaluate)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
685 dict_free(d);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
686 return FAIL;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
687 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
688
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
689 *arg = skipwhite(*arg + 1);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
690 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: 11142
diff changeset
691 rettv_dict_set(rettv, d);
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
692
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
693 return OK;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
694 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
695
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
696 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
697 * Go over all entries in "d2" and add them to "d1".
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
698 * When "action" is "error" then a duplicate key is an error.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
699 * When "action" is "force" then a duplicate key is overwritten.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
700 * Otherwise duplicate keys are ignored ("action" is "keep").
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
701 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
702 void
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
703 dict_extend(dict_T *d1, dict_T *d2, char_u *action)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
704 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
705 dictitem_T *di1;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
706 hashitem_T *hi2;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
707 int todo;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
708 char_u *arg_errmsg = (char_u *)N_("extend() argument");
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
709
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
710 todo = (int)d2->dv_hashtab.ht_used;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
711 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
712 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
713 if (!HASHITEM_EMPTY(hi2))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
714 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
715 --todo;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
716 di1 = dict_find(d1, hi2->hi_key, -1);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
717 if (d1->dv_scope != 0)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
718 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
719 /* Disallow replacing a builtin function in l: and g:.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
720 * Check the key to be valid when adding to any scope. */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
721 if (d1->dv_scope == VAR_DEF_SCOPE
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
722 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
723 && var_check_func_name(hi2->hi_key, di1 == NULL))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
724 break;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
725 if (!valid_varname(hi2->hi_key))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
726 break;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
727 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
728 if (di1 == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
729 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
730 di1 = dictitem_copy(HI2DI(hi2));
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
731 if (di1 != NULL && dict_add(d1, di1) == FAIL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
732 dictitem_free(di1);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
733 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
734 else if (*action == 'e')
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
735 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
736 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
737 break;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
738 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
739 else if (*action == 'f' && HI2DI(hi2) != di1)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
740 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
741 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
742 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
743 break;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
744 clear_tv(&di1->di_tv);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
745 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
746 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
747 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
748 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
749 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
750
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
751 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
752 * Return the dictitem that an entry in a hashtable points to.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
753 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
754 dictitem_T *
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
755 dict_lookup(hashitem_T *hi)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
756 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
757 return HI2DI(hi);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
758 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
759
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
760 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
761 * Return TRUE when two dictionaries have exactly the same key/values.
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
762 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
763 int
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
764 dict_equal(
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
765 dict_T *d1,
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
766 dict_T *d2,
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
767 int ic, /* ignore case for strings */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
768 int recursive) /* TRUE when used recursively */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
769 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
770 hashitem_T *hi;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
771 dictitem_T *item2;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
772 int todo;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
773
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
774 if (d1 == NULL && d2 == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
775 return TRUE;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
776 if (d1 == NULL || d2 == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
777 return FALSE;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
778 if (d1 == d2)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
779 return TRUE;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
780 if (dict_len(d1) != dict_len(d2))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
781 return FALSE;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
782
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
783 todo = (int)d1->dv_hashtab.ht_used;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
784 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
785 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
786 if (!HASHITEM_EMPTY(hi))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
787 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
788 item2 = dict_find(d2, hi->hi_key, -1);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
789 if (item2 == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
790 return FALSE;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
791 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
792 return FALSE;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
793 --todo;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
794 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
795 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
796 return TRUE;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
797 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
798
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
799 /*
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
800 * Turn a dict into a list:
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
801 * "what" == 0: list of keys
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
802 * "what" == 1: list of values
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
803 * "what" == 2: list of items
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
804 */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
805 void
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
806 dict_list(typval_T *argvars, typval_T *rettv, int what)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
807 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
808 list_T *l2;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
809 dictitem_T *di;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
810 hashitem_T *hi;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
811 listitem_T *li;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
812 listitem_T *li2;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
813 dict_T *d;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
814 int todo;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
815
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
816 if (argvars[0].v_type != VAR_DICT)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
817 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
818 EMSG(_(e_dictreq));
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
819 return;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
820 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
821 if ((d = argvars[0].vval.v_dict) == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
822 return;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
823
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
824 if (rettv_list_alloc(rettv) == FAIL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
825 return;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
826
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
827 todo = (int)d->dv_hashtab.ht_used;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
828 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
829 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
830 if (!HASHITEM_EMPTY(hi))
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
831 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
832 --todo;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
833 di = HI2DI(hi);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
834
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
835 li = listitem_alloc();
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
836 if (li == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
837 break;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
838 list_append(rettv->vval.v_list, li);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
839
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
840 if (what == 0)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
841 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
842 /* keys() */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
843 li->li_tv.v_type = VAR_STRING;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
844 li->li_tv.v_lock = 0;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
845 li->li_tv.vval.v_string = vim_strsave(di->di_key);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
846 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
847 else if (what == 1)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
848 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
849 /* values() */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
850 copy_tv(&di->di_tv, &li->li_tv);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
851 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
852 else
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
853 {
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
854 /* items() */
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
855 l2 = list_alloc();
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
856 li->li_tv.v_type = VAR_LIST;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
857 li->li_tv.v_lock = 0;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
858 li->li_tv.vval.v_list = l2;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
859 if (l2 == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
860 break;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
861 ++l2->lv_refcount;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
862
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
863 li2 = listitem_alloc();
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
864 if (li2 == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
865 break;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
866 list_append(l2, li2);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
867 li2->li_tv.v_type = VAR_STRING;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
868 li2->li_tv.v_lock = 0;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
869 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
870
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
871 li2 = listitem_alloc();
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
872 if (li2 == NULL)
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
873 break;
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
874 list_append(l2, li2);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
875 copy_tv(&di->di_tv, &li2->li_tv);
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
876 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
877 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
878 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
879 }
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
880
13037
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
881 /*
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
882 * Make each item in the dict readonly (not the value of the item).
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
883 */
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
884 void
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
885 dict_set_items_ro(dict_T *di)
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
886 {
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
887 int todo = (int)di->dv_hashtab.ht_used;
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
888 hashitem_T *hi;
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
889
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
890 /* Set readonly */
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
891 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
892 {
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
893 if (HASHITEM_EMPTY(hi))
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
894 continue;
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
895 --todo;
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
896 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
897 }
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
898 }
6e81a68d63a1 patch 8.0.1394: cannot intercept a yank command
Christian Brabandt <cb@256bit.org>
parents: 11418
diff changeset
899
9556
afaff1d283d3 commit https://github.com/vim/vim/commit/cd52459c387785796713826c63174cdeed295dd4
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
900 #endif /* defined(FEAT_EVAL) */