summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp/cl-macs.el
diff options
context:
space:
mode:
authorStefan Monnier <monnier@iro.umontreal.ca>2015-03-23 18:24:30 -0400
committerStefan Monnier <monnier@iro.umontreal.ca>2015-03-23 18:24:30 -0400
commitae277259b1cf8d913893417e4ca284040f5a543f (patch)
tree42d607424f8666780abe25fd49d9664abd13d81f /lisp/emacs-lisp/cl-macs.el
parent1b5c411e6a4dffd6a8dec9846da0d1650a85b879 (diff)
downloademacs-ae277259b1cf8d913893417e4ca284040f5a543f.tar.gz
emacs-ae277259b1cf8d913893417e4ca284040f5a543f.tar.bz2
emacs-ae277259b1cf8d913893417e4ca284040f5a543f.zip
Add new `cl-struct' and `eieio' pcase patterns.
* lisp/emacs-lisp/cl-macs.el (cl-struct): New pcase pattern. * lisp/emacs-lisp/eieio.el (eieio-pcase-slot-index-table) (eieio-pcase-slot-index-from-index-table): New functions. (eieio): New pcase pattern. * lisp/emacs-lisp/pcase.el (pcase--make-docstring): New function. (pcase): Use it to build the docstring. (pcase-defmacro): Make sure the macro is lazy-loaded. (\`): Move its docstring from `pcase'.
Diffstat (limited to 'lisp/emacs-lisp/cl-macs.el')
-rw-r--r--lisp/emacs-lisp/cl-macs.el22
1 files changed, 22 insertions, 0 deletions
diff --git a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el
index 75c6a5687c4..a81d217e4ee 100644
--- a/lisp/emacs-lisp/cl-macs.el
+++ b/lisp/emacs-lisp/cl-macs.el
@@ -2768,6 +2768,28 @@ non-nil value, that slot cannot be set via `setf'.
',print-auto))
',name)))
+;;; Add cl-struct support to pcase
+
+;;;###autoload
+(pcase-defmacro cl-struct (type &rest fields)
+ "Pcase patterns to match cl-structs.
+Elements of FIELDS can be of the form (NAME UPAT) in which case the contents of
+field NAME is matched against UPAT, or they can be of the form NAME which
+is a shorthand for (NAME NAME)."
+ ;; FIXME: This works well for a destructuring pcase-let, but for straight
+ ;; pcase, it suffers seriously from a lack of support for cl-typep in
+ ;; pcase--mutually-exclusive-p.
+ `(and (pred (pcase--swap cl-typep ',type))
+ ,@(mapcar
+ (lambda (field)
+ (let* ((name (if (consp field) (car field) field))
+ (pat (if (consp field) (cadr field) field)))
+ `(app ,(if (eq (cl-struct-sequence-type type) 'list)
+ `(nth ,(cl-struct-slot-offset type name))
+ `(pcase--flip aref ,(cl-struct-slot-offset type name)))
+ ,pat)))
+ fields)))
+
(defun cl-struct-sequence-type (struct-type)
"Return the sequence used to build STRUCT-TYPE.
STRUCT-TYPE is a symbol naming a struct type. Return 'vector or