Skip to content

Commit 62af115

Browse files
committed
[Fix #63] Avoid spurious output by using buffer redirection
1 parent b50102f commit 62af115

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
## master (unreleased)
44

55
### New Features
6-
* [#57](https://github.com/clojure-emacs/inf-clojure/pull/68): Add `inf-clojure-connect`
6+
* [#63](https://github.com/clojure-emacs/inf-clojure/pull/69): Fix spurious process output on init.
7+
* [#57](https://github.com/clojure-emacs/inf-clojure/pull/68): Add `inf-clojure-connect`.
78
* [#66](https://github.com/clojure-emacs/inf-clojure/pull/56): Add Planck support.
89
* [#51](https://github.com/clojure-emacs/inf-clojure/pull/51): Commands do not prompt by default anymore, unless they receive a non-nil prefix argument.
910
* [#44](https://github.com/clojure-emacs/inf-clojure/pull/44): Add REPL types and Lumo support.

inf-clojure.el

+45-27
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ It requires a REPL PROC for inspecting the correct type."
215215
(setq inf-clojure-repl-type (inf-clojure--detect-repl-type proc))
216216
inf-clojure-repl-type))
217217

218+
(defun inf-clojure--sanitize-command (command)
219+
"Sanitize COMMAND for sending it to a process.
220+
An example of things that this function does is to add a final
221+
newline at the end of the form."
222+
(concat (string-trim-right command) "\n"))
223+
218224
(defun inf-clojure--send-string (proc string)
219225
"A custom `comint-input-sender` / `comint-send-string`.
220226
It performs the required side effects on every send for PROC and
@@ -884,22 +890,47 @@ safe to call only from inside `inf-clojure-arglist`."
884890
(and (string-match "(.+)" string input-end) (match-string 0 string))))
885891
(_ (and (string-match "(.+)" string) (match-string 0 string)))))
886892

893+
;; Originally from:
894+
;; https://github.com/glycerine/lush2/blob/master/lush2/etc/lush.el#L287
895+
(defun inf-clojure-results-from-process (process command &optional beg-string end-string)
896+
"Send COMMAND to PROCESS.
897+
Return the result of COMMAND starting with BEG-STRING and ending
898+
with END-STRING if non-nil. If BEG-STRING is nil, the result
899+
string will start from (point) in the results buffer. If
900+
END-STRING is nil, the result string will end at (point-max) in
901+
the results buffer. It cuts out the output from
902+
`inf-clojure-prompt` onwards unconditionally."
903+
(let ((work-buffer " *Inf-Clojure Redirect Work Buffer*"))
904+
(save-excursion
905+
(set-buffer (get-buffer-create work-buffer))
906+
(erase-buffer)
907+
(comint-redirect-send-command-to-process
908+
(inf-clojure--sanitize-command command) work-buffer process nil t)
909+
;; Wait for the process to complete
910+
(set-buffer (process-buffer process))
911+
(while (null comint-redirect-completed)
912+
(accept-process-output nil 1))
913+
;; Collect the output
914+
(set-buffer work-buffer)
915+
(goto-char (point-min))
916+
;; Skip past the command, if it was echoed
917+
(and (looking-at command)
918+
(forward-line))
919+
(let* ((beg (if beg-string
920+
(progn (search-forward beg-string nil t) (match-beginning 0))
921+
(point)))
922+
(end (if end-string
923+
(search-forward end-string nil t)
924+
(point-max)))
925+
(buffer-string (buffer-substring-no-properties beg end)))
926+
(when (and buffer-string (string-match inf-clojure-prompt buffer-string))
927+
(substring buffer-string 0 (match-beginning 0)))))))
928+
887929
(defun inf-clojure-arglist (fn)
888930
"Send a query to the inferior Clojure for the arglist for function FN.
889931
See variable `inf-clojure-arglist-form'."
890-
(let* ((proc (inf-clojure-proc))
891-
(comint-filt (process-filter proc))
892-
(kept "")
893-
eldoc)
894-
(set-process-filter proc (lambda (_proc string) (setq kept (concat kept string))))
895-
(unwind-protect
896-
(let ((eldoc-snippet (format (inf-clojure-arglists-form) fn)))
897-
(inf-clojure--send-string proc eldoc-snippet)
898-
(while (and (not (string-match inf-clojure-prompt kept))
899-
(accept-process-output proc 2)))
900-
(setq eldoc (inf-clojure-match-arglists eldoc-snippet kept)))
901-
(set-process-filter proc comint-filt))
902-
eldoc))
932+
(let ((eldoc-snippet (format (inf-clojure-arglists-form) fn)))
933+
(inf-clojure-results-from-process (inf-clojure-proc) eldoc-snippet)))
903934

904935
(defun inf-clojure-show-arglist (prompt-for-symbol)
905936
"Show the arglist for function FN in the mini-buffer.
@@ -1145,20 +1176,7 @@ to suppress the usage of the target buffer discovery logic."
11451176
"Return MATCH-P on the result of sending FORM to PROC.
11461177
Note that this function will add a \n to the end of the string
11471178
for evaluation, therefore FORM should not include it."
1148-
(let* ((kept "")
1149-
(is-matching nil)
1150-
(prev-filter (process-filter proc)))
1151-
(set-process-filter proc (lambda (_ string) (setq kept (concat kept string))))
1152-
(unwind-protect
1153-
;; use the real comind-send-string in order to avoid stack overflows
1154-
(comint-send-string proc (concat form "\n"))
1155-
;; yey, loads of procedural code again
1156-
(while (and (not is-matching)
1157-
(not (string-match inf-clojure-prompt kept))
1158-
(accept-process-output proc 2))
1159-
(setq is-matching (funcall match-p kept)))
1160-
(set-process-filter proc prev-filter))
1161-
is-matching))
1179+
(funcall match-p (inf-clojure-results-from-process proc form nil)))
11621180

11631181
;;;; Lumo
11641182
;;;; ====

0 commit comments

Comments
 (0)