Skip to content

Function to select repls as the current active connection #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

### New features

* [#190](https://github.com/clojure-emacs/inf-clojure/pull/190): Helper function `inf-clojure-set-repl` to select inf-clojure process buffer.

### Bugs fixed

* [#152](https://github.com/clojure-emacs/inf-clojure/issues/152): Sanitize should only remove whitespace at the end of a command.
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,17 @@ one process, this does the right thing. If you run multiple
processes, you might need to change `inf-clojure-buffer` to
whichever process buffer you want to use.

You can use the helpful function `inf-clojure-set-repl`. If called in
an inf-clojure repl buffer, it will assign that buffer as the current
connection (`(setq inf-clojure-buffer (current-buffer)`). If you are
not in an inf-clojure repl buffer, it will offer a choice of
acceptable buffers to set as the repl buffer. If called with a prefix,
it will always give the list even if you are currently in an
acceptable repl buffer. Renaming buffers will greatly improve the
functionality of this list; the list "project-1: clojure repl",
"project-2: cljs repl" is far more understandable than "inf-clojure",
"inf-clojure<2>".

#### REPL Type

An `inf-clojure` REPL has an associated type. The available types can be
Expand Down
62 changes: 60 additions & 2 deletions inf-clojure.el
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,38 @@ has been found. See also variable `inf-clojure-buffer'."
(unless no-error
(error "No Clojure subprocess; see variable `inf-clojure-buffer'"))))

(defun inf-clojure-repl-p ()
"Indicates if current buffer is an inf-clojure repl.
Checks the mode and that there is a live process."
(and (derived-mode-p 'inf-clojure-mode)
(get-buffer-process (current-buffer))
(process-live-p (get-buffer-process (current-buffer)))))

(defun inf-clojure-repls-list ()
"Return a list of all known inf-clojure repls."
(let (repl-buffers)
(dolist (b (buffer-list))
(with-current-buffer b
(when (inf-clojure-repl-p)
(push (buffer-name b) repl-buffers))))
repl-buffers))

(defun inf-clojure-set-repl (always-ask)
"Set an inf clojure buffer as the active repl.
If in a repl already, use that unless a prefix is used (or
ALWAYS-ASK). Otherwise get a list of all active inf-clojure
repls and offer a choice. Recommended to rename buffers as they
are created with `rename-buffer`."
(interactive "P")
(if (and (not always-ask)
(inf-clojure-repl-p))
(setq inf-clojure-buffer (current-buffer))
(let ((repl-buffers (inf-clojure-repls-list)))
(if (> (length repl-buffers) 0)
(when-let ((repl-buffer (completing-read "Use for repl: " repl-buffers nil t)))
(setq inf-clojure-buffer (get-buffer repl-buffer)))
(user-error "No buffers have an inf-clojure process")))))

(defvar-local inf-clojure-repl-type nil
"Symbol to define your REPL type.
Its root binding is nil and it can be further customized using
Expand Down Expand Up @@ -242,6 +274,15 @@ mode. Default is whitespace followed by 0 or 1 single-letter colon-keyword
\(as in :a, :c, etc.)"
:type 'regexp)

(defun inf-clojure--modeline-info ()
"Return modeline info.
Either \"not connected\" or \"repl-type: buffer-name\""
(if (and (bufferp inf-clojure-buffer)
(buffer-live-p inf-clojure-buffer))
(with-current-buffer inf-clojure-buffer
(format "%s: %s" inf-clojure-repl-type (buffer-name (current-buffer))))
"not connected"))

(defvar inf-clojure-mode-map
(let ((map (copy-keymap comint-mode-map)))
(define-key map (kbd "C-x C-e") #'inf-clojure-eval-last-sexp)
Expand Down Expand Up @@ -326,14 +367,31 @@ mode. Default is whitespace followed by 0 or 1 single-letter colon-keyword
["Quit REPL" inf-clojure-quit]))
map))

;;;###autoload
(defcustom inf-clojure-mode-line
'(:eval (format " inf-clojure[%s]" (inf-clojure--modeline-info)))
"Mode line lighter for cider mode.

The value of this variable is a mode line template as in
`mode-line-format'. See Info Node `(elisp)Mode Line Format' for details
about mode line templates.

Customize this variable to change how inf-clojure-minor-mode
displays its status in the mode line. The default value displays
the current connection. Set this variable to nil to disable the
mode line entirely."
:type 'sexp
:risky t)

;;;###autoload
(define-minor-mode inf-clojure-minor-mode
"Minor mode for interacting with the inferior Clojure process buffer.

The following commands are available:

\\{inf-clojure-minor-mode-map}"
:lighter "" :keymap inf-clojure-minor-mode-map
:lighter inf-clojure-mode-line
:keymap inf-clojure-minor-mode-map
(setq-local comint-input-sender 'inf-clojure--send-string)
(inf-clojure-eldoc-setup)
(make-local-variable 'completion-at-point-functions)
Expand Down Expand Up @@ -671,7 +729,7 @@ process buffer for a list of commands.)"
(inf-clojure-mode)
(setq-local inf-clojure-repl-type repl-type)
(hack-dir-local-variables-non-file-buffer))))
(setq inf-clojure-buffer "*inf-clojure*")
(setq inf-clojure-buffer (get-buffer "*inf-clojure*"))
(if inf-clojure-repl-use-same-window
(pop-to-buffer-same-window "*inf-clojure*")
(pop-to-buffer "*inf-clojure*")))
Expand Down