diff --git a/center-content.el b/center-content.el index dcbab6f..86694eb 100644 --- a/center-content.el +++ b/center-content.el @@ -24,6 +24,15 @@ :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 t "Hide mode-line when centering." :type 'boolean @@ -32,6 +41,9 @@ (defvar-local center-content--horizontal-overlay nil "Overlay used to add left margin for horizontal centering.") +(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.") @@ -73,6 +85,15 @@ (face-remap-add-relative 'header-line 'default)) (setq header-line-format (propertize " " 'display `(height ,top-margin))))) +(defun center-content--vertical-by-overlay (content-height) + "Add overlay for vertical centering relative to CONTENT-HEIGHT." + (let* ((top-margin (center-content--calculate-top-margin content-height)) + (overlay (make-overlay (window-start) (window-start) nil t t)) + (space (propertize " " 'display `(space :width 0 :height ,top-margin)))) + (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) @@ -81,7 +102,9 @@ (when center-content-horizontal (center-content--horizontal (car content-size))) (when center-content-vertical - (center-content--vertical (cdr content-size))) + (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)))) @@ -91,6 +114,10 @@ (when (overlayp center-content--horizontal-overlay) (delete-overlay center-content--horizontal-overlay) (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--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)