How to determine the line number of the first visible line of a window?
1 Answers
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
line-number-at-pos
is okay -- it will get a line number even ifpoint
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