emacs-31 4d2701ecde9: Fix 'format-mode-line' when faces are in format string

Eli Zaretskii <[email protected]> Sun, 28 Jun 2026 08:00:30 -0400 (EDT)
Newsgroups gmane.emacs.diffs
Message-ID <[email protected]>
branch: emacs-31
commit 4d2701ecde9c3dfb7221ba6426c25dd4ca314033
Author: Eli Zaretskii <[email protected]>
Commit: Eli Zaretskii <[email protected]>

    Fix 'format-mode-line' when faces are in format string
    
    * src/xdisp.c (store_mode_line_string): Don't assume that PROPS
    can only specify the face for LISP_STRING; if PROPS don't specify
    a face, fall back on the 'face' property of LISP_STRING.
    (Bug#81316)
    
    * test/src/xdisp-tests.el (xdisp-test-format-mode-line): Add a
    test for this issue.
---
 src/xdisp.c             | 18 ++++++++++++++----
 test/src/xdisp-tests.el |  8 +++++++-
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/src/xdisp.c b/src/xdisp.c
index a88f7ce830a..8513139d77e 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -28934,14 +28934,24 @@ store_mode_line_string (const char *string, Lisp_Object lisp_string,
       if (!NILP (mode_line_string_face))
 	{
 	  Lisp_Object face;
-	  if (NILP (props))
-	    props = Ftext_properties_at (make_fixnum (0), lisp_string);
-	  face = plist_get (props, Qface);
+	  Lisp_Object string_face =
+	    plist_get (Ftext_properties_at (make_fixnum (0), lisp_string),
+		       Qface);
+	  /* Use the face in PROPS, if any, falling back to the face of
+	     LISP_STRING.  */
+	  face = string_face;
+	  if (!NILP (props))
+	    {
+	      Lisp_Object propface = plist_get (props, Qface);
+	      if (!NILP (propface))
+		face = propface;
+	    }
 	  if (NILP (face))
 	    face = mode_line_string_face;
 	  else
 	    face = list2 (face, mode_line_string_face);
-	  props = list2 (Qface, face);
+	  props = Fcopy_sequence (props);
+	  props = plist_put (props, Qface, face);
 	  if (copy_string)
 	    lisp_string = Fcopy_sequence (lisp_string);
 	}
diff --git a/test/src/xdisp-tests.el b/test/src/xdisp-tests.el
index 4369c00ca34..04dcfd6f59f 100644
--- a/test/src/xdisp-tests.el
+++ b/test/src/xdisp-tests.el
@@ -190,6 +190,12 @@ int main () {
     (insert (format-mode-line
              (propertize "x" 'face 'bold-italic)
              1200000000000000000000000000))
-    (should (null (get-text-property 1 'face)))))
+    (should (null (get-text-property 1 'face))))
+  (should
+   (equal
+    (text-properties-at
+     0
+     (format-mode-line '((:propertize "Hello!" face bold)) 'mode-line))
+    (list 'face '(bold mode-line) 'mode-line-elt-no 3))))
 
 ;;; xdisp-tests.el ends here