Subject: Re: bug#50660: 28.0.50; Text artifacting when the cursor moves over text under mouse face that originally displayed a box

From: Po Lu <[email-masked]>
Date: Thu, 14 Oct 2021 18:52:48 +0800

Eli Zaretskii <[email-masked]> writes:

> It looks like something is still amiss: the cursor blinking display is
> incorrect in some cases.  For example, evaluate this in a buffer under
> Fundamental mode:
>
>   (insert (propertize "some sample text" 'face '(:box 10) 'mouse-face 'highlight))
>
> and then put the mouse pointer above the text, so it's highlighted,
> and move the text cursor to the first 's' or the last 't'.  As long as
> the cursor blinks, you will see two characters drawn in the cursor
> face, not one as expected.

Thanks, here's a patch that should fix the issues, formatted with "git
format-patch", but with one caveat: in your example above, the
background of the character "s" at the beginning of the string "some
sample text" is drawn too wide, but I wasn't able to find the problem.

Could you please take a look at it?  Thanks.

I don't know how to apply the fixes to xterm.c to the other window
systems, so someone who can needs to apply them to the NS and MS-Windows
ports.

>From 6d277be789bf30e0db5b27780bff86151e48f622 Mon Sep 17 00:00:00 2001
From: oldosfan <[email-masked]>
Date: Thu, 14 Oct 2021 18:38:26 +0800
Subject: [PATCH] Fix minor issues with text display when cursor is in mouse
 face

 * src/xdisp.c (get_cursor_offset_for_mouse_face): Don't calculate
offsets for the glyph the cursor is on.

 * src/xterm.c (x_draw_glyph_string_foreground,
x_draw_composite_glyph_string_foreground,
x_draw_glyphless_glyph_string_foreground,
x_draw_image_foreground,
x_draw_image_foreground_1): Take mouse face into account when
offsetting X coordinate by the vertical line width.
---
 src/xdisp.c |   2 +-
 src/xterm.c | 107 ++++++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 93 insertions(+), 16 deletions(-)

diff --git a/src/xdisp.c b/src/xdisp.c
index 012c2ad8bf..0d964b1236 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -36042,7 +36042,7 @@ get_cursor_offset_for_mouse_face (struct window *w, struct glyph_row *row,
   /* Calculate the offset to correct phys_cursor x if we are
      drawing the cursor inside mouse-face highlighted text.  */
 
-  for (; row->reversed_p ? start >= end : start <= end;
+  for (; row->reversed_p ? start > end : start < end;
        row->reversed_p ? --start : ++start)
     {
       struct glyph *g = start;
diff --git a/src/xterm.c b/src/xterm.c
index 89885e0d88..d19f214019 100644
--- a/src/xterm.c
+++ b/src/xterm.c
@@ -1799,11 +1799,24 @@ x_draw_glyph_string_foreground (struct glyph_string *s)
 {
   int i, x;
 
+  struct face *face_for_box_line = s->face;
+
+  if (s->hl == DRAW_CURSOR && cursor_in_mouse_face_p (s->w))
+    {
+      /* In this situation, the cursor is in the mouse face, but
+	 s->face hasn't been updated with the mouse face yet. */
+      face_for_box_line =
+	FACE_FROM_ID_OR_NULL (s->f, MOUSE_HL_INFO (s->f)->mouse_face_face_id);
+
+      if (!face_for_box_line)
+	face_for_box_line = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
+    }
+
   /* If first glyph of S has a left box line, start drawing the text
      of S to the right of that box line.  */
-  if (s->face->box != FACE_NO_BOX
+  if (face_for_box_line->box != FACE_NO_BOX
       && s->first_glyph->left_box_line_p)
-    x = s->x + max (s->face->box_vertical_line_width, 0);
+    x = s->x + max (face_for_box_line->box_vertical_line_width, 0);
   else
     x = s->x;
 
@@ -1893,11 +1906,24 @@ x_draw_composite_glyph_string_foreground (struct glyph_string *s)
   int i, j, x;
   struct font *font = s->font;
 
+  struct face *face_for_box_line = s->face;
+
+  if (s->hl == DRAW_CURSOR && cursor_in_mouse_face_p (s->w))
+    {
+      /* In this situation, the cursor is in the mouse face, but
+	 s->face hasn't been updated with the mouse face yet. */
+      face_for_box_line =
+	FACE_FROM_ID_OR_NULL (s->f, MOUSE_HL_INFO (s->f)->mouse_face_face_id);
+
+      if (!face_for_box_line)
+	face_for_box_line = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
+    }
+
   /* If first glyph of S has a left box line, start drawing the text
      of S to the right of that box line.  */
-  if (s->face && s->face->box != FACE_NO_BOX
+  if (face_for_box_line->box != FACE_NO_BOX
       && s->first_glyph->left_box_line_p)
-    x = s->x + max (s->face->box_vertical_line_width, 0);
+    x = s->x + max (face_for_box_line->box_vertical_line_width, 0);
   else
     x = s->x;
 
@@ -2004,11 +2030,24 @@ x_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
   unsigned char2b[8];
   int x, i, j;
 
+  struct face *face_for_box_line = s->face;
+
+  if (s->hl == DRAW_CURSOR && cursor_in_mouse_face_p (s->w))
+    {
+      /* In this situation, the cursor is in the mouse face, but
+	 s->face hasn't been updated with the mouse face yet. */
+      face_for_box_line =
+	FACE_FROM_ID_OR_NULL (s->f, MOUSE_HL_INFO (s->f)->mouse_face_face_id);
+
+      if (!face_for_box_line)
+	face_for_box_line = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
+    }
+
   /* If first glyph of S has a left box line, start drawing the text
      of S to the right of that box line.  */
-  if (s->face && s->face->box != FACE_NO_BOX
+  if (face_for_box_line->box != FACE_NO_BOX
       && s->first_glyph->left_box_line_p)
-    x = s->x + max (s->face->box_vertical_line_width, 0);
+    x = s->x + max (face_for_box_line->box_vertical_line_width, 0);
   else
     x = s->x;
 
@@ -3073,12 +3112,25 @@ x_draw_image_foreground (struct glyph_string *s)
   int x = s->x;
   int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
 
+  struct face *face_for_box_line = s->face;
+
+  if (s->hl == DRAW_CURSOR && cursor_in_mouse_face_p (s->w))
+    {
+      /* In this situation, the cursor is in the mouse face, but
+	 s->face hasn't been updated with the mouse face yet. */
+      face_for_box_line =
+	FACE_FROM_ID_OR_NULL (s->f, MOUSE_HL_INFO (s->f)->mouse_face_face_id);
+
+      if (!face_for_box_line)
+	face_for_box_line = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
+    }
+
   /* If first glyph of S has a left box line, start drawing it to the
-     right of that line.  */
-  if (s->face->box != FACE_NO_BOX
+     right of that box line.  */
+  if (face_for_box_line->box != FACE_NO_BOX
       && s->first_glyph->left_box_line_p
       && s->slice.x == 0)
-    x += max (s->face->box_vertical_line_width, 0);
+    x += max (face_for_box_line->box_vertical_line_width, 0);
 
   /* If there is a margin around the image, adjust x- and y-position
      by that margin.  */
@@ -3191,13 +3243,25 @@ x_draw_image_relief (struct glyph_string *s)
   XRectangle r;
   int x = s->x;
   int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
+  struct face *face_for_box_line = s->face;
+
+  if (s->hl == DRAW_CURSOR && cursor_in_mouse_face_p (s->w))
+    {
+      /* In this situation, the cursor is in the mouse face, but
+	 s->face hasn't been updated with the mouse face yet. */
+      face_for_box_line =
+	FACE_FROM_ID_OR_NULL (s->f, MOUSE_HL_INFO (s->f)->mouse_face_face_id);
+
+      if (!face_for_box_line)
+	face_for_box_line = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
+    }
 
   /* If first glyph of S has a left box line, start drawing it to the
-     right of that line.  */
-  if (s->face->box != FACE_NO_BOX
+     right of that box line.  */
+  if (face_for_box_line->box != FACE_NO_BOX
       && s->first_glyph->left_box_line_p
       && s->slice.x == 0)
-    x += max (s->face->box_vertical_line_width, 0);
+    x += max (face_for_box_line->box_vertical_line_width, 0);
 
   /* If there is a margin around the image, adjust x- and y-position
      by that margin.  */
@@ -3282,12 +3346,25 @@ x_draw_image_foreground_1 (struct glyph_string *s, Pixmap pixmap)
   int x = 0;
   int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
 
+  struct face *face_for_box_line = s->face;
+
+  if (s->hl == DRAW_CURSOR && cursor_in_mouse_face_p (s->w))
+    {
+      /* In this situation, the cursor is in the mouse face, but
+	 s->face hasn't been updated with the mouse face yet. */
+      face_for_box_line =
+	FACE_FROM_ID_OR_NULL (s->f, MOUSE_HL_INFO (s->f)->mouse_face_face_id);
+
+      if (!face_for_box_line)
+	face_for_box_line = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
+    }
+
   /* If first glyph of S has a left box line, start drawing it to the
-     right of that line.  */
-  if (s->face->box != FACE_NO_BOX
+     right of that box line.  */
+  if (face_for_box_line->box != FACE_NO_BOX
       && s->first_glyph->left_box_line_p
       && s->slice.x == 0)
-    x += max (s->face->box_vertical_line_width, 0);
+    x += max (face_for_box_line->box_vertical_line_width, 0);
 
   /* If there is a margin around the image, adjust x- and y-position
      by that margin.  */
-- 
2.31.1