unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Add gv-define-expander for plist-get
@ 2020-08-17  9:05 Naoya Yamashita
  2020-08-27  4:35 ` Naoya Yamashita
  2020-09-05 18:03 ` Stefan Monnier
  0 siblings, 2 replies; 9+ messages in thread
From: Naoya Yamashita @ 2020-08-17  9:05 UTC (permalink / raw)
  To: emacs-devel@gnu.org


[-- Attachment #1.1: Type: text/plain, Size: 955 bytes --]

Hello, all.

I find `gv` has `gv-define-expander` for `alist-get`, but
there're no definition for `plist-get`

Therefore, `setf` works only for `alist-get` but not for
`plist-get`.

```
(let ((target '((a . "a") (b . "b"))))
  (setf (alist-get 'a target) "modify")
  target)
;;=> ((a . "modify") (b . "b"))

(let ((target '(:a "a" :b "b")))
  (setf (plist-get target :a) "modify")
  target)
;;=> Debugger entered--Lisp error: (void-function \(setf\ plist-get\))
;;     (\(setf\ plist-get\) "modify" v :a)
;;     (let* ((v v)) (\(setf\ plist-get\) "modify" v :a))
;;     (setf (plist-get v :a) "modify")
;;     (let ((v '(:a "a" :b "b"))) (setf (plist-get v :a) "modify"))
;;     (progn (let ((v '(:a "a" :b "b"))) (setf (plist-get v :a) "modify")))
;;     eval((progn (let ((v '(:a "a" :b "b"))) (setf (plist-get v :a)
"modify"))) t)
```

This patch adds the definition of gv-expander for `plist-get`.
Please refer to the additional test cases for usage.

[-- Attachment #1.2: Type: text/html, Size: 1407 bytes --]

[-- Attachment #2: 0001-Add-gv-define-expander-for-plist-get.patch --]
[-- Type: text/x-patch, Size: 3038 bytes --]

From e43c884a60fdffbedc6a94d86ddbe24ea6918220 Mon Sep 17 00:00:00 2001
From: conao3 <conao3@gmail.com>
Date: Mon, 17 Aug 2020 16:56:37 +0900
Subject: [PATCH] Add gv-define-expander for plist-get

It is necessary to make plist-get as a generalized variable, and this
definition allows user to use setf and other useful functions on
plist-get.

* lisp/emacs-lisp/gv.el: Add gv-define-expander for plist-get

* lisp/emacs-lisp/gv-tests.el: Add new test for plist-get
---
 lisp/emacs-lisp/gv.el            | 10 ++++++++++
 test/lisp/emacs-lisp/gv-tests.el | 28 ++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)

diff --git a/lisp/emacs-lisp/gv.el b/lisp/emacs-lisp/gv.el
index 513bd32889..604c62c301 100644
--- a/lisp/emacs-lisp/gv.el
+++ b/lisp/emacs-lisp/gv.el
@@ -417,6 +417,16 @@ alist-get
                                               `(delq ,p ,getter))))))
                             ,v))))))))))
 
+(gv-define-expander plist-get
+  (lambda (do plist prop)
+    (macroexp-let2 macroexp-copyable-p key prop
+      (gv-letplace (getter setter) plist
+        (macroexp-let2 nil p `(plist-member ,getter ,key)
+          (funcall do
+                   `(cadr ,p)
+                   (lambda (val)
+                     `(if (plist-member ,plist ,key) (setcar (cdr (plist-member ,plist ,key)) ,val)
+                        ,(funcall setter `(cons ,key (cons ,val ,getter)))))))))))
 
 ;;; Some occasionally handy extensions.
 
diff --git a/test/lisp/emacs-lisp/gv-tests.el b/test/lisp/emacs-lisp/gv-tests.el
index 7a8402be07..a9ceb7f730 100644
--- a/test/lisp/emacs-lisp/gv-tests.el
+++ b/test/lisp/emacs-lisp/gv-tests.el
@@ -156,6 +156,34 @@ gv-setter-edebug
       (eval-buffer)))
   (should (equal (get 'gv-setter-edebug 'gv-setter-edebug-prop) '(123))))
 
+(ert-deftest gv-plist-get ()
+  (require 'cl-lib)
+
+  ;; Simple setf usage for plist-get.
+  (should (equal (let ((target '(:a "a" :b "b" :c "c")))
+                   (setf (plist-get target :b) "modify")
+                   target)
+                 '(:a "a" :b "modify" :c "c")))
+
+  ;; Other function (cl-rotatef) usage for plist-get.
+  (should (equal (let ((target '(:a "a" :b "b" :c "c")))
+                   (cl-rotatef (plist-get target :b) (plist-get target :c))
+                   target)
+                 '(:a "a" :b "c" :c "b")))
+
+  ;; Add new key value pair at top of list if setf for missing key.
+  (should (equal (let ((target '(:a "a" :b "b" :c "c")))
+                   (setf (plist-get target :d) "modify")
+                   target)
+                 '(:d "modify" :a "a" :b "b" :c "c")))
+
+  ;; Rotate with missing value.
+  ;; The value corresponding to the missing key is assumed to be nil.
+  (should (equal (let ((target '(:a "a" :b "b" :c "c")))
+                   (cl-rotatef (plist-get target :b) (plist-get target :d))
+                   target)
+                 '(:d "b" :a "a" :b nil :c "c"))))
+
 ;; `ert-deftest' messes up macroexpansion when the test file itself is
 ;; compiled (see Bug #24402).
 
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-09-10 12:40 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-08-17  9:05 [PATCH] Add gv-define-expander for plist-get Naoya Yamashita
2020-08-27  4:35 ` Naoya Yamashita
2020-09-05 18:03 ` Stefan Monnier
2020-09-09  1:03   ` Naoya Yamashita
2020-09-09  3:23     ` Stefan Monnier
2020-09-09 12:32       ` Naoya Yamashita
2020-09-09 17:51         ` Stefan Monnier
2020-09-09 20:08           ` Naoya Yamashita
2020-09-10 12:40           ` Adam Porter

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).