Skip to content

Commit 495fb96

Browse files
authored
Merge pull request #1806 from cnngimenez/master
Inferior-haskell: do not insert repeated prompts.
2 parents fc1ea06 + e826b2f commit 495fb96

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

inf-haskell.el

+53-1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ The format should be the same as for `compilation-error-regexp-alist'.")
113113
correctly interpret multi-line input even when modules are
114114
imported.")
115115

116+
(defconst inferior-haskell-cont-prompt-regexp
117+
"^[[:alnum:].*_() |λ]*| "
118+
"Continuation prompt rexep.
119+
Used to remove them from the output by the comint preoutput filter. See
120+
`inferior-haskell-remove-extra-prompts'.
121+
122+
This should be a similar regexp as `haskell-prompt-regexp', but it usually
123+
ends with \"| \" instead of \"> \".")
124+
125+
(defconst inferior-haskell-maybe-cont-prompt-regexp
126+
"^[[:alnum:].*_() |λ]*[>|] "
127+
"A continuation or non-continuation prompt regexp.
128+
This should match any prompt, a continuation or a common prompt. This regexp
129+
should be similar to `haskell-prompt-regexp' and
130+
`inferior-haskell-cont-prompt-regex' as it should match both.
131+
132+
It is used to remove multiple prompts on the comint preoutput filter. See
133+
`inferior-haskell-remove-extra-prompts'.")
134+
116135
;;; TODO
117136
;;; -> Make font lock work for strings, directories, hyperlinks
118137
;;; -> Make font lock work for key words???
@@ -161,7 +180,40 @@ imported.")
161180
(define-key map keys (lookup-key compilation-minor-mode-map keys)))
162181
(add-to-list 'minor-mode-overriding-map-alist
163182
(cons 'compilation-minor-mode map))))
164-
(add-hook 'inferior-haskell-hook 'inferior-haskell-init))
183+
(add-hook 'inferior-haskell-hook 'inferior-haskell-init)
184+
185+
;; Avoid multiple prompts at the end of the output
186+
(add-hook 'comint-preoutput-filter-functions
187+
#'inferior-haskell-remove-extra-prompts nil t))
188+
189+
(defun inferior-haskell-remove-extra-prompts (str)
190+
"Remove any extra Haskell-prompts from STR.
191+
Remove multiple prompts from STR. All prompts indicating continuation are
192+
completely removed. Only remain the last non-continuantion prompt.
193+
194+
Examples:
195+
The input \"Prelude> Prelude> \" will return \"Prelude> \".
196+
The input \"Prelude| Prelude| \" will return \"\".
197+
198+
These kind of output are usually produced by the multiple line input (i.e. when
199+
using \":{ ... :}\" code in the GHCi interpreter). Sometimes, comint would note
200+
filter the prompts out. For this reason, this function shoud be added to the
201+
hook `comint-preoutput-filter-functions', to be executed before comint insert
202+
STR to the buffer.
203+
204+
Some libraries, such as ob-haskell.el, considers the multilple prompts as part
205+
of the evaluation output. Moreover, it does not provide any information to the
206+
user. Removing these prompts provides a better reading and less code for parsing
207+
the output."
208+
(let ((last-match nil))
209+
(while (string-match inferior-haskell-maybe-cont-prompt-regexp str)
210+
(setq last-match (match-string 0 str))
211+
(setq str (substring str (match-end 0))))
212+
;; Remove prompt-cont if it is the last one.
213+
(if (or (null last-match)
214+
(string-match inferior-haskell-cont-prompt-regexp last-match))
215+
str
216+
(concat last-match str))))
165217

166218
(defvar inferior-haskell-buffer nil
167219
"The buffer in which the inferior process is running.")

0 commit comments

Comments
 (0)