diff options
Diffstat (limited to 'text-conf.el')
-rw-r--r-- | text-conf.el | 57 |
1 files changed, 50 insertions, 7 deletions
diff --git a/text-conf.el b/text-conf.el index c8bc566..bd2ebaf 100644 --- a/text-conf.el +++ b/text-conf.el @@ -47,7 +47,7 @@ exactly the current ilne." (setq index (1+ index))) (insert remainder-string)))) -;;; Center text +;;; Make a centered string ;;;###autoload (defun correct-length (str) @@ -75,14 +75,57 @@ Characters that take up more than one column will be counted with (defun center-string-in-width (str width) "Add spaces to STR so that it is centered in a box which is WIDTH \ wide." - (let ((len (string-width str))) + (let* ((space-width (string-pixel-width (string 32))) + (width (* width space-width)) + (len (string-pixel-width str))) (cond ((> len width) (error "String %s longer than %d" str width)) - (t (format "%s%s" - (make-string (round (- width len) 2) - 32) - str))))) + (t (concat + (make-string + (round (- width len) (* space-width 2)) + 32) + str))))) + +;;; Center the string + +(defun make-center-line (&optional nlines) + "Center the line point is on, within the width specified by +`fill-column'. + +This means adjusting the indentation so that it equals the +distance between the end of the text and `fill-column'. The +argument NLINES says how many lines to center. + +This is adapted from the function `center-line' by Durand at +2024-02-20 23:27:43.839601." + (interactive "P") + (if nlines (setq nlines (prefix-numeric-value nlines))) + (while (not (eq nlines 0)) + (save-excursion + (let ((lm (current-left-margin)) + space) + (beginning-of-line) + (delete-horizontal-space) + (end-of-line) + (delete-horizontal-space) + (setq space (- fill-column + lm + (round + (car + (window-text-pixel-size + nil (pos-bol) (point))) + (string-pixel-width (string 32))))) + (if (> space 0) + (indent-line-to (+ lm (/ space 2)))))) + (cond ((null nlines) + (setq nlines 0)) + ((> nlines 0) + (setq nlines (1- nlines)) + (forward-line 1)) + ((< nlines 0) + (setq nlines (1+ nlines)) + (forward-line -1))))) ;;; Make a block @@ -121,7 +164,7 @@ DESCRIPTION above the block." (save-excursion (goto-char beg) (while (<= (point) (1- (marker-position end-marker))) - (center-line) + (make-center-line) (forward-line 1)) (goto-char beg) (mapc #'insert header) |