From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Pablo Barbachano via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#44589: [PATCH] Add an option to preserve ANSI sequences Date: Thu, 12 Nov 2020 16:03:27 +0100 Message-ID: References: <87blg2bw2x.fsf@gnus.org> Reply-To: Pablo Barbachano Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8758"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 44589@debbugs.gnu.org To: Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Nov 12 16:04:26 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kdE9B-00029C-P6 for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 12 Nov 2020 16:04:25 +0100 Original-Received: from localhost ([::1]:52210 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kdE9A-0005dm-Co for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 12 Nov 2020 10:04:24 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47324) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kdE8p-0005dI-60 for bug-gnu-emacs@gnu.org; Thu, 12 Nov 2020 10:04:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:33814) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kdE8o-0003X9-6W for bug-gnu-emacs@gnu.org; Thu, 12 Nov 2020 10:04:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kdE8o-0005ZV-3U for bug-gnu-emacs@gnu.org; Thu, 12 Nov 2020 10:04:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Pablo Barbachano Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 12 Nov 2020 15:04:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 44589 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 44589-submit@debbugs.gnu.org id=B44589.160519343721405 (code B ref 44589); Thu, 12 Nov 2020 15:04:02 +0000 Original-Received: (at 44589) by debbugs.gnu.org; 12 Nov 2020 15:03:57 +0000 Original-Received: from localhost ([127.0.0.1]:45360 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kdE8f-0005Z8-C8 for submit@debbugs.gnu.org; Thu, 12 Nov 2020 10:03:57 -0500 Original-Received: from smtp-fw-33001.amazon.com ([207.171.190.10]:33858) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kdE8a-0005Yr-HY for 44589@debbugs.gnu.org; Thu, 12 Nov 2020 10:03:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1605193429; x=1636729429; h=from:to:cc:in-reply-to:references:date:message-id: mime-version:subject; bh=Clv4HG+Ix7IkMt5RFZsbMXk9qR205PnyXrfSU3w5AHQ=; b=R6FDryVWWNfdQRs+EKQyNWTZtU+vtYMwfTDRmMZ9ImGQR83HkEcMC7Qx OzCsK1Eb/JSqJL/k71G1SEERXDBd9GZLE5q45dxquViaF8572ETit2VuU 5nJyTpO6rbpbjTeTcBrABbDIglDKB34ZVllXrdlXHRpW6Hd5s6oDwDwxT I=; X-Amazon-filename: 0001-Add-an-option-to-preserve-ANSI-sequences.patch X-IronPort-AV: E=Sophos; i="5.77,472,1596499200"; d="scan'208,223"; a="93394250" Original-Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-2a-69849ee2.us-west-2.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-33001.sea14.amazon.com with ESMTP; 12 Nov 2020 15:03:36 +0000 Original-Received: from EX13D11EUB004.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan2.pdx.amazon.com [10.236.137.194]) by email-inbound-relay-2a-69849ee2.us-west-2.amazon.com (Postfix) with ESMTPS id 48909A2372; Thu, 12 Nov 2020 15:03:35 +0000 (UTC) Original-Received: from u5dd771485afc5c.ant.amazon.com (10.43.160.229) by EX13D11EUB004.ant.amazon.com (10.43.166.188) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 12 Nov 2020 15:03:32 +0000 In-Reply-To: <87blg2bw2x.fsf@gnus.org> X-Originating-IP: [10.43.160.229] X-ClientProxiedBy: EX13D43UWC004.ant.amazon.com (10.43.162.42) To EX13D11EUB004.ant.amazon.com (10.43.166.188) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:193165 Archived-At: --=-=-= Content-Type: text/plain >> 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 --=-=-= Content-Type: text/x-diff; charset="utf-8" Content-Disposition: inline; filename="0001-Add-an-option-to-preserve-ANSI-sequences.patch" Content-Transfer-Encoding: quoted-printable >From 2009ff66c62f58f0fda680439e776e50ad518c6d Mon Sep 17 00:00:00 2001 From: =3D?UTF-8?q?Pablo=3D20Barb=3DC3=3DA1chano?=3D 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))))) =20 -(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. =20 @@ -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)) 'inv= isible 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=C3=A1chano +;; 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 . + +;;; 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 i= talics 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 --=20 2.17.1 --=-=-=--