emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/compat 9aac0f55d1 23/84: Add prefixed plist-member from


From: ELPA Syncer
Subject: [elpa] externals/compat 9aac0f55d1 23/84: Add prefixed plist-member from Emacs 29
Date: Tue, 3 Jan 2023 08:57:32 -0500 (EST)

branch: externals/compat
commit 9aac0f55d1935201f274b80477b5f109d70d4833
Author: Philip Kaludercic <philipk@posteo.net>
Commit: Philip Kaludercic <philipk@posteo.net>

    Add prefixed plist-member from Emacs 29
---
 compat-29.el    | 20 ++++++++++++++++++++
 compat-tests.el | 20 ++++++++++++++++++++
 compat.texi     | 15 +++++++++++++++
 3 files changed, 55 insertions(+)

diff --git a/compat-29.el b/compat-29.el
index 68fd657961..0e03b1fc01 100644
--- a/compat-29.el
+++ b/compat-29.el
@@ -149,6 +149,26 @@ The PLIST is modified by side effects."
           (setq tail (cddr tail))))
       (nconc plist (list prop val)))))
 
+(compat-defun plist-member (plist prop &optional predicate)
+  "Return non-nil if PLIST has the property PROP.
+PLIST is a property list, which is a list of the form
+\(PROP1 VALUE1 PROP2 VALUE2 ...).
+
+The comparison with PROP is done using PREDICATE, which defaults to
+`eq'.
+
+Unlike `plist-get', this allows you to distinguish between a missing
+property and a property with the value nil.
+The value is actually the tail of PLIST whose car is PROP."
+  :prefix t
+  (if (or (null predicate) (eq predicate 'eq))
+      (plist-member plist prop)
+    (catch 'found
+      (while (consp plist)
+        (when (funcall predicate prop (car plist))
+          (throw 'found plist))
+        (setq plist (cddr plist))))))
+
 ;;;; Defined in subr.el
 
 (compat-defun function-alias-p (func &optional noerror)
diff --git a/compat-tests.el b/compat-tests.el
index 76e640e0cf..567be85797 100644
--- a/compat-tests.el
+++ b/compat-tests.el
@@ -2077,6 +2077,26 @@ being compared against."
          '(1 :one 2 :two 3 :three)
          4 :vier #'>))
 
+(compat-deftests compat-plist-get-member
+  (ought '(:one 1 :two 2 :three 3)
+         '(:one 1 :two 2 :three 3)
+         :one)
+  (ought '(:two 2 :three 3)
+         '(:one 1 :two 2 :three 3)
+         :two)
+  (ought '(:three 3)
+         '(:one 1 :two 2 :three 3)
+         :three)
+  (ought nil '(:one 1 :two 2 :three 3) :four)
+  ;; With a custom predicate
+  (ought '(1 :one 2 :two 3 :three)
+         '(1 :one 2 :two 3 :three)
+         3 #'>)
+  (ought '(3 :three)
+         '(1 :one 2 :two 3 :three)
+         3 #'<=)
+  (ought nil '(1 :one 2 :two 3 :three) 4 #'<=))
+
 
 (provide 'compat-tests)
 ;;; compat-tests.el ends here
diff --git a/compat.texi b/compat.texi
index 0fb1ff2438..77df3082a1 100644
--- a/compat.texi
+++ b/compat.texi
@@ -2424,6 +2424,21 @@ This compatibility version handles the optional argument
 @var{predicate}.
 @end defun
 
+@c copied from lispref/lists.texi
+@defun compat-plist-member plist prop &optional predicate
+This returns non-@code{nil} if @var{plist} contains the given
+@var{property}.  Comparisons are done with @var{predicate}, and
+defaults to @code{eq}.  Unlike @code{plist-get}, this allows you to
+distinguish between a missing property and a property with the value
+@code{nil}.  The value is actually the tail of @var{plist} whose
+@code{car} is @var{property}.
+
+@xref{Plist Access,,,elisp}.
+
+This compatibility version handles the optional argument
+@var{predicate}.
+@end defun
+
 @subsection Missing Definitions
 Compat does not provide support for the following Lisp features
 implemented in 29.1:



reply via email to

[Prev in Thread] Current Thread [Next in Thread]