From ae8db0e4e69603dbe8dd009dfe38dc814bbe11f2 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Mon, 9 Aug 2021 12:50:54 +0800 Subject: new: expand elisp macros * elisp.el (elisp-macro-buffer): The buffer that displays the expansion. (elisp-macro-expand): Expand the macro, and either displays in a temporary buffer, or replace the original region. Right now this only expands one form at a time. I might want to modify this in the future so that this will expand every form found in the region. For now this is enough for me. (emacs-lisp-mode-map): Bind it to C-c C-e. --- elisp.el | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'elisp.el') diff --git a/elisp.el b/elisp.el index c944040..bb68713 100644 --- a/elisp.el +++ b/elisp.el @@ -310,6 +310,38 @@ PARSE-START is the starting position of the parse." (t normal-indent)))))) +;;; macro expansion + +(defconst elisp-macro-buffer "*ELisp-Macroexpand*" + "The name of the buffer that displays the expansion of \ +macros.") + +;;;###autoload +(defun elisp-macro-expand (start end subst) + "Expand ELisp macros in the region. +Normally display output in temp buffer, but +prefix arg means replace the region with it. + +Note that this only expands one Emacs Lisp form. So multiple +forms have to be expanded separately. + +Adapted from `c-macro-expand'." + (interactive "r\nP") + (let ((form (read (buffer-substring-no-properties start end))) + (buf (cond ((not subst) + (get-buffer-create elisp-macro-buffer))))) + (cond + ((not subst) + (with-current-buffer buf + (erase-buffer) + (insert (format "%s" (macroexpand-all form)))) + (display-buffer buf)) + (t + (delete-region start end) + (insert (format "%S" (macroexpand-all form))))))) + +(define-key emacs-lisp-mode-map (vector 3 5) #'elisp-macro-expand) + (provide 'elisp) ;;; elisp.el ends here -- cgit v1.2.3-18-g5258