Skip to content

Commit

Permalink
Improve the cider-selector handling of some edge cases (#2711)
Browse files Browse the repository at this point in the history
This commit adjusts cider-selector handling with the following benefits:

- If the selector is run outside of an active sesman-session, it will
still find the REPL when the REPL selector is chosen (it will fallback to the most recently visited REPL).
- If the selector is run while a window is visible, it will switch to
that window (focus) as opposed to giving a "No such buffer" message.
  • Loading branch information
ahungry authored and bbatsov committed Sep 21, 2019
1 parent b2449ec commit a89a87a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Changes

* `cider-selector` has more robust handling for edge cases.

### Bugs fixed

* [#2715](https://github.com/clojure-emacs/cider/issues/2715): Fix the `shadow-cljs` presence check.
Expand Down
22 changes: 15 additions & 7 deletions cider-selector.el
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,23 @@ DESCRIPTION is a one-line description of what the key selects.")
Not meant to be set by users. It's used internally
by `cider-selector'.")

(defun cider-selector--recently-visited-buffer (mode)
(defun cider-selector--recently-visited-buffer (mode &optional consider-visible-p)
"Return the most recently visited buffer, deriving its `major-mode' from MODE.
Only considers buffers that are not already visible."
CONSIDER-VISIBLE-P will allow handling of visible windows as well.
First pass only considers buffers that are not already visible.
Second pass will attempt one of visible ones for scenarios where the window
is visible, but not focused."
(cl-loop for buffer in (buffer-list)
when (and (with-current-buffer buffer
(derived-mode-p mode))
;; names starting with space are considered hidden by Emacs
(not (string-match-p "^ " (buffer-name buffer)))
(null (get-buffer-window buffer 'visible)))
(or consider-visible-p
(null (get-buffer-window buffer 'visible))))
return buffer
finally (error "Can't find unshown buffer in %S" mode)))
finally (if consider-visible-p
(error "Can't find unshown buffer in %S" mode)
(cider-selector--recently-visited-buffer mode t))))

;;;###autoload
(defun cider-selector (&optional other-window)
Expand Down Expand Up @@ -97,7 +103,7 @@ is chosen. The returned buffer is selected with
`switch-to-buffer'."
(let ((method `(lambda ()
(let ((buffer (progn ,@body)))
(cond ((not (get-buffer buffer))
(cond ((not (and buffer (get-buffer buffer)))
(message "No such buffer: %S" buffer)
(ding))
((get-buffer-window buffer)
Expand Down Expand Up @@ -138,8 +144,10 @@ is chosen. The returned buffer is selected with
(top-level))

(def-cider-selector-method ?r
"Current REPL buffer."
(cider-current-repl))
"Current REPL buffer or as a fallback, the most recently
visited cider-repl-mode buffer."
(or (cider-current-repl)
(cider-selector--recently-visited-buffer 'cider-repl-mode)))

(def-cider-selector-method ?m
"Current connection's *nrepl-messages* buffer."
Expand Down

0 comments on commit a89a87a

Please sign in to comment.