diff --git a/modules/feature/eval/autoload/eval.el b/modules/feature/eval/autoload/eval.el index 0c6e52cea..21bdf607e 100644 --- a/modules/feature/eval/autoload/eval.el +++ b/modules/feature/eval/autoload/eval.el @@ -4,7 +4,7 @@ (defun +eval/buffer () "Evaluate the whole buffer." (interactive) - (cond ((eq major-mode 'emacs-lisp-mode) + (cond ((assq major-mode +eval-runners-alist) (+eval/region (point-min) (point-max))) (t (quickrun)))) @@ -14,28 +14,9 @@ elisp buffer). Otherwise forward the region to Quickrun." (interactive "r") (let ((load-file-name buffer-file-name)) - (cond ((eq major-mode 'emacs-lisp-mode) - (require 'pp) - (let ((result (eval (read (buffer-substring-no-properties beg end)))) - lines) - (let ((buf (get-buffer-create "*eval*"))) - (with-current-buffer buf - ;; FIXME messy! - (read-only-mode -1) - (setq-local scroll-margin 0) - (erase-buffer) - (emacs-lisp-mode) - (prin1 result buf) - (pp-buffer) - (read-only-mode 1) - (setq lines (count-lines (point-min) (point-max))) - (goto-char (point-min)) - (when (< lines 5) - (message "%s" (buffer-substring (point-min) (point-max))) - (kill-buffer buf))) - (unless (< lines 5) - (doom-popup-buffer buf))))) - (t (quickrun-region beg end))))) + (if-let (runner (cdr (assq major-mode +eval-runners-alist))) + (funcall runner beg end) + (quickrun-region beg end)))) ;;;###autoload (defun +eval/region-and-replace (beg end) diff --git a/modules/feature/eval/config.el b/modules/feature/eval/config.el index 580619558..97ce85154 100644 --- a/modules/feature/eval/config.el +++ b/modules/feature/eval/config.el @@ -56,6 +56,9 @@ invokes the repl. Takes the same arguements as `rtog/add-repl'." (setq eval-expression-print-length nil eval-expression-print-level nil) +(defvar +eval-runners-alist nil + "Alist mapping major modes to interactive runner functions.") + (def-setting! :eval (mode command) "Define a code evaluator for major mode MODE with `quickrun'. @@ -65,9 +68,13 @@ invokes the repl. Takes the same arguements as `rtog/add-repl'." and COMMAND is a key (for `quickrun--language-alist'), and will be registered in `quickrun--major-mode-alist'. 3. If MODE is not a string and COMMAND is an alist, see `quickrun-add-command': - (quickrun-add-command MODE COMMAND :mode MODE)." + (quickrun-add-command MODE COMMAND :mode MODE). +4. If MODE is not a string and COMMANd is a symbol, add it to + `+eval-runners-alist', which is used by `+eval/region'." `(after! quickrun - ,(cond ((stringp command) + ,(cond ((symbolp command) + `(push ',(cons mode command) +eval-runners-alist)) + ((stringp command) `(push ',(cons mode command) ,(if (stringp mode) 'quickrun-file-alist diff --git a/modules/lang/emacs-lisp/autoload.el b/modules/lang/emacs-lisp/autoload.el index 904c42c23..b48cf075d 100644 --- a/modules/lang/emacs-lisp/autoload.el +++ b/modules/lang/emacs-lisp/autoload.el @@ -10,3 +10,26 @@ (let ((buf (get-buffer "*ielm*"))) (bury-buffer buf) buf))))) + +;;;###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." + (require 'pp) + (let ((result (eval (read (buffer-substring-no-properties beg end)))) + (buf (get-buffer-create "*eval*")) + (inhibit-read-only t) + lines) + (with-current-buffer buf + (read-only-mode +1) + (setq-local scroll-margin 0) + (emacs-lisp-mode) + (prin1 result buf) + (pp-buffer) + (setq lines (count-lines (point-min) (point-max))) + (goto-char (point-min)) + (when (<= lines 1) + (message "%s" (buffer-substring (point-min) (point-max))) + (kill-buffer buf))) + (when (> lines 1) + (doom-popup-buffer buf)))) diff --git a/modules/lang/emacs-lisp/config.el b/modules/lang/emacs-lisp/config.el index a9239fb5b..69ade0991 100644 --- a/modules/lang/emacs-lisp/config.el +++ b/modules/lang/emacs-lisp/config.el @@ -7,6 +7,7 @@ :config (set! :repl 'emacs-lisp-mode #'+emacs-lisp/repl) + (set! :eval 'emacs-lisp-mode #'+emacs-lisp-eval) (set! :rotate 'emacs-lisp-mode :symbols '(("t" "nil") ("let" "let*")