summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArthur Leonard Andersen <leoc.git@gmail.com>2013-01-22 15:30:12 +0100
committerArthur Leonard Andersen <leoc.git@gmail.com>2013-01-22 15:30:12 +0100
commitf6e0b5a16631a551469a22e140736dabe44b98d7 (patch)
treedce8152f3b9e9511f8b92922487342a2ccb16bda
parent608de1e608ba6f1a77d3ffff93dc0fea0fad3fea (diff)
Add kvplist-merge function, test and readme text
-rw-r--r--README.creole5
-rw-r--r--kv-tests.el7
-rw-r--r--kv.el14
3 files changed, 23 insertions, 3 deletions
diff --git a/README.creole b/README.creole
index b4a58ae..d5e49ab 100644
--- a/README.creole
+++ b/README.creole
@@ -145,9 +145,10 @@ Filter the plist to just those matching //keys//.
[[kvalist->filter-keys]] is actually used to do this work.
+=== kvplist->merge old-plist new-plist ===
+
+Merge the keys from new-plist into old-plist and return the new plist.
=== kvplist2->filter-keys plist2 &rest keys ===
Return the //plist2// (a list of plists) filtered to the //keys//.
-
-
diff --git a/kv-tests.el b/kv-tests.el
index 4a4823f..7cc6eed 100644
--- a/kv-tests.el
+++ b/kv-tests.el
@@ -179,4 +179,11 @@
(list :key1 "value1" :key2 t :key3 '(big list of symbols) :key4 10)
'key1 'key4))))
+(ert-deftest kvplist-merge ()
+ (should
+ (equal
+ '(:key1 "value1" :key2 "new value" :key3 "entirely new")
+ (kvplist-merge '(:key1 "value1" :key2 "old value")
+ '(:key2 "new value" :key3 "entirely new")))))
+
;;; kv-tests.el ends here
diff --git a/kv.el b/kv.el
index ffed5b6..57958e1 100644
--- a/kv.el
+++ b/kv.el
@@ -179,7 +179,7 @@ The keys are expected to be :prefixed and the colons are removed.
The keys in the resulting alist are symbols."
(when plist
(loop for (key value . rest) on plist by 'cddr
- collect (cons (keyword->symbol key) value))))
+ collect (cons (keyword->symbol key) value))))
(defun kvalist2->plist (alist2)
"Convert a list of alists too a list of plists."
@@ -385,6 +385,18 @@ SEXP will describe the structure desired."
(defalias 'map-bind 'kvmap-bind)
+(defun kvplist-merge (old new)
+ "Merges two plists. The keys from NEW will overwrite the ones in OLD."
+ (let ((key (car new))
+ (val (cadr new))
+ (new (cddr new)))
+ (while (and key val)
+ (setq old (plist-put old key val))
+ (setq key (car new))
+ (setq val (cadr new))
+ (setq new (cddr new)))
+ old))
+
(provide 'kv)
(provide 'dotassoc)