less sends its own "reset" at the end of the line, which happens to be derived from the terminfo
sgr0 by (ncurses) eliminating the
^O (reset alternate character set) because
less is using the termcap interface. The termcap capability me which corresponds to terminfo sgr0 conventionally doesn't modify the alternate character set state, as noted in the manual page curs_termcap(3x):
Note that termcap has nothing analogous to terminfo's
sgr string. One
consequence of this is that termcap applications assume
sgr0) does not reset the alternate character set. This implementation
checks for, and modifies the data shown to the termcap interface to ac-
commodate termcap's limitation in this respect.
less is doing that reset to recover from unexpected escape sequences: the
-R option is only designed to handle ANSI colors (and similarly-formatted escapes such as bold, underline, blink, standout). The source-code doesn't mention that, but the
A_NORMAL assignment tells
less to later emit the reset:
* Add a newline if necessary,
* and append a '\0' to the end of the line.
* We output a newline if we're not at the right edge of the screen,
* or if the terminal doesn't auto wrap,
* or if this is really the end of the line AND the terminal ignores
* a newline at the right edge.
* (In the last case we don't want to output a newline if the terminal
* doesn't ignore it since that would produce an extra blank line.
* But we do want to output a newline if the terminal ignores it in case
* the next line is blank. In that case the single newline output for
* that blank line would be ignored!)
if (column < sc_width || !auto_wrap || (endline && ignaw) || ctldisp == OPT_ON)
linebuf[curr] = '\n';
attr[curr] = AT_NORMAL;
As an alternative to
sgr0 (which resets all video attributes, and is only partly understood by less), you could do
and (for many terminals/many systems, including
TERM=screen-256color) reset just the underline. However, that does not affect bold, nor is there an conventional terminfo/termcap capability to reset bold. But screen implements the corresponding ECMA-48 sequence which does this (SGR 22 versus the 24 used in
rmul), so you could hardcode that case.