Skip to content

Commit

Permalink
Add option to remove result overlays after buffer change (#3149)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhan0 authored Feb 11, 2022
1 parent f3309c3 commit f556d30
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#3127](https://github.com/clojure-emacs/cider/pull/3040): Strip all exec-opts flags (`-A` `-M` `-T` `-X`) if they exist in `cider-clojure-cli-aliases`. Also addresses a duplicate `:` in the generated `clj` command.
* Enable `cider-enrich-classpath` by default.
* [#3148](https://github.com/clojure-emacs/cider/pull/3148): Display error messages in multiline comment eval results, and in result overlays when `cider-show-error-buffer` is set to nil.
* [#3149](https://github.com/clojure-emacs/cider/pull/3149): Add option `'change` to `cider-eval-result-duration`, allowing multiple eval result overlays to persist until the next change to the buffer.

### Bugs fixed

Expand Down
6 changes: 5 additions & 1 deletion cider-eval.el
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,11 @@ arguments and only proceed with evaluation if it returns nil."
(start (car-safe bounds))
(end (car-safe (cdr-safe bounds))))
(when (and start end)
(remove-overlays start end 'cider-temporary t))
;; NOTE: don't use `remove-overlays' as it splits and leaves behind
;; partial overlays, leading to duplicate eval results in some situations.
(dolist (ov (overlays-in start end))
(when (eq (overlay-get ov 'cider-temporary) t)
(delete-overlay ov))))
(unless (and cider-interactive-eval-override
(functionp cider-interactive-eval-override)
(funcall cider-interactive-eval-override form callback bounds))
Expand Down
24 changes: 18 additions & 6 deletions cider-overlays.el
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ applied with lower priority than the syntax highlighting."

(defface cider-error-overlay-face
'((((class color) (background light))
:background "orange red")
:background "orange red"
:extend t)
(((class color) (background dark))
:background "firebrick"))
:background "firebrick"
:extend t))
"Like `cider-result-overlay-face', but for evaluation errors."
:group 'cider
:package-version '(cider "0.25.0"))
Expand Down Expand Up @@ -98,9 +100,11 @@ If 'at-point, display at the end of the respective sexp."
"Duration, in seconds, of CIDER's eval-result overlays.
If nil, overlays last indefinitely.
If the symbol `command', they're erased after the next command.
If the symbol `change', they last until the next change to the buffer.
Also see `cider-use-overlays'."
:type '(choice (integer :tag "Duration in seconds")
(const :tag "Until next command" command)
(const :tag "Until next buffer change" change)
(const :tag "Last indefinitely" nil))
:group 'cider
:package-version '(cider . "0.10.0"))
Expand All @@ -125,10 +129,14 @@ PROPS is a plist of properties and values to add to the overlay."
(push #'cider--delete-overlay (overlay-get o 'modification-hooks))
o))

(defun cider--remove-result-overlay ()
(defun cider--remove-result-overlay (&rest _)
"Remove result overlay from current buffer.
This function also removes itself from `post-command-hook'."
(remove-hook 'post-command-hook #'cider--remove-result-overlay 'local)
This function also removes itself from `post-command-hook' and
`after-change-functions'."
(let ((hook (pcase cider-eval-result-duration
(`command 'post-command-hook)
(`change 'after-change-functions))))
(remove-hook hook #'cider--remove-result-overlay 'local))
(remove-overlays nil nil 'category 'result))

(defun cider--remove-result-overlay-after-command ()
Expand Down Expand Up @@ -258,7 +266,11 @@ overlay."
(add-hook 'post-command-hook
#'cider--remove-result-overlay-after-command
nil 'local)
(cider--remove-result-overlay-after-command))))
(cider--remove-result-overlay-after-command)))
(`change
(add-hook 'after-change-functions
#'cider--remove-result-overlay
nil 'local)))
(when-let* ((win (get-buffer-window buffer)))
;; Left edge is visible.
(when (and (<= (window-start win) (point) (window-end win))
Expand Down
18 changes: 18 additions & 0 deletions doc/modules/ROOT/pages/usage/code_evaluation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,24 @@ Note that this also affects the position of debugger overlays.
(setq cider-result-overlay-position 'at-point)
----


You can also customize how overlays are persisted using the variable
`cider-eval-result-duration`.

By default, its value is `'command`, meaning that result overlays disappear
after the next user-executed command, such as moving the point or scrolling.

Setting the variable to a number represents the duration in seconds until
overlays are removed, while setting it to `'change' persists overlays until the
next change to the buffer contents.


[source,lisp]
----
(setq cider-eval-result-duration 5.0)
(setq cider-eval-result-duration 'change)
----

=== Auto-Save Clojure Buffers on Load

Normally, CIDER prompts you to save a modified Clojure buffer when you
Expand Down

0 comments on commit f556d30

Please sign in to comment.