Using overlay at the top

this is really janky
This commit is contained in:
larstvei 2024-09-14 02:39:48 +02:00
parent 194631b978
commit 5f4a6580bf

View File

@ -2,29 +2,21 @@
;;; Commentary: ;;; Commentary:
;; This package provides a minor mode `center-content-mode` that centers the visible content ;; This package provides a minor mode `center-content-mode` that centers the content
;; of the current buffer both horizontally and vertically. It calculates the necessary margins ;; of the current buffer both horizontally and vertically. It calculates the necessary
;; and applies them using overlays and header line adjustments. The mode line is hidden to ;; margins and applies them using overlays. The mode line is hidden to provide a clean appearance.
;; provide a clean appearance.
;;; Code: ;;; Code:
(defvar-local center-content--horiz-overlay nil (defvar-local center-content--horizontal-overlay nil
"Overlay used by `center-content-mode` to add left margin for horizontal centering.") "Overlay used to add left margin for horizontal centering.")
(defvar-local center-content--original-header-line-format nil (defvar-local center-content--vertical-overlay nil
"Stores the original `header-line-format` to restore upon disabling.") "Overlay used to add top margin for vertical centering.")
(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.")
(defface center-content-header-line
`((t (:inherit default
:background ,(face-background 'default nil t)
:foreground ,(face-foreground 'default nil t))))
"Face for the header line in `center-content-mode`."
:group 'center-content)
(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))
@ -32,15 +24,14 @@
(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)))))
(space-width (string-pixel-width " ")) (/ (max 0 (- window-width content-width)) 2)))
(margin (/ (max 0 (- window-width content-width)) (* 2 space-width))))
margin))
(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-height (window-pixel-height)) (let* ((window (selected-window))
(content-height (cdr (window-text-pixel-size))) (window-height (window-pixel-height window))
(content-height (cdr (window-text-pixel-size window)))
(margin (/ (max 0 (- window-height content-height)) 2))) (margin (/ (max 0 (- window-height content-height)) 2)))
margin)) margin))
@ -49,35 +40,38 @@
(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 ((horiz-overlay (make-overlay (point-min) (point-max)))) (let ((horizontal-overlay (make-overlay (point-min) (point-max))))
(overlay-put horiz-overlay 'line-prefix (overlay-put horizontal-overlay 'line-prefix
(propertize " " 'display `(space :width ,left-margin))) (propertize " " 'display `(space :width (,left-margin))))
(setq center-content--horiz-overlay horiz-overlay)) (setq center-content--horizontal-overlay horizontal-overlay))
;; Save the original header-line-format ;; Apply vertical centering using a before-string overlay
(setq center-content--original-header-line-format header-line-format) (let ((overlay (make-overlay (point-min) (point-min))))
;; Apply vertical centering using header-line-format with custom face (overlay-put overlay 'before-string
(setq header-line-format (propertize "\n" 'display `(space :height (,top-margin))))
(propertize " " ;; Set low priority to avoid interfering with line-prefix
'display `(height ,(max 0 (/ top-margin (frame-char-height)))) (overlay-put overlay 'priority -50)
'face 'center-content-header-line)) (setq center-content--vertical-overlay overlay))
;; Save the original mode-line-format ;; 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)
;; Hide the mode line (setq mode-line-format nil)
(setq mode-line-format nil))) ;; Force redisplay
(force-mode-line-update)
(redisplay)))
(defun center-content--disable () (defun center-content--disable ()
"Disable centering of content." "Disable centering of content."
;; Remove horizontal centering overlay ;; Remove horizontal centering overlay
(when (overlayp center-content--horiz-overlay) (when (overlayp center-content--horizontal-overlay)
(delete-overlay center-content--horiz-overlay) (delete-overlay center-content--horizontal-overlay)
(setq center-content--horiz-overlay nil)) (setq center-content--horizontal-overlay nil))
;; Restore the original header-line-format ;; Remove vertical centering overlay
(setq header-line-format center-content--original-header-line-format) (when (overlayp center-content--vertical-overlay)
(setq center-content--original-header-line-format nil) (delete-overlay center-content--vertical-overlay)
(setq center-content--vertical-overlay 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-mode-line-format nil)
;; Force redisplay to remove any lingering effects ;; Force redisplay
(force-mode-line-update) (force-mode-line-update)
(redisplay)) (redisplay))
@ -85,7 +79,7 @@
(define-minor-mode center-content-mode (define-minor-mode center-content-mode
"Toggle horizontal and vertical centering of buffer content. "Toggle horizontal and vertical centering of buffer content.
When enabled, the visible content of the buffer is centered both When enabled, the content of the buffer is centered both
horizontally and vertically within the window. The mode line is horizontally and vertically within the window. The mode line is
hidden for a clean appearance." hidden for a clean appearance."
:init-value nil :init-value nil