[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/ilist 347effebbe 07/24: fix the calculation of lengths
From: |
ELPA Syncer |
Subject: |
[elpa] externals/ilist 347effebbe 07/24: fix the calculation of lengths of strings |
Date: |
Tue, 28 Dec 2021 16:58:13 -0500 (EST) |
branch: externals/ilist
commit 347effebbe9dfdce5daacf08ed82d0a9782c1a7c
Author: JSDurand <mmemmew@gmail.com>
Commit: JSDurand <mmemmew@gmail.com>
fix the calculation of lengths of strings
* ilist.el (ilist-length, ilist-display, ilist-string): The characters
of some languages may take up more than the width of an English
character. The examples I have in mind are Chinese characters. To
fix this problem, I calculate the length of strings with CJK
characters by a special function. This does not fix all problems,
but is close to a satisfactory work-around I suppose.
---
ilist.el | 35 +++++++++++++++++++++++++++++------
1 file changed, 29 insertions(+), 6 deletions(-)
diff --git a/ilist.el b/ilist.el
index 73cff3d158..9209c9b803 100644
--- a/ilist.el
+++ b/ilist.el
@@ -130,6 +130,28 @@ but got %S" align)))
"Return the ELIDE in COLUMN."
(nth 5 column))
+;;; Calculate the length of a string correctly
+
+(defun ilist-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 (and
+ (stringp name)
+ (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)))
+
;;; display a row
(defun ilist-display (ls columns &optional no-trailing-space)
@@ -170,7 +192,7 @@ trailing spaces."
(let* ((str (funcall
(ilist-column-fun column)
element))
- (str-len (length str))
+ (str-len (ilist-length str))
(max-len (ilist-column-max column))
(elide (ilist-column-elide column))
(str
@@ -183,12 +205,12 @@ trailing spaces."
(substring
str
0
- (max (- max-len (length elide))
+ (max (- max-len (ilist-length elide))
0))
elide))
((substring str 0 max-len))))
(str))))
- (cons (length str) str)))
+ (cons (ilist-length str) str)))
columns))
ls))
;; The list column-widths has a special convention: if a width is
@@ -434,7 +456,7 @@ trailing spaces."
(let* ((width (nth index column-widths))
(alignment (ilist-column-align col))
(name (ilist-column-name col))
- (complement (- width (length name)))
+ (complement (- width (ilist-length name)))
(floor-len (floor complement 2)))
;; we increase the index before the end of the form
(setq index (1+ index))
@@ -470,8 +492,9 @@ trailing spaces."
(let* ((width (nth index column-widths))
(alignment (ilist-column-align col))
(name (ilist-column-name col))
- (name-sep (make-string (length name) ?-))
- (complement (- width (length name)))
+ (name-len (ilist-length name))
+ (name-sep (make-string name-len ?-))
+ (complement (- width name-len))
(floor-len (floor complement 2)))
(setq index (1+ index))
(cond
- [elpa] externals/ilist 457f0b0d87 06/24: no trailing whitespaces if demanded, (continued)
- [elpa] externals/ilist 457f0b0d87 06/24: no trailing whitespaces if demanded, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist 4fb5695fe1 09/24: Add manuals and update README, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist 95bcbf4b00 12/24: ilist: leave new lines when hiding groups, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist 8b5d5f382f 22/24: Update docs, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist f021d7d8ab 20/24: fix: account for an invisible beginning, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist 282f60c93a 21/24: I forgot to byte-compile again. Silly me!, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist c3f0e61360 13/24: fix missing parameter, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist b2bf67c5d8 24/24: Add changelog, and update documentation, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist ed18678569 05/24: new: get group and marks, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist 5362c106a8 04/24: more functionalities, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist 347effebbe 07/24: fix the calculation of lengths of strings,
ELPA Syncer <=
- [elpa] externals/ilist ebb207c520 08/24: Fix an ilist error, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist 76f29b61bb 10/24: Minor format change, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist 8c4efd3d7c 11/24: add invisible text property., ELPA Syncer, 2021/12/28
- [elpa] externals/ilist c4a48491ee 15/24: fix previous bugs caused by invisibility, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist 1b16d5789d 14/24: ilist: fix some errors caused by invisibility again., ELPA Syncer, 2021/12/28
- [elpa] externals/ilist c9b456103c 16/24: Fix errors and byte-compile, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist df91da4d46 17/24: ilist: try to fix a bug, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist 0892dd8ffc 18/24: ilist: update the byte-compiled version, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist 2279028fd5 23/24: ilist: Adding automatic filter groups, ELPA Syncer, 2021/12/28
- [elpa] externals/ilist 66bc6e0101 19/24: ilist: fix a bug about moving between items, ELPA Syncer, 2021/12/28