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-startandwindow-endcannot be known with a 100% degree of accuracy until the end of the redisplay cycle.line-number-at-posis too slow in large buffers, and(string-to-number (format-mode-line "%l"))has some limitations: https://emacs.stackexchange.com/questions/3821/a-faster-method-to-obtain-line-number-at-pos-in-large-buffers For a few functions that require speed in large buffers, I have used(save-excursion (goto-char) (string-to-number (format-mode-line "%l"))). – lawlist Oct 30 '17 at 14:08post-command-hookandwindow-scroll-functionshook can be used to obtainwindow-startandwindow-endin 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-endto update the value. Due to the present impossibility of ascertain the correctwindow-start/window-endvalues with 100% accuracy, I have been implementing my own feature requests written in C as part of theredisplaycycle, which of course is not the answer you seek ... :) – lawlist Oct 30 '17 at 14:12window-startandwindow-endare not 100% accurate and I should consider the redisplay cycle when using them. Can you link me to any threads on the cases wherewindow-startandwindow-enddon'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 :) – jcarpenter2 Oct 30 '17 at 21:37window-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-numberif 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: https://stackoverflow.com/questions/23923371/emacs-calculating-new-window-start-end-without-redisplay – lawlist Oct 30 '17 at 22:05