4

I'm making a presentation mode where I want to vertically center some text in the window, which is the only window in the frame. I'm using a custom face so I can control how large the font is.

To horizontally center the text, I can use window-max-chars-per-line with the face argument, so I can get the number of characters put in a line.

But I also want to know how many lines are in the window. There's window-body-height, but that doesn't let me specify a face.

How can I tell how tall a window is in lines of a specific face?

  • 1
    Maybe you can use font-info and (window-body-height nil t)? – Tobias Apr 12 '17 at 12:28
  • Hrm, thanks! It looks like I can do something based off window-font-height. I'll look into it more and see if I can make it work. Thanks for the pointer towards looking into the font. – zck Apr 12 '17 at 14:53
3

One can get how many pixels tall a face is with window-font-height, and how many pixels tall a window is with window-body-height. From there it's a simple division with truncating.

(defun zck--lines-in-window (face &optional window)
  "Calculate how many lines of text with face FACE can fit in WINDOW."
  (truncate (window-body-height window t)
            (window-font-height window face)))
  • If you are waiting for better answers add some notice about that to this one otherwise accept this one please to mark the question as done. Have fun, – Tobias Apr 13 '17 at 13:35
  • I can't accept my own answer until 24 hours after I posted it, but I'm planning on doing so unless there's a flaw in this answer. Thanks for the suggestion to look into fonts and window-body-height! – zck Apr 13 '17 at 17:30

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.