unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#47712: 27.1; Provide `string-display-width` function, which takes properties into account, `substring-width`
@ 2021-04-11 21:16 Daniel Mendler
  2021-04-12  2:26 ` Eli Zaretskii
  0 siblings, 1 reply; 27+ messages in thread
From: Daniel Mendler @ 2021-04-11 21:16 UTC (permalink / raw)
  To: 47712

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))





^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2021-04-14 11:38 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-11 21:16 bug#47712: 27.1; Provide `string-display-width` function, which takes properties into account, `substring-width` Daniel Mendler
2021-04-12  2:26 ` Eli Zaretskii
2021-04-12  8:45   ` Daniel Mendler
2021-04-12  8:53     ` Lars Ingebrigtsen
2021-04-12  9:08       ` Daniel Mendler
2021-04-13  8:01         ` Lars Ingebrigtsen
2021-04-13 12:00           ` Eli Zaretskii
2021-04-13 12:25             ` Daniel Mendler
2021-04-14  8:50               ` Eli Zaretskii
2021-04-14 10:49                 ` Daniel Mendler
2021-04-14 11:38                   ` Eli Zaretskii
2021-04-12 12:21     ` Eli Zaretskii
2021-04-12 12:50       ` Daniel Mendler
2021-04-12 13:21         ` Eli Zaretskii
2021-04-12 13:32           ` Daniel Mendler
2021-04-12 13:40             ` Eli Zaretskii
2021-04-12 14:05               ` Daniel Mendler
2021-04-12 14:15                 ` Eli Zaretskii
2021-04-12 14:32                   ` Eli Zaretskii
2021-04-12 14:38                     ` Daniel Mendler
2021-04-12 17:01                       ` Eli Zaretskii
2021-04-12 17:18                         ` Daniel Mendler
2021-04-13  7:06                         ` martin rudalics
2021-04-13 12:23                           ` Eli Zaretskii
2021-04-12 14:36                   ` Daniel Mendler
2021-04-12 17:09                     ` Eli Zaretskii
2021-04-12 17:13                       ` Daniel Mendler

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).