I am not sure if the following code will give any problems, but I did not experience any while testing it quickly:
(defun limit-scrolling (&optional win start)
(let ((visible-lines (count-lines (or start (window-start)) (buffer-size)))
(lines-to-end (count-lines (point) (buffer-size))))
(when (< visible-lines (window-text-height))
(recenter (- lines-to-end)))))
;; (add-hook 'window-scroll-functions #'limit-scrolling)
(add-hook 'post-command-hook #'limit-scrolling)
I have initially written the function to be used as a window-scroll-function
(which takes two arguments), which seems to work fine. But as the docstring of window-scroll-functions
warns that such functions should not be used to 'alter the way a window is scrolled', I figured it would be wiser to add the function to the post-command-hook
instead. I have written the function so that it can be used in any of those two hooks (but make sure to not use it in both hooks at the same time).
Also, remember that, if you experience some 'weird' scrolling problems somewhere, you should remove the function from the hook to see if the problems disappear.
notepad
on MS-Windows. – shynur Nov 14 '23 at 11:13cua-mode
enables some behavior that i think you're looking for, but then you get all ofcua-mode
:( – nega Nov 21 '23 at 15:48