diff options
-rw-r--r-- | durand-align.el | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/durand-align.el b/durand-align.el new file mode 100644 index 0000000..079f421 --- /dev/null +++ b/durand-align.el @@ -0,0 +1,61 @@ +;;; durand-align.el --- Pixel alignment -*- lexical-binding: t; -*- + +;; Copyright (C) 2024 Jean Sévère Durand + +;; Author: Jean Sévère Durand <durand@jsdurand.xyz> +;; Keywords: wp, faces + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see <https://www.gnu.org/licenses/>. + +;;; Commentary: + +;; Some little functions to center buffer texts at pixel level +;; precision. + +;;; Code: + +(defun durand-center (&optional pixels) + "Center the buffer. +If PIXELS is not-nil, use that as body width in pixels." + (let* ((body-width (cond ((and pixels (numberp pixels) (> pixels 0)) + pixels) + ((car (window-text-pixel-size + nil + (cons (window-start) 0) + (window-end)))))) + (len (floor (- (window-body-width nil t) body-width) 2)) + (prop (list 'space :width (list len))) + (str (propertize (string 32) 'display prop 'durand-center t))) + (save-excursion + (goto-char (point-min)) + (while (not (eobp)) + (cond + ((get-text-property (point) 'durand-center) + (put-text-property (point) (1+ (point)) 'display prop)) + ((insert str))) + (forward-line 1))))) + +(defun durand-cancel-center () + "Cancel the inserted characters." + (save-excursion + (goto-char (point-min)) + (while (not (eobp)) + (cond + ((get-text-property (point) 'durand-center) + (delete-char 1))) + (forward-line 1)))) + + +(provide 'durand-align) +;;; durand-align.el ends here |