I am trying to write a macro. I have read the manual and learned about macroexpand
. However, when I use it, I find the expansion difficult to read and work with.
How can I expand a macro in a readable, workable format?
Let me illustrate.
If I simply call macroexpand
, the result is echoed in the minibuffer. When the expansion is large, the echo is all scrunched up:
(macroexpand '(with-temp-file (make-temp-file "test-file")))
I can go to *Messages*
with C-h e
, copy and paste it back into *scratch*
, and then format it. But clearly that gets tiresome.
To save on the copy/paste, I have tried using eval-last-sexp
with a universal argument, C-u C-x C-e
. This indeed prints the output to the buffer...but it's in some weird interactive state that I cannot edit. Pressing RET
causes it to alternate between an abbreviated and expanded form:
Abbreviated (expressions contain ellipses):
Expanded (Placing point before the expansion and pressing RET
causes the ellipses to expand and not insert a line break):
After calling macroexpand
, C-h k RET
shows that elisp-last-sexp-toggle-display
is now the binding for RET
.
All of this implies some kind of workflow. However, the documentation for macroexpand
describes none of this behavior. The source code is not distributed with my version of Emacs (Emacs 26.3 for Windows). Scouring the web has found no meaningful explanation of how to work with macro expansions.
C-j
evaluates the expression before point (it is bound toeval-print-last-sexp
). The result is printed in the buffer. Does that make things a bit easier?M-x facemenu-remove-all
to remove the fancy text properties.