From: "Pablo Barbáchano" <pablo.barbachano@gmail.com>
To: 37814@debbugs.gnu.org
Cc: "Pablo Barbáchano" <pablo.barbachano@gmail.com>
Subject: bug#37814: [PATCH] Add an option to preserve ANSI sequences
Date: Fri, 18 Oct 2019 20:18:13 +0200 [thread overview]
Message-ID: <20191018181813.21848-1-pablo.barbachano@gmail.com> (raw)
* 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
next reply other threads:[~2019-10-18 18:18 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-18 18:18 Pablo Barbáchano [this message]
2019-10-19 6:12 ` bug#37814: [PATCH] Add an option to preserve ANSI sequences Eli Zaretskii
2019-11-01 20:25 ` Pablo Barbachano
2019-11-01 21:01 ` Eli Zaretskii
2020-08-09 18:42 ` Lars Ingebrigtsen
2020-08-09 19:38 ` Eli Zaretskii
2020-09-14 15:04 ` Lars Ingebrigtsen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191018181813.21848-1-pablo.barbachano@gmail.com \
--to=pablo.barbachano@gmail.com \
--cc=37814@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.