Subject: bug#18618: 25.0.50; `window-end win t` produces erroenous result with `window-scroll-functions` hook.

From: Eli Zaretskii <[email-masked]>
Date: Sat, 29 May 2021 09:24:23 +0300

tags 18618 notabug
close 18618
thanks

> From: Lars Ingebrigtsen <[email-masked]>
> Date: Sat, 29 May 2021 05:37:18 +0200
> Cc: [email-masked]
> 
> Keith David Bershatsky <[email-masked]> writes:
> 
> > Steps to reproduce the issue.
> >
> > 1.  Create a function that reports (e.g., a message) the value of `(window-end win t)` and attach that function to the `window-scroll-functions` hook.
> >
> > 2.  Open a long file in either fundamental-mode or text-mode.
> >
> > 3.  M-x end-of-buffer
> >
> > 4.  M-x beginning-of-buffer
> >
> > The result of step 4 reports an erroneous window-end value that is at
> > the very end of the buffer, instead of the correct window-end (i.e.,
> > which is much closer to the beginning of the buffer).
> 
> (I'm going through old bug reports that unfortunately got no response at
> the time.)
> 
> This problem is still present in Emacs 28.  Here's an easier test case:
> 
> (defun foo (win _)
>   (message "End: %s" (window-end win t))
>   nil)
> (push 'foo window-scroll-functions)
> 
> This reports the same number in both 3) and 4) when transient-mark-mode
> is switched on, but not when it's off.  It's also correct if that mode
> is on, and the region is active.
> 
> I haven't tried to debug further -- perhaps it's immediately obvious to
> somebody what could be causing this glitch?

This isn't supposed to work.  The doc string of window-end says:

  Return position at which display currently ends in WINDOW.
  WINDOW must be a live window and defaults to the selected one.
  This is updated by redisplay, when it runs to completion.
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

By contrast, window-scroll-functions is a hook run by the display
engine in the middle of redisplaying a window, when the display engine
concludes that it is about to scroll the window.  At that point, the
window's redisplay is by definition not complete yet, so this can only
work by chance.  Which is why window-scroll-functions' doc string says
explicitly this doesn't work:

  Note that the value of ‘window-end’ is not valid when these functions are
  called.

So Emacs behaves here as designed and as documented, and I'm therefore
closing this bug.