Subject: Re: bug#26909: 25.1; A face for margins

From: Clément Pit-Claudel <[email-masked]>
Date: Mon, 11 May 2020 17:01:23 -0400

Hi Eli and Yuri,

On 13/05/2017 10.27, Eli Zaretskii wrote:>> From: Yuri Khan <[email-masked]>
>> Date: Sat, 13 May 2017 21:07:03 +0700
>>
>> The last two actions demonstrate that customizing the faces of
>> whatever is displayed in the margin is not sufficient. A mechanism is
>> needed that will allow customization of the margin where nothing is
>> displayed. A new face would serve nicely.
> 
> A face can only affect places where something is displayed using that
> face.  Display margins only display text if the buffer specifies text
> properties or overlays which display in the margins.  But what you
> would like to do calls for having a face that would affect screen
> space where _nothing_ is displayed, and such screen space in Emacs is
> always displayed using the frame's background color, not by using some
> face.
> 
> IOW, I don't think introducing a new face would help here.  Some
> additional mechanism would be necessary.

A margin face would be great to have.  
How reasonable would it be to fill the margins with a stretched space?  Would it be too costly?
Currently we almost do that in extend_face_to_end_of_line, but short-circuits earlier in that function mean that this part is only applicable when there is e.g. a region.

  if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
      && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
    {
      it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
      it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
        default_face->id;
      it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
    }
  if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
      && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
    {
      it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
      it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
        default_face->id;
      it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
    }

The (silly) attached patch confirms that removing the short-circuits makes it possible to set a face in the margins, but besides the performance aspect it only applies to lines that have contents.

Is this a reasonable way to go? If not, what might be better way?

Clément.
>From a8a13cec225b00117557e5c84999a877843099da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20Pit-Claudel?= <[email-masked]>
Date: Mon, 11 May 2020 16:52:51 -0400
Subject: [PATCH] Add a margin face, remove short-circuits in
 extend_face_to_end_of_line

---
 src/dispextern.h |  1 +
 src/xdisp.c      | 10 ++++++----
 src/xfaces.c     |  3 +++
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/dispextern.h b/src/dispextern.h
index 0b1f3d14ae..2650d49e02 100644
--- a/src/dispextern.h
+++ b/src/dispextern.h
@@ -1814,6 +1814,7 @@ #define FACE_UNIBYTE_P(FACE) ((FACE)->charset < 0)
   MODE_LINE_INACTIVE_FACE_ID,
   TOOL_BAR_FACE_ID,
   FRINGE_FACE_ID,
+  MARGIN_FACE_ID,
   HEADER_LINE_FACE_ID,
   SCROLL_BAR_FACE_ID,
   BORDER_FACE_ID,
diff --git a/src/xdisp.c b/src/xdisp.c
index 140d134572..5a7bf58828 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -21759,7 +21759,8 @@ extend_face_to_end_of_line (struct it *it)
       /* If the window has display margins, we will need to extend
 	 their face even if the text area is filled.  */
       && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
-	   || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
+	   || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
+      && false)
     return;
 
   const int extend_face_id = (it->face_id == DEFAULT_FACE_ID
@@ -21785,7 +21786,8 @@ extend_face_to_end_of_line (struct it *it)
       && !face->stipple
 #endif
       && !it->glyph_row->reversed_p
-      && !Vdisplay_fill_column_indicator)
+      && !Vdisplay_fill_column_indicator
+      && false)
     return;
 
   /* Set the glyph row flag indicating that the face of the last glyph
@@ -21834,7 +21836,7 @@ extend_face_to_end_of_line (struct it *it)
 	    {
 	      it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
 	      it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
-		default_face->id;
+		lookup_named_face (it->w, f, Qmargin, false);
 	      it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
 	    }
 	  if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
@@ -21842,7 +21844,7 @@ extend_face_to_end_of_line (struct it *it)
 	    {
 	      it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
 	      it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
-		default_face->id;
+		lookup_named_face (it->w, f, Qmargin, false);
 	      it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
 	    }
 
diff --git a/src/xfaces.c b/src/xfaces.c
index bab142ade0..f604c43928 100644
--- a/src/xfaces.c
+++ b/src/xfaces.c
@@ -4768,6 +4768,7 @@ lookup_basic_face (struct window *w, struct frame *f, int face_id)
     case TAB_BAR_FACE_ID:		name = Qtab_bar;		break;
     case TOOL_BAR_FACE_ID:		name = Qtool_bar;		break;
     case FRINGE_FACE_ID:		name = Qfringe;			break;
+    case MARGIN_FACE_ID:		name = Qmargin;			break;
     case SCROLL_BAR_FACE_ID:		name = Qscroll_bar;		break;
     case BORDER_FACE_ID:		name = Qborder;			break;
     case CURSOR_FACE_ID:		name = Qcursor;			break;
@@ -5463,6 +5464,7 @@ realize_basic_faces (struct frame *f)
       realize_named_face (f, Qmode_line_inactive, MODE_LINE_INACTIVE_FACE_ID);
       realize_named_face (f, Qtool_bar, TOOL_BAR_FACE_ID);
       realize_named_face (f, Qfringe, FRINGE_FACE_ID);
+      realize_named_face (f, Qmargin, MARGIN_FACE_ID);
       realize_named_face (f, Qheader_line, HEADER_LINE_FACE_ID);
       realize_named_face (f, Qscroll_bar, SCROLL_BAR_FACE_ID);
       realize_named_face (f, Qborder, BORDER_FACE_ID);
@@ -6808,6 +6810,7 @@ syms_of_xfaces (void)
   DEFSYM (Qtool_bar, "tool-bar");
   DEFSYM (Qtab_bar, "tab-bar");
   DEFSYM (Qfringe, "fringe");
+  DEFSYM (Qmargin, "margin");
   DEFSYM (Qtab_line, "tab-line");
   DEFSYM (Qheader_line, "header-line");
   DEFSYM (Qscroll_bar, "scroll-bar");
-- 
2.17.1