Subject: Re: bug#38181: Actual height of mode-line not taken into account

From: Eli Zaretskii <[email-masked]>
Date: Sat, 16 Nov 2019 18:19:55 +0200

> From: Jonas Bernoulli <[email-masked]>
> Cc: Eli Zaretskii <[email-masked]>, [email-masked]
> Date: Sat, 16 Nov 2019 16:27:12 +0100
> 
> >> Could fit-window-to-buffer invoke
> >> 'redisplay' internally, perhaps?
> >
> > It could but it's called way too often to warrant such behavior by
> > default.  But we could give it a separate optional argument so users
> > can avoid the advice.  I think Jonas could easily write and a test a
> > patch along this idea.
> 
> Again, the mode-line-prettifiers are not the ones who create new buffers
> and then call fit-buffer-to-window.  It's arbitrary other packages that
> do that.

So how are mode-line-prettifiers triggered by those packages creating
and showing new buffers?

> Of course fit-buffer-to-window itself could be changed to do that and it
> could also be taught to only do so iff the user opted in to doing it.

Can you suggest a way of knowing that this situation happened?

> Creating and displaying a new buffer and creating and resizing a new
> window surely *already* causes a "redisplay" without the programmer
> having to explicitly call `redisplay'.  So if we explicitly tell
> fit-window-to-buffer to redisplay, then that means that we are
> redisplaying twice, right?

Yes.  But if you don't call 'redisplay' _before_ fit-window-to-buffer,
that function will use stale data about the window's text area height,
computed before the mode line was updated.

You are right saying that displaying a window causes a redisplay, but
keep in mind that redisplay triggered by that happens _after_ the
command which enlarged the mode-line height finishes, and by that time
fit-window-to-buffer will have already run (using stale window
dimensions).

> I am under the impression (but this is just wild speculation) that
> redisplay only performs some of the necessary size calculations before
> doing the actual redisplaying.  But some other calculations (including
> those concerning the mode-lien) are done only after the actual
> redisplaying has already happened.  That is too late for this redisplay
> round but causes the values to be in place for all subsequent
> redisplays.  So the fix could be to do the mode-line based calculations
> earlier?

If you look at the code of redisplay_window, which is the function
that handles redisplay of each window, you will see that it indeed
calls display_mode_lines near its end (because the mode line includes
constructs that depend on position of point and other state, and that
could change as result of redisplaying a window).  However, if after
calling display_mode_lines we detect that the height of the mode line
changed, we schedule an immediate thorough redisplay.  So your theory
doesn't sound correct, at least not when taken at face value.

And once again, please keep in mind that by the time redisplay runs
(without an explicit call to 'redisplay' inside the recipe you
posted), 'fit-window-to-buffer' was already called, and it already
used stale value of height stored in the window object.