branch: elpa/macrostep
commit 0b04a89f698c335c9ea492553470a8d45c113edd
Author: Torbjörn Norinder <[email protected]>
Commit: Jonas Bernoulli <[email protected]>
macrostep-collect-macro-forms: conditionally override macroexpand-1
As of Emacs commit ca4bc9baf9d2c861ad776da07e56381da8e3722a,
macroexpand-1 is used by macroexpand-all rather than macroexpand.
Also see #9.
---
macrostep.el | 52 ++++++++++++++++++++++++++++++----------------------
1 file changed, 30 insertions(+), 22 deletions(-)
diff --git a/macrostep.el b/macrostep.el
index 4a00a603f7..56b9dfe6a2 100644
--- a/macrostep.el
+++ b/macrostep.el
@@ -942,28 +942,36 @@ corresponding local environments are collected for these.
Forms and environments are extracted from FORM by instrumenting
Emacs's builtin `macroexpand' function and calling
`macroexpand-all'."
- (let ((real-macroexpand (indirect-function #'macroexpand))
- (macro-form-alist '())
- (compiler-macro-forms '()))
- (cl-letf
- (((symbol-function #'macroexpand)
- (lambda (form environment &rest args)
- (let ((expansion
- (apply real-macroexpand form environment args)))
- (cond ((not (eq expansion form))
- (setq macro-form-alist
- (cons (cons form environment)
- macro-form-alist)))
- ((and (consp form)
- (symbolp (car form))
- macrostep-expand-compiler-macros
- (not (eq form
- (cl-compiler-macroexpand form))))
- (setq compiler-macro-forms
- (cons form compiler-macro-forms))))
- expansion))))
- (ignore-errors
- (macroexpand-all form environment)))
+ (let* ((macro-form-alist '())
+ (compiler-macro-forms '())
+ (override (lambda (real-macroexpand form environment &rest args)
+ (let ((expansion
+ (apply real-macroexpand form environment args)))
+ (cond ((not (eq expansion form))
+ (setq macro-form-alist
+ (cons (cons form environment)
+ macro-form-alist)))
+ ((and (consp form)
+ (symbolp (car form))
+ macrostep-expand-compiler-macros
+ (not (eq form
+ (cl-compiler-macroexpand form))))
+ (setq compiler-macro-forms
+ (cons form compiler-macro-forms))))
+ expansion))))
+ (cl-macrolet ((with-override (fn &rest body)
+ `(cl-letf (((symbol-function ,fn)
+ (apply-partially override (indirect-function
,fn))))
+ ,@body))
+ (with-macroexpand-1 (&rest body)
+ (if (< emacs-major-version 30)
+ `(progn ,@body) `(with-override #'macroexpand-1
,@body)))
+ (with-macroexpand (&rest body)
+ `(with-override #'macroexpand ,@body)))
+ (with-macroexpand-1
+ (with-macroexpand
+ (ignore-errors
+ (macroexpand-all form environment)))))
(list macro-form-alist compiler-macro-forms)))
(defvar macrostep-collected-macro-form-alist nil