8

How to determine the line number of the first visible line of a window?

Name
  • 7,689
  • 4
  • 38
  • 84
  • 1
    If you are *not* getting the line number multiple times each command loop for *various* positions, then using `line-number-at-pos` is okay -- it will get a line number even if `point` is *not* visible: `(line-number-at-pos (window-start))` If you *are* getting the line number multiple times each command loop for various positions, then it is faster to move to point and tap into the power of the C-source code: `(save-excursion (goto-char (window-start)) (format-mode-line "%l"))` See: http://emacs.stackexchange.com/q/3821/2287 – lawlist Aug 20 '15 at 23:44
  • @lawlist Thanks, your comment answers my question. If you are conformable, please convert it to an answer. – Name Aug 20 '15 at 23:49

1 Answers1

8

If the user is not getting the line number multiple times each command loop for various positions, then using line-number-at-pos is sufficient -- it will get a line number even if point is not visible: (line-number-at-pos (window-start))

If the user is getting the line number multiple times each command loop for various positions, then it can be much faster to move to point and tap into the power of the C-source code: (save-excursion (goto-char (window-start)) (format-mode-line "%l")) -- see: A faster method to obtain `line-number-at-pos` in large buffers The string can be converted to a number with string-to-number

There are some important exceptions to calculating window-start prior to redisplay, but that is beyond the scope of this question/answer: https://emacs.stackexchange.com/a/10768/2287

lawlist
  • 18,826
  • 5
  • 37
  • 118