Skip to content

Commit 4a87862

Browse files
arichiardibbatsov
authored andcommitted
Calculate response boundaries explicitly
Change the way the response is calculated, not by search-forward the buffer used for redirection but by parsing the raw response. Taking into consideration the optional regexps, a list is calculated with beginning, end and prompt position (the logic is now in inf-clojure--string-boundaries, which can be unit tested). This implicitly solves a hidden bug in inf-clojure--process-response, which was not able to identify the prompt in some cases because of the many search-forward. Finally, this new information is logged so that bugs can be detected more easily.
1 parent f7ec13a commit 4a87862

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

inf-clojure.el

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -977,16 +977,27 @@ STRING if present."
977977
'append
978978
'no-annoying-write-file-in-minibuffer)))
979979

980+
(defun inf-clojure--string-boundaries (string prompt &optional beg-regexp end-regexp)
981+
"Calculate the STRING boundaries, including PROMPT.
982+
Return a list of positions (beginning end prompt). If the
983+
optional BEG-REGEXP and END-REGEXP are present, the boundaries
984+
are going to match those."
985+
(list (or (and beg-regexp (string-match beg-regexp string)) 0)
986+
(or (and end-regexp (when (string-match end-regexp string)
987+
(match-end 0)))
988+
(length string))
989+
(or (string-match prompt string) (length string))))
990+
980991
;; Originally from:
981992
;; https://github.com/glycerine/lush2/blob/master/lush2/etc/lush.el#L287
982-
(defun inf-clojure--process-response (command process &optional beg-string end-string)
993+
(defun inf-clojure--process-response (command process &optional beg-regexp end-regexp)
983994
"Send COMMAND to PROCESS and return the response.
984-
Return the result of COMMAND starting with BEG-STRING and ending
985-
with END-STRING if non-nil. If BEG-STRING is nil, the result
986-
string will start from (point) in the results buffer. If
987-
END-STRING is nil, the result string will end at (point-max) in
988-
the results buffer. It cuts out the output from and including
989-
the `inf-clojure-prompt`."
995+
Return the result of COMMAND, filtering it from BEG-REGEXP to the
996+
end of the matching END-REGEXP if non-nil.
997+
If BEG-REGEXP is nil, the result string will start from (point)
998+
in the results buffer. If END-REGEXP is nil, the result string
999+
will end at (point-max) in the results buffer. It cuts out the
1000+
output from and including the `inf-clojure-prompt`."
9901001
(inf-clojure--log-string command "----CMD->")
9911002
(let ((work-buffer inf-clojure--redirect-buffer-name))
9921003
(save-excursion
@@ -1002,17 +1013,16 @@ the `inf-clojure-prompt`."
10021013
;; Collect the output
10031014
(set-buffer work-buffer)
10041015
(goto-char (point-min))
1005-
(let* ((beg (or (when (and beg-string (search-forward beg-string nil t))
1006-
(match-beginning 0))
1007-
(point-min)))
1008-
(end (or (when end-string
1009-
(search-forward end-string nil t))
1010-
(point-max)))
1011-
(prompt (when (search-forward inf-clojure-prompt nil t)
1012-
(match-beginning 0)))
1013-
(buffer-string (buffer-substring-no-properties beg (or prompt end))))
1014-
(inf-clojure--log-string buffer-string "<-RES----")
1015-
buffer-string))))
1016+
(let* ((buffer-string (buffer-substring-no-properties (point-min) (point-max)))
1017+
(boundaries (inf-clojure--string-boundaries buffer-string inf-clojure-prompt beg-regexp end-regexp))
1018+
(beg-pos (car boundaries))
1019+
(end-pos (car (cdr boundaries)))
1020+
(prompt-pos (car (cdr (cdr boundaries))))
1021+
(response-string (substring buffer-string beg-pos (min end-pos prompt-pos))))
1022+
(inf-clojure--log-string buffer-string "<-BUF----")
1023+
(inf-clojure--log-string boundaries "<-BND----")
1024+
(inf-clojure--log-string response-string "<-RES----")
1025+
response-string))))
10161026

10171027
(defun inf-clojure--nil-string-match-p (string)
10181028
"Return true iff STRING is not nil.
@@ -1044,7 +1054,7 @@ readable sexp only."
10441054

10451055
(defun inf-clojure--process-response-match-p (match-p proc form)
10461056
"Eval MATCH-P on the response of sending to PROC the input FORM.
1047-
Note that this function will add a \n to the end (or )f the string
1057+
Note that this function will add a \n to the end of the string
10481058
for evaluation, therefore FORM should not include it."
10491059
(when-let ((response (inf-clojure--process-response form proc)))
10501060
(funcall match-p response)))

0 commit comments

Comments
 (0)