diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5f0eb40a8..e9ad270ae 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,7 @@
### New features
+* [#1127](https://github.com/clojure-emacs/cider/issues/1127): Make it possible to associate a buffer with a connection (via `cider-assoc-buffer-with-connection`).
* [#1217](https://github.com/clojure-emacs/cider/issues/1217): Add new command `cider-assoc-project-with-connection` to associate a project directory with a connection.
* [#1248](https://github.com/clojure-emacs/cider/pull/1248): Add TAB and RET keys to the test-report buffer.
* [#1245](https://github.com/clojure-emacs/cider/pull/1245): New variable, `cider-ovelays-use-font-lock` controls whether results overlay should be font-locked or just use a single face.
diff --git a/cider-interaction.el b/cider-interaction.el
index 62fd6a12e..0190002c4 100644
--- a/cider-interaction.el
+++ b/cider-interaction.el
@@ -245,6 +245,12 @@ should be extracted from the buffer's ns form.")
(defvar-local cider-repl-type nil
"The type of this REPL buffer, usually either \"clj\" or \"cljs\".")
+(defvar-local cider-buffer-connection nil
+ "A connection associated with a specific buffer.
+
+If this is set to a non-nil value it will take precedence over both
+the project associated with a connection and the default connection.")
+
(defun cider-ensure-op-supported (op)
"Check for support of middleware op OP.
Signal an error if it is not supported."
@@ -482,18 +488,37 @@ such a link cannot be established automatically."
(with-current-buffer conn-buf
(setq nrepl-project-dir project-dir)))))
+(defun cider-assoc-buffer-with-connection ()
+ "Associate the current buffer with a connection.
+
+Useful for connections created using `cider-connect', as for them
+such a link cannot be established automatically."
+ (interactive)
+ (cider-ensure-connected)
+ (let ((conn (completing-read "Connection: " (nrepl-connection-buffers))))
+ (when conn
+ (setq-local cider-buffer-connection conn))))
+
+(defun cider-clear-buffer-local-connection ()
+ "Remove association between the current buffer and a connection."
+ (interactive)
+ (cider-ensure-connected)
+ (setq-local cider-buffer-connection nil))
+
(defun cider-find-relevant-connection ()
"Try to find the matching REPL buffer for the current Clojure source buffer.
If succesful, return the new connection buffer."
(interactive "P")
(cider-ensure-connected)
- (let* ((project-directory (clojure-project-dir (cider-current-dir)))
- (connection-buffer
- (or
- (and (= 1 (length nrepl-connection-list)) (car nrepl-connection-list))
- (and project-directory
- (cider-find-connection-buffer-for-project-directory project-directory)))))
- connection-buffer))
+ (if cider-buffer-connection
+ cider-buffer-connection
+ (let* ((project-directory (clojure-project-dir (cider-current-dir)))
+ (connection-buffer
+ (or
+ (and (= 1 (length nrepl-connection-list)) (car nrepl-connection-list))
+ (and project-directory
+ (cider-find-connection-buffer-for-project-directory project-directory)))))
+ connection-buffer)))
(defun cider-switch-to-relevant-repl-buffer (&optional arg)
"Select the REPL buffer, when possible in an existing window.