1

Anyway to calculate it? (either in lines or pixels)

E.g.,

 +-------------------+
4|                   | <- top line
5|                   |
6|                   |
7| ■ <- cursor here. |

Here it is 3 (or 4, if you prefer to include both lines).

shynur
  • 4,065
  • 1
  • 3
  • 23

2 Answers2

2

I don't know if there is a straightforward way to know that, but you can have:

  • (windows-text-height) , which is the amount of lines in current window
  • (line-number-at-pos) will return the line number in this buffer.
  • (move-to-window-line) will be able to move the point to the first visible line using argument 0.

Knowning this writing a piece of elisp code wich will return the desired distance in lines and letting the point where it was is pretty trivial.

window-line-height will return the height in pixels of a given line. If you're not using variable fonts it can be used to have the pixel length too, but I'm not sure at all of its accuracy.

Muihlinn
  • 2,576
  • 1
  • 14
  • 22
  • +1 - Thanks. It's just a little roundabout. I think Emacs might have stored such similar values that can be used directly, rather than by moving cursor this way to calculate the distance. – shynur Jul 29 '23 at 16:27
  • if it does it should be internally, as usually the used value is the cursor position in the buffer, AFAIK the window handling part is written in C and I don't know what it exposes to elisp – Muihlinn Jul 29 '23 at 19:58
  • "*if it does it should be internally*" -- You can take a look at my answer. I see no need to hide this value from the user, which only introduces troubles. – shynur Aug 28 '23 at 10:55
1
(count-lines (window-start) (point))

Better method:

(keymap-global-set "C-c p"
  (lambda ()
    "Report the distance in lines & pixels."
    (interactive)
    (let ((posn (posn-at-point)))
      (message "%s; %spx"
               (cdr (posn-col-row posn))
               (cdr (posn-x-y     posn))))))
shynur
  • 4,065
  • 1
  • 3
  • 23
  • 1
    For pixels see [here](https://emacs.stackexchange.com/a/19556/26163) – dalanicolai Aug 24 '23 at 19:24
  • It computes the distance from the 'current' beginning of the window, not the 'very' beginning, so I think it is similar to your question. (I did not fully understand your 'sketch' b.t.w., but now I do :) – dalanicolai Aug 25 '23 at 14:14
  • 1
    @dalanicolai: "*It computes the distance from the 'current' beginning of the window, not the 'very' beginning, so I think it is similar to your question.*" -- I tested it again, this time more carefully. Yes, it is. Thank you! To avoid overlap between these 2 questions in terms of pixels, I changed my question's title to take only the number of lines into account. ||| Feel free to edit my answer to include the link you mentioned in the comment. – shynur Aug 25 '23 at 16:02
  • I think it is fine as it is... with the link here in the comments (especially after you edited your question :) Thanks for checking and for updating the question... – dalanicolai Aug 25 '23 at 20:00
  • 1
    @dalanicolai: In fact, the linked post has some shortcomings -- it seems to calculate some extra distance. I updated my answer; this time it is perfect. – shynur Aug 28 '23 at 10:52