Subject: bug#54774: 28.1; ansi-color with no colors in Emacs-28

From: Thierry Volpiatto <[email-masked]>
Date: Sat, 09 Apr 2022 18:18:18 +0000

Jim Porter <[email-masked]> writes:

> On 4/9/2022 12:25 AM, Thierry Volpiatto wrote:
>> Apart advicing `ansi-color-get-face-1` I couldn't find how to fix my
>> code as your changes are binding ansi-code 5 to `ansi-color-slow-blink`
>> face which create unwanted squares.
>
> I had a chance to look into this a bit more.

Many thanks to take the time to look into this.

> ansi-color.el has mis-parsed 256-color and 24-bit color escapes for a
> long time. For example, you can try this under Emacs 27.2:
>
>   (ansi-color-apply "\033[38;5;2mhi\033[0m")
>     -> #("hi" 0 2 (font-lock-face success))
>
> This should actually be equivalent to the 8-color green value (ANSI
> SGR value 32). (Note: Under Emacs 27.2, the "slow blink" face is
> mapped to `success', whereas in 28.1, it's mapped to
> `ansi-color-slow-blink'. In Emacs 29, this properly sets the
> foreground to green.)
>
> Normally this isn't an issue, since Emacs' shell/term modes indicate
> that they only support 8-color mode, so commands usually don't emit 
> 256-color sequences. However, in your case this looks to be some text
> from a web service, so it obviously can't inspect your terminal 
> capabilities (unless there's a way to send them in the request?).

Ok, thanks for explaining.

> Looking at your code in more detail, I think you just got lucky that
> things worked ok in Emacs 27. See here:
>
>             (while (re-search-forward "38;5;\\([0-9]+\\)m" nil t)
>                ;; ...
>                (replace-match (pcase (match-string 1)
>                                ("154" "32")
>                                ("190" "31")
>                                ("118" "32")
>                                ("208" "37")
>                                ("202" "34")
>                                ("214" "35")
>                                ("220" "36")
>                                ("226" "33")
>                                (r     r))
>                              t t nil 1))
>
> This replaces an ANSI escape like "\033[38;5;154m" with
> "\033[38;5;32m". So the 256-color ANSI escape is still there, just
> slightly transformed. I think you want the result to be "\033[32m"
> instead, so that you're not using 256-color escape sequences at all.
>
> If you adjust the regexp/replacement to remove the "38;5;" bit, I
> think your existing code should work fine in both Emacs 27 and 28.

Indeed following your advice it work fine with both Emacs-28 and
Emacs-27:

    (while (re-search-forward "\\(38;5;[0-9]+\\)m" nil t)
      (when (< emacs-major-version 29)
        (replace-match (pcase (match-string 1)
                         ("38;5;154" "32")             ; green
                         ("38;5;190" "31")             ; red
                         ("38;5;118" "32")             ; green
                         ("38;5;208" "37")             ; white
                         ("38;5;202" "34")             ; blue
                         ("38;5;214" "35")             ; magenta
                         ("38;5;220" "36")             ; cyan
                         ("38;5;226" "33")             ; yellow
                         (r     r))
                       t t nil 1)))

-- 
Thierry