Re: Position buffer contents at bottom of window
Joost Kremers <[email protected]> Wed, 24 Jun 2026 15:40:21 +0200
| Newsgroups | gmane.emacs.help |
|---|---|
| Message-ID | <[email protected]> |
On Wed, Jun 24 2026, Eli Zaretskii wrote: >> 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))))) >> ``` [...] > Does window-end return nil when you call this from Lisp? AFAICT it returns a number, at least some of the time, because the `message` call within the `when-let*` does get run. > More generally, please tell which part(s) don't work, and why do you > need them to work. Honestly, I have not been able to determine what exactly goes wrong. My idea was to get the pixel-height of the last line in the buffer, the pixel height of the buffer bottom, and then use that to calculate the pixel distance between the last line and the window bottom, and scroll the window up that many pixels. (I'm using `pixel-scroll-precision-mode`, hence the call to `pixel-scroll-precision-scroll-up`.) When I run the function interactively, the values reported by the `message` call are reasonable and the scrolling works. But when called from Lisp, I get different values, and I can't explain why. I guess the window and/or buffer are not in the state I expect them to be when the function is run, but I don't know how to control for that. > There are probably more reliable ways of obtaining > the same information. I'm open to suggestions. > Btw, why don't you use recenter-top-bottom? My first idea was to use `(recenter -1)`, which works predictably, but it often leaves a one-line gap between the bottom window edge and the last buffer line. I'm trying to get rid of that gap. -- Joost Kremers Life has its moments