Skip to content

Commit 761d6bb

Browse files
author
dan sutton
committed
Scroll buffer after using cider-insert-X-in-repl
As it stood, buffer would not scroll, even if you included a `(goto-char (point-max))` call at the end. Using solution from https://stackoverflow.com/questions/13530025/emacs-scroll-automatically-when-inserting-text Have to watch out if the buffer is visible or not. If it is, we can scroll it with `with-selected-window`. Otherwise, the best we can do is the current behavior. Note `get-buffer-window` returns nil if something is not visible which is why we need to branch on it.
1 parent 2b089d7 commit 761d6bb

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

cider-mode.el

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ Clojure buffer."
121121
(defun cider-switch-to-last-clojure-buffer ()
122122
"Switch to the last Clojure buffer.
123123
The default keybinding for this command is
124-
the same as `cider-switch-to-repl-buffer',
124+
the same as variable `cider-switch-to-repl-buffer',
125125
so that it is very convenient to jump between a
126126
Clojure buffer and the REPL buffer."
127127
(interactive)
@@ -232,20 +232,34 @@ and eval and the prefix is required to prevent evaluation."
232232
:group 'cider
233233
:package-version '(cider . "0.18.0"))
234234

235+
(defun cider--insert-in-repl (form eval)
236+
"Internal function to insert FORM into a repl buffer.
237+
Will eval if EVAL is truthy or `cider-invert-insert-eval-p' is truthy and
238+
EVAL is nil. Extracted so that the same actions can be performed with
239+
slightly different invocation depending upon if buffer is visible or not."
240+
(goto-char (point-max))
241+
(let ((beg (point)))
242+
(insert form)
243+
(indent-region beg (point)))
244+
(when (if cider-invert-insert-eval-p
245+
(not eval)
246+
eval)
247+
(cider-repl-return))
248+
(goto-char (point-max)))
249+
235250
(defun cider-insert-in-repl (form eval)
236251
"Insert FORM in the REPL buffer and switch to it.
237252
If EVAL is non-nil the form will also be evaluated."
238253
(while (string-match "\\`[ \t\n\r]+\\|[ \t\n\r]+\\'" form)
239254
(setq form (replace-match "" t t form)))
240-
(with-current-buffer (cider-current-repl)
241-
(goto-char (point-max))
242-
(let ((beg (point)))
243-
(insert form)
244-
(indent-region beg (point)))
245-
(when (if cider-invert-insert-eval-p
246-
(not eval)
247-
eval)
248-
(cider-repl-return)))
255+
(let ((repl (cider-current-repl)))
256+
(if-let* ((window (get-buffer-window repl)))
257+
;; if buffer is visible we can scroll it
258+
(with-selected-window window
259+
(cider--insert-in-repl form eval))
260+
;; if not visible, we can only insert into it
261+
(with-current-buffer repl
262+
(cider--insert-in-repl form eval))))
249263
(when cider-switch-to-repl-after-insert-p
250264
(cider-switch-to-repl-buffer)))
251265

0 commit comments

Comments
 (0)