diff options
author | JSDurand <mmemmew@gmail.com> | 2021-04-20 17:35:52 +0800 |
---|---|---|
committer | JSDurand <mmemmew@gmail.com> | 2021-04-20 17:35:52 +0800 |
commit | 3d4b11ee83c86d764ab9b2dcb36c3354983c1426 (patch) | |
tree | 8d1f99184a4ab4b514ce95c17e26ead485ee1670 | |
parent | a0597743c6f4d46a709bbdcd0cfe6881d35d7b5d (diff) |
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.
-rw-r--r-- | modeline.el | 25 |
1 files changed, 23 insertions, 2 deletions
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 |