summaryrefslogtreecommitdiff
path: root/elisp.el
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2021-08-09 12:50:54 +0800
committerJSDurand <mmemmew@gmail.com>2021-08-09 12:50:54 +0800
commitae8db0e4e69603dbe8dd009dfe38dc814bbe11f2 (patch)
tree6f5aaa1192ac46da428a86a91a0e4e6550f42529 /elisp.el
parent136e040593d3dc3a142ddd507146b6ee34a975d2 (diff)
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.
Diffstat (limited to 'elisp.el')
-rw-r--r--elisp.el32
1 files changed, 32 insertions, 0 deletions
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