From 81b0679d72fa99badcc414b74a0e43680d7bc4d7 Mon Sep 17 00:00:00 2001 From: Daniel Colascione Date: Wed, 2 Oct 2024 21:33:50 -0700 Subject: [PATCH] Track nil colors in term Teach term the difference between an unset foreground or background color and a color that happens to match an existing color. This way, when we insert text after a color reset:(e.g. from an SGR0), we insert it without :foreground or :background (whichever we've reset) face properties, allowing that inserted text to inherit the colors of the underlying face. This way, if we change, e.g., the background color of a buffer, the background color of already-inserted text from the term child changes along with that of the buffer instead of being "locked" to whatever the background color of the buffer was at the time the text was inserted. This change aligns term.el with other terminal emulators. * lisp/term.el (term-ansi-current-color): (term--color-as-hex): track nil fg, bg colors (cherry picked from commit 3d609375f99cd0d4b7a441802d4616bad385e31d) --- lisp/term.el | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/lisp/term.el b/lisp/term.el index cc896acaabf..b4e159d09fc 100644 --- a/lisp/term.el +++ b/lisp/term.el @@ -729,9 +729,9 @@ Buffer local variable.") (defvar term-ansi-current-underline nil) (defvar term-ansi-current-slow-blink nil) (defvar term-ansi-current-fast-blink nil) -(defvar term-ansi-current-color 0) +(defvar term-ansi-current-color nil) (defvar term-ansi-face-already-done nil) -(defvar term-ansi-current-bg-color 0) +(defvar term-ansi-current-bg-color nil) (defvar term-ansi-current-reverse nil) (defvar term-ansi-current-invisible nil) @@ -1080,9 +1080,9 @@ For custom keybindings purposes please note there is also (setq term-ansi-current-slow-blink nil) (setq term-ansi-current-fast-blink nil) (setq term-ansi-current-reverse nil) - (setq term-ansi-current-color 0) + (setq term-ansi-current-color nil) (setq term-ansi-current-invisible nil) - (setq term-ansi-current-bg-color 0)) + (setq term-ansi-current-bg-color nil)) (defvar touch-screen-display-keyboard) @@ -3426,19 +3426,21 @@ option is enabled. See `term-set-goto-process-mark'." (defun term--color-as-hex (for-foreground) "Return the current ANSI color as a hexadecimal color string. Use the current background color if FOR-FOREGROUND is nil, -otherwise use the current foreground color." +otherwise use the current foreground color. Return nil if the +color is unset in the terminal state." (let ((color (if for-foreground term-ansi-current-color term-ansi-current-bg-color))) - (or (ansi-color--code-as-hex (1- color)) - (progn - (and ansi-color-bold-is-bright term-ansi-current-bold - (<= 1 color 8) - (setq color (+ color 8))) - (if for-foreground - (face-foreground (elt ansi-term-color-vector color) - nil 'default) - (face-background (elt ansi-term-color-vector color) - nil 'default)))))) + (when color + (or (ansi-color--code-as-hex (1- color)) + (progn + (and ansi-color-bold-is-bright term-ansi-current-bold + (<= 1 color 8) + (setq color (+ color 8))) + (if for-foreground + (face-foreground (elt ansi-term-color-vector color) + nil 'default) + (face-background (elt ansi-term-color-vector color) + nil 'default))))))) ;; New function to deal with ansi colorized output, as you can see you can ;; have any bold/underline/fg/bg/reverse combination. -mm @@ -3495,7 +3497,7 @@ otherwise use the current foreground color." (_ (term-ansi-reset)))) ;; Reset foreground (terminfo: op) - (39 (setq term-ansi-current-color 0)) + (39 (setq term-ansi-current-color nil)) ;; Background (terminfo: setab) ((and param (guard (<= 40 param 47))) @@ -3525,7 +3527,7 @@ otherwise use the current foreground color." (_ (term-ansi-reset)))) ;; Reset background (terminfo: op) - (49 (setq term-ansi-current-bg-color 0)) + (49 (setq term-ansi-current-bg-color nil)) ;; 0 (Reset) (terminfo: sgr0) or unknown (reset anyway) (_ (term-ansi-reset)))) @@ -3537,10 +3539,10 @@ otherwise use the current foreground color." (setq fg (term--color-as-hex t) bg (term--color-as-hex nil))) (setq term-current-face - `( :foreground ,fg - :background ,bg - ,@(unless term-ansi-current-invisible - (list :inverse-video term-ansi-current-reverse))))) + `(,@(when fg `(:foreground ,fg)) + ,@(when bg `(:background ,bg)) + ,@(unless term-ansi-current-invisible + (list :inverse-video term-ansi-current-reverse))))) (setq term-current-face `(,term-current-face -- 2.39.2