emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/valign 1d2ecd2 09/11: * valign.el (valign--pixel-width-


From: Stefan Monnier
Subject: [elpa] externals/valign 1d2ecd2 09/11: * valign.el (valign--pixel-width-from-to): Return 0 if FROM = TO.
Date: Thu, 28 Jan 2021 00:10:25 -0500 (EST)

branch: externals/valign
commit 1d2ecd2afdb77021eaffc213d9fbbc6d9c38ed82
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>

    * valign.el (valign--pixel-width-from-to): Return 0 if FROM = TO.
---
 valign.el | 48 +++++++++++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 21 deletions(-)

diff --git a/valign.el b/valign.el
index 9b83371..6555a6c 100644
--- a/valign.el
+++ b/valign.el
@@ -311,27 +311,33 @@ properties must be cleaned before using this.
 
 If WITH-PREFIX is non-nil, don’t subtract the width of line
 prefix."
-  (let* ((window (get-buffer-window))
-         ;; This computes the prefix width.  This trick doesn’t seem
-         ;; work if the point is at the beginning of a line, so we use
-         ;; TO instead of FROM.
-         ;;
-         ;; Why all this fuss: Org puts some display property on white
-         ;; spaces in a cell: (space :relative-width 1).  And that
-         ;; messes up the calculation of prefix: now it returns the
-         ;; width of a space instead of 0 when there is no line
-         ;; prefix.  So we move the test point around until it doesn’t
-         ;; sit on a character with display properties.
-         (line-prefix
-          (let ((pos to))
-            (while (get-char-property pos 'display)
-              (cl-decf pos))
-            (car (window-text-pixel-size window pos pos)))))
-    (- (car (window-text-pixel-size window from to))
-       (if with-prefix 0 line-prefix)
-       (if (bound-and-true-p display-line-numbers-mode)
-           (line-number-display-width 'pixel)
-         0))))
+  ;; HACK: You would expect (window-text-pixel-size WINDOW FROM TO) to
+  ;; return line-number-display-width when FROM equals to TO, but no,
+  ;; it returns 0.  Then if we still subtract line number width, we
+  ;; get a negative number.  So if FROM = TO, we simply return 0.
+  (if (eq from to)
+      0
+    (let* ((window (get-buffer-window))
+           ;; This computes the prefix width.  This trick doesn’t seem
+           ;; work if the point is at the beginning of a line, so we use
+           ;; TO instead of FROM.
+           ;;
+           ;; Why all this fuss: Org puts some display property on
+           ;; white spaces in a cell: like (space :relative-width 1).
+           ;; That messes up the calculation of the prefix: now it
+           ;; returns the width of a space instead of 0 when there is
+           ;; no line prefix.  So we move the test point around until
+           ;; it doesn’t sit on a character with display properties.
+           (line-prefix
+            (let ((pos to))
+              (while (get-char-property pos 'display)
+                (cl-decf pos))
+              (car (window-text-pixel-size window pos pos)))))
+      (- (car (window-text-pixel-size window from to))
+         (if with-prefix 0 line-prefix)
+         (if (bound-and-true-p display-line-numbers-mode)
+             (line-number-display-width 'pixel)
+           0)))))
 
 (defun valign--separator-p ()
   "If the current cell is actually a separator.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]