Re: Position buffer contents at bottom of window
Stéphane Marks <[email protected]> Wed, 24 Jun 2026 08:43:23 -0400
| Newsgroups | gmane.emacs.help |
|---|---|
| Message-ID | <CAN+1HbqQ3yroFoUD+1g7CssKT6xbAXnbG8KM6KLxi+p1W04wiA@mail.gmail.com> |
On Wed, Jun 24, 2026 at 6:53 AM Joost Kremers <[email protected]> wrote: > Hi list, > > I've been trying to create a function that fills a buffer and then > positions the last line of the buffer at the bottom of the window. > > Doing this manually works just fine: > > ``` > (defun my--recenter-window-to-bottom (&optional window) > "Align contents of BUFFER to the bottom of WINDOW." > (interactive (list (selected-window))) > (when (window-live-p window) > (with-current-buffer (window-buffer window) > (when-let* ((_ (>= (window-end window t) (point-max))) > (edges (window-body-pixel-edges window)) > (window-top (nth 1 edges)) > (window-bottom (nth 3 edges)) > (text-height (cdr (window-text-pixel-size window > (window-start window)))) > (dy (- window-bottom window-top text-height))) > (message "↑%d ↓%d ↕%d dy%d" window-top window-bottom text-height > dy) > (pixel-scroll-precision-scroll-up dy))))) > ``` > > I can bind this to a key and then calling this function does exactly what I > want it to do. > > The problem is that it does not seem to work from Lisp, at least not in the > context I'm using it. Specifically, I have a function in the buffer-local > value of `window-buffer-change-functions` that basically erases the buffer > and fills it with content. At the end, it calls `(goto-char (point-max))` > and then sets a timer for 0 seconds to run the function above. > > I'm using the timer because the buffer-fill functions is called from > `window-buffer-change-functions` which, AFAIU, is called during redisplay, > so I was hoping the timer would ensure that redisplay is done before > `my--recenter-window-to-bottom` is called. > > Things, however, just don't work... The numbers reported in the `message` > call are different than when I call the function interactively, and the > window is scrolled incorrectly, or not at all. > > I'm probably doing something very wrong here, but I'm unsure what. If > anyone can cast an eye and point me in the right direction, I'd be > grateful. Have you ensured that the window is the correct one? You didn't share the timer code, so it is possible that you could be passing in the correct window at that point rather than the function being triggered by the timer when the selected window isn't the one you want.