Subject: bug#22404: 25.1.50; Forcing `window-scroll-functions` to run.

From: Keith David Bershatsky <[email-masked]>
Date: Thu, 28 Jan 2016 19:08:22 -0800

Thanks, John, for taking another look at #22404.

Let's say for example that someone has a function named `foo` which uses `window-start` and `window-end`, and the user wants that function to run every command loop.  Everyone will immediately think of the `post-command-hook` because the buffer or position may have been modified by `this-command` (whatever that may be).  The way Emacs is presently designed, people are needlessly calling `foo` multiple times when scrolling occurs because the `post-command-hook` can't provide correct numbers.  Most people probably don't care because `foo` is trivial in terms of the time it takes to run -- so if it runs 3 times per command loop, no problem.  I personally think calling `foo` 3 times per command loop, when it only needed to run once, is a poor design.

There is no 100% guaranteed test from the `post-command-hook` to know whether the `window-scroll-functions` hook will run, and if so, whether it will run 2 times (instead of just one).

Here is an excerpt from `window.h` that tells us what `optional_new_start` is currently used for:

    /* True means we have explicitly changed the value of start,
       but that the next redisplay is not obliged to use the new value.
       This is used in Fdelete_other_windows to force a call to
       Vwindow_scroll_functions; also by Frecenter with argument.  */
    bool_bf optional_new_start : 1;

It is also used for `set-window-buffer`; so perhaps that should be added to the doc-string.

By setting `optional_new_start` to `true`, we force the `window_scroll_functions` hook to run every command loop (during redisplay).  If that happens, then the `post-command-hook` is no longer needed to run `foo`.  So we have just reduced `foo` being called 2 to 3 times, to 1 to 2 times instead.  I have been studying `xdisp.c` and have concluded that it is extremely difficult to know whether the `window-scroll-functions` hook will run 1 or 2 times, because that hook can modify the buffer and there are some other hooks that may modify the buffer too -- thus requiring a second call to the WSF.

So, the moral of the story is whether it is a good thing to call `foo` 2 to 3 times per command loop, instead of 1 to 2 times per command loop.  And, it boils down to whether `foo` is costly.  It is costly for me because I use `vertical-motion`.  It may be costly to other users for a different reason.

Another idea would be to create a new animal that permits users to THROW_ANY_SWITCH from `elisp`.  It doesn't have to be `optional_new_start` specifically.  If users have an `elisp` mechanism to THROW_ANY_SWITCH, they can throw it from anywhere -- `pre-command-hook`, `post-command-hook`, etc.  [By switch, I mean the bool_bf items in `window.h`.]

Keith

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

At Thu, 28 Jan 2016 18:14:43 -0800,
John Wiegley wrote:
> 
> >>>>> Keith David Bershatsky <[email-masked]> writes:
> 
> >> What do you need from window-scroll-functions? Only the correct values of
> >> window-start and window-end? Or something else?
> 
> > Correct, I just need the 100% accurate `window-start` and
> > `window-end`. :) :)
> 
> Keith, your use case here does strike me as a bit exotic. I'm not saying it's
> not a bug, but I'm wondering if your suggested solution is really the best
> path to the final result you're reaching for.  The cost of `vertical-motion',
> for example, seems to be a driving force behind your request.