GNU bug report logs -
#47712
27.1; Provide `string-display-width` function, which takes properties into account, `substring-width`
Previous Next
Reported by: Daniel Mendler <mail <at> daniel-mendler.de>
Date: Sun, 11 Apr 2021 21:17:02 UTC
Severity: normal
Found in version 27.1
Done: Eli Zaretskii <eliz <at> gnu.org>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
[Message part 1 (text/plain, inline)]
Your bug report
#47712: 27.1; Provide `string-display-width` function, which takes properties into account, `substring-width`
which was filed against the emacs package, has been closed.
The explanation is attached below, along with your original report.
If you require more details, please reply to 47712 <at> debbugs.gnu.org.
--
47712: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=47712
GNU Bug Tracking System
Contact help-debbugs <at> gnu.org with problems
[Message part 2 (message/rfc822, inline)]
> Cc: larsi <at> gnus.org, 47712 <at> debbugs.gnu.org
> From: Daniel Mendler <mail <at> daniel-mendler.de>
> Date: Wed, 14 Apr 2021 12:49:17 +0200
>
> On 4/14/21 10:50 AM, Eli Zaretskii wrote:
> > Your wishes have been granted, see the latest master branch.
> >
> > Is there anything else to do with this bug report, or can we close it?
>
> Thank you for the quick resolution, Eli! Please close this issue.
Done, thanks.
[Message part 3 (message/rfc822, inline)]
Provide a `string-display-width' function, which computes the
`string-width', but takes invisible display properties into account.
This function is often needed by packages.
For example my Consult package (https://github.com/minad/consult) has a
crude (non-recursive) version of that function. Then the Embark package
has (https://github.com/oantolin/embark) has a similar function. Last
but not least, the function already exists in Org under the name
`org-string-width'.
It is reasonable to implement this function in Elisp. But all the
implementations have to use `string-width' for measuring the parts which
make up the actual string. Unfortunately this requires allocations for
the substrings. A possible solution to this problem is to implement a
primitive function `substring-width' and implement `string-width' in
terms of that function.
(defun consult--display-width (string)
"Compute width of STRING taking display and invisible properties into
account."
(let ((pos 0) (width 0) (end (length string)))
(while (< pos end)
(let ((nextd (next-single-property-change
pos 'display string end))
(display (get-text-property
pos 'display string)))
(if (stringp display)
(setq width (+ width (string-width display))
pos nextd)
(while (< pos nextd)
(let ((nexti (next-single-property-change
pos 'invisible string nextd)))
(unless (get-text-property
pos 'invisible string)
(setq width
(+ width
(string-width
;; Avoid allocation for the full string.
;; There should be a `substring-width'
;; provided by Emacs. TODO: Propose
;; upstream? Alternatively propose this
;; whole `display-width' function to
;; upstream.
(if (and (= pos 0) (= nexti end))
string
(substring-no-properties
string pos nexti))))))
(setq pos nexti))))))
width))
This bug report was last modified 4 years and 37 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.