From 0305c495df91220dae780bb79c7293db2c5150c4 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 28 Jun 2026 03:32:14 -0400 Subject: refactor: extract the auto-fit input guard, fix a unit-stale docstring Pull the eight-condition validity check in wttrin--fit-font-height into wttrin--fit-inputs-usable-p, so the arithmetic branch reads as one line. Reword wttrin--center-margin's docstring, which still said "column counts" after the pixel-based centering rewrite started passing it pixels. --- wttrin.el | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/wttrin.el b/wttrin.el index c5cff9e..75e32ab 100644 --- a/wttrin.el +++ b/wttrin.el @@ -1060,14 +1060,24 @@ properties). Returns 0 for nil or empty TEXT." (defun wttrin--center-margin (block-width window-width) "Return the left margin that centers BLOCK-WIDTH within WINDOW-WIDTH. -Both are column counts. The result is floored to a whole column and is never -negative, so a block at least as wide as the window yields 0. Non-integer -inputs yield 0." +Both are in the same unit (columns or pixels — `wttrin--center-buffer' passes +pixels). The result is floored to a whole unit and is never negative, so a +block at least as wide as the window yields 0. Non-integer inputs yield 0." (if (and (integerp block-width) (integerp window-width) (> window-width block-width)) (/ (- window-width block-width) 2) 0)) +(defun wttrin--fit-inputs-usable-p (block-cols avail-px char-px cur-height) + "Return non-nil when the auto-fit inputs are usable. +BLOCK-COLS and CUR-HEIGHT must be positive integers, AVAIL-PX and CHAR-PX +positive numbers. Guards `wttrin--fit-font-height' against a divide-by-zero or +a nonsense ratio." + (and (integerp block-cols) (> block-cols 0) + (numberp avail-px) (> avail-px 0) + (numberp char-px) (> char-px 0) + (integerp cur-height) (> cur-height 0))) + (defun wttrin--fit-font-height (block-cols avail-px char-px cur-height floor cap) "Return a font height (1/10 pt) so BLOCK-COLS chars span AVAIL-PX pixels. CHAR-PX is the per-character pixel width at CUR-HEIGHT. The height scales by @@ -1075,10 +1085,7 @@ the ratio of AVAIL-PX to the block's current pixel width, then clamps to \[FLOOR, CAP]. When an input is unusable (non-positive BLOCK-COLS, CHAR-PX, AVAIL-PX, or CUR-HEIGHT), the current height is returned, still clamped." (let ((target - (if (and (integerp block-cols) (> block-cols 0) - (numberp avail-px) (> avail-px 0) - (numberp char-px) (> char-px 0) - (integerp cur-height) (> cur-height 0)) + (if (wttrin--fit-inputs-usable-p block-cols avail-px char-px cur-height) (round (* cur-height (/ (float avail-px) (* block-cols char-px)))) cur-height))) (max floor (min cap target)))) -- cgit v1.2.3