Ihor Radchenko <yanta...@posteo.net> writes:

> Ruijie Yu <rui...@netyu.xyz> writes:
>
>>> https://debbugs.gnu.org/cgi/bugreport.cgi?bug=63029
>>
>> From my understanding of Eli's response, org-colview is at fault here, by
>> calling `length' instead of `string-width' in
>> `org-columns-add-ellipses'.  Changing this function call might be all it
>> needs to fix the colview test failure?
>
> This is tricky because string width may be different depending on
> in-buffer settings, fonts used, etc.
>
> We can try to be a tiny little bit more accurate using
> `truncate-string-to-width', `string-width', or `org-string-width'.

Just had a try at this, fully untested code (other than starting a
LANG=zh_CN.UTF-8 Emacs session and running this substring function to
see its effects).  It is a bit too late for me now, so I'll call it a
day (literally) for coding today.

diff --git a/lisp/org-colview.el b/lisp/org-colview.el
index 92a3b473d..e8106f9cd 100644
--- a/lisp/org-colview.el
+++ b/lisp/org-colview.el
@@ -452,14 +452,30 @@ DATELINE is non-nil when the face used should be
 	    "Type \\<org-columns-map>`\\[org-columns-edit-value]' \
 to edit property")))))))
 
+(defun org-columns--substring-below-width (string start width)
+  "Similar to `substring', but use `string-width' to check width.
+The returned value is a substring of STRING, starting at START,
+and is the largest possible substring whose width does not exceed
+WIDTH."
+  (let ((end (min (+ start width) (length string))) res)
+    (while (and end (>= end start))
+      (let* ((curr (string-width string start end))
+             (excess (- curr width)))
+        (if (cl-plusp excess)
+            (cl-decf end (max 1 (/ excess 2)))
+          (setq res (substring string start end) end nil))))
+    res))
+
 (defun org-columns-add-ellipses (string width)
   "Truncate STRING with WIDTH characters, with ellipses."
   (cond
-   ((<= (length string) width) string)
-   ((<= width (length org-columns-ellipses))
-    (substring org-columns-ellipses 0 width))
-   (t (concat (substring string 0 (- width (length org-columns-ellipses)))
-	      org-columns-ellipses))))
+   ((<= (string-width string) width) string)
+   ((<= width (string-width org-columns-ellipses))
+    (org-columns--substring-below-width org-columns-ellipses 0 width))
+   (t (concat
+       (org-columns--substring-below-width
+        string 0 (- width (length org-columns-ellipses)))
+       org-columns-ellipses))))
 
 (defvar org-columns-full-header-line-format nil
   "The full header line format, will be shifted by horizontal scrolling." )
-- 
Best,


RY

[Please note that this mail might go to spam due to some
misconfiguration in my mail server -- still investigating.]

Reply via email to