diff --git a/modules/lang/emacs-lisp/autoload.el b/modules/lang/emacs-lisp/autoload.el index 3cabfc25e..e40b20ee0 100644 --- a/modules/lang/emacs-lisp/autoload.el +++ b/modules/lang/emacs-lisp/autoload.el @@ -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'). diff --git a/modules/lang/emacs-lisp/config.el b/modules/lang/emacs-lisp/config.el index 733d9e742..934e201d6 100644 --- a/modules/lang/emacs-lisp/config.el +++ b/modules/lang/emacs-lisp/config.el @@ -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