Subject: Re: bug#43381: 27.1.50; Maybe wrong cursor FG color in NS port

From: Alan Third <[email-masked]>
Date: Mon, 14 Sep 2020 00:37:16 +0100

On Sun, Sep 13, 2020 at 03:02:02PM -0500, Dale Sedivec wrote:
> The Emacs manual says[1], "the text shown under the cursor is drawn using
> the frame’s background color."  However, on the NS port, the foreground
> for text under the cursor seems to be taken from the face at point
> rather than the frame's background color.
> 
> [1]: https://www.gnu.org/software/emacs/manual/html_node/emacs/Cursor-Display.html
> 
> To illustrate this, start emacs -Q and eval:
> 
>     (progn
>       (switch-to-buffer (generate-new-buffer "test"))
>       (insert (propertize "testing"
>                           'face '((:background "blue" :foreground "white"))))
>       (goto-char 1))
> 
> You should be in a new buffer with some white-on-blue text.  Put point
> over any of that text and you should see the text under the default box
> cursor becomes (a hard-to-read) blue-on-black, taking the background
> color at point, rather than white-on-black, which it should be if it
> took the frame's background color.

The attached appears to fix it for me, however I'm not sure it's the
right fix. The X ports appear to have a much neater system for setting
the colours which may be worth emulating in the NS port.

-- 
Alan Third
>From 4c6509108bc2d3d9c37f88b1d915e8d04ce9a0c6 Mon Sep 17 00:00:00 2001
From: Alan Third <[email-masked]>
Date: Mon, 14 Sep 2020 00:19:05 +0100
Subject: [PATCH] Fix NS cursor colors (bug#43381)

* src/macfont.m (macfont_draw): Select the correct color when drawing
a cursor.
* src/nsterm.m (ns_draw_glyph_string): Don't fiddle with the face.
---
 src/macfont.m | 10 +++++++++-
 src/nsterm.m  | 14 --------------
 2 files changed, 9 insertions(+), 15 deletions(-)

diff --git a/src/macfont.m b/src/macfont.m
index 904814647f..a73c1f01fe 100644
--- a/src/macfont.m
+++ b/src/macfont.m
@@ -2923,7 +2923,15 @@ So we use CTFontDescriptorCreateMatchingFontDescriptor (no
       CGAffineTransform atfm;
 
       CGContextScaleCTM (context, 1, -1);
-      CG_SET_FILL_COLOR_WITH_FACE_FOREGROUND (context, face, s->f);
+
+      if (s->hl == DRAW_CURSOR && s->w->phys_cursor_type == FILLED_BOX_CURSOR)
+        {
+          struct face *frame_face = FRAME_DEFAULT_FACE (s->f);
+          CG_SET_FILL_COLOR_WITH_FACE_BACKGROUND (context, frame_face, s->f);
+        }
+      else
+        CG_SET_FILL_COLOR_WITH_FACE_FOREGROUND (context, face, s->f);
+
       if (macfont_info->synthetic_italic_p)
         atfm = synthetic_italic_atfm;
       else
diff --git a/src/nsterm.m b/src/nsterm.m
index 26059ab67c..7e3fb4a0cb 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -4397,13 +4397,6 @@ overwriting cursor (usually when cursor on a tab) */
         ns_maybe_dumpglyphs_background
           (s, s->first_glyph->type == COMPOSITE_GLYPH);
 
-      if (s->hl == DRAW_CURSOR && s->w->phys_cursor_type == FILLED_BOX_CURSOR)
-        {
-          unsigned long tmp = NS_FACE_BACKGROUND (s->face);
-          NS_FACE_BACKGROUND (s->face) = NS_FACE_FOREGROUND (s->face);
-          NS_FACE_FOREGROUND (s->face) = tmp;
-        }
-
       {
         BOOL isComposite = s->first_glyph->type == COMPOSITE_GLYPH;
 
@@ -4424,13 +4417,6 @@ overwriting cursor (usually when cursor on a tab) */
         ns_draw_text_decoration (s, s->face, col, s->width, s->x);
       }
 
-      if (s->hl == DRAW_CURSOR && s->w->phys_cursor_type == FILLED_BOX_CURSOR)
-        {
-          unsigned long tmp = NS_FACE_BACKGROUND (s->face);
-          NS_FACE_BACKGROUND (s->face) = NS_FACE_FOREGROUND (s->face);
-          NS_FACE_FOREGROUND (s->face) = tmp;
-        }
-
       ns_unfocus (s->f);
       break;
 
-- 
2.26.1