branch: externals/leaf
commit 06935bd9c3810c0475f8b5cf192517d21a93392f
Author: conao3 <[email protected]>
Commit: conao3 <[email protected]>
add leaf-plist-get gv-expander
---
leaf-tests.el | 23 +++++++++++++++++++++++
leaf.el | 15 +++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/leaf-tests.el b/leaf-tests.el
index 477ccc3..ff47f0e 100644
--- a/leaf-tests.el
+++ b/leaf-tests.el
@@ -2780,6 +2780,29 @@ Example:
(mapcar 'car (cdr (assoc "Leaf" (funcall
imenu-create-index-function)))))
'("scala-mode" "lsp-metals" "*scala-flycheck-integration"))))
+(when (version<= "24.3" emacs-version)
+ (require 'cl-lib)
+ (cort-deftest-with-equal leaf/leaf-plist-get
+ '(((let ((target '(:a "a" :b "b" :c "c")))
+ (setf (leaf-plist-get :b target) "modify")
+ target)
+ '(:a "a" :b "modify" :c "c"))
+
+ ((let ((target '(:a "a" :b "b" :c "c")))
+ (cl-rotatef (leaf-plist-get :b target) (leaf-plist-get :c target))
+ target)
+ '(:a "a" :b "c" :c "b"))
+
+ ((let ((target '(:a "a" :b "b" :c "c")))
+ (setf (leaf-plist-get :d target) "modify")
+ target)
+ '(:d "modify" :a "a" :b "b" :c "c"))
+
+ ((let ((target '(:a "a" :b "b" :c "c")))
+ (cl-rotatef (leaf-plist-get :b target) (leaf-plist-get :d target))
+ target)
+ '(:d "b" :a "a" :b nil :c "c")))))
+
;; (provide 'leaf-tests)
;; Local Variables:
diff --git a/leaf.el b/leaf.el
index 03a62c4..84ea83a 100644
--- a/leaf.el
+++ b/leaf.el
@@ -542,6 +542,21 @@ Unlike `butlast', it works well with dotlist (last cdr is
non-nil list)."
(declare (indent 1))
(or (and (plist-member plist key) (plist-get plist key)) default))
+(when (version<= "24.3" emacs-version)
+ (gv-define-expander leaf-plist-get
+ (lambda (do key plist &optional default)
+ (macroexp-let2 macroexp-copyable-p k key
+ (gv-letplace (getter setter) plist
+ (macroexp-let2 nil p `(plist-member ,getter ,k)
+ (funcall
+ do
+ (if (null default) `(cadr ,p)
+ `(if ,p (cadr ,p) ,default))
+ (lambda (val)
+ `(if (plist-member ,plist ,k)
+ (setcar (cdr (plist-member ,plist ,k)) ,val)
+ ,(funcall setter `(cons ,k (cons ,val ,getter))))))))))))
+
;;; General alist functions
;; for Emacs < 25.1