branch: externals/valign
commit 81de91320e38bb741b62bfe60fb84f891073f06f
Author: Yuan Fu <[email protected]>
Commit: Yuan Fu <[email protected]>
Property support tabs
* valign.el (valign--tab-width): New function.
(valign--glyph-width-at-point): Treat tabs differently.
---
valign.el | 37 +++++++++++++++++++++++++++----------
1 file changed, 27 insertions(+), 10 deletions(-)
diff --git a/valign.el b/valign.el
index bba3140..07dcb77 100644
--- a/valign.el
+++ b/valign.el
@@ -91,6 +91,21 @@ Return nil if not in a cell."
;; nil
;; (char-after)))))
+(defun valign--tab-width (font)
+ "Return the pixel width of a tab in FONT."
+ ;; FIXME We have to over-estimate the pixel width of a tab. Since
+ ;; it is very hard to compute the exact width of it (basically Emacs
+ ;; redisplay calculates tab width based one the iterators position
+ ;; on-the-fly, to calculate the exact width of a tab, you need to
+ ;; know it’s _position_, that means calculating from the left edge).
+ (* (or tab-width 8) ;; FIXME For some unknown reason, tab-width is
+ ;; sometimes nil, someone should investigate.
+ (aref (car (mapcar
+ #'identity (font-get-glyphs
+ font
+ 0 1 " ")))
+ 4)))
+
(defun valign--glyph-width-at-point (&optional point)
"Return the pixel width of the glyph at POINT.
The buffer has to be visible. If point is at an image, this
@@ -98,16 +113,18 @@ function doens’t return the image’s width, but the
underlining
character’s glyph width."
(let* ((p (or point (point))))
;; car + mapcar to translate the vector to a list.
- (aref (car (mapcar
- #'identity (font-get-glyphs
- ;; (font-at 0 nil (buffer-substring p (1+
- ;; p))) doesn’t work, the font is
- ;; sometimes wrong. (font-at p) doesn’t
- ;; work, because it requires the buffer to
- ;; be visible.
- (font-at p)
- p (1+ p))))
- 4)))
+ (if (eq (char-after point) ?\t)
+ (valign--tab-width (font-at p))
+ (aref (car (mapcar
+ #'identity (font-get-glyphs
+ ;; (font-at 0 nil (buffer-substring p (1+
+ ;; p))) doesn’t work, the font is
+ ;; sometimes wrong. (font-at p) doesn’t
+ ;; work, because it requires the buffer to
+ ;; be visible.
+ (font-at p)
+ p (1+ p))))
+ 4))))
(defun valign--pixel-width-from-to (from to)
"Return the width of the glyphs from FROM (inclusive) to TO (exclusive).