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


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 ()
   (save-excursion (beginning-of-visual-line) (point))))
  • 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 Nov 16 '15 at 14:10

Your Answer

