jupyter-repl-available-repl-buffers: Refactor

Also give an option to return the first found REPL buffer
that can be used for a major mode.
This commit is contained in:
Nathaniel Nicandro 2018-10-10 00:17:44 -05:00
parent 6c836bd3d8
commit 4a7e95eb18

View file

@ -2473,19 +2473,21 @@ it."
(pop-to-buffer (current-buffer))) (pop-to-buffer (current-buffer)))
(error "Buffer not associated with a REPL, see `jupyter-repl-associate-buffer'"))) (error "Buffer not associated with a REPL, see `jupyter-repl-associate-buffer'")))
(defun jupyter-repl-available-repl-buffers (&optional mode) (defun jupyter-repl-available-repl-buffers (&optional mode first)
"Return a list of REPL buffers that are connected to live kernels. "Return a list of REPL buffers that are connected to live kernels.
If MODE is non-nil, return all REPL buffers whose If MODE is non-nil, return all REPL buffers whose
`jupyter-repl-lang-mode' is MODE." `jupyter-repl-lang-mode' is MODE.
(delq
nil If FIRST is non-nil, only return the first REPL buffer that matches."
(mapcar (lambda (b) (cl-loop
(with-current-buffer b for buf in (buffer-list)
(and (eq major-mode 'jupyter-repl-mode) for match = (with-current-buffer buf
(if mode (eq mode jupyter-repl-lang-mode) t) (and (eq major-mode 'jupyter-repl-mode)
(jupyter-repl-connected-p) (if mode (eq mode jupyter-repl-lang-mode) t)
(buffer-name b)))) (jupyter-repl-connected-p)
(buffer-list)))) (buffer-name)))
if (and match first) return buf
else if match collect buf))
;;;###autoload ;;;###autoload
(defun jupyter-repl-associate-buffer (client) (defun jupyter-repl-associate-buffer (client)