1

Sometimes I have the current buffer in the current window displayed sub-optimally: there is a lot of buffer not shown, and there is a lot of empty space at the bottom below the EOB. E.g.:

  • the buffer is 100 lines
  • the window is 10 lines
  • the point is on the line 3 of the window
  • EOB is on the line 5 of the window

so that the bottom 5 lines of the window are empty and the first 95 lines of the buffer are not shown.

I want a function that would "move the window 5 lines up along the buffer" so that

  • the EOB is on the bottom line of the window
  • the last 10 lines of the buffer are shown
  • the point is on the line 8(=3+5) or the window, the value of (point) did not change

Note:

  • I do not want the point to move
  • I do not want to change window configuration
  • I do not want to change the current window size

I am pretty sure something like that exists already, I just cannot figure out how to find it.

Edit:

Apparently, I need something like this:

(defun sds-improve-window-utilization ()
  "Ensure that there is no empty space below EOB in the current window"
  (interactive)
  (when-let ((w (selected-window))
             (eob-pos (cadr (pos-visible-in-window-p (point-max) w t)))
             (point (cadr (pos-visible-in-window-p (point) w t)))
             (height-c (window-total-height w))
             (height-p (window-pixel-height w)))
    (recenter (/ (- point eob-pos) (/ height-p height-c)))))

I wonder, again, if something like that already exists...

sds
  • 6,104
  • 22
  • 39

2 Answers2

1

If you just want to move EOB to the bottom of the window without moving point, then the simplest thing I can think of is

(save-excursion
   (goto-char (point-max))
   (recenter-top-bottom (window-height)))

It's effectively a no-op if the EOB is not visible in the window and otherwise saves point, positions EOB at the bottom of the window and restores point.

NickD
  • 29,717
  • 3
  • 27
  • 44
  • Nah, this still leaves a few un-used lines at the bottom ;-( – sds Jan 02 '24 at 02:58
  • What's the value of scroll-margin? Try let-binding it to 0 around the recenter call. – NickD Jan 02 '24 at 03:14
  • scroll-margin is 0 – sds Jan 02 '24 at 05:31
  • Then it's probably because EOB is on an empty line after the last visible text. If you have trailing newlines, try deleting them; although if you insist on having a final newline (which is always a good idea IMO), there is going to be an empty line at the bottom: you'd have to position EOB on the line given by (1+ (window-height)) - you might try that, but I wouldn't hold my breath. – NickD Jan 02 '24 at 15:31
0

Use recenter-top-bottom, normally bound to C-l.

db48x
  • 17,977
  • 1
  • 22
  • 28
  • Nope, C-l makes the current line center, but there is still empty space at the bottom. – sds Apr 10 '22 at 02:25
  • Hit C-l twice more. Seriously. – db48x Apr 10 '22 at 13:41
  • I know how C-l works, I tried it and I read the code. It does NOT do what I need. I want *EOB* to be the bottom of the window, not the *point*. – sds Apr 10 '22 at 14:52
  • I think you’re being particular enough that you’re going to have to write your own. – db48x Apr 10 '22 at 17:29