From 3d4b11ee83c86d764ab9b2dcb36c3354983c1426 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Tue, 20 Apr 2021 17:35:52 +0800 Subject: Calculate the widths of non-Latin characters correctly. * modeline.el (modeline-format-main): Now use the following function to calculate the correct widths. (modeline-length): Use the name and the decomposition of a character to determine if a character should take 1.7 columns instead of 1 column. --- modeline.el | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'modeline.el') diff --git a/modeline.el b/modeline.el index b93b833..cb06b0d 100644 --- a/modeline.el +++ b/modeline.el @@ -5,8 +5,8 @@ "The main mode line format." (let* ((left (modeline-format-left)) (right (modeline-format-right)) - (left-len (length left)) - (right-len (length right)) + (left-len (modeline-length left)) + (right-len (modeline-length right)) (middle (propertize " " 'display (make-string (max (- (window-total-width) @@ -78,6 +78,27 @@ (modeline-format-major-mode) (modeline-format-vc-mode))) +;;; Calculate the correct lengths of characters + +;;;###autoload +(defun modeline-length (str) + "Return the length of STR. +Characters that take up more than one column will be counted with +1.7 columns." + (declare (side-effect-free t) (pure t)) + (let ((len 0)) + (mapc (lambda (char) + (let ((name (get-char-code-property char 'name)) + (decomposition (get-char-code-property char 'decomposition))) + (cond + ((or (string-match (rx-to-string '(seq bos "CJK")) + name) + (eq (car decomposition) 'wide)) + (setq len (+ len 1.7))) + ((setq len (1+ len)))))) + str) + (floor len))) + ;;; Conveniently add text properties ;;;###autoload -- cgit v1.2.3-18-g5258