Subject: [PATCH] fix term color reset handling
From: Daniel Colascione <[email-masked]>
To: [email-masked]
Date: Sun, 29 Sep 2024 22:45:22 -0400
List-Id: <debbugs-submit.debbugs.gnu.org>

term responds to SGR0 by resetting term-current-face face to the
foreground and background of the current face instead of omitting these
attributes entirely.  Consequently, text inserted by term becomes
"opinionated" about what the foreground and background colors are and if
the default face changes, already-inserted-by-term text doesn't change
to match, since it has hardcoded hex color face text properties.

The patch below makes term behave more like other terminal emulators and
distinguish the absence of a foreground or background color from
specifying some specific foreground or background color.

commit 49de0c38598bf00db84029dbef8a94dcf6a27b21
Author: Daniel Colascione <[email-masked]>
Date:   Sun Sep 29 17:18:28 2024 -0400

    Track current terminal colors better

diff --git a/lisp/term.el b/lisp/term.el
index 9a8dc25e1a2..7b9886e720a 100644
--- a/lisp/term.el
+++ b/lisp/term.el
@@ -732,9 +732,9 @@ term-ansi-current-italic
 (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)
 
@@ -1084,9 +1084,9 @@ term-ansi-reset
   (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)
 
@@ -3430,19 +3430,21 @@ term-reset-terminal
 (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
@@ -3499,7 +3501,7 @@ term--handle-colors-list
          (_ (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)))
@@ -3529,7 +3531,7 @@ term--handle-colors-list
          (_ (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))))
@@ -3541,10 +3543,10 @@ term--handle-colors-list
       (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