Skip to content

Commit b39bfb4

Browse files
committed
Streamline process response parsing
1 parent 8f431f3 commit b39bfb4

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

inf-clojure.el

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -937,9 +937,14 @@ prefix argument PROMPT-FOR-SYMBOL, it prompts for a symbol name."
937937
(inf-clojure-symbol-at-point))))
938938
(comint-proc-query (inf-clojure-proc) (format (inf-clojure-var-source-form) var))))
939939

940+
;;;; Response parsing
941+
;;;; ================
942+
943+
(defvar inf-clojure--work-buffer-name "*Inf-Clojure Work Buffer*")
944+
940945
;; Originally from:
941946
;; https://github.com/glycerine/lush2/blob/master/lush2/etc/lush.el#L287
942-
(defun inf-clojure-results-from-process (process command &optional beg-string end-string)
947+
(defun inf-clojure--process-response (process command &optional beg-string end-string)
943948
"Send to PROCESS the given COMMAND.
944949
Return the result of COMMAND starting with BEG-STRING and ending
945950
with END-STRING if non-nil. If BEG-STRING is nil, the result
@@ -973,17 +978,45 @@ the results buffer. It cuts out the output from
973978
(when (and buffer-string (string-match inf-clojure-prompt buffer-string))
974979
(substring buffer-string 0 (match-beginning 0)))))))
975980

981+
(defun inf-clojure--nil-string-match-p (string)
982+
"Return true iff STRING is not nil.
983+
This function also takes into consideration weird escape
984+
character and matches if nil is anywhere within the input
985+
string."
986+
(string-match-p "\\Ca*nil\\Ca*" string))
987+
988+
(defun inf-clojure--read-response (response)
989+
"Common post-processing of a process RESPONSE."
990+
;; The following reads the first LISP expression from the (string )
991+
(let ((read-data (when response (ignore-errors (read response)))))
992+
(cond
993+
((null read-data) nil)
994+
((and (stringp read-data) (inf-clojure--nil-string-match-p read-data)) nil)
995+
((listp read-data) read-data))))
996+
997+
(defun inf-clojure--process-response-match-p (match-p proc form)
998+
"Eval MATCH-P on the response of sending to PROC the input FORM.
999+
Note that this function will add a \n to the end (or )f the string
1000+
for evaluation, therefore FORM should not include it."
1001+
(when-let ((response (inf-clojure--process-response proc form)))
1002+
(funcall match-p response)))
1003+
1004+
(defun inf-clojure--non-nil-response-p (proc form)
1005+
"Return true iff PROC's response after evaluating FORM is not nil."
1006+
(inf-clojure--process-response-match-p
1007+
(lambda (string)
1008+
(not (inf-clojure--nil-string-match-p string)))
1009+
proc form))
1010+
1011+
;;;; Commands
1012+
;;;; ========
1013+
9761014
(defun inf-clojure-arglists (fn)
9771015
"Send a query to the inferior Clojure for the arglists for function FN.
9781016
See variable `inf-clojure-arglists-form'."
9791017
(let* ((arglists-snippet (format (inf-clojure-arglists-form) fn))
980-
(arglists-result (inf-clojure-results-from-process (inf-clojure-proc) arglists-snippet))
981-
;; The following reads the first LISP expression from the string
982-
(arglists-data (when arglists-result (ignore-errors (read arglists-result)))))
983-
(cond
984-
((null arglists-data) nil)
985-
((stringp arglists-data) arglists-data)
986-
((listp arglists-data) arglists-result))))
1018+
(arglist-response (inf-clojure--process-response (inf-clojure-proc) arglists-snippet))
1019+
(inf-clojure--read-response arglist-response))))
9871020

9881021
(defun inf-clojure-show-arglists (prompt-for-symbol)
9891022
"Show the arglists for function FN in the mini-buffer.
@@ -1051,24 +1084,9 @@ See variable `inf-clojure-buffer'."
10511084

10521085
(defun inf-clojure-completions (expr)
10531086
"Return a list of completions for the Clojure expression starting with EXPR."
1054-
(let* ((proc (inf-clojure-proc))
1055-
(comint-filt (process-filter proc))
1056-
(kept "")
1057-
completions)
1058-
(set-process-filter proc (lambda (_proc string) (setq kept (concat kept string))))
1059-
(unwind-protect
1060-
(let ((completion-snippet
1061-
(format
1062-
(inf-clojure-completion-form) (substring-no-properties expr))))
1063-
(process-send-string proc completion-snippet)
1064-
(while (and (not (string-match inf-clojure-prompt kept))
1065-
(accept-process-output proc 2)))
1066-
(setq completions (read kept))
1067-
;; Subprocess echoes output on Windows and OS X.
1068-
(when (and completions (string= (concat (car completions) "\n") completion-snippet))
1069-
(setq completions (cdr completions))))
1070-
(set-process-filter proc comint-filt))
1071-
completions))
1087+
(let* ((compl-snippet (format (inf-clojure-completion-form) (substring-no-properties expr)))
1088+
(compl-response (inf-clojure--process-response (inf-clojure-proc) compl-snippet)))
1089+
(inf-clojure--read-response compl-response)))
10721090

10731091
(defconst inf-clojure-clojure-expr-break-chars " \t\n\"\'`><,;|&{(")
10741092

@@ -1226,13 +1244,6 @@ to suppress the usage of the target buffer discovery logic."
12261244
(inf-clojure (inf-clojure-cmd (inf-clojure-project-type)))
12271245
(rename-buffer target-buffer-name)))
12281246

1229-
(defun inf-clojure--response-match-p (form match-p proc)
1230-
"Return MATCH-P on the result of sending FORM to PROC.
1231-
Note that this function will add a \n to the end of the string
1232-
for evaluation, therefore FORM should not include it."
1233-
(when-let* ((response (inf-clojure-results-from-process proc form)))
1234-
(funcall match-p response)))
1235-
12361247
;;;; Lumo
12371248
;;;; ====
12381249

@@ -1242,14 +1253,6 @@ for evaluation, therefore FORM should not include it."
12421253
:type 'string
12431254
:package-version '(inf-clojure . "2.0.0"))
12441255

1245-
(defalias 'inf-clojure--lumo-p
1246-
(apply-partially 'inf-clojure--response-match-p
1247-
inf-clojure--lumo-repl-form
1248-
(lambda (string)
1249-
(string-match-p "\\Ca*true\\Ca*" string)))
1250-
"Ascertain that PROC is a Lumo REPL.")
1251-
1252-
12531256
;;;; Planck
12541257
;;;; ====
12551258

0 commit comments

Comments
 (0)