feature/eval: :eval now accepts functions (refactor emacs-lisp eval fn)

This commit is contained in:
Henrik Lissner 2017-05-07 02:27:54 +02:00
parent d64ee3c2f6
commit 4aa1427811
4 changed files with 37 additions and 25 deletions

View file

@ -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)

View file

@ -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

View file

@ -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))))

View file

@ -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*")