Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Eval-result-overlays in Emacs-lisp

One of the things I like most in CIDER is how evaluation results are displayed by inline overlays. And yet, for some reason, it’s taken me almost a year to transfer that to Elisp.

elisp-inline-eval-result.png

It’s the tiniest of changes — you’re just taking something that would be displayed in the minibuffer and moving it up a dozen or so lines — but the difference is palpable. By displaying the result inline, you display it where the user is looking, instead of forcing them to shift focus. The quick-feedback loop we all love in our lisps becomes even faster (something I would’ve thought impossible one year ago).

Assuming you already have CIDER installed, porting this feature to Elisp is almost trivial. We just define a small wrapper around the function that creates the overlay, and then advise the relevant elisp commands to call it.

(autoload 'cider--make-result-overlay "cider-overlays")

(defun endless/eval-overlay (value point)
  (cider--make-result-overlay (format "%S" value)
    :where point
    :duration 'command)
  ;; Preserve the return value.
  value)

(advice-add 'eval-region :around
            (lambda (f beg end &rest r)
              (endless/eval-overlay
               (apply f beg end r)
               end)))

(advice-add 'eval-last-sexp :filter-return
            (lambda (r)
              (endless/eval-overlay r (point))))

(advice-add 'eval-defun :filter-return
            (lambda (r)
              (endless/eval-overlay
               r
               (save-excursion
                 (end-of-defun)
                 (point)))))

If I like this enough, I might implement it more properly and propose its addition to Emacs core. For now the advices are more than enough.

If you don’t want to install CIDER, you can just copy that function to your configs (you’ll also have to copy the functions above it in the same file, and the when-let definition from cider-compat.el).

comments powered by Disqus