Re: Position buffer contents at bottom of window
Eli Zaretskii <[email protected]> Wed, 24 Jun 2026 16:19:04 +0300
| Newsgroups | gmane.emacs.help |
|---|---|
| Message-ID | <[email protected]> |
> From: Joost Kremers <[email protected]> > Date: Wed, 24 Jun 2026 12:52:27 +0200 > > 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. Does window-end return nil when you call this from Lisp? More generally, please tell which part(s) don't work, and why do you need them to work. There are probably more reliable ways of obtaining the same information. Btw, why don't you use recenter-top-bottom?