Compare commits
10 Commits
c2524b420e
...
509db9bbde
Author | SHA1 | Date | |
---|---|---|---|
509db9bbde | |||
8fd156e918 | |||
ea42c54438 | |||
936f79e5db | |||
21dfc29045 | |||
2d82f55835 | |||
f1a1c4a172 | |||
1db2c7bff6 | |||
d8551328f0 | |||
bfc3e1c792 |
@ -1,4 +1,4 @@
|
|||||||
;;; center-content.el --- Center buffer content horizontally and vertically -*- lexical-binding: t; -*-
|
;;; center-content.el --- Center buffer content -*- lexical-binding: t; -*-
|
||||||
|
|
||||||
;;; Commentary:
|
;;; Commentary:
|
||||||
|
|
||||||
@ -9,13 +9,45 @@
|
|||||||
|
|
||||||
(require 'face-remap)
|
(require 'face-remap)
|
||||||
|
|
||||||
|
(defgroup center-content ()
|
||||||
|
"Center buffer content."
|
||||||
|
:group 'convenience
|
||||||
|
:prefix "center-content-")
|
||||||
|
|
||||||
|
(defcustom center-content-horizontal t
|
||||||
|
"Enable horizontal centering."
|
||||||
|
:type 'boolean
|
||||||
|
:group 'center-content)
|
||||||
|
|
||||||
|
(defcustom center-content-vertical t
|
||||||
|
"Enable vertical centering."
|
||||||
|
:type 'boolean
|
||||||
|
:group 'center-content)
|
||||||
|
|
||||||
|
(defcustom center-content-vertical-by-overlay nil
|
||||||
|
"Enable vertical by using overlays.
|
||||||
|
|
||||||
|
Using overlays for vertical centering may give some undesirable
|
||||||
|
effects on the first line. By default, vertical centering is achieved
|
||||||
|
by using setting `header-line-format'."
|
||||||
|
:type 'boolean
|
||||||
|
:group 'center-content)
|
||||||
|
|
||||||
|
(defcustom center-content-hide-mode-line nil
|
||||||
|
"Hide mode-line when centering."
|
||||||
|
:type 'boolean
|
||||||
|
:group 'center-content)
|
||||||
|
|
||||||
(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--original-header-line-format nil
|
(defvar-local center-content--vertical-overlay nil
|
||||||
|
"Overlay used to add top margin for vertical centering.")
|
||||||
|
|
||||||
|
(defvar-local center-content--original-header-line-format header-line-format
|
||||||
"Stores the original `header-line-format` to restore upon disabling.")
|
"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 mode-line-format
|
||||||
"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
|
(defvar-local center-content--header-line-face-remap-cookie nil
|
||||||
@ -23,7 +55,7 @@
|
|||||||
|
|
||||||
(defun center-content--content-pixel-size ()
|
(defun center-content--content-pixel-size ()
|
||||||
"Return the size of the content of the visible text in pixels."
|
"Return the size of the content of the visible text in pixels."
|
||||||
(window-text-pixel-size (selected-window) (window-start) (window-end)))
|
(window-text-pixel-size (selected-window) (window-start) (window-end nil t)))
|
||||||
|
|
||||||
(defun center-content--calculate-left-margin (content-width)
|
(defun center-content--calculate-left-margin (content-width)
|
||||||
"Calculate the left margin relative to CONTENT-WIDTH."
|
"Calculate the left margin relative to CONTENT-WIDTH."
|
||||||
@ -37,29 +69,44 @@
|
|||||||
(line-height (default-line-height)))
|
(line-height (default-line-height)))
|
||||||
(/ (max 0 (- window-height content-height)) (* 2 line-height))))
|
(/ (max 0 (- window-height content-height)) (* 2 line-height))))
|
||||||
|
|
||||||
(defun center-content--enable ()
|
(defun center-content--horizontal (content-width)
|
||||||
"Enable horizontal and vertical centering of content."
|
"Add overlay for horizontal centering relative to CONTENT-WIDTH."
|
||||||
(let* ((content-size (center-content--content-pixel-size))
|
(let* ((left-margin (center-content--calculate-left-margin content-width))
|
||||||
(left-margin (center-content--calculate-left-margin (car content-size)))
|
(overlay (make-overlay (point-min) (point-max)))
|
||||||
(top-margin (center-content--calculate-top-margin (cdr content-size))))
|
(space (propertize " " 'display `(space :width ,left-margin))))
|
||||||
;; Apply horizontal centering using overlay
|
(overlay-put overlay 'line-prefix space)
|
||||||
(let ((overlay (make-overlay (point-min) (point-max))))
|
(setq center-content--horizontal-overlay overlay)))
|
||||||
(overlay-put overlay 'line-prefix
|
|
||||||
(propertize " " 'display `(space :width ,left-margin)))
|
(defun center-content--vertical (content-height)
|
||||||
(setq center-content--horizontal-overlay overlay))
|
"Add overlay for vertical centering relative to CONTENT-HEIGHT."
|
||||||
;; Apply vertical centering using header-line-format
|
(let* ((top-margin (center-content--calculate-top-margin content-height)))
|
||||||
;; Save and remap the header-line face to match the default face
|
;; 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
|
(setq center-content--header-line-face-remap-cookie
|
||||||
(face-remap-add-relative 'header-line 'default))
|
(face-remap-add-relative 'header-line 'default))
|
||||||
(setq header-line-format
|
(setq header-line-format (propertize " " 'display `(height ,top-margin)))))
|
||||||
(propertize " " 'display `(height ,top-margin)))
|
|
||||||
;; Save and hide the mode line
|
(defun center-content--vertical-by-overlay (content-height)
|
||||||
(setq center-content--original-mode-line-format mode-line-format)
|
"Add overlay for vertical centering relative to CONTENT-HEIGHT."
|
||||||
(setq mode-line-format nil)
|
(let* ((top-margin (center-content--calculate-top-margin content-height))
|
||||||
;; Force redisplay
|
(overlay (make-overlay (window-start) (window-start) nil t t))
|
||||||
(force-mode-line-update)
|
(space (propertize " " 'display `(space :width 0 :height ,top-margin))))
|
||||||
(redisplay)))
|
(overlay-put overlay 'intangible t)
|
||||||
|
(overlay-put overlay 'before-string space)
|
||||||
|
(setq center-content--vertical-overlay overlay)))
|
||||||
|
|
||||||
|
(defun center-content--enable ()
|
||||||
|
"Enable horizontal and vertical centering of content."
|
||||||
|
(setq center-content--original-header-line-format header-line-format)
|
||||||
|
(setq center-content--original-mode-line-format mode-line-format)
|
||||||
|
(let* ((content-size (center-content--content-pixel-size)))
|
||||||
|
(when center-content-horizontal
|
||||||
|
(center-content--horizontal (car content-size)))
|
||||||
|
(when center-content-vertical
|
||||||
|
(if center-content-vertical-by-overlay
|
||||||
|
(center-content--vertical-by-overlay (cdr content-size))
|
||||||
|
(center-content--vertical (cdr content-size))))
|
||||||
|
(when center-content-hide-mode-line
|
||||||
|
(setq mode-line-format nil))))
|
||||||
|
|
||||||
(defun center-content--disable ()
|
(defun center-content--disable ()
|
||||||
"Disable centering of content."
|
"Disable centering of content."
|
||||||
@ -67,30 +114,35 @@
|
|||||||
(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
|
||||||
|
(when (overlayp center-content--vertical-overlay)
|
||||||
|
(delete-overlay center-content--vertical-overlay)
|
||||||
|
(setq center-content--vertical-overlay nil))
|
||||||
;; Restore the header-line face remapping
|
;; Restore the header-line face remapping
|
||||||
(when center-content--header-line-face-remap-cookie
|
(when center-content--header-line-face-remap-cookie
|
||||||
(face-remap-remove-relative 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))
|
(setq center-content--header-line-face-remap-cookie nil))
|
||||||
;; Restore the header-line-format
|
;; Restore the header-line-format
|
||||||
(setq header-line-format center-content--original-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-header-line-format nil)
|
|
||||||
;; Force redisplay
|
(defun center-content-update ()
|
||||||
(force-mode-line-update)
|
"Updates the centering if `center-content-mode' is enabled."
|
||||||
(redisplay))
|
(interactive)
|
||||||
|
(when (bound-and-true-p center-content-mode)
|
||||||
|
(center-content--disable)
|
||||||
|
(center-content--enable)))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(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 content of the buffer is centered both
|
When enabled, the content of the buffer is centered both horizontally
|
||||||
horizontally and vertically within the window. The mode line is
|
and vertically within the window."
|
||||||
hidden for a clean appearance."
|
|
||||||
:init-value nil
|
:init-value nil
|
||||||
:global nil
|
:global nil
|
||||||
:lighter nil ;; Remove the lighter to keep the mode line clean
|
:lighter " Center"
|
||||||
(if center-content-mode
|
(if center-content-mode
|
||||||
(center-content--enable)
|
(center-content--enable)
|
||||||
(center-content--disable)))
|
(center-content--disable)))
|
||||||
|
Loading…
Reference in New Issue
Block a user