I'm interested in the region of a buffer that is visible on-screen.
How do you get the current scroll position of a buffer? I.e. the line number of the topmost line in the viewable area? (Given that I can use window-height
to find the bottommost visible line of the buffer.)
(The point-min
and point-max
functions give the min and max buffer positions, but those might be offscreen. I want to know the min and max on-screen buffer positions.)
window-start
andwindow-end
cannot be known with a 100% degree of accuracy until the end of the redisplay cycle.line-number-at-pos
is too slow in large buffers, and(string-to-number (format-mode-line "%l"))
has some limitations: emacs.stackexchange.com/questions/3821/… For a few functions that require speed in large buffers, I have used(save-excursion (goto-char) (string-to-number (format-mode-line "%l")))
.post-command-hook
andwindow-scroll-functions
hook can be used to obtainwindow-start
andwindow-end
in about 90% of the situations, but the latter fires several times under certain circumstances before the final values will be almost accurate. There is an option argument forwindow-end
to update the value. Due to the present impossibility of ascertain the correctwindow-start
/window-end
values with 100% accuracy, I have been implementing my own feature requests written in C as part of theredisplay
cycle, which of course is not the answer you seek ... :)window-start
andwindow-end
are not 100% accurate and I should consider the redisplay cycle when using them. Can you link me to any threads on the cases wherewindow-start
andwindow-end
don't work? Also,(string-to-number (format-mode-line "%l"))
appears to only give me the line number ofpoint
, from which I cannot tell the top visible line in the window. However, thanks again :)window-start
, you could use(save-excursion (goto-char (window-start (selected-window))) (format-mode-line "%l"))
. To get a line atwindow-end
, you could use(save-excursion (goto-char (window-end (selected-window) 'force)) (format-mode-line "%l"))
. Addstring-to-number
if so desired. Beware of certain limitations discussed in the comments of the linked thread hereinabove. Here is a link to a thread where I began my journey: stackoverflow.com/questions/23923371/…