From mboxrd@z Thu Jan  1 00:00:00 1970
Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail
From: Pablo =?UTF-8?Q?Barb=C3=A1chano?= <pablo.barbachano@gmail.com>
Newsgroups: gmane.emacs.bugs
Subject: bug#37814: [PATCH] Add an option to preserve ANSI sequences
Date: Fri, 18 Oct 2019 20:18:13 +0200
Message-ID: <20191018181813.21848-1-pablo.barbachano@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226";
	logging-data="255785"; mail-complaints-to="usenet@blaine.gmane.org"
Cc: Pablo =?UTF-8?Q?Barb=C3=A1chano?= <pablo.barbachano@gmail.com>
To: 37814@debbugs.gnu.org
Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Fri Oct 18 20:24:02 2019
Return-path: <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org>
Envelope-to: geb-bug-gnu-emacs@m.gmane.org
Original-Received: from lists.gnu.org ([209.51.188.17])
	by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
	(Exim 4.89)
	(envelope-from <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org>)
	id 1iLWuv-0014Np-RF
	for geb-bug-gnu-emacs@m.gmane.org; Fri, 18 Oct 2019 20:24:01 +0200
Original-Received: from localhost ([::1]:44912 helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org>)
	id 1iLWuu-00089D-82
	for geb-bug-gnu-emacs@m.gmane.org; Fri, 18 Oct 2019 14:24:00 -0400
Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:35074)
 by lists.gnu.org with esmtp (Exim 4.90_1)
 (envelope-from <Debian-debbugs@debbugs.gnu.org>) id 1iLWq7-0000z4-R8
 for bug-gnu-emacs@gnu.org; Fri, 18 Oct 2019 14:19:05 -0400
Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
 (envelope-from <Debian-debbugs@debbugs.gnu.org>) id 1iLWq6-0002O8-Ag
 for bug-gnu-emacs@gnu.org; Fri, 18 Oct 2019 14:19:03 -0400
Original-Received: from debbugs.gnu.org ([209.51.188.43]:42715)
 by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16)
 (Exim 4.71) (envelope-from <Debian-debbugs@debbugs.gnu.org>)
 id 1iLWq6-0002NG-7b
 for bug-gnu-emacs@gnu.org; Fri, 18 Oct 2019 14:19:02 -0400
Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2)
 (envelope-from <Debian-debbugs@debbugs.gnu.org>) id 1iLWq5-0005fw-TV
 for bug-gnu-emacs@gnu.org; Fri, 18 Oct 2019 14:19:01 -0400
X-Loop: help-debbugs@gnu.org
Resent-From: Pablo =?UTF-8?Q?Barb=C3=A1chano?= <pablo.barbachano@gmail.com>
Original-Sender: "Debbugs-submit" <debbugs-submit-bounces@debbugs.gnu.org>
Resent-CC: bug-gnu-emacs@gnu.org
Resent-Date: Fri, 18 Oct 2019 18:19:01 +0000
Resent-Message-ID: <handler.37814.B.157142271321777@debbugs.gnu.org>
Resent-Sender: help-debbugs@gnu.org
X-GNU-PR-Message: report 37814
X-GNU-PR-Package: emacs
X-GNU-PR-Keywords: patch
X-Debbugs-Original-To: bug-gnu-emacs@gnu.org
Original-Received: via spool by submit@debbugs.gnu.org id=B.157142271321777
 (code B ref -1); Fri, 18 Oct 2019 18:19:01 +0000
Original-Received: (at submit) by debbugs.gnu.org; 18 Oct 2019 18:18:33 +0000
Original-Received: from localhost ([127.0.0.1]:51536 helo=debbugs.gnu.org)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <debbugs-submit-bounces@debbugs.gnu.org>)
 id 1iLWpc-0005fA-VY
 for submit@debbugs.gnu.org; Fri, 18 Oct 2019 14:18:33 -0400
Original-Received: from lists.gnu.org ([209.51.188.17]:59092)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <pablo.barbachano@gmail.com>) id 1iLWpb-0005f2-28
 for submit@debbugs.gnu.org; Fri, 18 Oct 2019 14:18:31 -0400
Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:34983)
 by lists.gnu.org with esmtp (Exim 4.90_1)
 (envelope-from <pablo.barbachano@gmail.com>) id 1iLWpY-0008WU-Su
 for bug-gnu-emacs@gnu.org; Fri, 18 Oct 2019 14:18:30 -0400
Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
 (envelope-from <pablo.barbachano@gmail.com>) id 1iLWpX-000264-5e
 for bug-gnu-emacs@gnu.org; Fri, 18 Oct 2019 14:18:28 -0400
Original-Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]:35452)
 by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16)
 (Exim 4.71) (envelope-from <pablo.barbachano@gmail.com>)
 id 1iLWpW-00025M-Vz
 for bug-gnu-emacs@gnu.org; Fri, 18 Oct 2019 14:18:27 -0400
Original-Received: by mail-wr1-x443.google.com with SMTP id l10so6819984wrb.2
 for <bug-gnu-emacs@gnu.org>; Fri, 18 Oct 2019 11:18:26 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; 
 h=from:to:cc:subject:date:message-id:mime-version
 :content-transfer-encoding;
 bh=kSD3/L6/EO6s8irCSJNx8B6nmLEH34q1b1KZx6eNDWM=;
 b=jkITG1QAaHxJHmedX2Rjh5rOwZpTUWdhJinICBinzbKE1pGyevx78nNmOVfHC50xzo
 0zIba7eXwDbXVFjcW0QWeGv2vkQfqETshYngSqC7Rm1bOAdwRIkuK/VegYMF0Vl6hjUr
 kpfCePZ97IEudVVj3Vl69XmB7XOxo0rNTUlj7j0+N1sCuZz62eyIky8rWfEmCJrZ9EDg
 B+k6N2Yi864LSxYuluJb0LBGMw3y3yFdXTK+b6ZMNYZeT2tTI44IWSZdUNuMfAB7wVLW
 lEJ1k4mCL5F/pH9k67ofMlwSSGlNO3vfvj5Ov1XVoX/hq4y57FYI6n2upXASlWHsMbUf
 EP9Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20161025;
 h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version
 :content-transfer-encoding;
 bh=kSD3/L6/EO6s8irCSJNx8B6nmLEH34q1b1KZx6eNDWM=;
 b=WuceKpkSUJbvR7g+oPkvgfsh44Bbl7kJCbg8mvcfYe0hhDvSqFgLQhml+nC099cK6q
 TlMOlA7/z4qZvjBdkEVc7B759dzhm8WP9i1fftHUdTcVwvpGraS1oXx9ZBhTFgOAHFuO
 rDh5bIby7hwu9nrgW3wKwL/eROZHDCPxgDpS8yBRRzzqcJP5JnaX6/joauZL3ICuOg3a
 /0w7ZetcQcvgqZTt5/YPS5WXi5I17qgvQzrtHW8ob2FrdXC6FqRVd8YSRK9nv3od1B8b
 9zNLBxWt+HOvpAAxDD27cETajfa02Zlt18AfMVYRUD4u500ZQdDihFFw/Dn0B2sJdvF8
 qdmw==
X-Gm-Message-State: APjAAAWs6cOIKS32yED4zVeJ6tenPchnRI+jgOVDAj/W2XgHFMaZYVAX
 gpmZfVGdxDofzfNJra5WFXC/GItJlp8=
X-Google-Smtp-Source: APXvYqwID9leCy4uRiLFn52tCNXF9k7tmk8Fk0T1hqdEP8jYEykvBAoodK2UZibnDDOdm7sKSyx0Sg==
X-Received: by 2002:adf:f3c9:: with SMTP id g9mr8871018wrp.7.1571422705080;
 Fri, 18 Oct 2019 11:18:25 -0700 (PDT)
Original-Received: from exupery.localdomain (x4e371f8e.dyn.telefonica.de.
 [78.55.31.142])
 by smtp.gmail.com with ESMTPSA id b62sm8412956wmc.13.2019.10.18.11.18.24
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 18 Oct 2019 11:18:24 -0700 (PDT)
X-Mailer: git-send-email 2.23.0
X-detected-operating-system: by eggs.gnu.org: Genre and OS details not
 recognized.
X-BeenThere: debbugs-submit@debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]
X-Received-From: 209.51.188.43
X-BeenThere: bug-gnu-emacs@gnu.org
List-Id: "Bug reports for GNU Emacs,
 the Swiss army knife of text editors" <bug-gnu-emacs.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-gnu-emacs>,
 <mailto:bug-gnu-emacs-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/bug-gnu-emacs>
List-Post: <mailto:bug-gnu-emacs@gnu.org>
List-Help: <mailto:bug-gnu-emacs-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-gnu-emacs>,
 <mailto:bug-gnu-emacs-request@gnu.org?subject=subscribe>
Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org
Original-Sender: "bug-gnu-emacs"
 <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org>
Xref: news.gmane.org gmane.emacs.bugs:169674
Archived-At: <http://permalink.gmane.org/gmane.emacs.bugs/169674>

* 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 31bed6028c..e2fb205995 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..8a6cf2e41b
--- /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) 2019 Free Software Foundation, Inc.
+
+;; Author: Pablo Barbáchano <pablo.barbachano@gmail.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