Subject: Re: bug#5718: scroll-margin in buffer with small line count.

From: [email-masked]
Date: Sat, 21 Jan 2017 13:46:37 -0500

Eli Zaretskii <[email-masked]> writes:

>
> There's already such a calculation in window_scroll_pixel_based:
>
>       /* See if point is on a partially visible line at the end.  */
>       if (it.what == IT_EOB)
> 	partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
>       else
> 	{
> 	  move_it_by_lines (&it, 1);
> 	  partial_p = it.current_y > it.last_visible_y;
> 	}
>
> (This is preceded by moving the iterator to the point's screen line or
> to EOB, whichever comes first.)  The value it.current_y is the Y
> coordinate of the top edge of a glyph row (the value is zero for the
> first screen line), so if it.last_visible_y is farther away from that
> than the height of the glyph row, that glyph row is fully visible;
> otherwise it isn't.

I'm not entirely clear why there is a branch in that code.  I came up
with this; it works, though I'm not sure if it's the best way:

>From 6944c7badade2477f381acf3b0922e5309e063d8 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <[email-masked]>
Date: Sat, 21 Jan 2017 13:24:47 -0500
Subject: [PATCH v3] Fix scrolling with partial lines

* src/xdisp.c (partial_line_height): New function.
(try_scrolling):
* src/window.c (window_scroll_pixel_based): Use it for calculating the
pixel scroll margin correctly in a window with partial lines.
---
 src/dispextern.h |  1 +
 src/window.c     |  2 +-
 src/xdisp.c      | 14 +++++++++++++-
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/dispextern.h b/src/dispextern.h
index 51222e6..470330a 100644
--- a/src/dispextern.h
+++ b/src/dispextern.h
@@ -3263,6 +3263,7 @@ void move_it_past_eol (struct it *);
 void move_it_in_display_line (struct it *it,
 			      ptrdiff_t to_charpos, int to_x,
 			      enum move_operation_enum op);
+int partial_line_height (const struct it *it_origin);
 bool in_display_vector_p (struct it *);
 int frame_mode_line_height (struct frame *);
 extern bool redisplaying_p;
diff --git a/src/window.c b/src/window.c
index f664597..300472e 100644
--- a/src/window.c
+++ b/src/window.c
@@ -5148,7 +5148,7 @@ window_scroll_pixel_based (Lisp_Object window, int n, bool whole, bool noerror)
 	 in the scroll margin at the bottom.  */
       move_it_to (&it, PT, -1,
 		  (it.last_visible_y - WINDOW_HEADER_LINE_HEIGHT (w)
-		   - this_scroll_margin - 1),
+                   - partial_line_height (&it)- this_scroll_margin - 1),
 		  -1,
 		  MOVE_TO_POS | MOVE_TO_Y);
 
diff --git a/src/xdisp.c b/src/xdisp.c
index 55bb34a..9dc65b8 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -9859,6 +9859,17 @@ move_it_by_lines (struct it *it, ptrdiff_t dvpos)
     }
 }
 
+int
+partial_line_height (const struct it *it_origin)
+{
+  struct it it = *it_origin;
+  move_it_to (&it, ZV, -1, it.last_visible_y, -1,
+              MOVE_TO_POS | MOVE_TO_Y);
+  int vis_height = it.last_visible_y - it.current_y;
+  int log_height = it.ascent + it.descent;
+  return max (0, log_height - vis_height);
+}
+
 /* Return true if IT points into the middle of a display vector.  */
 
 bool
@@ -15368,7 +15379,8 @@ try_scrolling (Lisp_Object window, bool just_this_one_p,
       /* Compute the pixel ypos of the scroll margin, then move IT to
 	 either that ypos or PT, whichever comes first.  */
       start_display (&it, w, startp);
-      scroll_margin_y = it.last_visible_y - this_scroll_margin
+      scroll_margin_y = it.last_visible_y - partial_line_height (&it)
+        - this_scroll_margin
 	- frame_line_height * extra_scroll_margin_lines;
       move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
 		  (MOVE_TO_POS | MOVE_TO_Y));
-- 
2.9.3