Skip to content

Commit d453656

Browse files
committed
Handle newlines between forms for inf-clojure-eval-buffer
This sends the contents of the buffer. However, newlines cause the prompt to be returned. You can see this with even a regular clojure repl: ```bash ❯❯❯ clojure Clojure 1.10.2 user=> user=> user=> ``` (note using `clj` has rlwrap which hides a bit of this so make sure to use `clojure`). But what we can do is make sure to transform ```clojure (defn foo [] ...) (defn bar [] ...) ``` into ```clojure (defn foo [] ...) (defn bar [] ...) ``` So that the newlines don't trigger more repl prompts. Real world usage below: Before: ```clojure parse=> nil parse=> parse=> nil parse=> parse=> #'parse/data parse=> parse=> #'parse/parse-where parse=> parse=> #'parse/keywords parse=> parse=> #'parse/tokenize parse=> parse=> #'parse/parse parse=> parse=> #'parse/translate-where parse=> parse=> nil parse=> parse=> #'parse/query parse=> parse=> #'parse/query-test parse=> parse=> #'parse/bonus-points-test parse=> ``` After: ```clojure user=> nil parse=> nil parse=> #'parse/data parse=> #'parse/parse-where parse=> #'parse/keywords parse=> parse=> #'parse/parse parse=> #'parse/translate-where parse=> nil parse=> #'parse/query parse=> #'parse/query-test parse=> #'parse/bonus-points-test parse=> ```
1 parent f4a279e commit d453656

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

inf-clojure.el

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -688,11 +688,27 @@ HOST is the host the process is running on, PORT is where it's listening."
688688
Sends substring between START and END. Prefix argument AND-GO
689689
means switch to the Clojure buffer afterwards."
690690
(interactive "r\nP")
691-
;; drops newlines at the end of the region
692-
(let ((str (replace-regexp-in-string
693-
"[\n]+\\'" ""
694-
(buffer-substring-no-properties start end))))
695-
(inf-clojure--send-string (inf-clojure-proc) str))
691+
(let* ((str (buffer-substring-no-properties start end))
692+
;; newlines over a socket repl between top level forms cause
693+
;; a prompt to be returned. so here we dump the region into a
694+
;; temp buffer, and delete all newlines between the forms
695+
(formatted (condition-case nil
696+
(with-temp-buffer
697+
(progn
698+
(clojurec-mode)
699+
(insert str)
700+
(whitespace-cleanup)
701+
(goto-char (point-min))
702+
(while (not (eobp))
703+
(while (looking-at "\n")
704+
(delete-char 1))
705+
(unless (eobp)
706+
(clojure-forward-logical-sexp))
707+
(unless (eobp)
708+
(forward-char)))
709+
(buffer-substring-no-properties (point-min) (point-max))))
710+
(scan-error str))))
711+
(inf-clojure--send-string (inf-clojure-proc) formatted))
696712
(when and-go (inf-clojure-switch-to-repl t)))
697713

698714
(defun inf-clojure-eval-string (code)

0 commit comments

Comments
 (0)