From cd6ef9d946028b3e0a071a1c7713895c54358dec Mon Sep 17 00:00:00 2001 From: Matthew Carter Date: Fri, 13 Sep 2019 16:49:30 -0400 Subject: [PATCH 1/4] Add note about the problem --- cider-selector.el | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cider-selector.el b/cider-selector.el index f9a174ad7..56358df40 100644 --- a/cider-selector.el +++ b/cider-selector.el @@ -97,6 +97,11 @@ is chosen. The returned buffer is selected with `switch-to-buffer'." (let ((method `(lambda () (let ((buffer (progn ,@body))) + ;; Need a guard here, for when buffer evals to NIL + ;; This is possible if nothing is selected that is 'CIDER' based. + ;; An appropriate fix would be to then just pull the 'last' buffer of a given type. + ;; The buffer list tends to be sorted by last visit, so we can leverage that perhaps. + (when (not buffer) (message "Darn!")) (cond ((not (get-buffer buffer)) (message "No such buffer: %S" buffer) (ding)) From e8ab9e757303e25d9e4d85d97a1e0f70297afc8b Mon Sep 17 00:00:00 2001 From: Matthew Carter Date: Fri, 13 Sep 2019 22:38:39 -0400 Subject: [PATCH 2/4] 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 (based on the last cider REPL mode). - 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. --- cider-selector.el | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cider-selector.el b/cider-selector.el index 56358df40..2d74851b4 100644 --- a/cider-selector.el +++ b/cider-selector.el @@ -57,7 +57,10 @@ Only considers buffers that are not already visible." (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))) + ;; Why not? If we have a window split and want to use cider-selector + ;; to jump between the latest clojure / REPL? + ;; (null (get-buffer-window buffer 'visible)) + ) return buffer finally (error "Can't find unshown buffer in %S" mode))) @@ -97,12 +100,8 @@ is chosen. The returned buffer is selected with `switch-to-buffer'." (let ((method `(lambda () (let ((buffer (progn ,@body))) - ;; Need a guard here, for when buffer evals to NIL - ;; This is possible if nothing is selected that is 'CIDER' based. - ;; An appropriate fix would be to then just pull the 'last' buffer of a given type. - ;; The buffer list tends to be sorted by last visit, so we can leverage that perhaps. - (when (not buffer) (message "Darn!")) - (cond ((not (get-buffer buffer)) + (cond ((or (not buffer) + (not (get-buffer buffer))) (message "No such buffer: %S" buffer) (ding)) ((get-buffer-window buffer) @@ -144,7 +143,8 @@ is chosen. The returned buffer is selected with (def-cider-selector-method ?r "Current REPL buffer." - (cider-current-repl)) + (or (cider-current-repl) + (cider-selector--recently-visited-buffer 'cider-repl-mode))) (def-cider-selector-method ?m "Current connection's *nrepl-messages* buffer." From 7b2966e3f5ace5074442cee0f6fcb257035d6417 Mon Sep 17 00:00:00 2001 From: Matthew Carter Date: Fri, 13 Sep 2019 23:04:43 -0400 Subject: [PATCH 3/4] Further tuning of the selector handling --- CHANGELOG.md | 4 ++++ cider-selector.el | 20 +++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff8221d3f..29203ac8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Changes + +* `cider-selector` has more robust handling for edge cases. + ## 0.22.0 (2019-09-01) ### New features diff --git a/cider-selector.el b/cider-selector.el index 2d74851b4..555dbddc0 100644 --- a/cider-selector.el +++ b/cider-selector.el @@ -49,20 +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))) - ;; Why not? If we have a window split and want to use cider-selector - ;; to jump between the latest clojure / REPL? - ;; (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) @@ -100,8 +103,7 @@ is chosen. The returned buffer is selected with `switch-to-buffer'." (let ((method `(lambda () (let ((buffer (progn ,@body))) - (cond ((or (not buffer) - (not (get-buffer buffer))) + (cond ((not (and buffer (get-buffer buffer))) (message "No such buffer: %S" buffer) (ding)) ((get-buffer-window buffer) From 7e4de5caa4ca67c9f4a4f9b679e9e92a5549c509 Mon Sep 17 00:00:00 2001 From: Matthew Carter Date: Sat, 14 Sep 2019 22:25:09 -0400 Subject: [PATCH 4/4] Update docstring --- cider-selector.el | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cider-selector.el b/cider-selector.el index 555dbddc0..b0d449679 100644 --- a/cider-selector.el +++ b/cider-selector.el @@ -144,7 +144,8 @@ is chosen. The returned buffer is selected with (top-level)) (def-cider-selector-method ?r - "Current REPL buffer." + "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)))