center-content-mode/center-content.el

103 lines
4.2 KiB
EmacsLisp
Raw Normal View History

;;; center-content.el --- Center buffer content horizontally and vertically -*- lexical-binding: t; -*-
;;; Commentary:
;; This package provides a minor mode `center-content-mode' that centers the
;; content of the current buffer both horizontally and vertically.
;;; Code:
(defvar-local center-content--horizontal-overlay nil
"Overlay used to add left margin for horizontal centering.")
(defvar-local center-content--original-header-line-format nil
"Stores the original `header-line-format` to restore upon disabling.")
(defvar-local center-content--original-mode-line-format nil
"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 ()
"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 " ")))
(/ (max 0 (- window-width content-width)) (* 2 column-width))))
(defun center-content--calculate-top-margin ()
"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)))))
(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)))
;; Apply horizontal centering using overlay
(let ((overlay (make-overlay (point-min) (point-max))))
(overlay-put overlay 'line-prefix
(propertize " " 'display `(space :width ,left-margin)))
(setq center-content--horizontal-overlay overlay))
;; Apply vertical centering using header-line-format
;; 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 center-content--original-mode-line-format mode-line-format)
(setq mode-line-format nil)
;; Force redisplay
(force-mode-line-update)
(redisplay)))
(defun center-content--disable ()
"Disable centering of content."
;; Remove horizontal centering overlay
(when (overlayp center-content--horizontal-overlay)
(delete-overlay center-content--horizontal-overlay)
(setq center-content--horizontal-overlay nil))
;; Restore the header-line face remapping
(when center-content--header-line-face-remap-cookie
(face-remap-remove-relative center-content--header-line-face-remap-cookie)
(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
(setq mode-line-format center-content--original-mode-line-format)
(setq center-content--original-header-line-format nil)
;; Force redisplay
(force-mode-line-update)
(redisplay))
;;;###autoload
(define-minor-mode center-content-mode
"Toggle horizontal and vertical centering of buffer content.
When enabled, the content of the buffer is centered both
horizontally and vertically within the window. The mode line is
hidden for a clean appearance."
:init-value nil
:global nil
:lighter nil ;; Remove the lighter to keep the mode line clean
(if center-content-mode
(center-content--enable)
(center-content--disable)))
(provide 'center-content)
;;; center-content.el ends here