GNU bug report logs - #49609
[PATCH] Add option for handling SGR control sequences in compilation-mode

Previous Next

Package: emacs;

Reported by: Ivan Sokolov <ivan-p-sokolov <at> ya.ru>

Date: Sat, 17 Jul 2021 19:38:01 UTC

Severity: normal

Tags: patch

Fixed in version 28.1

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 49609 in the body.
You can then email your comments to 49609 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Sat, 17 Jul 2021 19:38:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Ivan Sokolov <ivan-p-sokolov <at> ya.ru>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sat, 17 Jul 2021 19:38:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Ivan Sokolov <ivan-p-sokolov <at> ya.ru>
To: bug-gnu-emacs <at> gnu.org
Subject: [PATCH] Add option for handling SGR control sequences in
 compilation-mode
Date: Sat, 17 Jul 2021 22:37:40 +0300




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Sat, 17 Jul 2021 19:49:01 GMT) Full text and rfc822 format available.

Message #8 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Ivan Sokolov <ivan-p-sokolov <at> ya.ru>
To: 49609 <at> debbugs.gnu.org
Subject: [PATCH] Add option for handling SGR control sequences in
 compilation-mode
Date: Sat, 17 Jul 2021 22:48:43 +0300
[Message part 1 (text/plain, inline)]
I don't have much to say about the patch -- it's a well known solution,
I don't know why we don't have it yet.

[0001-Add-option-for-handling-SGR-control-sequences-in-com.patch (text/x-patch, inline)]
From e41f7bfa24c0fac575ebd7513ea5c527aa4c38ac Mon Sep 17 00:00:00 2001
From: Ivan Sokolov <ivan-p-sokolov <at> ya.ru>
Date: Sat, 17 Jul 2021 21:56:37 +0300
Subject: [PATCH] Add option for handling SGR control sequences in
 compilation-mode

---
 lisp/ansi-color.el | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/lisp/ansi-color.el b/lisp/ansi-color.el
index 44dc0351d4..013f2a8c5d 100644
--- a/lisp/ansi-color.el
+++ b/lisp/ansi-color.el
@@ -75,6 +75,7 @@
 ;;; Code:
 
 (defvar comint-last-output-start)
+(defvar compilation-filter-start)
 
 ;; Customization
 
@@ -181,6 +182,21 @@ ansi-color-for-comint-mode
   :group 'ansi-colors
   :version "23.2")
 
+(defcustom ansi-color-for-compilation-mode t
+  "Determines what to do with compilation output.
+If nil, do nothing.
+If the symbol `filter', then filter all SGR control sequences.
+If anything else (such as t), then translate SGR control sequences
+into text properties.
+
+In order for this to have any effect, `ansi-color-compilation-filter'
+must be in `compilation-filter-hook'."
+  :type '(choice (const :tag "Do nothing" nil)
+                 (const :tag "Filter" filter)
+                 (other :tag "Translate" t))
+  :group 'ansi-colors
+  :version "28.1")
+
 (defvar ansi-color-apply-face-function #'ansi-color-apply-overlay-face
   "Function for applying an Ansi Color face to text in a buffer.
 This function should accept three arguments: BEG, END, and FACE,
@@ -228,6 +244,22 @@ ansi-color-process-output
 	  (t
 	   (ansi-color-apply-on-region start-marker end-marker)))))
 
+;;;###autoload
+(defun ansi-color-compilation-filter ()
+  "Maybe translate SGR control sequences into text properties.
+Depending on variable `ansi-color-for-compilation-mode' the
+compilation output is either nor processed, SGR control sequences
+are filtered using `ansi-color-filter-region', or SGR control
+sequences are translated into text properties using
+`ansi-color-apply-on-region'."
+  (let ((inhibit-read-only t))
+    (pcase ansi-color-for-compilation-mode
+      ('nil nil)
+      ('filter
+       (ansi-color-filter-region compilation-filter-start (point)))
+      (_
+       (ansi-color-apply-on-region compilation-filter-start (point))))))
+
 (define-obsolete-function-alias 'ansi-color-unfontify-region
   'font-lock-default-unfontify-region "24.1")
 
-- 
2.32.0


Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Sat, 17 Jul 2021 20:58:02 GMT) Full text and rfc822 format available.

Message #11 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Jim Porter <jporterbugs <at> gmail.com>
To: 49609 <at> debbugs.gnu.org
Subject: Re: [PATCH] Add option for handling SGR control sequences in
 compilation-mode
Date: Sat, 17 Jul 2021 13:57:41 -0700
One issue I've seen with handling SGR control sequences in
compilation-mode is that it can sometimes cause issues with grep-mode
and other similar modes in third-party packages. `grep-filter' also
handles SGR control sequences, but only works with full lines; this is
a simplification to (mostly) ensure that both the start and end of a
"match" highlight are present at the same time, so that `grep-filter'
can just use a simple regexp.

However, as far as I understand it, `ansi-color-apply-on-region' has
no such limitation and can handle parsing the *start* of some
highlighted text in one call to the function, and then handle the end
of it in another call. If, when grep is printing results, the output
is flushed in the middle of a match highlight, this results in the SGR
sequence to start a highlight being stripped by the time `grep-filter'
runs, so it's not able to fontify the match.

It might be nice if `grep-filter' were smart enough to work with the
output as-is rather than only operating on whole lines. That would
probably be more robust. However, for this patch, maybe it would be
enough to locally set `ansi-color-for-compilation-mode' to nil for
grep-mode.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Sun, 18 Jul 2021 00:19:01 GMT) Full text and rfc822 format available.

Message #14 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Ivan Sokolov <ivan-p-sokolov <at> ya.ru>
Cc: 49609 <at> debbugs.gnu.org
Subject: Re: bug#49609: [PATCH] Add option for handling SGR control
 sequences in compilation-mode
Date: Sun, 18 Jul 2021 02:18:38 +0200
Ivan Sokolov <ivan-p-sokolov <at> ya.ru> writes:

> +If the symbol `filter', then filter all SGR control sequences.

Perhaps this should say what "SGR" is?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Sun, 18 Jul 2021 00:47:03 GMT) Full text and rfc822 format available.

Message #17 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Ivan Sokolov <ivan-p-sokolov <at> ya.ru>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: 49609 <at> debbugs.gnu.org
Subject: Re: bug#49609: [PATCH] Add option for handling SGR control
 sequences in compilation-mode
Date: Sun, 18 Jul 2021 03:45:58 +0300
Lars Ingebrigtsen <larsi <at> gnus.org> writes:

> Ivan Sokolov <ivan-p-sokolov <at> ya.ru> writes:
>
>> +If the symbol `filter', then filter all SGR control sequences.
>
> Perhaps this should say what "SGR" is?

One of the ANSI escape sequences.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Sun, 18 Jul 2021 06:53:01 GMT) Full text and rfc822 format available.

Message #20 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Ivan Sokolov <ivan-p-sokolov <at> ya.ru>
Cc: larsi <at> gnus.org, 49609 <at> debbugs.gnu.org
Subject: Re: bug#49609: [PATCH] Add option for handling SGR control sequences
 in compilation-mode
Date: Sun, 18 Jul 2021 09:52:24 +0300
> From: Ivan Sokolov <ivan-p-sokolov <at> ya.ru>
> Date: Sun, 18 Jul 2021 03:45:58 +0300
> Cc: 49609 <at> debbugs.gnu.org
> 
> Lars Ingebrigtsen <larsi <at> gnus.org> writes:
> 
> > Ivan Sokolov <ivan-p-sokolov <at> ya.ru> writes:
> >
> >> +If the symbol `filter', then filter all SGR control sequences.
> >
> > Perhaps this should say what "SGR" is?
> 
> One of the ANSI escape sequences.

SGR = Select Graphic Rendition.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Mon, 19 Jul 2021 13:54:01 GMT) Full text and rfc822 format available.

Message #23 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: Ivan Sokolov <ivan-p-sokolov <at> ya.ru>, 49609 <at> debbugs.gnu.org
Subject: Re: bug#49609: [PATCH] Add option for handling SGR control
 sequences in compilation-mode
Date: Mon, 19 Jul 2021 15:53:26 +0200
Eli Zaretskii <eliz <at> gnu.org> writes:

>> >> +If the symbol `filter', then filter all SGR control sequences.
>> >
>> > Perhaps this should say what "SGR" is?
>> 
>> One of the ANSI escape sequences.
>
> SGR = Select Graphic Rendition.

I see.  And these are control sequences that are output by...  gcc?  As
opposed to ... other ANSI escape sequences?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Mon, 19 Jul 2021 15:46:02 GMT) Full text and rfc822 format available.

Message #26 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: ivan-p-sokolov <at> ya.ru, 49609 <at> debbugs.gnu.org
Subject: Re: bug#49609: [PATCH] Add option for handling SGR control
 sequences in compilation-mode
Date: Mon, 19 Jul 2021 18:45:29 +0300
> From: Lars Ingebrigtsen <larsi <at> gnus.org>
> Cc: Ivan Sokolov <ivan-p-sokolov <at> ya.ru>,  49609 <at> debbugs.gnu.org
> Date: Mon, 19 Jul 2021 15:53:26 +0200
> 
> >> >> +If the symbol `filter', then filter all SGR control sequences.
> >> >
> >> > Perhaps this should say what "SGR" is?
> >> 
> >> One of the ANSI escape sequences.
> >
> > SGR = Select Graphic Rendition.
> 
> I see.  And these are control sequences that are output by...  gcc?  As
> opposed to ... other ANSI escape sequences?

No, these two terms are synonyms, AFAIK.  GCC doesn't emit any SGR
sequences that other similar applications, like Grep, don't.  They are
just sequences to produced colors and other attributes: bold,
underline, etc.  The intent is to make the important parts of the
output easier to spot.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Mon, 19 Jul 2021 15:55:02 GMT) Full text and rfc822 format available.

Message #29 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: ivan-p-sokolov <at> ya.ru, 49609 <at> debbugs.gnu.org
Subject: Re: bug#49609: [PATCH] Add option for handling SGR control
 sequences in compilation-mode
Date: Mon, 19 Jul 2021 17:53:54 +0200
Eli Zaretskii <eliz <at> gnu.org> writes:

> No, these two terms are synonyms, AFAIK.  GCC doesn't emit any SGR
> sequences that other similar applications, like Grep, don't.  They are
> just sequences to produced colors and other attributes: bold,
> underline, etc.  The intent is to make the important parts of the
> output easier to spot.

Right.  Then we should just say "ANSI control sequences" here -- whether
they're SGR or not seems irrelevant?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Mon, 19 Jul 2021 16:39:01 GMT) Full text and rfc822 format available.

Message #32 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: ivan-p-sokolov <at> ya.ru, 49609 <at> debbugs.gnu.org
Subject: Re: bug#49609: [PATCH] Add option for handling SGR control
 sequences in compilation-mode
Date: Mon, 19 Jul 2021 19:38:47 +0300
> From: Lars Ingebrigtsen <larsi <at> gnus.org>
> Cc: ivan-p-sokolov <at> ya.ru,  49609 <at> debbugs.gnu.org
> Date: Mon, 19 Jul 2021 17:53:54 +0200
> 
> Eli Zaretskii <eliz <at> gnu.org> writes:
> 
> > No, these two terms are synonyms, AFAIK.  GCC doesn't emit any SGR
> > sequences that other similar applications, like Grep, don't.  They are
> > just sequences to produced colors and other attributes: bold,
> > underline, etc.  The intent is to make the important parts of the
> > output easier to spot.
> 
> Right.  Then we should just say "ANSI control sequences" here -- whether
> they're SGR or not seems irrelevant?

That'd be fine, I think.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Mon, 19 Jul 2021 16:56:01 GMT) Full text and rfc822 format available.

Message #35 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: ivan-p-sokolov <at> ya.ru, 49609 <at> debbugs.gnu.org
Subject: Re: bug#49609: [PATCH] Add option for handling SGR control
 sequences in compilation-mode
Date: Mon, 19 Jul 2021 18:55:19 +0200
Eli Zaretskii <eliz <at> gnu.org> writes:

>> Right.  Then we should just say "ANSI control sequences" here -- whether
>> they're SGR or not seems irrelevant?
>
> That'd be fine, I think.

I've now applied the patch (with some doc string changes and NEWS
entries).

I wonder whether `ansi-color-compilation-filter' should be in
`compilation-filter-hook' by default?

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Mon, 19 Jul 2021 17:21:01 GMT) Full text and rfc822 format available.

Message #38 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: ivan-p-sokolov <at> ya.ru, 49609 <at> debbugs.gnu.org
Subject: Re: bug#49609: [PATCH] Add option for handling SGR control
 sequences in compilation-mode
Date: Mon, 19 Jul 2021 20:20:41 +0300
> From: Lars Ingebrigtsen <larsi <at> gnus.org>
> Cc: ivan-p-sokolov <at> ya.ru,  49609 <at> debbugs.gnu.org
> Date: Mon, 19 Jul 2021 18:55:19 +0200
> 
> I wonder whether `ansi-color-compilation-filter' should be in
> `compilation-filter-hook' by default?

Doesn't it depend on the compiler?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Mon, 19 Jul 2021 20:20:01 GMT) Full text and rfc822 format available.

Message #41 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Ivan Sokolov <ivan-p-sokolov <at> ya.ru>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: Eli Zaretskii <eliz <at> gnu.org>, 49609 <at> debbugs.gnu.org
Subject: Re: bug#49609: [PATCH] Add option for handling SGR control
 sequences in compilation-mode
Date: Mon, 19 Jul 2021 23:19:17 +0300
Lars Ingebrigtsen <larsi <at> gnus.org> writes:

> Eli Zaretskii <eliz <at> gnu.org> writes:
>
>> No, these two terms are synonyms, AFAIK.  GCC doesn't emit any SGR
>> sequences that other similar applications, like Grep, don't.  They are
>> just sequences to produced colors and other attributes: bold,
>> underline, etc.  The intent is to make the important parts of the
>> output easier to spot.
>
> Right.  Then we should just say "ANSI control sequences" here -- whether
> they're SGR or not seems irrelevant?

SGR is one particular control sequence, the ansi-colors package does not
handle others.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Mon, 19 Jul 2021 21:49:01 GMT) Full text and rfc822 format available.

Message #44 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Ivan Sokolov <ivan-p-sokolov <at> ya.ru>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: Eli Zaretskii <eliz <at> gnu.org>, 49609 <at> debbugs.gnu.org
Subject: Re: bug#49609: [PATCH] Add option for handling SGR control
 sequences in compilation-mode
Date: Tue, 20 Jul 2021 00:47:45 +0300
Lars Ingebrigtsen <larsi <at> gnus.org> writes:

> I wonder whether `ansi-color-compilation-filter' should be in
> `compilation-filter-hook' by default?

I don't think so. Well designed (machine readable) output can be parsed
and colored by compilation-mode itself, this filter is most usable in
two cases.

First, if the output is exclusively human readable and cannot be parsed,
it would be nice to have at least a native coloring. Examples: Elixir.

Second, if the output is machine redable, but the tool cannot detect
that the terminal is non-interactive and/or it does not have an option
to turn off colorization, `filter' will allow compilation-mode to
process the output.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49609; Package emacs. (Tue, 20 Jul 2021 11:36:01 GMT) Full text and rfc822 format available.

Message #47 received at 49609 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Ivan Sokolov <ivan-p-sokolov <at> ya.ru>
Cc: Eli Zaretskii <eliz <at> gnu.org>, 49609 <at> debbugs.gnu.org
Subject: Re: bug#49609: [PATCH] Add option for handling SGR control
 sequences in compilation-mode
Date: Tue, 20 Jul 2021 13:35:06 +0200
Ivan Sokolov <ivan-p-sokolov <at> ya.ru> writes:

>> I wonder whether `ansi-color-compilation-filter' should be in
>> `compilation-filter-hook' by default?
>
> I don't think so. Well designed (machine readable) output can be parsed
> and colored by compilation-mode itself, this filter is most usable in
> two cases.
>
> First, if the output is exclusively human readable and cannot be parsed,
> it would be nice to have at least a native coloring. Examples: Elixir.
>
> Second, if the output is machine redable, but the tool cannot detect
> that the terminal is non-interactive and/or it does not have an option
> to turn off colorization, `filter' will allow compilation-mode to
> process the output.

OK; makes sense.  Thanks for the explanation.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




bug marked as fixed in version 28.1, send any further explanations to 49609 <at> debbugs.gnu.org and Ivan Sokolov <ivan-p-sokolov <at> ya.ru> Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Tue, 20 Jul 2021 11:36:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Wed, 18 Aug 2021 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 2 years and 245 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.