[PATCH] emacs: fix author field width with double-width chars

Subject: [PATCH] emacs: fix author field width with double-width chars

Date: Sat, 28 Jun 2025 17:49:58 +0800

To: notmuch@notmuchmail.org

Cc:

From: Yikai Zhao


Some non-ascii characters is double-width (e.g. CJK characters), which
means that its `length' is not the same as `string-width' (display
width). This commit fixes the width calculation in
notmuch-search-insert-authors so that author field with these
characters can be properly aligned.
---
 emacs/notmuch.el | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 69aff347..379a0d46 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -802,14 +802,14 @@ non-authors is found, assume that all of the authors match."
 	   (invisible-string "")
 	   (padding ""))
       ;; Truncate the author string to fit the specification.
-      (when (> (length formatted-authors)
-	       (length formatted-sample))
-	(let ((visible-length (- (length formatted-sample)
-				 (length "... "))))
+      (when (> (string-width formatted-authors)
+	       (string-width formatted-sample))
+	(let ((visible-width (- (string-width formatted-sample)
+				 (string-width "... "))))
 	  ;; Truncate the visible string according to the width of
 	  ;; the display string.
-	  (setq visible-string (substring formatted-authors 0 visible-length))
-	  (setq invisible-string (substring formatted-authors visible-length))
+	  (setq visible-string (truncate-string-to-width formatted-authors visible-width))
+	  (setq invisible-string (substring formatted-authors (length visible-string)))
 	  ;; If possible, truncate the visible string at a natural
 	  ;; break (comma or pipe), as incremental search doesn't
 	  ;; match across the visible/invisible border.
@@ -823,9 +823,9 @@ non-authors is found, assume that all of the authors match."
 	  ;; `visible-string' may be shorter than the space allowed
 	  ;; by `format-string'. If so we must insert some padding
 	  ;; after `invisible-string'.
-	  (setq padding (make-string (- (length formatted-sample)
-					(length visible-string)
-					(length "..."))
+	  (setq padding (make-string (- (string-width formatted-sample)
+					(string-width visible-string)
+					(string-width "..."))
 				     ? ))))
       ;; Use different faces to show matching and non-matching authors.
       (if (string-match "\\(.*\\)|\\(.*\\)" visible-string)
-- 
2.49.0

_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: