From 1db2c7bff64cc0885d90dcf4d075297fc3094d45 Mon Sep 17 00:00:00 2001 From: larstvei Date: Sat, 14 Sep 2024 23:42:16 +0200 Subject: [PATCH] Split into horizontal and vertical centering Also remove forcibly redisplaing. Not sure it's necessary, so let's wait and see if things break. --- center-content.el | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/center-content.el b/center-content.el index bedf25c..6bc66cc 100644 --- a/center-content.el +++ b/center-content.el @@ -37,28 +37,30 @@ (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* ((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))) - (space (propertize " " 'display `(space :width ,left-margin)))) - (overlay-put overlay 'line-prefix space) - (setq center-content--horizontal-overlay overlay)) - ;; Apply vertical centering using header-line-format +(defun center-content--horizontal (content-width) + "Add overlay for horizontal centering relative to CONTENT-WIDTH." + (let* ((left-margin (center-content--calculate-left-margin content-width)) + (overlay (make-overlay (point-min) (point-max))) + (space (propertize " " 'display `(space :width ,left-margin)))) + (overlay-put overlay 'line-prefix space) + (setq center-content--horizontal-overlay overlay))) + +(defun center-content--vertical (content-height) + "Add overlay for vertical centering relative to CONTENT-HEIGHT." + (let* ((top-margin (center-content--calculate-top-margin content-height))) ;; Save and remap the header-line face to match the default face (setq center-content--original-header-line-format header-line-format) (setq center-content--header-line-face-remap-cookie (face-remap-add-relative 'header-line 'default)) - (setq header-line-format (propertize " " 'display `(height ,top-margin))) - ;; Save and hide the mode line + (setq header-line-format (propertize " " 'display `(height ,top-margin))))) + +(defun center-content--enable () + "Enable horizontal and vertical centering of content." + (let* ((content-size (center-content--content-pixel-size))) + (center-content--horizontal (car content-size)) + (center-content--vertical (cdr content-size)) (setq center-content--original-mode-line-format mode-line-format) - (setq mode-line-format nil) - ;; Force redisplay - (force-mode-line-update) - (redisplay))) + (setq mode-line-format nil))) (defun center-content--disable () "Disable centering of content."