Skip to content

Commit

Permalink
Use repl buffer as connection buffer
Browse files Browse the repository at this point in the history
  Ancillary changes:

 - new functions `nrepl-start-client-process' and `nrepl-start-server-process'.
 - `cider-init-repl-buffer' and `cider-create-repl-buffer' have clear cut
    separation of functionality
 - `nrepl-make-buffer-name` and all `cider-create-xxx-buffer-name` take explicit
    dir, port and host variables.
 - increase `nrepl-decode-timeout` to 0.025 for greater reliability
 - merge `nrepl--handle-process-output` into client process filter
 - remove `nrepl-connect' in favor of `nrepl-start-client-process'
  • Loading branch information
vspinu committed Aug 25, 2014
1 parent b58ba0a commit 09dcec9
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 190 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ are used to translate filenames from/to the nREPL server (default Cygwin impleme

### Changes

* REPL buffers are now connection buffers for REPL client connections.
* Server and client cranking were isolated into `nrepl-start-server-process` and
`nrepl-start-client-process`.

* nrepl-client.el refactoring:

- `nrepl-send-request-sync` was renamed into `nrepl-send-sync-request` to comply
Expand Down
4 changes: 2 additions & 2 deletions cider-interaction.el
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Signal an error if it is not supported."
(unless (nrepl-op-supported-p op)
(error "Can't find nREPL middleware providing op \"%s\". Please, install (or update) cider-nrepl %s and restart CIDER" op cider-version)))

(defun cider-verify-required-nrepl-ops ()
(defun cider--check-required-nrepl-ops ()
"Check whether all required nREPL ops are present."
(let ((missing-ops (-remove 'nrepl-op-supported-p cider-required-nrepl-ops)))
(when missing-ops
Expand Down Expand Up @@ -317,7 +317,7 @@ of the namespace in the Clojure source buffer."
(let ((buffer (current-buffer)))
(when (eq 4 arg)
(cider-repl-set-ns (cider-current-ns)))
(pop-to-buffer (cider-find-or-create-repl-buffer))
(pop-to-buffer (cider-get-repl-buffer))
(cider-remember-clojure-buffer buffer)
(goto-char (point-max))))

Expand Down
98 changes: 47 additions & 51 deletions cider-repl.el
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
(defvar paredit-version)
(defvar paredit-space-for-delimiter-predicates))


(defgroup cider-repl nil
"Interaction with the REPL."
:prefix "cider-repl-"
Expand Down Expand Up @@ -115,6 +116,7 @@ you'd like to use the default Emacs behavior use
:type 'symbol
:group 'cider-repl)


;;;; REPL buffer local variables
(defvar-local cider-repl-input-start-mark nil)

Expand Down Expand Up @@ -151,31 +153,42 @@ joined together.")
(set markname (make-marker))
(set-marker (symbol-value markname) (point))))


;;; REPL init
(defun cider-repl-buffer-name ()
"Generate a REPL buffer name based on current connection buffer."
(with-current-buffer (get-buffer (nrepl-current-connection-buffer))
(nrepl-make-buffer-name nrepl-repl-buffer-name-template)))

(defun cider-create-repl-buffer ()
"Create a REPL buffer."
(cider-init-repl-buffer
(let ((buffer-name (cider-repl-buffer-name)))
(if cider-repl-display-in-current-window
(add-to-list 'same-window-buffer-names buffer-name))
(if cider-repl-pop-to-buffer-on-connect
(pop-to-buffer buffer-name)
(generate-new-buffer buffer-name))
buffer-name)))

(defun cider-make-repl (process)
"Make a REPL for the connection PROCESS."
(let ((connection-buffer (process-buffer process))
(repl-buffer (cider-create-repl-buffer)))
(with-current-buffer repl-buffer
(setq nrepl-connection-buffer (buffer-name connection-buffer)))
(with-current-buffer connection-buffer
(setq nrepl-repl-buffer (buffer-name repl-buffer)))))
(defun cider-repl-buffer-name (&optional project-dir host port)
"Generate a REPL buffer name based on current connection buffer.
PROJECT-DIR, PORT and HOST are as in `nrepl-make-buffer-name'."
(with-current-buffer (or (get-buffer (nrepl-current-connection-buffer))
(current-buffer))
(nrepl-make-buffer-name nrepl-repl-buffer-name-template project-dir host port)))

(defun cider-repl-create (&optional project-dir host port)
"Create a REPL buffer and install `cider-repl-mode'.
PROJECT-DIR, PORT and HOST are as in `nrepl-make-buffer-name'."
;; Connection might not have been set as yet. Please don't send requests here.
(let ((buf (nrepl-make-buffer-name nrepl-repl-buffer-name-template
project-dir host port)))
(with-current-buffer (get-buffer-create buf)
(unless (derived-mode-p 'cider-repl-mode)
(cider-repl-mode))
(cider-repl-reset-markers))
buf))

(defun cider-repl-init (buffer &optional no-banner)
"Initialize the REPL in BUFFER.
BUFFER must be a REPL buffer with `cider-repl-mode' and a running
clienprocessL connection. Unless NO-BANNER is non-nil, insert a banner."
(with-current-buffer buffer
;; honor :init-ns from lein's :repl-options on startup
(setq nrepl-buffer-ns (cider-eval-and-get-value "(str *ns*)"))
(unless no-banner
(cider-repl--insert-banner-and-prompt nrepl-buffer-ns))
(when cider-repl-display-in-current-window
(add-to-list 'same-window-buffer-names buf))
(when cider-repl-pop-to-buffer-on-connect
(pop-to-buffer buffer))
(cider-remember-clojure-buffer cider-current-clojure-buffer)
buffer))

(defun cider-repl--banner ()
"Generate the welcome REPL buffer banner."
Expand All @@ -194,33 +207,14 @@ joined together.")
(cider-repl--mark-input-start)
(cider-repl--insert-prompt ns))

(defun cider-init-repl-buffer (buffer &optional noprompt)
"Initialize the REPL in BUFFER.
Insert a banner, unless NOPROMPT is non-nil."
(with-current-buffer buffer
(unless (eq major-mode 'cider-repl-mode)
(cider-repl-mode))
(cider-repl-reset-markers)
(unless noprompt
(cider-repl--insert-banner-and-prompt nrepl-buffer-ns))
(cider-remember-clojure-buffer cider-current-clojure-buffer)
(current-buffer)))

(defun cider-find-or-create-repl-buffer ()
"Return the REPL buffer, create it if necessary."
(let ((buffer (cider-current-repl-buffer)))
(if (null buffer)
(error "No active nREPL connection")
(let ((buffer (get-buffer buffer)))
(or (when (buffer-live-p buffer) buffer)
(let ((buffer (nrepl-current-connection-buffer)))
(if (null buffer)
(error "No active nREPL connection")
(cider-init-repl-buffer
(get-process buffer)
(get-buffer-create
(cider-repl-buffer-name))))))))))
(defun cider-get-repl-buffer ()
"Return the REPL buffer for current connection."
(let ((buffer (get-buffer-create (cider-current-repl-buffer))))
(if (buffer-live-p buffer)
buffer
(error "No active REPL"))))


;;; REPL interaction

(defun cider-repl--in-input-area-p ()
Expand Down Expand Up @@ -685,6 +679,7 @@ namespace to switch to."
(cider-repl-handler (current-buffer))))
(error "Cannot determine the current namespace")))


;;;;; History

(defcustom cider-repl-wrap-history nil
Expand Down Expand Up @@ -904,6 +899,7 @@ constructs."
(append (cl-subseq session-hist 0 n-added-items)
file-hist))


;;; REPL shortcuts
(defcustom cider-repl-shortcut-dispatch-char ?\,
"Character used to distinguish REPL commands from Lisp forms."
Expand Down Expand Up @@ -952,7 +948,7 @@ constructs."
(call-interactively (gethash command cider-repl-shortcuts))
(error "No command selected")))))



;;;;; CIDER REPL mode

;;; Prevent paredit from inserting some inappropriate spaces.
Expand Down
6 changes: 3 additions & 3 deletions cider-selector.el
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

(require 'cider-client)
(require 'cider-interaction)
(require 'cider-repl) ; for cider-find-or-create-repl-buffer
(require 'cider-repl) ; for cider-get-repl-buffer

(defconst cider-selector-help-buffer "*Selector Help*"
"The name of the selector's help buffer.")
Expand Down Expand Up @@ -135,7 +135,7 @@ is chosen. The returned buffer is selected with

(def-cider-selector-method ?r
"Current REPL buffer."
(cider-find-or-create-repl-buffer))
(cider-get-repl-buffer))

(def-cider-selector-method ?n
"Connections browser buffer."
Expand All @@ -153,7 +153,7 @@ is chosen. The returned buffer is selected with
(def-cider-selector-method ?s
"Cycle to the next CIDER connection's REPL."
(cider-rotate-connection)
(cider-find-or-create-repl-buffer))
(cider-get-repl-buffer))

(provide 'cider-selector)

Expand Down
36 changes: 13 additions & 23 deletions cider.el
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,17 @@ start the server."
(let* ((project (when prompt-project
(read-directory-name "Project: ")))
(project-dir (nrepl-project-directory-for
(or project (nrepl-current-dir)))))
(or project (nrepl-current-dir))))
(lein-params (if prompt-project
(read-string (format "nREPL server command: %s "
cider-lein-command)
cider-lein-parameters)
cider-lein-parameters))
(cmd (format "%s %s" cider-lein-command lein-params)))
(when (nrepl-check-for-repl-buffer nil project-dir)
(let* ((nrepl-project-dir project-dir)
(lein-params (if prompt-project
(read-string (format "nREPL server command: %s " cider-lein-command) cider-lein-parameters)
cider-lein-parameters))
(cmd (format "%s %s" cider-lein-command lein-params))
(default-directory (or project-dir default-directory))
(serv-buf-name (generate-new-buffer-name (nrepl-server-buffer-name)))
(process
(progn
;; the buffer has to be created before the proc:
(get-buffer-create serv-buf-name)
(start-file-process-shell-command "nrepl-server" serv-buf-name cmd))))
(set-process-filter process 'nrepl-server-filter)
(set-process-sentinel process 'nrepl-server-sentinel)
(set-process-coding-system process 'utf-8-unix 'utf-8-unix)
(with-current-buffer (process-buffer process)
(setq nrepl-project-dir project-dir))
(message "Starting nREPL server via %s..."
(propertize cmd 'face 'font-lock-keyword-face)))))
(message "The %s executable (specified by `cider-lein-command') isn't on your exec-path" cider-lein-command)))
(nrepl-start-server-process project-dir cmd)))
(message "The %s executable (specified by `cider-lein-command') isn't on your exec-path"
cider-lein-command)))

(defun cider-known-endpoint-candidates ()
"Known endpoint candidates for establishing an nREPL connection.
Expand All @@ -162,7 +151,8 @@ The returned endpoint has the label removed."

;;;###autoload
(defun cider-connect (host port)
"Connect to an nREPL server identified by HOST and PORT."
"Connect to an nREPL server identified by HOST and PORT.
Create REPL buffer and start an nREPL client connection."
(interactive (let ((known-endpoint (when cider-known-endpoints
(cider-select-known-endpoint))))
(list (or (car known-endpoint)
Expand All @@ -171,7 +161,7 @@ The returned endpoint has the label removed."
(read-string "Port: " port nil port))))))
(setq cider-current-clojure-buffer (current-buffer))
(when (nrepl-check-for-repl-buffer `(,host ,port) nil)
(nrepl-connect host port)))
(nrepl-start-client-process default-directory host port t)))

(define-obsolete-function-alias
'cider
Expand Down
Loading

0 comments on commit 09dcec9

Please sign in to comment.