GNU bug report logs - #44589
[PATCH] Add an option to preserve ANSI sequences

Previous Next

Package: emacs;

Reported by: Pablo Barbachano <pablob <at> amazon.com>

Date: Thu, 12 Nov 2020 10:14:02 UTC

Severity: normal

Tags: fixed, 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 44589 in the body.
You can then email your comments to 44589 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#44589; Package emacs. (Thu, 12 Nov 2020 10:14:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Pablo Barbachano <pablob <at> amazon.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Thu, 12 Nov 2020 10:14:02 GMT) Full text and rfc822 format available.

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

From: Pablo Barbachano <pablob <at> amazon.com>
To: <bug-gnu-emacs <at> gnu.org>
Subject: [PATCH] Add an option to preserve ANSI sequences
Date: Thu, 12 Nov 2020 11:13:31 +0100
Hi, sorry for the long delay, but I had to clear it with my company's legal department. Unfortunately the original Bug#37814 is archived and I cannot seem to reopen it, so sending this as a new patch.

As requested, I send the patch from my company's email address and an updated commit message.

Thanks!
Pablo



Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879







Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#44589; Package emacs. (Thu, 12 Nov 2020 12:27:01 GMT) Full text and rfc822 format available.

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

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Pablo Barbachano <pablob <at> amazon.com>
Cc: 44589 <at> debbugs.gnu.org
Subject: Re: bug#44589: [PATCH] Add an option to preserve ANSI sequences
Date: Thu, 12 Nov 2020 13:25:58 +0100
Pablo Barbachano <pablob <at> amazon.com> writes:

> As requested, I send the patch from my company's email address and an
> updated commit message.

I think you forgot to include the patch here?  At least it wasn't
received by the bug tracker.

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




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#44589; Package emacs. (Thu, 12 Nov 2020 15:04:02 GMT) Full text and rfc822 format available.

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

From: Pablo Barbachano <pablob <at> amazon.com>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: 44589 <at> debbugs.gnu.org
Subject: RE: bug#44589: [PATCH] Add an option to preserve ANSI sequences
Date: Thu, 12 Nov 2020 16:03:27 +0100
[Message part 1 (text/plain, inline)]
>> As requested, I send the patch from my company's email address and an
>> updated commit message.
>
> I think you forgot to include the patch here?  At least it wasn't
> received by the bug tracker.

Sorry, I did forget :) Attaching now.

-- 
Pablo




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879


[0001-Add-an-option-to-preserve-ANSI-sequences.patch (text/x-diff, inline)]
From 2009ff66c62f58f0fda680439e776e50ad518c6d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pablo=20Barb=C3=A1chano?= <pablob <at> amazon.com>
Date: Mon, 9 Nov 2020 16:06:35 +0100
Subject: [PATCH] Add an option to preserve ANSI sequences

Provide an option to not strip ANSI sequences (Bug#37814).
* lisp/ansi-color.el Add an option to preserve the ANSI sequences
* test/lisp/ansi-color-tests.el: Add tests
---
 lisp/ansi-color.el            | 23 +++++++++++-----
 test/lisp/ansi-color-tests.el | 49 +++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 7 deletions(-)
 create mode 100644 test/lisp/ansi-color-tests.el

diff --git a/lisp/ansi-color.el b/lisp/ansi-color.el
index d20260b185..9adfe7340b 100644
--- a/lisp/ansi-color.el
+++ b/lisp/ansi-color.el
@@ -363,7 +363,7 @@ ansi-color-filter-region
 	  (setq ansi-color-context-region (list nil (match-beginning 0)))
 	(setq ansi-color-context-region nil)))))
 
-(defun ansi-color-apply-on-region (begin end)
+(defun ansi-color-apply-on-region (begin end &optional preserve-sequences)
   "Translates SGR control sequences into overlays or extents.
 Delete all other control sequences without processing them.
 
@@ -380,18 +380,27 @@ ansi-color-apply-on-region
 `ansi-color-apply-on-region'.  Specifically, it will override
 BEGIN, the start of the region and set the face with which to
 start.  Set `ansi-color-context-region' to nil if you don't want
-this."
+this.
+
+If PRESERVE-SEQUENCES is t, the sequences are hidden instead of
+being deleted."
   (let ((codes (car ansi-color-context-region))
-	(start-marker (or (cadr ansi-color-context-region)
-			  (copy-marker begin)))
-	(end-marker (copy-marker end)))
+        (start-marker (or (cadr ansi-color-context-region)
+                          (copy-marker begin)))
+        (end-marker (copy-marker end)))
     (save-excursion
       (goto-char start-marker)
       ;; Find the next escape sequence.
       (while (re-search-forward ansi-color-control-seq-regexp end-marker t)
-        ;; Remove escape sequence.
-        (let ((esc-seq (delete-and-extract-region
+        ;; Extract escape sequence.
+        (let ((esc-seq (buffer-substring
                         (match-beginning 0) (point))))
+          (if preserve-sequences
+              ;; make the escape sequence transparent
+              (overlay-put (make-overlay (match-beginning 0) (point)) 'invisible t)
+            ;; else, strip
+            (delete-region (match-beginning 0) (point)))
+
           ;; Colorize the old block from start to end using old face.
           (funcall ansi-color-apply-face-function
                    (prog1 (marker-position start-marker)
diff --git a/test/lisp/ansi-color-tests.el b/test/lisp/ansi-color-tests.el
new file mode 100644
index 0000000000..5c3da875f8
--- /dev/null
+++ b/test/lisp/ansi-color-tests.el
@@ -0,0 +1,49 @@
+;;; ansi-color-tests.el --- Test suite for ansi-color  -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2020 Free Software Foundation, Inc.
+
+;; Author: Pablo Barbáchano <pablob <at> amazon.com>
+;; Keywords: ansi
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(require 'ansi-color)
+
+(defvar test-strings '(("\e[33mHello World\e[0m" . "Hello World")
+                       ("\e[1m\e[3m\e[5mbold italics blink\e[0m" . "bold italics blink")))
+
+(ert-deftest ansi-color-apply-on-region-test ()
+    (dolist (pair test-strings)
+      (with-temp-buffer
+        (insert (car pair))
+        (ansi-color-apply-on-region (point-min) (point-max))
+        (should (equal (buffer-string) (cdr pair)))
+        (should (not (equal (overlays-at (point-min)) nil))))))
+
+(ert-deftest ansi-color-apply-on-region-preserving-test ()
+    (dolist (pair test-strings)
+      (with-temp-buffer
+        (insert (car pair))
+        (ansi-color-apply-on-region (point-min) (point-max) t)
+        (should (equal (buffer-string) (car pair))))))
+
+(provide 'ansi-color-tests)
+
+;;; ansi-color-tests.el ends here
-- 
2.17.1


Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#44589; Package emacs. (Sat, 14 Nov 2020 15:27:01 GMT) Full text and rfc822 format available.

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

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Pablo Barbachano <pablob <at> amazon.com>
Cc: 44589 <at> debbugs.gnu.org
Subject: Re: bug#44589: [PATCH] Add an option to preserve ANSI sequences
Date: Sat, 14 Nov 2020 16:26:07 +0100
Pablo Barbachano <pablob <at> amazon.com> writes:

>>> As requested, I send the patch from my company's email address and an
>>> updated commit message.
>>
>> I think you forgot to include the patch here?  At least it wasn't
>> received by the bug tracker.
>
> Sorry, I did forget :) Attaching now.

Thanks; looks good to me, so I've applied it to Emacs 28.

I see that the FSF now has a copyright assignment on file for all
contributions from Amazon?  That's pretty cool.  :-)

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




Added tag(s) fixed. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sat, 14 Nov 2020 15:27:02 GMT) Full text and rfc822 format available.

bug marked as fixed in version 28.1, send any further explanations to 44589 <at> debbugs.gnu.org and Pablo Barbachano <pablob <at> amazon.com> Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sat, 14 Nov 2020 15:27: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. (Sun, 13 Dec 2020 12:24:08 GMT) Full text and rfc822 format available.

This bug report was last modified 4 years and 267 days ago.

Previous Next


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