From 4925bca107a32e0de102bef04b74afded2c0b765 Mon Sep 17 00:00:00 2001 From: larstvei Date: Sat, 14 Sep 2024 23:22:11 +0200 Subject: [PATCH] Fix in content-size calculation --- center-content.el | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/center-content.el b/center-content.el index d739a42..c6edbce 100644 --- a/center-content.el +++ b/center-content.el @@ -19,31 +19,26 @@ (defvar-local center-content--header-line-face-remap-cookie nil "Cookie for the face remapping of `header-line`.") -(defun center-content--calculate-left-margin () +(defun center-content--content-pixel-size () + (window-text-pixel-size (selected-window) (window-start) (window-end))) + +(defun center-content--calculate-left-margin (content-width) "Calculate the left margin needed to center the content horizontally." - (let* ((window (selected-window)) - (window-width (window-pixel-width window)) - (content-width (car (window-text-pixel-size - window - (window-start window) - (window-end window t)))) - (column-width (string-pixel-width " "))) + (let ((window-width (window-pixel-width)) + (column-width (string-pixel-width " "))) (/ (max 0 (- window-width content-width)) (* 2 column-width)))) -(defun center-content--calculate-top-margin () +(defun center-content--calculate-top-margin (content-height) "Calculate the top margin needed to center the content vertically." - (let* ((window (selected-window)) - (window-height (window-pixel-height window)) - (content-height (cdr (window-text-pixel-size - window - (window-start window) - (window-end window t))))) - (/ (max 0 (- window-height content-height)) (* 2 (frame-char-height))))) + (let ((window-height (window-pixel-height)) + (line-height (default-line-height))) + (/ (max 0 (- window-height content-height)) (* 2 line-height)))) (defun center-content--enable () "Enable horizontal and vertical centering of content." - (let ((left-margin (center-content--calculate-left-margin)) - (top-margin (center-content--calculate-top-margin))) + (let* ((content-size (center-content--content-pixel-size)) + (left-margin (center-content--calculate-left-margin (car content-size))) + (top-margin (center-content--calculate-top-margin (cdr content-size)))) ;; Apply horizontal centering using overlay (let ((overlay (make-overlay (point-min) (point-max)))) (overlay-put overlay 'line-prefix