Skip to content

Commit 39e794c

Browse files
arichiardibbatsov
authored andcommitted
Protect against empty commands
Improves the code so that empty strings are not sent to the process anymore. It also avoids trimming \t because they are actually used by some REPL.
1 parent aaaeef6 commit 39e794c

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

inf-clojure.el

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,28 @@ It requires a REPL PROC for inspecting the correct type."
256256
inf-clojure-repl-type))
257257

258258
(defun inf-clojure--single-linify (string)
259-
"Convert a multi-line STRING in a single-line STRING."
260-
(replace-regexp-in-string "[[:space:]\\|\n]+" " " string))
259+
"Convert a multi-line STRING in a single-line STRING.
260+
It also reduces/adds redundant whitespace for readability. Note
261+
that this function will transform the empty string in \" \" (it
262+
adds an empty space)."
263+
(replace-regexp-in-string "[ \\|\n]+" " " string))
264+
265+
(defun inf-clojure--trim-newline-right (string)
266+
"Trim newlines (only) in STRING."
267+
(if (string-match "\n+\\'" string)
268+
(replace-match "" t t string)
269+
string))
261270

262271
(defun inf-clojure--sanitize-command (command)
263272
"Sanitize COMMAND for sending it to a process.
264273
An example of things that this function does is to add a final
265-
newline at the end of the form."
266-
(concat (string-trim-right (inf-clojure--single-linify command))
267-
"\n"))
274+
newline at the end of the form. Return an empty string if the
275+
sanitized command is empty."
276+
(let* ((linified (inf-clojure--single-linify command))
277+
(sanitized (inf-clojure--trim-newline-right linified)))
278+
(if (or (string-blank-p linified) (string-blank-p sanitized))
279+
""
280+
(concat sanitized "\n"))))
268281

269282
(defun inf-clojure--send-string (proc string)
270283
"A custom `comint-input-sender` / `comint-send-string`.
@@ -275,7 +288,8 @@ always be preferred over `comint-send-string`. It delegates to
275288
the string for evaluation. Refer to `comint-simple-send` for
276289
customizations."
277290
(inf-clojure--set-repl-type proc)
278-
(comint-simple-send proc string))
291+
(when (> (length string) 0)
292+
(comint-simple-send proc string)))
279293

280294
(defcustom inf-clojure-load-form "(clojure.core/load-file \"%s\")"
281295
"Format-string for building a Clojure expression to load a file.
@@ -1041,31 +1055,32 @@ If BEG-REGEXP is nil, the result string will start from (point)
10411055
in the results buffer. If END-REGEXP is nil, the result string
10421056
will end at (point-max) in the results buffer. It cuts out the
10431057
output from and including the `inf-clojure-prompt`."
1044-
(inf-clojure--log-string command "----CMD->")
1045-
(let ((work-buffer inf-clojure--redirect-buffer-name))
1046-
(save-excursion
1047-
(set-buffer (get-buffer-create work-buffer))
1048-
(erase-buffer)
1049-
(comint-redirect-send-command-to-process
1050-
(inf-clojure--sanitize-command command) work-buffer process nil t)
1051-
;; Wait for the process to complete
1052-
(set-buffer (process-buffer process))
1053-
(while (and (null comint-redirect-completed)
1054-
(accept-process-output process 1 0 t))
1055-
(sleep-for 0.01))
1056-
;; Collect the output
1057-
(set-buffer work-buffer)
1058-
(goto-char (point-min))
1059-
(let* ((buffer-string (buffer-substring-no-properties (point-min) (point-max)))
1060-
(boundaries (inf-clojure--string-boundaries buffer-string inf-clojure-prompt beg-regexp end-regexp))
1061-
(beg-pos (car boundaries))
1062-
(end-pos (car (cdr boundaries)))
1063-
(prompt-pos (car (cdr (cdr boundaries))))
1064-
(response-string (substring buffer-string beg-pos (min end-pos prompt-pos))))
1065-
(inf-clojure--log-string buffer-string "<-BUF----")
1066-
(inf-clojure--log-string boundaries "<-BND----")
1067-
(inf-clojure--log-string response-string "<-RES----")
1068-
response-string))))
1058+
(let ((work-buffer inf-clojure--redirect-buffer-name)
1059+
(sanitized-command (inf-clojure--sanitize-command command)))
1060+
(when (not (string-empty-p sanitized-command))
1061+
(inf-clojure--log-string command "----CMD->")
1062+
(save-excursion
1063+
(set-buffer (get-buffer-create work-buffer))
1064+
(erase-buffer)
1065+
(comint-redirect-send-command-to-process sanitized-command work-buffer process nil t)
1066+
;; Wait for the process to complete
1067+
(set-buffer (process-buffer process))
1068+
(while (and (null comint-redirect-completed)
1069+
(accept-process-output process 1 0 t))
1070+
(sleep-for 0.01))
1071+
;; Collect the output
1072+
(set-buffer work-buffer)
1073+
(goto-char (point-min))
1074+
(let* ((buffer-string (buffer-substring-no-properties (point-min) (point-max)))
1075+
(boundaries (inf-clojure--string-boundaries buffer-string inf-clojure-prompt beg-regexp end-regexp))
1076+
(beg-pos (car boundaries))
1077+
(end-pos (car (cdr boundaries)))
1078+
(prompt-pos (car (cdr (cdr boundaries))))
1079+
(response-string (substring buffer-string beg-pos (min end-pos prompt-pos))))
1080+
(inf-clojure--log-string buffer-string "<-BUF----")
1081+
(inf-clojure--log-string boundaries "<-BND----")
1082+
(inf-clojure--log-string response-string "<-RES----")
1083+
response-string)))))
10691084

10701085
(defun inf-clojure--nil-string-match-p (string)
10711086
"Return true iff STRING is not nil.

0 commit comments

Comments
 (0)