feat(emacs-lisp): add +emacs-lisp/change-working-buffer command

This changes what buffer `+emacs-lisp-eval` evaluates elisp in (useful
for the :tool eval module's commands, like `+eval/buffer` and
`+eval/region`, or the Evil operators on gr/gR).

Creates a scratch buffer/org src block alternative to ielm.
This commit is contained in:
Henrik Lissner
2025-04-13 04:39:54 -04:00
parent e096e7d79e
commit dddc01982b
2 changed files with 39 additions and 16 deletions

View File

@ -3,29 +3,38 @@
;;
;;; Library
(defvar +emacs-lisp-eval-working-buffer nil)
;;;###autoload
(defun +emacs-lisp-eval (beg end)
"Evaluate a region and print it to the echo area (if one line long), otherwise
to a pop up buffer."
to a pop up buffer.
Meant as an eval handler for Doom's :tools eval module."
(+eval-display-results
(string-trim-right
(let ((buffer (generate-new-buffer " *+eval-output*"))
(working-buffer (or +emacs-lisp-eval-working-buffer (current-buffer)))
(debug-on-error t))
(unwind-protect
(condition-case-unless-debug e
(with-doom-module
(doom-module-from-path
(or (buffer-file-name (buffer-base-buffer))
default-directory))
(with-doom-context 'eval
(eval-region beg end buffer load-read-function))
(with-current-buffer buffer
(let ((pp-max-width nil))
(require 'pp)
(pp-buffer)
(replace-regexp-in-string "\\\\n" "\n" (string-trim-left (buffer-string))))))
(error (format "ERROR: %s" e)))
(kill-buffer buffer))))
(unless (buffer-live-p working-buffer)
(setq +emacs-lisp-eval-working-buffer nil
working-buffer (current-buffer)))
(with-current-buffer working-buffer
(unwind-protect
(condition-case-unless-debug e
(with-doom-module
(doom-module-from-path
(or (buffer-file-name (buffer-base-buffer))
default-directory))
(with-doom-context 'eval
(eval-region beg end buffer load-read-function))
(with-current-buffer buffer
(let ((pp-max-width nil))
(require 'pp)
(pp-buffer)
(replace-regexp-in-string "\\\\n" "\n" (string-trim-left (buffer-string))))))
(error (format "ERROR: %s" e)))
(kill-buffer buffer)))))
(current-buffer)))
;;;###autoload
@ -147,6 +156,19 @@ if it's callable, `apropos' otherwise."
;;
;;; Commands
;;;###autoload
(defun +emacs-lisp/change-working-buffer (buffer &optional clear?)
"Change what buffer to run `+emacs-lisp-eval' in.
If given the prefix arg (CLEAR?), clears the current working buffer."
(interactive
(list (read-buffer "Set working buffer to: ")
current-prefix-arg))
(let ((buffer (get-buffer buffer)))
(if (and buffer (buffer-live-p buffer))
(setq +emacs-lisp-eval-working-buffer buffer)
(user-error "No such buffer: %S" buf))))
;;;###autoload
(defun +emacs-lisp/open-repl ()
"Open the Emacs Lisp REPL (`ielm').

View File

@ -137,6 +137,7 @@ See `+emacs-lisp-non-package-mode' for details.")
(map! :localleader
:map (emacs-lisp-mode-map lisp-interaction-mode-map)
:desc "Set working buffer" "b" #'+emacs-lisp/change-working-buffer
:desc "Expand macro" "m" #'macrostep-expand
(:prefix ("d" . "debug")
"f" #'+emacs-lisp/edebug-instrument-defun-on