Emacsist RSS --- If never, make it ever: C-h C-h

Eval-result-overlays in Emacs-lisp

7392

这里 查看更多 Emacs 相关推荐文章最新 Emacs 圈的动态. 欢迎关注微信公众账号: Emacsist

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

原文出处: EndlessParenthese
原文地址: http://endlessparentheses.com/eval-result-overlays-in-emacs-lisp.html?source=rss
原文时间: 2016-03-29 08:00
本文地址: http://emacsist.com/10827
整理时间: 2016-04-21 01:58

本文由 Hick 整理,转载请保留以上信息;
COPYLEFTThe articles on this site come from Internet, thanks to all the original authors.
      If anything about COPYRIGHT, or LEFT, please contact Emacsist at gmail dot com .