Skip to content

command for macroexpanding the expression #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions nrepl.el
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,41 @@ into the current buffer."
(interactive)
(nrepl-interactive-eval-print (nrepl-last-expression)))

(defun nrepl-macroexpand-last-expression (pretty)
"Evaluate the expression preceding point and print the result
into the special buffer. Prefix argument forces the pretty-printed output."
(interactive "P")
(let ((expr (nrepl-last-expression))
(command (if pretty "(pprint (macroexpand '%s))"
"(macroexpand '%s)")))
(nrepl-initialize-macroexpansion-buffer
(lambda ()
(nrepl-interactive-eval-print (format command expr))))))

(defun nrepl-initialize-macroexpansion-buffer (print-function &optional buffer)
(pop-to-buffer (or buffer (nrepl-create-macroexpansion-buffer)))
(setq buffer-undo-list nil) ; Get rid of undo information from
; previous expansions.
(let ((inhibit-read-only t)
(buffer-undo-list t)) ; Make the initial insertion not be undoable.
(erase-buffer)
(funcall print-function)
(goto-char (point-min))
(indent-sexp)
(font-lock-fontify-buffer)))

(defun nrepl-create-macroexpansion-buffer ()
(let ((name "*nREPL Macroexpansion*"))
(with-current-buffer (get-buffer-create name)
(kill-all-local-variables)
(erase-buffer)
(set-syntax-table lisp-mode-syntax-table)
(current-buffer)
(lisp-mode)
(nrepl-mode)
(setq font-lock-keywords-case-fold-search t)
(current-buffer))))

(defun nrepl-interactive-eval-print (form)
"Evaluate the given form and print value in current buffer."
(lexical-let ((buffer (current-buffer)))
Expand Down Expand Up @@ -334,6 +369,7 @@ DIRECTION is 'forward' or 'backward' (in the history list)."
(define-key map (kbd "\C-x\C-e") 'nrepl-eval-last-expression)
(define-key map (kbd "\C-c\C-e") 'nrepl-eval-last-expression)
(define-key map (kbd "\C-c\C-p") 'nrepl-eval-print-last-expression)
(define-key map (kbd "\C-c\C-m") 'nrepl-macroexpand-last-expression)
map))

(defvar nrepl-mode-map
Expand Down