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

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

[elpa] externals/compat 2e206a8304 21/84: Add prefixed plist-get from Em


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

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

    Add prefixed plist-get from Emacs 29
---
 compat-29.el    | 19 +++++++++++++++++++
 compat-tests.el | 10 ++++++++++
 compat.texi     | 14 ++++++++++++++
 3 files changed, 43 insertions(+)

diff --git a/compat-29.el b/compat-29.el
index af29d96446..3d823ee6aa 100644
--- a/compat-29.el
+++ b/compat-29.el
@@ -107,6 +107,25 @@ Unibyte strings are converted to multibyte for comparison."
   (declare (pure t) (side-effect-free t))
   (eq t (compare-strings string1 0 nil string2 0 nil t)))
 
+(compat-defun plist-get (plist prop &optional predicate)
+  "Extract a value from a property list.
+PLIST is a property list, which is a list of the form
+\(PROP1 VALUE1 PROP2 VALUE2...).
+
+This function returns the value corresponding to the given PROP, or
+nil if PROP is not one of the properties on the list.  The comparison
+with PROP is done using PREDICATE, which defaults to `eq'.
+
+This function doesn't signal an error if PLIST is invalid."
+  :prefix t
+  (if (or (null predicate) (eq predicate 'eq))
+      (plist-get plist prop)
+    (catch 'found
+      (while (consp plist)
+        (when (funcall predicate prop (car plist))
+          (throw 'found (cadr 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 4f91473a85..9ad7651534 100644
--- a/compat-tests.el
+++ b/compat-tests.el
@@ -2037,6 +2037,16 @@ being compared against."
   (ought t "AAA bbb" "aaa bbb")
   (ought t "aaa BBB" "aaa bbb"))
 
+(compat-deftests compat-plist-get
+  (ought 1 '(:one 1 :two 2 :three 3) :one)
+  (ought 2 '(:one 1 :two 2 :three 3) :two)
+  (ought 3 '(:one 1 :two 2 :three 3) :three)
+  (ought nil '(:one 1 :two 2 :three 3) :four)
+  ;; With a custom predicate
+  (ought :one '(1 :one 2 :two 3 :three) 3 #'>)
+  (ought :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 e82badff2a..c4e8aa710f 100644
--- a/compat.texi
+++ b/compat.texi
@@ -2395,6 +2395,20 @@ differences, like @code{char-equal} when 
@code{case-fold-search} is
 These functions are prefixed with @code{compat} prefix, and will require
 manual loading even after the release of Compat 29.1.0.0:
 
+@c copied from lispref/lists.texi
+@defun compat-plist-get plist prop &optional predicate
+This returns the value of the @var{property} property stored in the
+property list @var{plist}.  Comparisons are done with @var{predicate},
+and defaults to @code{eq}.  It accepts a malformed @var{plist}
+argument.  If @var{property} is not found in the @var{plist}, it
+returns @code{nil}.
+
+@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]