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

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

[elpa] externals/compat ff331f6c29 22/84: Add prefixed plist-put from Em


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

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

    Add prefixed plist-put from Emacs 29
---
 compat-29.el    | 23 +++++++++++++++++++++++
 compat-tests.el | 30 ++++++++++++++++++++++++++++++
 compat.texi     | 15 +++++++++++++++
 3 files changed, 68 insertions(+)

diff --git a/compat-29.el b/compat-29.el
index 3d823ee6aa..68fd657961 100644
--- a/compat-29.el
+++ b/compat-29.el
@@ -126,6 +126,29 @@ This function doesn't signal an error if PLIST is invalid."
           (throw 'found (cadr plist)))
         (setq plist (cddr plist))))))
 
+(compat-defun plist-put (plist prop val &optional predicate)
+  "Change value in PLIST of PROP to VAL.
+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'.
+
+If PROP is already a property on the list, its value is set to VAL,
+otherwise the new PROP VAL pair is added.  The new plist is returned;
+use `(setq x (plist-put x prop val))' to be sure to use the new value.
+The PLIST is modified by side effects."
+  :prefix t
+  (if (or (null predicate) (eq predicate 'eq))
+      (plist-put plist prop val)
+    (catch 'found
+      (let ((tail plist))
+        (while (consp tail)
+          (when (funcall predicate prop (car tail))
+            (setcar (cdr tail) val)
+            (throw 'found plist))
+          (setq tail (cddr tail))))
+      (nconc plist (list prop val)))))
+
 ;;;; Defined in subr.el
 
 (compat-defun function-alias-p (func &optional noerror)
diff --git a/compat-tests.el b/compat-tests.el
index 9ad7651534..76e640e0cf 100644
--- a/compat-tests.el
+++ b/compat-tests.el
@@ -2047,6 +2047,36 @@ being compared against."
   (ought :three '(1 :one 2 :two 3 :three) 3 #'<=)
   (ought nil '(1 :one 2 :two 3 :three) 4 #'<=))
 
+(compat-deftests compat-plist-put
+  (ought '(:one -1 :two 2 :three 3)
+         (list :one 1 :two 2 :three 3)
+         :one -1)
+  (ought '(:one -1 :one 2 :one 3)
+         (list :one 1 :one 2 :one 3)
+         :one -1)
+  (ought '(:zero 1 :one -1 :one 3)
+         (list :zero 1 :one 2 :one 3)
+         :one -1)
+  (ought '(:one -1 :two 2 :three 3)
+         (list :one 1 :two 2 :three 3)
+         :one -1)
+  (ought '(:one 1 :two 2 :three 3 :four -1)
+         (list :one 1 :two 2 :three 3)
+         :four -1)
+  ;; With a custom predicate
+  (ought '(1 :eins 2 :two 3 :three)
+         '(1 :one 2 :two 3 :three)
+         3 :eins #'>)
+  (ought '(1 :one 2 :zwei 3 :three)
+         '(1 :one 2 :two 3 :three)
+         2 :zwei #'<=)
+  (ought '(1 :one 2 :two 3 :three 4 :vier)
+         '(1 :one 2 :two 3 :three)
+         4 :vier #'<=)
+  (ought '(1 :vier 2 :two 3 :three)
+         '(1 :one 2 :two 3 :three)
+         4 :vier #'>))
+
 
 (provide 'compat-tests)
 ;;; compat-tests.el ends here
diff --git a/compat.texi b/compat.texi
index c4e8aa710f..0fb1ff2438 100644
--- a/compat.texi
+++ b/compat.texi
@@ -2409,6 +2409,21 @@ This compatibility version handles the optional argument
 @var{predicate}.
 @end defun
 
+@c copied from lispref/lists.texi
+@defun compat-plist-put plist prop val &optional predicate
+This stores @var{value} as the value of the @var{property} property in
+the property list @var{plist}.  Comparisons are done with @var{predicate},
+and defaults to @code{eq}.  It may modify @var{plist} destructively,
+or it may construct a new list structure without altering the old.  The
+function returns the modified property list, so you can store that back
+in the place where you got @var{plist}.
+
+@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]