From 51c2b003d4671114988924bd8a6cec5f8522a1c3 Mon Sep 17 00:00:00 2001
From: Aaron Jensen <aaronjensen@gmail.com>
Date: Wed, 28 Feb 2018 07:17:14 -0800
Subject: [PATCH] Allow buffer local underline position vars

* src/nsterm.m (ns_draw_text_decoration):
* src/w32term.c (x_draw_glyph_string):
* src/xterm.c (x_draw_glyph_string): Allow buffer local underline
position vars

* src/xdisp.c: Add symbol
---
 src/nsterm.m  | 24 ++++++++++++++++++++----
 src/w32term.c | 23 ++++++++++++++++++++---
 src/xdisp.c   |  1 +
 src/xterm.c   | 24 ++++++++++++++++++++----
 4 files changed, 61 insertions(+), 11 deletions(-)

diff --git a/src/nsterm.m b/src/nsterm.m
index 1919c6defa..b95d3b25d9 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -3487,23 +3487,37 @@ Note that CURSOR_WIDTH is meaningful only for (h)bar cursors.
             {
 	      struct font *font = font_for_underline_metrics (s);
               unsigned long descent = s->y + s->height - s->ybase;
+              unsigned long minimum_offset;
+              BOOL underline_at_descent_line, use_underline_position_properties;
+              Lisp_Object val = buffer_local_value (Qunderline_minimum_offset,
+                                                    s->w->contents);
+              if (INTEGERP (val))
+                minimum_offset = XFASTINT (val);
+              else
+                minimum_offset = 1;
+              val = buffer_local_value (Qx_underline_at_descent_line,
+                                        s->w->contents);
+              underline_at_descent_line = !(NILP (val) || EQ (val, Qunbound));
+              val = buffer_local_value (Qx_use_underline_position_properties,
+                                        s->w->contents);
+              use_underline_position_properties = !(NILP (val) || EQ (val, Qunbound));
 
               /* Use underline thickness of font, defaulting to 1. */
               thickness = (font && font->underline_thickness > 0)
                 ? font->underline_thickness : 1;
 
               /* Determine the offset of underlining from the baseline. */
-              if (x_underline_at_descent_line)
+              if (underline_at_descent_line)
                 position = descent - thickness;
-              else if (x_use_underline_position_properties
+              else if (use_underline_position_properties
                        && font && font->underline_position >= 0)
                 position = font->underline_position;
               else if (font)
                 position = lround (font->descent / 2);
               else
-                position = underline_minimum_offset;
+                position = minimum_offset;
 
-              position = max (position, underline_minimum_offset);
+              position = max (position, minimum_offset);
 
               /* Ensure underlining is not cropped. */
               if (descent <= position)
@@ -9465,11 +9479,13 @@ Nil means use fullscreen the old (< 10.7) way.  The old way works better with
 	       x_use_underline_position_properties,
      doc: /* SKIP: real doc in xterm.c.  */);
   x_use_underline_position_properties = 0;
+  DEFSYM (Qx_use_underline_position_properties, "x-use-underline-position-properties");
 
   DEFVAR_BOOL ("x-underline-at-descent-line",
 	       x_underline_at_descent_line,
      doc: /* SKIP: real doc in xterm.c.  */);
   x_underline_at_descent_line = 0;
+  DEFSYM (Qx_underline_at_descent_line, "x-underline-at-descent-line");
 
   /* Tell Emacs about this window system.  */
   Fprovide (Qns, Qnil);
diff --git a/src/w32term.c b/src/w32term.c
index 97afb678c1..47540ef72e 100644
--- a/src/w32term.c
+++ b/src/w32term.c
@@ -2475,13 +2475,28 @@ x_draw_glyph_string (struct glyph_string *s)
               else
                 {
 		  struct font *font = font_for_underline_metrics (s);
+                  unsigned long minimum_offset;
+                  BOOL underline_at_descent_line, use_underline_position_properties;
+                  Lisp_Object val = buffer_local_value (Qunderline_minimum_offset,
+                                                        s->w->contents);
+                  if (INTEGERP (val))
+                    minimum_offset = XFASTINT (val);
+                  else
+                    minimum_offset = 1;
+                  val = buffer_local_value (Qx_underline_at_descent_line,
+                                            s->w->contents);
+                  underline_at_descent_line = !(NILP (val) || EQ (val, Qunbound));
+                  val = buffer_local_value (Qx_use_underline_position_properties,
+                                            s->w->contents);
+                  use_underline_position_properties = !(NILP (val) || EQ (val, Qunbound));
 
                   /* Get the underline thickness.  Default is 1 pixel.  */
                   if (font && font->underline_thickness > 0)
                     thickness = font->underline_thickness;
                   else
                     thickness = 1;
-                  if (x_underline_at_descent_line || !font)
+                  if (underline_at_descent_line
+                      || !font)
                     position = (s->height - thickness) - (s->ybase - s->y);
                   else
                     {
@@ -2493,13 +2508,13 @@ x_draw_glyph_string (struct glyph_string *s)
                          ROUND ((maximum_descent) / 2), with
                          ROUND (x) = floor (x + 0.5)  */
 
-                      if (x_use_underline_position_properties
+                      if (use_underline_position_properties
                           && font->underline_position >= 0)
                         position = font->underline_position;
                       else
                         position = (font->descent + 1) / 2;
                     }
-                  position = max (position, underline_minimum_offset);
+                  position = max (position, minimum_offset);
                 }
               /* Check the sanity of thickness and position.  We should
                  avoid drawing underline out of the current line area.  */
@@ -7385,11 +7400,13 @@ the cursor have no effect.  */);
 	       x_use_underline_position_properties,
      doc: /* SKIP: real doc in xterm.c.  */);
   x_use_underline_position_properties = 0;
+  DEFSYM (Qx_use_underline_position_properties, "x-use-underline-position-properties");
 
   DEFVAR_BOOL ("x-underline-at-descent-line",
 	       x_underline_at_descent_line,
      doc: /* SKIP: real doc in xterm.c.  */);
   x_underline_at_descent_line = 0;
+  DEFSYM (Qx_underline_at_descent_line, "x-underline-at-descent-line");
 
   DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
 	       doc: /* SKIP: real doc in xterm.c.  */);
diff --git a/src/xdisp.c b/src/xdisp.c
index 9170d6b777..13f281e8ea 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -32978,6 +32978,7 @@ particularly when using variable `x-use-underline-position-properties'
 with fonts that specify an UNDERLINE_POSITION relatively close to the
 baseline.  The default value is 1.  */);
   underline_minimum_offset = 1;
+  DEFSYM (Qunderline_minimum_offset, "underline-minimum-offset");
 
   DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
 	       doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
diff --git a/src/xterm.c b/src/xterm.c
index 0d25c7f1a2..02f9e25c23 100644
--- a/src/xterm.c
+++ b/src/xterm.c
@@ -3707,13 +3707,27 @@ x_draw_glyph_string (struct glyph_string *s)
               else
                 {
 		  struct font *font = font_for_underline_metrics (s);
+                  unsigned long minimum_offset;
+                  BOOL underline_at_descent_line, use_underline_position_properties;
+                  Lisp_Object val = buffer_local_value (Qunderline_minimum_offset,
+                                                        s->w->contents);
+                  if (INTEGERP (val))
+                    minimum_offset = XFASTINT (val);
+                  else
+                    minimum_offset = 1;
+                  val = buffer_local_value (Qx_underline_at_descent_line,
+                                            s->w->contents);
+                  underline_at_descent_line = !(NILP (val) || EQ (val, Qunbound));
+                  val = buffer_local_value (Qx_use_underline_position_properties,
+                                            s->w->contents);
+                  use_underline_position_properties = !(NILP (val) || EQ (val, Qunbound));
 
                   /* Get the underline thickness.  Default is 1 pixel.  */
                   if (font && font->underline_thickness > 0)
                     thickness = font->underline_thickness;
                   else
                     thickness = 1;
-                  if (x_underline_at_descent_line)
+                  if (underline_at_descent_line)
                     position = (s->height - thickness) - (s->ybase - s->y);
                   else
                     {
@@ -3725,15 +3739,15 @@ x_draw_glyph_string (struct glyph_string *s)
                          ROUND ((maximum descent) / 2), with
                          ROUND(x) = floor (x + 0.5)  */
 
-                      if (x_use_underline_position_properties
+                      if (use_underline_position_properties
                           && font && font->underline_position >= 0)
                         position = font->underline_position;
                       else if (font)
                         position = (font->descent + 1) / 2;
                       else
-                        position = underline_minimum_offset;
+                        position = minimum_offset;
                     }
-                  position = max (position, underline_minimum_offset);
+                  position = max (position, minimum_offset);
                 }
               /* Check the sanity of thickness and position.  We should
                  avoid drawing underline out of the current line area.  */
@@ -13246,6 +13260,7 @@ UNDERLINE_POSITION font properties, set this to nil.  You can also use
 `underline-minimum-offset' to override the font's UNDERLINE_POSITION for
 small font display sizes.  */);
   x_use_underline_position_properties = true;
+  DEFSYM (Qx_use_underline_position_properties, "x-use-underline-position-properties");
 
   DEFVAR_BOOL ("x-underline-at-descent-line",
 	       x_underline_at_descent_line,
@@ -13256,6 +13271,7 @@ A value of nil means to draw the underline according to the value of the
 variable `x-use-underline-position-properties', which is usually at the
 baseline level.  The default value is nil.  */);
   x_underline_at_descent_line = false;
+  DEFSYM (Qx_underline_at_descent_line, "x-underline-at-descent-line");
 
   DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
 	       x_mouse_click_focus_ignore_position,
-- 
2.15.1

