2

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.)

5
  • This is actually much more complex than it seems because window-start and window-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"))).
    – lawlist
    Commented Oct 30, 2017 at 14:08
  • A combination of the post-command-hook and window-scroll-functions hook can be used to obtain window-start and window-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 for window-end to update the value. Due to the present impossibility of ascertain the correct window-start/window-end values with 100% accuracy, I have been implementing my own feature requests written in C as part of the redisplay cycle, which of course is not the answer you seek ... :)
    – lawlist
    Commented Oct 30, 2017 at 14:12
  • @lawlist Thanks for the answers and information! It is good to know that window-start and window-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 where window-start and window-end don't work? Also, (string-to-number (format-mode-line "%l")) appears to only give me the line number of point, from which I cannot tell the top visible line in the window. However, thanks again :) Commented Oct 30, 2017 at 21:37
  • To get the line number at window-start, you could use (save-excursion (goto-char (window-start (selected-window))) (format-mode-line "%l")). To get a line at window-end, you could use (save-excursion (goto-char (window-end (selected-window) 'force)) (format-mode-line "%l")). Add string-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/…
    – lawlist
    Commented Oct 30, 2017 at 22:05
  • To follow my journey, you may wish to read through feature request 22404: debbugs.gnu.org/cgi/bugreport.cgi?bug=22404 And see also Eli Z.'s comment regarding bug 22637: lists.gnu.org/archive/html/bug-gnu-emacs/2016-02/msg00952.html
    – lawlist
    Commented Oct 30, 2017 at 22:09

1 Answer 1

2
(line-number-at-pos (window-start))

should do what you asked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.