2

line-number-at-pos works fine when visual-line-mode is off. But I can't find its equivalent for visual-line-mode.

1 Answer 1

4

The "visual line number, counting from point-min" depends on the window width, font choice, font size, etc... so a given buffer's position can have several different "visual line numbers" at the same time if it's displayed in different windows.

So Emacs does not really try to provide this info.

OTOH you can easily get the "visual line number, counting from windows-start", using posn-at-point.

Oh, and there's apparently count-screen-lines (as well as compute-motion) which should be able to provide the info you want.

Example code:

(defun visual-line-number-at-pos ()
  (count-screen-lines
   (point-min)
   (save-excursion (beginning-of-visual-line) (point))))
1
  • The window width, font choice, etc are all fixed at the moment that I'm asking for the visual line number. Is there a way to calculate it efficiently?
    – abo-abo
    Commented Nov 16, 2015 at 14:10

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.