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

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

bug#47712: 27.1; Provide `string-display-width` function, which takes pr


From: Daniel Mendler
Subject: bug#47712: 27.1; Provide `string-display-width` function, which takes properties into account, `substring-width`
Date: Tue, 13 Apr 2021 14:25:18 +0200

On 4/13/21 2:00 PM, Eli Zaretskii wrote:
From: Lars Ingebrigtsen <larsi@gnus.org>
Note that I would like to have an implementation of
`substring-width`/`string-display-width`/`substring-display-width`
which does not allocate, since this reduces the GC pressure when
formatting many items. How do you think about this?

Sure; I think a non-allocating function to count monospaced string width
would be useful.

I won't mount the barricades over this, but please keep in mind that
even with fixed-pitch fonts this will not always produce correct
results, because on GUI frames the "double-width" characters not
always take exactly two columns (it depends on the font).  So this
function will only work reliably on text-mode (a.k.a. TTY) frames and
for Latin characters on GUI frames.

If you still think we should have such a semi-broken function, at
least include some warning in its doc string, so that users wouldn't
be unpleasantly surprised.

Yes, `string-width` is broken for face-dependent purposes. However there are still these handful of use cases which will probably not go away (org-mode table, formatting monospaced text, ...). In those existing cases the `string-width` function is often used in combination with `substring`, i.e., `(string-width (substring str beg end)`.

Therefore I would be happy with the following resolution:

1. Add two arguments begin and end to `string-width` to improve the current uses of `string-width`.

2. Document the caveats of `string-width` in the docstring (works only reliable in text mode for multi-width chars) and maybe mention `window-text-pixel-size` or the `string-pixel-width` function by Martin.

----

(defun string-pixel-width (string)
 (let ((buffer (get-buffer-create " *string-pixel-width*"))
       (old-buffer (window-buffer)))
   (unwind-protect
       (with-current-buffer buffer
         (erase-buffer)
         (insert string)
         (set-window-buffer (selected-window) buffer)
         (car (window-text-pixel-size nil (point-min) (point-max))))
     (set-window-buffer (selected-window) old-buffer))))

(I don't think it is needed to add the `string-pixel-width` function to the core. The function or some variant of it is easy enough for packages to include and it cannot be used in this form since it leads to these temporary buffers lying around.)





reply via email to

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