Back to using the header for vertical space

This commit is contained in:
larstvei 2024-09-14 23:14:02 +02:00
parent 5f4a6580bf
commit 26785e7ac4

View File

@ -2,21 +2,23 @@
;;; Commentary: ;;; Commentary:
;; This package provides a minor mode `center-content-mode` that centers the content ;; This package provides a minor mode `center-content-mode' that centers the
;; of the current buffer both horizontally and vertically. It calculates the necessary ;; content of the current buffer both horizontally and vertically.
;; margins and applies them using overlays. The mode line is hidden to provide a clean appearance.
;;; Code: ;;; Code:
(defvar-local center-content--horizontal-overlay nil (defvar-local center-content--horizontal-overlay nil
"Overlay used to add left margin for horizontal centering.") "Overlay used to add left margin for horizontal centering.")
(defvar-local center-content--vertical-overlay nil (defvar-local center-content--original-header-line-format nil
"Overlay used to add top margin for vertical centering.") "Stores the original `header-line-format` to restore upon disabling.")
(defvar-local center-content--original-mode-line-format nil (defvar-local center-content--original-mode-line-format nil
"Stores the original `mode-line-format` to restore upon disabling.") "Stores the original `mode-line-format` to restore upon disabling.")
(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--calculate-left-margin ()
"Calculate the left margin needed to center the content horizontally." "Calculate the left margin needed to center the content horizontally."
(let* ((window (selected-window)) (let* ((window (selected-window))
@ -24,33 +26,36 @@
(content-width (car (window-text-pixel-size (content-width (car (window-text-pixel-size
window window
(window-start window) (window-start window)
(window-end window))))) (window-end window t))))
(/ (max 0 (- window-width content-width)) 2))) (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 ()
"Calculate the top margin needed to center the content vertically." "Calculate the top margin needed to center the content vertically."
(let* ((window (selected-window)) (let* ((window (selected-window))
(window-height (window-pixel-height window)) (window-height (window-pixel-height window))
(content-height (cdr (window-text-pixel-size window))) (content-height (cdr (window-text-pixel-size
(margin (/ (max 0 (- window-height content-height)) 2))) window
margin)) (window-start window)
(window-end window t)))))
(/ (max 0 (- window-height content-height)) (* 2 (frame-char-height)))))
(defun center-content--enable () (defun center-content--enable ()
"Enable horizontal and vertical centering of content." "Enable horizontal and vertical centering of content."
(let ((left-margin (center-content--calculate-left-margin)) (let ((left-margin (center-content--calculate-left-margin))
(top-margin (center-content--calculate-top-margin))) (top-margin (center-content--calculate-top-margin)))
;; Apply horizontal centering using overlay ;; Apply horizontal centering using overlay
(let ((horizontal-overlay (make-overlay (point-min) (point-max)))) (let ((overlay (make-overlay (point-min) (point-max))))
(overlay-put horizontal-overlay 'line-prefix (overlay-put overlay 'line-prefix
(propertize " " 'display `(space :width (,left-margin)))) (propertize " " 'display `(space :width ,left-margin)))
(setq center-content--horizontal-overlay horizontal-overlay)) (setq center-content--horizontal-overlay overlay))
;; Apply vertical centering using a before-string overlay ;; Apply vertical centering using header-line-format
(let ((overlay (make-overlay (point-min) (point-min)))) ;; Save and remap the header-line face to match the default face
(overlay-put overlay 'before-string (setq center-content--original-header-line-format header-line-format)
(propertize "\n" 'display `(space :height (,top-margin)))) (setq center-content--header-line-face-remap-cookie
;; Set low priority to avoid interfering with line-prefix (face-remap-add-relative 'header-line 'default))
(overlay-put overlay 'priority -50) (setq header-line-format
(setq center-content--vertical-overlay overlay)) (propertize " " 'display `(height ,top-margin)))
;; Save and hide the mode line ;; Save and hide the mode line
(setq center-content--original-mode-line-format mode-line-format) (setq center-content--original-mode-line-format mode-line-format)
(setq mode-line-format nil) (setq mode-line-format nil)
@ -64,13 +69,16 @@
(when (overlayp center-content--horizontal-overlay) (when (overlayp center-content--horizontal-overlay)
(delete-overlay center-content--horizontal-overlay) (delete-overlay center-content--horizontal-overlay)
(setq center-content--horizontal-overlay nil)) (setq center-content--horizontal-overlay nil))
;; Remove vertical centering overlay ;; Restore the header-line face remapping
(when (overlayp center-content--vertical-overlay) (when center-content--header-line-face-remap-cookie
(delete-overlay center-content--vertical-overlay) (face-remap-remove-relative center-content--header-line-face-remap-cookie)
(setq center-content--vertical-overlay nil)) (setq center-content--header-line-face-remap-cookie nil))
;; Restore the header-line-format
(setq header-line-format center-content--original-header-line-format)
(setq center-content--original-mode-line-format nil)
;; Restore the original mode-line-format ;; Restore the original mode-line-format
(setq mode-line-format center-content--original-mode-line-format) (setq mode-line-format center-content--original-mode-line-format)
(setq center-content--original-mode-line-format nil) (setq center-content--original-header-line-format nil)
;; Force redisplay ;; Force redisplay
(force-mode-line-update) (force-mode-line-update)
(redisplay)) (redisplay))