Subject: bug#45380: 28.0.50; Error in coloured output in Emacs 28.05(AUR package: emacs-git)

From: Lars Ingebrigtsen <[email-masked]>
Date: Sun, 24 Jan 2021 00:09:34 +0100

Utkarsh Singh <[email-masked]> writes:

> In the attached screenshot I am trying to compare eshell behaviour
> for 2 colored outputs. On the left hand side 'pacman -Ss emacs' command
> generates a small coloured output which works as expected but on the
> right hand side 'yay -Ss emacs' generates a fairly large output in which
> half of the colour in distorted. Screenshot have been obtained using
> 'emacs -Q' command.

I've seen similar bug reports about Emacs losing track of ANSI
highlighting -- especially when using programs like pacman, but I've so
far not been able to track down when/why this happens.  My theory is
that the ansi-color-apply-on-region state machine somehow becomes
confused.

If this is repeatable to you, could you apply the following patch to
record the data, and then go to some buffer,

M-: (pp my-replay (current-buffer))

write the buffer to a file, gzip it, and then send it as an attachment
to this debbugs address?

diff --git a/lisp/ansi-color.el b/lisp/ansi-color.el
index e5bfccdb8b..e1fcd3d2ce 100644
--- a/lisp/ansi-color.el
+++ b/lisp/ansi-color.el
@@ -363,6 +363,8 @@ ansi-color-filter-region
 	  (setq ansi-color-context-region (list nil (match-beginning 0)))
 	(setq ansi-color-context-region nil)))))
 
+(defvar my-replay nil)
+
 (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.
@@ -384,6 +386,7 @@ ansi-color-apply-on-region
 
 If PRESERVE-SEQUENCES is t, the sequences are hidden instead of
 being deleted."
+  (push (buffer-substring begin end) my-replay)
   (let ((codes (car ansi-color-context-region))
         (start-marker (or (cadr ansi-color-context-region)
                           (copy-marker begin)))

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