Skip to content

Commit 0c6f998

Browse files
plandesbbatsov
authored andcommitted
Handle ANSI color REPL evaluation created by Puget (#2021)
Basically we simply apply the ANSI color and discard it, so it would interfere with the `clojure-mode` font-locking we're doing.
1 parent 2f3c9f5 commit 0c6f998

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
### Changes
3434

35+
* Handle ANSI REPL evaluation created by Puget.
3536
* Drop support for Emacs 24.3.
3637
* Don't try to use ssh automatically when connecting to remote hosts and a direct connection fails. See `nrepl-use-ssh-fallback-for-remote-hosts`.
3738
* [#1945](https://github.com/clojure-emacs/cider/pull/1945): Start nREPL servers bound to `::` by default using `cider-jack-in`.

cider-util.el

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;;; cider-util.el --- Common utility functions that don't belong anywhere else -*- lexical-binding: t -*-
1+
;; cider-util.el --- Common utility functions that don't belong anywhere else -*- lexical-binding: t -*-
22

33
;; Copyright © 2012-2013 Tim King, Phil Hagelberg, Bozhidar Batsov
44
;; Copyright © 2013-2017 Bozhidar Batsov, Artur Malabarba and CIDER contributors
@@ -36,6 +36,7 @@
3636
(require 'subr-x)
3737
(require 'cider-compat)
3838
(require 'nrepl-dict)
39+
(require 'ansi-color)
3940

4041
(defalias 'cider-pop-back 'pop-tag-mark)
4142

@@ -250,16 +251,23 @@ This buffer is not designed to display anything to the user. For that, use
250251
(funcall mode))
251252
b)))
252253

254+
(defun cider-ansi-color-string-p (string)
255+
"Return non-nil if STRING is an ANSI string."
256+
(string-match "^\\[" string))
257+
253258
(defun cider-font-lock-as (mode string)
254259
"Use MODE to font-lock the STRING."
255-
(if (or (null cider-font-lock-max-length)
256-
(< (length string) cider-font-lock-max-length))
257-
(with-current-buffer (cider--make-buffer-for-mode mode)
258-
(erase-buffer)
259-
(insert string)
260-
(font-lock-fontify-region (point-min) (point-max))
261-
(buffer-string))
262-
string))
260+
(let ((string (if (cider-ansi-color-string-p string)
261+
(substring-no-properties (ansi-color-apply string))
262+
string)))
263+
(if (or (null cider-font-lock-max-length)
264+
(< (length string) cider-font-lock-max-length))
265+
(with-current-buffer (cider--make-buffer-for-mode mode)
266+
(erase-buffer)
267+
(insert string)
268+
(font-lock-fontify-region (point-min) (point-max))
269+
(buffer-string))
270+
string)))
263271

264272
(defun cider-font-lock-region-as (mode beg end &optional buffer)
265273
"Use MODE to font-lock text between BEG and END.

test/cider-util-tests.el

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,14 @@
191191
(insert "(test-function arg1 arg2 arg3)")
192192
(backward-char 2)
193193
(expect (cider-second-sexp-in-list) :to-equal "arg1"))))
194+
195+
(describe "cider-ansi-color-string-detect"
196+
(it "detect ansi color successfully"
197+
(expect (cider-ansi-color-string-p "an-ansi-str")
198+
:to-be-truthy)
199+
(expect (cider-ansi-color-string-p "[an-ansi-str]")
200+
:to-be-truthy)
201+
(expect (cider-ansi-color-string-p "[an-ansi-str]")
202+
:not :to-be-truthy)
203+
(expect (cider-ansi-color-string-p "'an-ansi-str")
204+
:not :to-be-truthy)))

0 commit comments

Comments
 (0)