emacs-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#47712: closed (27.1; Provide `string-display-width` function, which


From: GNU bug Tracking System
Subject: bug#47712: closed (27.1; Provide `string-display-width` function, which takes properties into account, `substring-width`)
Date: Wed, 14 Apr 2021 11:39:02 +0000

Your message dated Wed, 14 Apr 2021 14:38:29 +0300
with message-id <83tuo9yu2y.fsf@gnu.org>
and subject line Re: bug#47712: 27.1; Provide `string-display-width` function, 
which takes properties into account, `substring-width`
has caused the debbugs.gnu.org bug report #47712,
regarding 27.1; Provide `string-display-width` function, which takes properties 
into account, `substring-width`
to be marked as done.

(If you believe you have received this mail in error, please contact
help-debbugs@gnu.org.)


-- 
47712: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=47712
GNU Bug Tracking System
Contact help-debbugs@gnu.org with problems
--- Begin Message --- Subject: 27.1; Provide `string-display-width` function, which takes properties into account, `substring-width` Date: Sun, 11 Apr 2021 23:16:13 +0200
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))



--- End Message ---
--- Begin Message --- Subject: Re: bug#47712: 27.1; Provide `string-display-width` function, which takes properties into account, `substring-width` Date: Wed, 14 Apr 2021 14:38:29 +0300
> Cc: larsi@gnus.org, 47712@debbugs.gnu.org
> From: Daniel Mendler <mail@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.


--- End Message ---

reply via email to

[Prev in Thread] Current Thread [Next in Thread]