From 5b43126c06f9eca04bfe2b9b47aaf65edae916e5 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Sun, 6 Jul 2025 17:50:09 +0800 Subject: durand-align: Pixel-level centering. * durand-align.el (durand-center, durand-cancel-center, durand-align): Experimental for the time being. --- durand-align.el | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 durand-align.el (limited to 'durand-align.el') 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 +;; 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 . + +;;; 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 -- cgit v1.2.3-18-g5258