6

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

  • 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: 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
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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