Changing the syntax highlighting for \e?

Is there a way I can change the color of \e and \r characters in my log file?

image

Firstly, I do not want to use set-face global Error to change my error colors. This does work, but than my Error face doesn’t highlight errors.

Here’s what I tried:

hook global WinSetOption filetype=.* %{
	add-highlighter window/ansi regex 'e\[[;0-9]+m' '0:Whitespace'
}

As you can see from my screenshot (above) this approach mostly worked. In the \e[32m string, the [32m part did get changed, but the actual. \e character did not.

Edit: I accidentally hit <tab><space> instead of <backtick><space> which posted my message before I finished typing it.

\e and (stray) \r characters are not errors in the sense of “a human-readable error message”, but they are errors in the sense that a proper Unix text file is not supposed to include those bytes. You get the same effect when a text-file includes corrupt UTF-8 sequences, and for the same reason: a text file is not supposed to include such things.

For handling ANSI formatting codes, you might want to try the kak-ansi plugin which converts them into Kakoune highlighters.

If you just want to remove them, you might do something like:

def delete-terminal-codes %{
    # Delete anything that looks like a CSI terminal code
    try %{ exec -draft '%s\e\[[;0-9]+[A-Za-z]<ret>d' }
    # Change bare CRs to LFs
    try %{ exec -draft '%s\r<ret>r<ret>' }
}

…and then you can call :delete-terminal-codes manually, or (if the output comes from an external proccess being piped into Kakoune through a FIFO) add it to a hook like BufCloseFifo so it’ll automatically run when the process is complete.

I actually don’t mind that this file includes these bytes because these are actually log files. So if I grep something from a few different log files, it’s kind of nice to have the extra color output. The only problem is viewing these log files in kak isn’t great.

So I don’t mind seeing the characters explicitly there in the log file (much like how I can see space and tab characters), I just don’t want it to use my Error face. Is there a simple way just to change the color of the escape character? I’m surprised what I tried didn’t work.

I’ve been trying to avoid using plugins for kakoune so I can figure out how things are done, so I’ll start digging through kak-ansi to see how that plugin accomplishes it. Thanks!

There’s no way to change how broken characters are displayed. In the expand_unprintable() function, everything that’s not a printable character or a newline is replaced, and that function hardcodes the replacement (�) and the face (Error). From the fact that this code processes “atoms”, I assume it runs after all the other highlighters have been run, which explains why it overrules your custom highlighter.

The usual ways to deal with something like this would be to:

  • ignore it
  • delete it (with something like the code fragment I pasted previously)
  • hide it (with some kind of replace-ranges highlighter)
  • interpret it (with the kak-ansi plugin or some trimmed-down equivalent)
1 Like